WPILibC++ 2024.3.2
PWMSim.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 PWM;
14class PWMMotorController;
15
16namespace sim {
17
18/**
19 * Class to control a simulated PWM output.
20 */
21class PWMSim {
22 public:
23 /**
24 * Constructs from a PWM object.
25 *
26 * @param pwm PWM to simulate
27 */
28 explicit PWMSim(const PWM& pwm);
29
30 /**
31 * Constructs from a PWMMotorController object.
32 *
33 * @param motorctrl PWMMotorController to simulate
34 */
35 explicit PWMSim(const PWMMotorController& motorctrl);
36
37 /**
38 * Constructs from a PWM channel number.
39 *
40 * @param channel Channel number
41 */
42 explicit PWMSim(int channel);
43
44 /**
45 * Register a callback to be run when the PWM is initialized.
46 *
47 * @param callback the callback
48 * @param initialNotify whether to run the callback with the initial state
49 * @return the CallbackStore object associated with this callback
50 */
51 [[nodiscard]]
52 std::unique_ptr<CallbackStore> RegisterInitializedCallback(
53 NotifyCallback callback, bool initialNotify);
54
55 /**
56 * Check whether the PWM has been initialized.
57 *
58 * @return true if initialized
59 */
60 bool GetInitialized() const;
61
62 /**
63 * Define whether the PWM has been initialized.
64 *
65 * @param initialized whether this object is initialized
66 */
67 void SetInitialized(bool initialized);
68
69 /**
70 * Register a callback to be run when the PWM pulse microsecond value changes.
71 *
72 * @param callback the callback
73 * @param initialNotify whether to run the callback with the initial value
74 * @return the CallbackStore object associated with this callback
75 */
76 [[nodiscard]]
77 std::unique_ptr<CallbackStore> RegisterPulseMicrosecondCallback(
78 NotifyCallback callback, bool initialNotify);
79
80 /**
81 * Get the PWM pulse microsecond value.
82 *
83 * @return the PWM pulse microsecond value
84 */
85 int32_t GetPulseMicrosecond() const;
86
87 /**
88 * Set the PWM pulse microsecond value.
89 *
90 * @param microsecondPulseTime the PWM pulse microsecond value
91 */
92 void SetPulseMicrosecond(int32_t microsecondPulseTime);
93
94 /**
95 * Register a callback to be run when the PWM speed changes.
96 *
97 * @param callback the callback
98 * @param initialNotify whether to run the callback with the initial value
99 * @return the CallbackStore object associated with this callback
100 */
101 [[nodiscard]]
102 std::unique_ptr<CallbackStore> RegisterSpeedCallback(NotifyCallback callback,
103 bool initialNotify);
104
105 /**
106 * Get the PWM speed.
107 *
108 * @return the PWM speed (-1.0 to 1.0)
109 */
110 double GetSpeed() const;
111
112 /**
113 * Set the PWM speed.
114 *
115 * @param speed the PWM speed (-1.0 to 1.0)
116 */
117 void SetSpeed(double speed);
118
119 /**
120 * Register a callback to be run when the PWM position changes.
121 *
122 * @param callback the callback
123 * @param initialNotify whether to run the callback with the initial value
124 * @return the CallbackStore object associated with this callback
125 */
126 [[nodiscard]]
127 std::unique_ptr<CallbackStore> RegisterPositionCallback(
128 NotifyCallback callback, bool initialNotify);
129
130 /**
131 * Get the PWM position.
132 *
133 * @return the PWM position (0.0 to 1.0)
134 */
135 double GetPosition() const;
136
137 /**
138 * Set the PWM position.
139 *
140 * @param position the PWM position (0.0 to 1.0)
141 */
142 void SetPosition(double position);
143
144 /**
145 * Register a callback to be run when the PWM period scale changes.
146 *
147 * @param callback the callback
148 * @param initialNotify whether to run the callback with the initial value
149 * @return the CallbackStore object associated with this callback
150 */
151 [[nodiscard]]
152 std::unique_ptr<CallbackStore> RegisterPeriodScaleCallback(
153 NotifyCallback callback, bool initialNotify);
154
155 /**
156 * Get the PWM period scale.
157 *
158 * @return the PWM period scale
159 */
160 int GetPeriodScale() const;
161
162 /**
163 * Set the PWM period scale.
164 *
165 * @param periodScale the PWM period scale
166 */
167 void SetPeriodScale(int periodScale);
168
169 /**
170 * Register a callback to be run when the PWM zero latch state changes.
171 *
172 * @param callback the callback
173 * @param initialNotify whether to run the callback with the initial state
174 * @return the CallbackStore object associated with this callback
175 */
176 [[nodiscard]]
177 std::unique_ptr<CallbackStore> RegisterZeroLatchCallback(
178 NotifyCallback callback, bool initialNotify);
179
180 /**
181 * Check whether the PWM is zero latched.
182 *
183 * @return true if zero latched
184 */
185 bool GetZeroLatch() const;
186
187 /**
188 * Define whether the PWM has been zero latched.
189 *
190 * @param zeroLatch true to indicate zero latched
191 */
192 void SetZeroLatch(bool zeroLatch);
193
194 /**
195 * Reset all simulation data.
196 */
197 void ResetData();
198
199 private:
200 int m_index;
201};
202} // namespace sim
203} // namespace frc
Class implements the PWM generation in the FPGA.
Definition: PWM.h:26
Common base class for all PWM Motor Controllers.
Definition: PWMMotorController.h:35
Class to control a simulated PWM output.
Definition: PWMSim.h:21
PWMSim(const PWM &pwm)
Constructs from a PWM object.
double GetSpeed() const
Get the PWM speed.
void SetPeriodScale(int periodScale)
Set the PWM period scale.
void SetInitialized(bool initialized)
Define whether the PWM has been initialized.
bool GetInitialized() const
Check whether the PWM has been initialized.
double GetPosition() const
Get the PWM position.
std::unique_ptr< CallbackStore > RegisterPeriodScaleCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be run when the PWM period scale changes.
std::unique_ptr< CallbackStore > RegisterSpeedCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be run when the PWM speed changes.
PWMSim(const PWMMotorController &motorctrl)
Constructs from a PWMMotorController object.
void SetPosition(double position)
Set the PWM position.
bool GetZeroLatch() const
Check whether the PWM is zero latched.
int GetPeriodScale() const
Get the PWM period scale.
void ResetData()
Reset all simulation data.
std::unique_ptr< CallbackStore > RegisterPulseMicrosecondCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be run when the PWM pulse microsecond value changes.
PWMSim(int channel)
Constructs from a PWM channel number.
std::unique_ptr< CallbackStore > RegisterInitializedCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be run when the PWM is initialized.
std::unique_ptr< CallbackStore > RegisterPositionCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be run when the PWM position changes.
std::unique_ptr< CallbackStore > RegisterZeroLatchCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be run when the PWM zero latch state changes.
void SetPulseMicrosecond(int32_t microsecondPulseTime)
Set the PWM pulse microsecond value.
void SetSpeed(double speed)
Set the PWM speed.
int32_t GetPulseMicrosecond() const
Get the PWM pulse microsecond value.
void SetZeroLatch(bool zeroLatch)
Define whether the PWM has been zero latched.
std::function< void(std::string_view, const HAL_Value *)> NotifyCallback
Definition: CallbackStore.h:14
Definition: AprilTagPoseEstimator.h:15