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