WPILibC++ 2023.4.3
PWM.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 <stdint.h>
8
9#include <hal/Types.h>
12
13namespace frc {
14class AddressableLED;
15class DMA;
16
17/**
18 * Class implements the PWM generation in the FPGA.
19 *
20 * The values supplied as arguments for PWM outputs range from -1.0 to 1.0. They
21 * are mapped to the hardware dependent values, in this case 0-2000 for the
22 * FPGA. Changes are immediately sent to the FPGA, and the update occurs at the
23 * next FPGA cycle (5.005ms). There is no delay.
24 *
25 * As of revision 0.1.10 of the FPGA, the FPGA interprets the 0-2000 values as
26 * follows:
27 * - 2000 = maximum pulse width
28 * - 1999 to 1001 = linear scaling from "full forward" to "center"
29 * - 1000 = center value
30 * - 999 to 2 = linear scaling from "center" to "full reverse"
31 * - 1 = minimum pulse width (currently 0.5ms)
32 * - 0 = disabled (i.e. PWM output is held low)
33 */
34class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
35 public:
36 friend class AddressableLED;
37 friend class DMA;
38 /**
39 * Represents the amount to multiply the minimum servo-pulse pwm period by.
40 */
42 /**
43 * Don't skip pulses. PWM pulses occur every 5.005 ms
44 */
46 /**
47 * Skip every other pulse. PWM pulses occur every 10.010 ms
48 */
50 /**
51 * Skip three out of four pulses. PWM pulses occur every 20.020 ms
52 */
54 };
55
56 /**
57 * Allocate a PWM given a channel number.
58 *
59 * Checks channel value range and allocates the appropriate channel.
60 * The allocation is only done to help users ensure that they don't double
61 * assign channels.
62 *
63 * @param channel The PWM channel number. 0-9 are on-board, 10-19 are on the
64 * MXP port
65 * @param registerSendable If true, adds this instance to SendableRegistry
66 * and LiveWindow
67 */
68 explicit PWM(int channel, bool registerSendable = true);
69
70 /**
71 * Free the PWM channel.
72 *
73 * Free the resource associated with the PWM channel and set the value to 0.
74 */
75 ~PWM() override;
76
77 PWM(PWM&&) = default;
78 PWM& operator=(PWM&&) = default;
79
80 /**
81 * Set the PWM value directly to the hardware.
82 *
83 * Write a raw value to a PWM channel.
84 *
85 * @param value Raw PWM value.
86 */
87 virtual void SetRaw(uint16_t value);
88
89 /**
90 * Get the PWM value directly from the hardware.
91 *
92 * Read a raw value from a PWM channel.
93 *
94 * @return Raw PWM control value.
95 */
96 virtual uint16_t GetRaw() const;
97
98 /**
99 * Set the PWM value based on a position.
100 *
101 * This is intended to be used by servos.
102 *
103 * @pre SetMaxPositivePwm() called.
104 * @pre SetMinNegativePwm() called.
105 *
106 * @param pos The position to set the servo between 0.0 and 1.0.
107 */
108 virtual void SetPosition(double pos);
109
110 /**
111 * Get the PWM value in terms of a position.
112 *
113 * This is intended to be used by servos.
114 *
115 * @pre SetMaxPositivePwm() called.
116 * @pre SetMinNegativePwm() called.
117 *
118 * @return The position the servo is set to between 0.0 and 1.0.
119 */
120 virtual double GetPosition() const;
121
122 /**
123 * Set the PWM value based on a speed.
124 *
125 * This is intended to be used by motor controllers.
126 *
127 * @pre SetMaxPositivePwm() called.
128 * @pre SetMinPositivePwm() called.
129 * @pre SetCenterPwm() called.
130 * @pre SetMaxNegativePwm() called.
131 * @pre SetMinNegativePwm() called.
132 *
133 * @param speed The speed to set the motor controller between -1.0 and 1.0.
134 */
135 virtual void SetSpeed(double speed);
136
137 /**
138 * Get the PWM value in terms of speed.
139 *
140 * This is intended to be used by motor controllers.
141 *
142 * @pre SetMaxPositivePwm() called.
143 * @pre SetMinPositivePwm() called.
144 * @pre SetMaxNegativePwm() called.
145 * @pre SetMinNegativePwm() called.
146 *
147 * @return The most recently set speed between -1.0 and 1.0.
148 */
149 virtual double GetSpeed() const;
150
151 /**
152 * Temporarily disables the PWM output. The next set call will re-enable
153 * the output.
154 */
155 virtual void SetDisabled();
156
157 /**
158 * Slow down the PWM signal for old devices.
159 *
160 * @param mult The period multiplier to apply to this channel
161 */
163
165
166 /**
167 * Optionally eliminate the deadband from a motor controller.
168 *
169 * @param eliminateDeadband If true, set the motor curve on the motor
170 * controller to eliminate the deadband in the middle
171 * of the range. Otherwise, keep the full range
172 * without modifying any values.
173 */
174 void EnableDeadbandElimination(bool eliminateDeadband);
175
176 /**
177 * Set the bounds on the PWM pulse widths.
178 *
179 * This sets the bounds on the PWM values for a particular type of controller.
180 * The values determine the upper and lower speeds as well as the deadband
181 * bracket.
182 *
183 * @param max The max PWM pulse width in ms
184 * @param deadbandMax The high end of the deadband range pulse width in ms
185 * @param center The center (off) pulse width in ms
186 * @param deadbandMin The low end of the deadband pulse width in ms
187 * @param min The minimum pulse width in ms
188 */
189 void SetBounds(double max, double deadbandMax, double center,
190 double deadbandMin, double min);
191
192 /**
193 * Set the bounds on the PWM values.
194 *
195 * This sets the bounds on the PWM values for a particular each type of
196 * controller. The values determine the upper and lower speeds as well as the
197 * deadband bracket.
198 *
199 * @param max The Minimum pwm value
200 * @param deadbandMax The high end of the deadband range
201 * @param center The center speed (off)
202 * @param deadbandMin The low end of the deadband range
203 * @param min The minimum pwm value
204 */
205 void SetRawBounds(int max, int deadbandMax, int center, int deadbandMin,
206 int min);
207
208 /**
209 * Get the bounds on the PWM values.
210 *
211 * This Gets the bounds on the PWM values for a particular each type of
212 * controller. The values determine the upper and lower speeds as well as the
213 * deadband bracket.
214 *
215 * @param max The Minimum pwm value
216 * @param deadbandMax The high end of the deadband range
217 * @param center The center speed (off)
218 * @param deadbandMin The low end of the deadband range
219 * @param min The minimum pwm value
220 */
221 void GetRawBounds(int32_t* max, int32_t* deadbandMax, int32_t* center,
222 int32_t* deadbandMin, int32_t* min);
223
224 int GetChannel() const;
225
226 protected:
227 void InitSendable(wpi::SendableBuilder& builder) override;
228
229 private:
230 int m_channel;
231 hal::Handle<HAL_DigitalHandle> m_handle;
232};
233
234} // namespace frc
A class for driving addressable LEDs, such as WS2812s and NeoPixels.
Definition: AddressableLED.h:24
Definition: DMA.h:20
Class implements the PWM generation in the FPGA.
Definition: PWM.h:34
virtual void SetRaw(uint16_t value)
Set the PWM value directly to the hardware.
PeriodMultiplier
Represents the amount to multiply the minimum servo-pulse pwm period by.
Definition: PWM.h:41
@ kPeriodMultiplier_1X
Don't skip pulses.
Definition: PWM.h:45
@ kPeriodMultiplier_4X
Skip three out of four pulses.
Definition: PWM.h:53
@ kPeriodMultiplier_2X
Skip every other pulse.
Definition: PWM.h:49
void SetRawBounds(int max, int deadbandMax, int center, int deadbandMin, int min)
Set the bounds on the PWM values.
virtual void SetPosition(double pos)
Set the PWM value based on a position.
void EnableDeadbandElimination(bool eliminateDeadband)
Optionally eliminate the deadband from a motor controller.
virtual void SetDisabled()
Temporarily disables the PWM output.
virtual void SetSpeed(double speed)
Set the PWM value based on a speed.
virtual uint16_t GetRaw() const
Get the PWM value directly from the hardware.
int GetChannel() const
PWM(int channel, bool registerSendable=true)
Allocate a PWM given a channel number.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
void GetRawBounds(int32_t *max, int32_t *deadbandMax, int32_t *center, int32_t *deadbandMin, int32_t *min)
Get the bounds on the PWM values.
virtual double GetPosition() const
Get the PWM value in terms of a position.
void SetZeroLatch()
void SetBounds(double max, double deadbandMax, double center, double deadbandMin, double min)
Set the bounds on the PWM pulse widths.
~PWM() override
Free the PWM channel.
PWM & operator=(PWM &&)=default
void SetPeriodMultiplier(PeriodMultiplier mult)
Slow down the PWM signal for old devices.
PWM(PWM &&)=default
virtual double GetSpeed() const
Get the PWM value in terms of speed.
Definition: core.h:1240
Definition: SendableBuilder.h:18
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:19
Interface for Sendable objects.
Definition: Sendable.h:16
constexpr common_t< T1, T2 > max(const T1 x, const T2 y) noexcept
Compile-time pairwise maximum function.
Definition: max.hpp:35
constexpr common_t< T1, T2 > min(const T1 x, const T2 y) noexcept
Compile-time pairwise minimum function.
Definition: min.hpp:35
::uint16_t uint16_t
Definition: Meta.h:54
::int32_t int32_t
Definition: Meta.h:57
Definition: AprilTagFieldLayout.h:22