WPILibC++ 2024.3.2
EncoderSim.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
10
11namespace frc {
12
13class Encoder;
14
15namespace sim {
16
17/**
18 * Class to control a simulated encoder.
19 */
21 public:
22 /**
23 * Constructs from an Encoder object.
24 *
25 * @param encoder Encoder to simulate
26 */
27 explicit EncoderSim(const Encoder& encoder);
28
29 /**
30 * Creates an EncoderSim for a digital input channel. Encoders take two
31 * channels, so either one may be specified.
32 *
33 * @param channel digital input channel
34 * @return Simulated object
35 * @throws NoSuchElementException if no Encoder is configured for that channel
36 */
37 static EncoderSim CreateForChannel(int channel);
38
39 /**
40 * Creates an EncoderSim for a simulated index.
41 * The index is incremented for each simulated Encoder.
42 *
43 * @param index simulator index
44 * @return Simulated object
45 */
46 static EncoderSim CreateForIndex(int index);
47
48 /**
49 * Register a callback on the Initialized property of the encoder.
50 *
51 * @param callback the callback that will be called whenever the Initialized
52 * property is changed
53 * @param initialNotify if true, the callback will be run on the initial value
54 * @return the CallbackStore object associated with this callback
55 */
56 [[nodiscard]]
57 std::unique_ptr<CallbackStore> RegisterInitializedCallback(
58 NotifyCallback callback, bool initialNotify);
59
60 /**
61 * Read the Initialized value of the encoder.
62 *
63 * @return true if initialized
64 */
65 bool GetInitialized() const;
66
67 /**
68 * Change the Initialized value of the encoder.
69 *
70 * @param initialized the new value
71 */
72 void SetInitialized(bool initialized);
73
74 /**
75 * Register a callback on the count property of the encoder.
76 *
77 * @param callback the callback that will be called whenever the count
78 * property is changed
79 * @param initialNotify if true, the callback will be run on the initial value
80 * @return the CallbackStore object associated with this callback
81 */
82 [[nodiscard]]
83 std::unique_ptr<CallbackStore> RegisterCountCallback(NotifyCallback callback,
84 bool initialNotify);
85
86 /**
87 * Read the count of the encoder.
88 *
89 * @return the count
90 */
91 int GetCount() const;
92
93 /**
94 * Change the count of the encoder.
95 *
96 * @param count the new count
97 */
98 void SetCount(int count);
99
100 /**
101 * Register a callback on the period of the encoder.
102 *
103 * @param callback the callback that will be called whenever the period is
104 * changed
105 * @param initialNotify if true, the callback will be run on the initial value
106 * @return the CallbackStore object associated with this callback
107 */
108 [[nodiscard]]
109 std::unique_ptr<CallbackStore> RegisterPeriodCallback(NotifyCallback callback,
110 bool initialNotify);
111
112 /**
113 * Read the period of the encoder.
114 *
115 * @return the encoder period
116 */
117 double GetPeriod() const;
118
119 /**
120 * Change the encoder period.
121 *
122 * @param period the new period
123 */
124 void SetPeriod(double period);
125
126 /**
127 * Register a callback to be called whenever the encoder is reset.
128 *
129 * @param callback the callback
130 * @param initialNotify whether to run the callback on the initial value
131 * @return the CallbackStore object associated with this callback
132 */
133 [[nodiscard]]
134 std::unique_ptr<CallbackStore> RegisterResetCallback(NotifyCallback callback,
135 bool initialNotify);
136
137 /**
138 * Check if the encoder has been reset.
139 *
140 * @return true if reset
141 */
142 bool GetReset() const;
143
144 /**
145 * Change the reset property of the encoder.
146 *
147 * @param reset the new value
148 */
149 void SetReset(bool reset);
150
151 /**
152 * Register a callback to be run whenever the max period of the encoder is
153 * changed.
154 *
155 * @param callback the callback
156 * @param initialNotify whether to run the callback on the initial value
157 * @return the CallbackStore object associated with this callback
158 */
159 [[nodiscard]]
160 std::unique_ptr<CallbackStore> RegisterMaxPeriodCallback(
161 NotifyCallback callback, bool initialNotify);
162
163 /**
164 * Get the max period of the encoder.
165 *
166 * @return the max period of the encoder
167 */
168 double GetMaxPeriod() const;
169
170 /**
171 * Change the max period of the encoder.
172 *
173 * @param maxPeriod the new value
174 */
175 void SetMaxPeriod(double maxPeriod);
176
177 /**
178 * Register a callback on the direction of the encoder.
179 *
180 * @param callback the callback that will be called whenever the direction
181 * is changed
182 * @param initialNotify if true, the callback will be run on the initial value
183 * @return the CallbackStore object associated with this callback
184 */
185 [[nodiscard]]
186 std::unique_ptr<CallbackStore> RegisterDirectionCallback(
187 NotifyCallback callback, bool initialNotify);
188
189 /**
190 * Get the direction of the encoder.
191 *
192 * @return the direction of the encoder
193 */
194 bool GetDirection() const;
195
196 /**
197 * Set the direction of the encoder.
198 *
199 * @param direction the new direction
200 */
201 void SetDirection(bool direction);
202
203 /**
204 * Register a callback on the reverse direction.
205 *
206 * @param callback the callback that will be called whenever the reverse
207 * direction is changed
208 * @param initialNotify if true, the callback will be run on the initial value
209 * @return the CallbackStore object associated with this callback
210 */
211 [[nodiscard]]
212 std::unique_ptr<CallbackStore> RegisterReverseDirectionCallback(
213 NotifyCallback callback, bool initialNotify);
214
215 /**
216 * Get the reverse direction of the encoder.
217 *
218 * @return the reverse direction of the encoder
219 */
221
222 /**
223 * Set the reverse direction.
224 *
225 * @param reverseDirection the new value
226 */
227 void SetReverseDirection(bool reverseDirection);
228
229 /**
230 * Register a callback on the samples-to-average value of this encoder.
231 *
232 * @param callback the callback that will be called whenever the
233 * samples-to-average is changed
234 * @param initialNotify if true, the callback will be run on the initial value
235 * @return the CallbackStore object associated with this callback
236 */
237 [[nodiscard]]
238 std::unique_ptr<CallbackStore> RegisterSamplesToAverageCallback(
239 NotifyCallback callback, bool initialNotify);
240
241 /**
242 * Get the samples-to-average value.
243 *
244 * @return the samples-to-average value
245 */
247
248 /**
249 * Set the samples-to-average value.
250 *
251 * @param samplesToAverage the new value
252 */
253 void SetSamplesToAverage(int samplesToAverage);
254
255 /**
256 * Register a callback on the distance per pulse value of this encoder.
257 *
258 * @param callback the callback that will be called whenever the
259 * distance per pulse is changed
260 * @param initialNotify if true, the callback will be run on the initial value
261 * @return the CallbackStore object associated with this callback
262 */
263 [[nodiscard]]
264 std::unique_ptr<CallbackStore> RegisterDistancePerPulseCallback(
265 NotifyCallback callback, bool initialNotify);
266
267 /**
268 * Read the distance per pulse of the encoder.
269 *
270 * @return the encoder distance per pulse
271 */
272 double GetDistancePerPulse() const;
273
274 /**
275 * Change the encoder distance per pulse.
276 *
277 * @param distancePerPulse the new distance per pulse
278 */
279 void SetDistancePerPulse(double distancePerPulse);
280
281 /**
282 * Resets all simulation data for this encoder.
283 */
284 void ResetData();
285
286 /**
287 * Change the encoder distance.
288 *
289 * @param distance the new distance
290 */
291 void SetDistance(double distance);
292
293 /**
294 * Read the distance of the encoder.
295 *
296 * @return the encoder distance
297 */
298 double GetDistance() const;
299
300 /**
301 * Change the rate of the encoder.
302 *
303 * @param rate the new rate
304 */
305 void SetRate(double rate);
306
307 /**
308 * Get the rate of the encoder.
309 *
310 * @return the rate of change
311 */
312 double GetRate() const;
313
314 private:
315 explicit EncoderSim(int index) : m_index{index} {}
316
317 int m_index;
318};
319} // namespace sim
320} // namespace frc
Class to read quad encoders.
Definition: Encoder.h:40
Class to control a simulated encoder.
Definition: EncoderSim.h:20
int GetSamplesToAverage() const
Get the samples-to-average value.
std::unique_ptr< CallbackStore > RegisterInitializedCallback(NotifyCallback callback, bool initialNotify)
Register a callback on the Initialized property of the encoder.
bool GetDirection() const
Get the direction of the encoder.
double GetRate() const
Get the rate of the encoder.
bool GetInitialized() const
Read the Initialized value of the encoder.
double GetPeriod() const
Read the period of the encoder.
std::unique_ptr< CallbackStore > RegisterReverseDirectionCallback(NotifyCallback callback, bool initialNotify)
Register a callback on the reverse direction.
EncoderSim(const Encoder &encoder)
Constructs from an Encoder object.
std::unique_ptr< CallbackStore > RegisterCountCallback(NotifyCallback callback, bool initialNotify)
Register a callback on the count property of the encoder.
void SetPeriod(double period)
Change the encoder period.
void ResetData()
Resets all simulation data for this encoder.
int GetCount() const
Read the count of the encoder.
void SetDirection(bool direction)
Set the direction of the encoder.
double GetMaxPeriod() const
Get the max period of the encoder.
double GetDistance() const
Read the distance of the encoder.
double GetDistancePerPulse() const
Read the distance per pulse of the encoder.
void SetMaxPeriod(double maxPeriod)
Change the max period of the encoder.
std::unique_ptr< CallbackStore > RegisterDistancePerPulseCallback(NotifyCallback callback, bool initialNotify)
Register a callback on the distance per pulse value of this encoder.
std::unique_ptr< CallbackStore > RegisterDirectionCallback(NotifyCallback callback, bool initialNotify)
Register a callback on the direction of the encoder.
std::unique_ptr< CallbackStore > RegisterResetCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be called whenever the encoder is reset.
std::unique_ptr< CallbackStore > RegisterSamplesToAverageCallback(NotifyCallback callback, bool initialNotify)
Register a callback on the samples-to-average value of this encoder.
std::unique_ptr< CallbackStore > RegisterMaxPeriodCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be run whenever the max period of the encoder is changed.
void SetSamplesToAverage(int samplesToAverage)
Set the samples-to-average value.
void SetCount(int count)
Change the count of the encoder.
bool GetReverseDirection() const
Get the reverse direction of the encoder.
void SetReverseDirection(bool reverseDirection)
Set the reverse direction.
std::unique_ptr< CallbackStore > RegisterPeriodCallback(NotifyCallback callback, bool initialNotify)
Register a callback on the period of the encoder.
void SetReset(bool reset)
Change the reset property of the encoder.
void SetDistance(double distance)
Change the encoder distance.
static EncoderSim CreateForChannel(int channel)
Creates an EncoderSim for a digital input channel.
void SetRate(double rate)
Change the rate of the encoder.
bool GetReset() const
Check if the encoder has been reset.
void SetInitialized(bool initialized)
Change the Initialized value of the encoder.
void SetDistancePerPulse(double distancePerPulse)
Change the encoder distance per pulse.
static EncoderSim CreateForIndex(int index)
Creates an EncoderSim for a simulated index.
constexpr auto count() -> size_t
Definition: core.h:1203
std::function< void(std::string_view, const HAL_Value *)> NotifyCallback
Definition: CallbackStore.h:14
Definition: AprilTagPoseEstimator.h:15