WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
DutyCycleEncoder.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <memory>
8
9#include <hal/SimDevice.h>
10#include <hal/Types.h>
11#include <units/frequency.h>
12#include <units/time.h>
15
16namespace frc {
17class DutyCycle;
18class DigitalSource;
19
20/**
21 * Class for supporting duty cycle/PWM encoders, such as the US Digital MA3 with
22 * PWM Output, the CTRE Mag Encoder, the Rev Hex Encoder, and the AM Mag
23 * Encoder.
24 */
26 public wpi::SendableHelper<DutyCycleEncoder> {
27 public:
28 /**
29 * Construct a new DutyCycleEncoder on a specific channel.
30 *
31 * <p>This has a fullRange of 1 and an expectedZero of 0.
32 *
33 * @param channel the channel to attach to
34 */
35 explicit DutyCycleEncoder(int channel);
36
37 /**
38 * Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
39 *
40 * <p>This has a fullRange of 1 and an expectedZero of 0.
41 *
42 * @param dutyCycle the duty cycle to attach to
43 */
44 explicit DutyCycleEncoder(DutyCycle& dutyCycle);
45
46 /**
47 * Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
48 *
49 * <p>This has a fullRange of 1 and an expectedZero of 0.
50 *
51 * @param dutyCycle the duty cycle to attach to
52 */
53 explicit DutyCycleEncoder(DutyCycle* dutyCycle);
54
55 /**
56 * Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
57 *
58 * <p>This has a fullRange of 1 and an expectedZero of 0.
59 *
60 * @param dutyCycle the duty cycle to attach to
61 */
62 explicit DutyCycleEncoder(std::shared_ptr<DutyCycle> dutyCycle);
63
64 /**
65 * Construct a new DutyCycleEncoder attached to a DigitalSource object.
66 *
67 * <p>This has a fullRange of 1 and an expectedZero of 0.
68 *
69 * @param digitalSource the digital source to attach to
70 */
71 explicit DutyCycleEncoder(DigitalSource& digitalSource);
72
73 /**
74 * Construct a new DutyCycleEncoder attached to a DigitalSource object.
75 *
76 * <p>This has a fullRange of 1 and an expectedZero of 0.
77 *
78 * @param digitalSource the digital source to attach to
79 */
80 explicit DutyCycleEncoder(DigitalSource* digitalSource);
81
82 /**
83 * Construct a new DutyCycleEncoder attached to a DigitalSource object.
84 *
85 * <p>This has a fullRange of 1 and an expectedZero of 0.
86 *
87 * @param digitalSource the digital source to attach to
88 */
89 explicit DutyCycleEncoder(std::shared_ptr<DigitalSource> digitalSource);
90
91 /**
92 * Construct a new DutyCycleEncoder on a specific channel.
93 *
94 * @param channel the channel to attach to
95 * @param fullRange the value to report at maximum travel
96 * @param expectedZero the reading where you would expect a 0 from get()
97 */
98 DutyCycleEncoder(int channel, double fullRange, double expectedZero);
99
100 /**
101 * Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
102 *
103 * @param dutyCycle the duty cycle to attach to
104 * @param fullRange the value to report at maximum travel
105 * @param expectedZero the reading where you would expect a 0 from get()
106 */
107 DutyCycleEncoder(DutyCycle& dutyCycle, double fullRange, double expectedZero);
108
109 /**
110 * Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
111 *
112 * @param dutyCycle the duty cycle to attach to
113 * @param fullRange the value to report at maximum travel
114 * @param expectedZero the reading where you would expect a 0 from get()
115 */
116 DutyCycleEncoder(DutyCycle* dutyCycle, double fullRange, double expectedZero);
117
118 /**
119 * Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
120 *
121 * @param dutyCycle the duty cycle to attach to
122 * @param fullRange the value to report at maximum travel
123 * @param expectedZero the reading where you would expect a 0 from get()
124 */
125 DutyCycleEncoder(std::shared_ptr<DutyCycle> dutyCycle, double fullRange,
126 double expectedZero);
127
128 /**
129 * Construct a new DutyCycleEncoder attached to a DigitalSource object.
130 *
131 * @param digitalSource the digital source to attach to
132 * @param fullRange the value to report at maximum travel
133 * @param expectedZero the reading where you would expect a 0 from get()
134 */
135 DutyCycleEncoder(DigitalSource& digitalSource, double fullRange,
136 double expectedZero);
137
138 /**
139 * Construct a new DutyCycleEncoder attached to a DigitalSource object.
140 *
141 * @param digitalSource the digital source to attach to
142 * @param fullRange the value to report at maximum travel
143 * @param expectedZero the reading where you would expect a 0 from get()
144 */
145 DutyCycleEncoder(DigitalSource* digitalSource, double fullRange,
146 double expectedZero);
147
148 /**
149 * Construct a new DutyCycleEncoder attached to a DigitalSource object.
150 *
151 * @param digitalSource the digital source to attach to
152 * @param fullRange the value to report at maximum travel
153 * @param expectedZero the reading where you would expect a 0 from get()
154 */
155 DutyCycleEncoder(std::shared_ptr<DigitalSource> digitalSource,
156 double fullRange, double expectedZero);
157
158 ~DutyCycleEncoder() override = default;
159
162
163 /**
164 * Get the frequency in Hz of the duty cycle signal from the encoder.
165 *
166 * @return duty cycle frequency in Hz
167 */
168 int GetFrequency() const;
169
170 /**
171 * Get if the sensor is connected
172 *
173 * This uses the duty cycle frequency to determine if the sensor is connected.
174 * By default, a value of 100 Hz is used as the threshold, and this value can
175 * be changed with SetConnectedFrequencyThreshold.
176 *
177 * @return true if the sensor is connected
178 */
179 bool IsConnected() const;
180
181 /**
182 * Change the frequency threshold for detecting connection used by
183 * IsConnected.
184 *
185 * @param frequency the minimum frequency in Hz.
186 */
188
189 /**
190 * Get the encoder value.
191 *
192 * @return the encoder value scaled by the full range input
193 */
194 double Get() const;
195
196 /**
197 * Set the encoder duty cycle range. As the encoder needs to maintain a duty
198 * cycle, the duty cycle cannot go all the way to 0% or all the way to 100%.
199 * For example, an encoder with a 4096 us period might have a minimum duty
200 * cycle of 1 us / 4096 us and a maximum duty cycle of 4095 / 4096 us. Setting
201 * the range will result in an encoder duty cycle less than or equal to the
202 * minimum being output as 0 rotation, the duty cycle greater than or equal to
203 * the maximum being output as 1 rotation, and values in between linearly
204 * scaled from 0 to 1.
205 *
206 * @param min minimum duty cycle (0-1 range)
207 * @param max maximum duty cycle (0-1 range)
208 */
209 void SetDutyCycleRange(double min, double max);
210
211 /**
212 * Sets the assumed frequency of the connected device.
213 *
214 * <p>By default, the DutyCycle engine has to compute the frequency of the
215 * input signal. This can result in both delayed readings and jumpy readings.
216 * To solve this, you can pass the expected frequency of the sensor to this
217 * function. This will use that frequency to compute the DutyCycle percentage,
218 * rather than the computed frequency.
219 *
220 * @param frequency the assumed frequency of the sensor
221 */
222 void SetAssumedFrequency(units::hertz_t frequency);
223
224 /**
225 * Set if this encoder is inverted.
226 *
227 * @param inverted true to invert the encoder, false otherwise
228 */
229 void SetInverted(bool inverted);
230
231 /**
232 * Get the FPGA index for the DutyCycleEncoder.
233 *
234 * @return the FPGA index
235 */
236 int GetFPGAIndex() const;
237
238 /**
239 * Get the channel of the source.
240 *
241 * @return the source channel
242 */
243 int GetSourceChannel() const;
244
245 void InitSendable(wpi::SendableBuilder& builder) override;
246
247 private:
248 void Init(double fullRange, double expectedZero);
249 double MapSensorRange(double pos) const;
250
251 std::shared_ptr<DutyCycle> m_dutyCycle;
252 int m_frequencyThreshold = 100;
253 double m_fullRange;
254 double m_expectedZero;
255 units::second_t m_period{0_s};
256 double m_sensorMin{0.0};
257 double m_sensorMax{1.0};
258 bool m_isInverted{false};
259
260 hal::SimDevice m_simDevice;
261 hal::SimDouble m_simPosition;
262 hal::SimBoolean m_simIsConnected;
263};
264} // namespace frc
DigitalSource Interface.
Definition DigitalSource.h:22
Class for supporting duty cycle/PWM encoders, such as the US Digital MA3 with PWM Output,...
Definition DutyCycleEncoder.h:26
double Get() const
Get the encoder value.
DutyCycleEncoder(std::shared_ptr< DigitalSource > digitalSource)
Construct a new DutyCycleEncoder attached to a DigitalSource object.
DutyCycleEncoder(DutyCycle &dutyCycle)
Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
DutyCycleEncoder(int channel, double fullRange, double expectedZero)
Construct a new DutyCycleEncoder on a specific channel.
DutyCycleEncoder(DutyCycle *dutyCycle)
Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
DutyCycleEncoder(DutyCycle *dutyCycle, double fullRange, double expectedZero)
Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
bool IsConnected() const
Get if the sensor is connected.
DutyCycleEncoder(DigitalSource &digitalSource)
Construct a new DutyCycleEncoder attached to a DigitalSource object.
DutyCycleEncoder(DigitalSource *digitalSource)
Construct a new DutyCycleEncoder attached to a DigitalSource object.
~DutyCycleEncoder() override=default
int GetFrequency() const
Get the frequency in Hz of the duty cycle signal from the encoder.
int GetSourceChannel() const
Get the channel of the source.
void SetInverted(bool inverted)
Set if this encoder is inverted.
int GetFPGAIndex() const
Get the FPGA index for the DutyCycleEncoder.
DutyCycleEncoder(std::shared_ptr< DigitalSource > digitalSource, double fullRange, double expectedZero)
Construct a new DutyCycleEncoder attached to a DigitalSource object.
DutyCycleEncoder(std::shared_ptr< DutyCycle > dutyCycle)
Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
DutyCycleEncoder(DigitalSource *digitalSource, double fullRange, double expectedZero)
Construct a new DutyCycleEncoder attached to a DigitalSource object.
DutyCycleEncoder & operator=(DutyCycleEncoder &&)=default
DutyCycleEncoder(DigitalSource &digitalSource, double fullRange, double expectedZero)
Construct a new DutyCycleEncoder attached to a DigitalSource object.
DutyCycleEncoder(DutyCycle &dutyCycle, double fullRange, double expectedZero)
Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
void SetAssumedFrequency(units::hertz_t frequency)
Sets the assumed frequency of the connected device.
void SetDutyCycleRange(double min, double max)
Set the encoder duty cycle range.
void SetConnectedFrequencyThreshold(int frequency)
Change the frequency threshold for detecting connection used by IsConnected.
DutyCycleEncoder(int channel)
Construct a new DutyCycleEncoder on a specific channel.
DutyCycleEncoder(std::shared_ptr< DutyCycle > dutyCycle, double fullRange, double expectedZero)
Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
DutyCycleEncoder(DutyCycleEncoder &&)=default
Class to read a duty cycle PWM input.
Definition DutyCycle.h:32
C++ wrapper around a HAL simulator boolean value handle.
Definition SimDevice.h:611
A move-only C++ wrapper around a HAL simulator device handle.
Definition SimDevice.h:645
C++ wrapper around a HAL simulator double value handle.
Definition SimDevice.h:536
Helper class for building Sendable dashboard representations.
Definition SendableBuilder.h:21
A helper class for use with objects that add themselves to SendableRegistry.
Definition SendableHelper.h:21
Interface for Sendable objects.
Definition Sendable.h:16
Definition CAN.h:11