WPILibC++ 2023.4.3
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]] std::unique_ptr<CallbackStore> RegisterInitializedCallback(
52 NotifyCallback callback, bool initialNotify);
53
54 /**
55 * Check whether the PWM has been initialized.
56 *
57 * @return true if initialized
58 */
59 bool GetInitialized() const;
60
61 /**
62 * Define whether the PWM has been initialized.
63 *
64 * @param initialized whether this object is initialized
65 */
66 void SetInitialized(bool initialized);
67
68 /**
69 * Register a callback to be run when the PWM raw value changes.
70 *
71 * @param callback the callback
72 * @param initialNotify whether to run the callback with the initial value
73 * @return the CallbackStore object associated with this callback
74 */
75 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterRawValueCallback(
76 NotifyCallback callback, bool initialNotify);
77
78 /**
79 * Get the PWM raw value.
80 *
81 * @return the PWM raw value
82 */
83 int GetRawValue() const;
84
85 /**
86 * Set the PWM raw value.
87 *
88 * @param rawValue the PWM raw value
89 */
90 void SetRawValue(int rawValue);
91
92 /**
93 * Register a callback to be run when the PWM speed changes.
94 *
95 * @param callback the callback
96 * @param initialNotify whether to run the callback with the initial value
97 * @return the CallbackStore object associated with this callback
98 */
99 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterSpeedCallback(
100 NotifyCallback callback, bool initialNotify);
101
102 /**
103 * Get the PWM speed.
104 *
105 * @return the PWM speed (-1.0 to 1.0)
106 */
107 double GetSpeed() const;
108
109 /**
110 * Set the PWM speed.
111 *
112 * @param speed the PWM speed (-1.0 to 1.0)
113 */
114 void SetSpeed(double speed);
115
116 /**
117 * Register a callback to be run when the PWM position changes.
118 *
119 * @param callback the callback
120 * @param initialNotify whether to run the callback with the initial value
121 * @return the CallbackStore object associated with this callback
122 */
123 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterPositionCallback(
124 NotifyCallback callback, bool initialNotify);
125
126 /**
127 * Get the PWM position.
128 *
129 * @return the PWM position (0.0 to 1.0)
130 */
131 double GetPosition() const;
132
133 /**
134 * Set the PWM position.
135 *
136 * @param position the PWM position (0.0 to 1.0)
137 */
138 void SetPosition(double position);
139
140 /**
141 * Register a callback to be run when the PWM period scale changes.
142 *
143 * @param callback the callback
144 * @param initialNotify whether to run the callback with the initial value
145 * @return the CallbackStore object associated with this callback
146 */
147 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterPeriodScaleCallback(
148 NotifyCallback callback, bool initialNotify);
149
150 /**
151 * Get the PWM period scale.
152 *
153 * @return the PWM period scale
154 */
155 int GetPeriodScale() const;
156
157 /**
158 * Set the PWM period scale.
159 *
160 * @param periodScale the PWM period scale
161 */
162 void SetPeriodScale(int periodScale);
163
164 /**
165 * Register a callback to be run when the PWM zero latch state changes.
166 *
167 * @param callback the callback
168 * @param initialNotify whether to run the callback with the initial state
169 * @return the CallbackStore object associated with this callback
170 */
171 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterZeroLatchCallback(
172 NotifyCallback callback, bool initialNotify);
173
174 /**
175 * Check whether the PWM is zero latched.
176 *
177 * @return true if zero latched
178 */
179 bool GetZeroLatch() const;
180
181 /**
182 * Define whether the PWM has been zero latched.
183 *
184 * @param zeroLatch true to indicate zero latched
185 */
186 void SetZeroLatch(bool zeroLatch);
187
188 /**
189 * Reset all simulation data.
190 */
191 void ResetData();
192
193 private:
194 int m_index;
195};
196} // namespace sim
197} // namespace frc
Class implements the PWM generation in the FPGA.
Definition: PWM.h:34
Common base class for all PWM Motor Controllers.
Definition: PWMMotorController.h:26
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.
int GetRawValue() const
Get the PWM raw value.
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.
void SetRawValue(int rawValue)
Set the PWM raw value.
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.
std::unique_ptr< CallbackStore > RegisterRawValueCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be run when the PWM raw value changes.
void SetSpeed(double speed)
Set the PWM speed.
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: AprilTagFieldLayout.h:22