WPILibC++ 2025.3.2
Loading...
Searching...
No Matches
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/PWM.h>
10#include <hal/Types.h>
11#include <units/time.h>
14
15namespace frc {
16class AddressableLED;
17class DMA;
18
19/**
20 * Class implements the PWM generation in the FPGA.
21 *
22 * The values supplied as arguments for PWM outputs range from -1.0 to 1.0. They
23 * are mapped to the microseconds to keep the pulse high, with a range of 0
24 * (off) to 4096. Changes are immediately sent to the FPGA, and the update
25 * occurs at the next FPGA cycle (5.05ms). There is no delay.
26 */
27class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
28 public:
29 friend class AddressableLED;
30 friend class DMA;
31 /**
32 * Represents the amount to multiply the minimum servo-pulse pwm period by.
33 */
35 /**
36 * Don't skip pulses. PWM pulses occur every 5.05 ms
37 */
39 /**
40 * Skip every other pulse. PWM pulses occur every 10.10 ms
41 */
43 /**
44 * Skip three out of four pulses. PWM pulses occur every 20.20 ms
45 */
47 };
48
49 /**
50 * Allocate a PWM given a channel number.
51 *
52 * Checks channel value range and allocates the appropriate channel.
53 * The allocation is only done to help users ensure that they don't double
54 * assign channels.
55 *
56 * @param channel The PWM channel number. 0-9 are on-board, 10-19 are on the
57 * MXP port
58 * @param registerSendable If true, adds this instance to SendableRegistry
59 * and LiveWindow
60 */
61 explicit PWM(int channel, bool registerSendable = true);
62
63 PWM(PWM&&) = default;
64 PWM& operator=(PWM&&) = default;
65
66 /**
67 * Free the PWM channel.
68 *
69 * Free the resource associated with the PWM channel and set the value to 0.
70 */
71 ~PWM() override;
72
73 /**
74 * Set the PWM pulse time directly to the hardware.
75 *
76 * Write a microsecond value to a PWM channel.
77 *
78 * @param time Microsecond PWM value.
79 */
80 virtual void SetPulseTime(units::microsecond_t time);
81
82 /**
83 * Get the PWM pulse time directly from the hardware.
84 *
85 * Read a microsecond value from a PWM channel.
86 *
87 * @return Microsecond PWM control value.
88 */
89 virtual units::microsecond_t GetPulseTime() const;
90
91 /**
92 * Set the PWM value based on a position.
93 *
94 * This is intended to be used by servos.
95 *
96 * @pre SetBounds() called.
97 *
98 * @param pos The position to set the servo between 0.0 and 1.0.
99 */
100 virtual void SetPosition(double pos);
101
102 /**
103 * Get the PWM value in terms of a position.
104 *
105 * This is intended to be used by servos.
106 *
107 * @pre SetBounds() called.
108 *
109 * @return The position the servo is set to between 0.0 and 1.0.
110 */
111 virtual double GetPosition() const;
112
113 /**
114 * Set the PWM value based on a speed.
115 *
116 * This is intended to be used by motor controllers.
117 *
118 * @pre SetBounds() called.
119 *
120 * @param speed The speed to set the motor controller between -1.0 and 1.0.
121 */
122 virtual void SetSpeed(double speed);
123
124 /**
125 * Get the PWM value in terms of speed.
126 *
127 * This is intended to be used by motor controllers.
128 *
129 * @pre SetBounds() called.
130 *
131 * @return The most recently set speed between -1.0 and 1.0.
132 */
133 virtual double GetSpeed() const;
134
135 /**
136 * Temporarily disables the PWM output. The next set call will re-enable
137 * the output.
138 */
139 virtual void SetDisabled();
140
141 /**
142 * Slow down the PWM signal for old devices.
143 *
144 * @param mult The period multiplier to apply to this channel
145 */
147
148 /**
149 * Latches PWM to zero.
150 */
152
153 /**
154 * Optionally eliminate the deadband from a motor controller.
155 *
156 * @param eliminateDeadband If true, set the motor curve on the motor
157 * controller to eliminate the deadband in the middle
158 * of the range. Otherwise, keep the full range
159 * without modifying any values.
160 */
161 void EnableDeadbandElimination(bool eliminateDeadband);
162
163 /**
164 * Set the bounds on the PWM pulse widths.
165 *
166 * This sets the bounds on the PWM values for a particular type of controller.
167 * The values determine the upper and lower speeds as well as the deadband
168 * bracket.
169 *
170 * @param max The max PWM pulse width in us
171 * @param deadbandMax The high end of the deadband range pulse width in us
172 * @param center The center (off) pulse width in us
173 * @param deadbandMin The low end of the deadband pulse width in us
174 * @param min The minimum pulse width in us
175 */
176 void SetBounds(units::microsecond_t max, units::microsecond_t deadbandMax,
177 units::microsecond_t center, units::microsecond_t deadbandMin,
178 units::microsecond_t min);
179
180 /**
181 * Get the bounds on the PWM values.
182 *
183 * This gets the bounds on the PWM values for a particular each type of
184 * controller. The values determine the upper and lower speeds as well as the
185 * deadband bracket.
186 *
187 * @param max The maximum pwm value
188 * @param deadbandMax The high end of the deadband range
189 * @param center The center speed (off)
190 * @param deadbandMin The low end of the deadband range
191 * @param min The minimum pwm value
192 */
193 void GetBounds(units::microsecond_t* max, units::microsecond_t* deadbandMax,
194 units::microsecond_t* center,
195 units::microsecond_t* deadbandMin, units::microsecond_t* min);
196
197 /**
198 * Sets the PWM output to be a continuous high signal while enabled.
199 *
200 */
202
203 int GetChannel() const;
204
205 protected:
206 void InitSendable(wpi::SendableBuilder& builder) override;
207
208 private:
209 int m_channel;
211};
212
213} // namespace frc
A class for driving addressable LEDs, such as WS2812B, WS2815, and NeoPixels.
Definition AddressableLED.h:34
Class for configuring Direct Memory Access (DMA) of FPGA inputs.
Definition DMA.h:23
Class implements the PWM generation in the FPGA.
Definition PWM.h:27
PeriodMultiplier
Represents the amount to multiply the minimum servo-pulse pwm period by.
Definition PWM.h:34
@ kPeriodMultiplier_1X
Don't skip pulses.
Definition PWM.h:38
@ kPeriodMultiplier_4X
Skip three out of four pulses.
Definition PWM.h:46
@ kPeriodMultiplier_2X
Skip every other pulse.
Definition PWM.h:42
virtual void SetPosition(double pos)
Set the PWM value based on a position.
virtual units::microsecond_t GetPulseTime() const
Get the PWM pulse time directly from the hardware.
void GetBounds(units::microsecond_t *max, units::microsecond_t *deadbandMax, units::microsecond_t *center, units::microsecond_t *deadbandMin, units::microsecond_t *min)
Get the bounds on the PWM values.
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.
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.
virtual double GetPosition() const
Get the PWM value in terms of a position.
void SetAlwaysHighMode()
Sets the PWM output to be a continuous high signal while enabled.
void SetZeroLatch()
Latches PWM to zero.
void SetBounds(units::microsecond_t max, units::microsecond_t deadbandMax, units::microsecond_t center, units::microsecond_t deadbandMin, units::microsecond_t 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.
virtual void SetPulseTime(units::microsecond_t time)
Set the PWM pulse time directly to the hardware.
PWM(PWM &&)=default
virtual double GetSpeed() const
Get the PWM value in terms of speed.
A move-only C++ wrapper around a HAL handle.
Definition Types.h:96
Helper class for building Sendable dashboard representations.
Definition SendableBuilder.h:21
A helper class for use with objects that add themselves to SendableRegistry.
Definition SendableHelper.h:21
Interface for Sendable objects.
Definition Sendable.h:16
Definition CAN.h:11