WPILibC++ 2023.4.3
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]] std::unique_ptr<CallbackStore> RegisterInitializedCallback(
57 NotifyCallback callback, bool initialNotify);
58
59 /**
60 * Read the Initialized value of the encoder.
61 *
62 * @return true if initialized
63 */
64 bool GetInitialized() const;
65
66 /**
67 * Change the Initialized value of the encoder.
68 *
69 * @param initialized the new value
70 */
71 void SetInitialized(bool initialized);
72
73 /**
74 * Register a callback on the count property of the encoder.
75 *
76 * @param callback the callback that will be called whenever the count
77 * property is changed
78 * @param initialNotify if true, the callback will be run on the initial value
79 * @return the CallbackStore object associated with this callback
80 */
81 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterCountCallback(
82 NotifyCallback callback, bool initialNotify);
83
84 /**
85 * Read the count of the encoder.
86 *
87 * @return the count
88 */
89 int GetCount() const;
90
91 /**
92 * Change the count of the encoder.
93 *
94 * @param count the new count
95 */
96 void SetCount(int count);
97
98 /**
99 * Register a callback on the period of the encoder.
100 *
101 * @param callback the callback that will be called whenever the period is
102 * changed
103 * @param initialNotify if true, the callback will be run on the initial value
104 * @return the CallbackStore object associated with this callback
105 */
106 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterPeriodCallback(
107 NotifyCallback callback, bool initialNotify);
108
109 /**
110 * Read the period of the encoder.
111 *
112 * @return the encoder period
113 */
114 double GetPeriod() const;
115
116 /**
117 * Change the encoder period.
118 *
119 * @param period the new period
120 */
121 void SetPeriod(double period);
122
123 /**
124 * Register a callback to be called whenever the encoder is reset.
125 *
126 * @param callback the callback
127 * @param initialNotify whether to run the callback on the initial value
128 * @return the CallbackStore object associated with this callback
129 */
130 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterResetCallback(
131 NotifyCallback callback, bool initialNotify);
132
133 /**
134 * Check if the encoder has been reset.
135 *
136 * @return true if reset
137 */
138 bool GetReset() const;
139
140 /**
141 * Change the reset property of the encoder.
142 *
143 * @param reset the new value
144 */
145 void SetReset(bool reset);
146
147 /**
148 * Register a callback to be run whenever the max period of the encoder is
149 * changed.
150 *
151 * @param callback the callback
152 * @param initialNotify whether to run the callback on the initial value
153 * @return the CallbackStore object associated with this callback
154 */
155 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterMaxPeriodCallback(
156 NotifyCallback callback, bool initialNotify);
157
158 /**
159 * Get the max period of the encoder.
160 *
161 * @return the max period of the encoder
162 */
163 double GetMaxPeriod() const;
164
165 /**
166 * Change the max period of the encoder.
167 *
168 * @param maxPeriod the new value
169 */
170 void SetMaxPeriod(double maxPeriod);
171
172 /**
173 * Register a callback on the direction of the encoder.
174 *
175 * @param callback the callback that will be called whenever the direction
176 * is changed
177 * @param initialNotify if true, the callback will be run on the initial value
178 * @return the CallbackStore object associated with this callback
179 */
180 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterDirectionCallback(
181 NotifyCallback callback, bool initialNotify);
182
183 /**
184 * Get the direction of the encoder.
185 *
186 * @return the direction of the encoder
187 */
188 bool GetDirection() const;
189
190 /**
191 * Set the direction of the encoder.
192 *
193 * @param direction the new direction
194 */
195 void SetDirection(bool direction);
196
197 /**
198 * Register a callback on the reverse direction.
199 *
200 * @param callback the callback that will be called whenever the reverse
201 * direction is changed
202 * @param initialNotify if true, the callback will be run on the initial value
203 * @return the CallbackStore object associated with this callback
204 */
205 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterReverseDirectionCallback(
206 NotifyCallback callback, bool initialNotify);
207
208 /**
209 * Get the reverse direction of the encoder.
210 *
211 * @return the reverse direction of the encoder
212 */
214
215 /**
216 * Set the reverse direction.
217 *
218 * @param reverseDirection the new value
219 */
220 void SetReverseDirection(bool reverseDirection);
221
222 /**
223 * Register a callback on the samples-to-average value of this encoder.
224 *
225 * @param callback the callback that will be called whenever the
226 * samples-to-average is changed
227 * @param initialNotify if true, the callback will be run on the initial value
228 * @return the CallbackStore object associated with this callback
229 */
230 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterSamplesToAverageCallback(
231 NotifyCallback callback, bool initialNotify);
232
233 /**
234 * Get the samples-to-average value.
235 *
236 * @return the samples-to-average value
237 */
239
240 /**
241 * Set the samples-to-average value.
242 *
243 * @param samplesToAverage the new value
244 */
245 void SetSamplesToAverage(int samplesToAverage);
246
247 /**
248 * Register a callback on the distance per pulse value of this encoder.
249 *
250 * @param callback the callback that will be called whenever the
251 * distance per pulse is changed
252 * @param initialNotify if true, the callback will be run on the initial value
253 * @return the CallbackStore object associated with this callback
254 */
255 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterDistancePerPulseCallback(
256 NotifyCallback callback, bool initialNotify);
257
258 /**
259 * Read the distance per pulse of the encoder.
260 *
261 * @return the encoder distance per pulse
262 */
263 double GetDistancePerPulse() const;
264
265 /**
266 * Change the encoder distance per pulse.
267 *
268 * @param distancePerPulse the new distance per pulse
269 */
270 void SetDistancePerPulse(double distancePerPulse);
271
272 /**
273 * Resets all simulation data for this encoder.
274 */
275 void ResetData();
276
277 /**
278 * Change the encoder distance.
279 *
280 * @param distance the new distance
281 */
282 void SetDistance(double distance);
283
284 /**
285 * Read the distance of the encoder.
286 *
287 * @return the encoder distance
288 */
289 double GetDistance();
290
291 /**
292 * Change the rate of the encoder.
293 *
294 * @param rate the new rate
295 */
296 void SetRate(double rate);
297
298 /**
299 * Get the rate of the encoder.
300 *
301 * @return the rate of change
302 */
303 double GetRate();
304
305 private:
306 explicit EncoderSim(int index) : m_index{index} {}
307
308 int m_index;
309};
310} // namespace sim
311} // namespace frc
Class to read quad encoders.
Definition: Encoder.h:41
Class to control a simulated encoder.
Definition: EncoderSim.h:20
double GetRate()
Get the rate of the encoder.
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.
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 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.
double GetDistance()
Read the distance of the 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:1204
std::function< void(std::string_view, const HAL_Value *)> NotifyCallback
Definition: CallbackStore.h:14
Definition: AprilTagFieldLayout.h:22