mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +00:00
[wpilib][hal] PWM Raw using microseconds (#5283)
Co-authored-by: Joe <sciencewhiz@users.noreply.github.com>
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include <hal/Types.h>
|
||||
#include <units/time.h>
|
||||
#include <wpi/sendable/Sendable.h>
|
||||
#include <wpi/sendable/SendableHelper.h>
|
||||
|
||||
@@ -18,18 +19,9 @@ class DMA;
|
||||
* Class implements the PWM generation in the FPGA.
|
||||
*
|
||||
* The values supplied as arguments for PWM outputs range from -1.0 to 1.0. They
|
||||
* are mapped to the hardware dependent values, in this case 0-2000 for the
|
||||
* FPGA. Changes are immediately sent to the FPGA, and the update occurs at the
|
||||
* next FPGA cycle (5.005ms). There is no delay.
|
||||
*
|
||||
* As of revision 0.1.10 of the FPGA, the FPGA interprets the 0-2000 values as
|
||||
* follows:
|
||||
* - 2000 = maximum pulse width
|
||||
* - 1999 to 1001 = linear scaling from "full forward" to "center"
|
||||
* - 1000 = center value
|
||||
* - 999 to 2 = linear scaling from "center" to "full reverse"
|
||||
* - 1 = minimum pulse width (currently 0.5ms)
|
||||
* - 0 = disabled (i.e. PWM output is held low)
|
||||
* are mapped to the microseconds to keep the pulse high, with a range of 0
|
||||
* (off) to 4096. Changes are immediately sent to the FPGA, and the update
|
||||
* occurs at the next FPGA cycle (5.05ms). There is no delay.
|
||||
*/
|
||||
class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
|
||||
public:
|
||||
@@ -40,15 +32,15 @@ class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
|
||||
*/
|
||||
enum PeriodMultiplier {
|
||||
/**
|
||||
* Don't skip pulses. PWM pulses occur every 5.005 ms
|
||||
* Don't skip pulses. PWM pulses occur every 5.05 ms
|
||||
*/
|
||||
kPeriodMultiplier_1X = 1,
|
||||
/**
|
||||
* Skip every other pulse. PWM pulses occur every 10.010 ms
|
||||
* Skip every other pulse. PWM pulses occur every 10.10 ms
|
||||
*/
|
||||
kPeriodMultiplier_2X = 2,
|
||||
/**
|
||||
* Skip three out of four pulses. PWM pulses occur every 20.020 ms
|
||||
* Skip three out of four pulses. PWM pulses occur every 20.20 ms
|
||||
*/
|
||||
kPeriodMultiplier_4X = 4
|
||||
};
|
||||
@@ -78,30 +70,29 @@ class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
|
||||
PWM& operator=(PWM&&) = default;
|
||||
|
||||
/**
|
||||
* Set the PWM value directly to the hardware.
|
||||
* Set the PWM pulse time directly to the hardware.
|
||||
*
|
||||
* Write a raw value to a PWM channel.
|
||||
* Write a microsecond value to a PWM channel.
|
||||
*
|
||||
* @param value Raw PWM value.
|
||||
* @param time Microsecond PWM value.
|
||||
*/
|
||||
virtual void SetRaw(uint16_t value);
|
||||
virtual void SetPulseTime(units::microsecond_t time);
|
||||
|
||||
/**
|
||||
* Get the PWM value directly from the hardware.
|
||||
* Get the PWM pulse time directly from the hardware.
|
||||
*
|
||||
* Read a raw value from a PWM channel.
|
||||
* Read a microsecond value from a PWM channel.
|
||||
*
|
||||
* @return Raw PWM control value.
|
||||
* @return Microsecond PWM control value.
|
||||
*/
|
||||
virtual uint16_t GetRaw() const;
|
||||
virtual units::microsecond_t GetPulseTime() const;
|
||||
|
||||
/**
|
||||
* Set the PWM value based on a position.
|
||||
*
|
||||
* This is intended to be used by servos.
|
||||
*
|
||||
* @pre SetMaxPositivePwm() called.
|
||||
* @pre SetMinNegativePwm() called.
|
||||
* @pre SetBounds() called.
|
||||
*
|
||||
* @param pos The position to set the servo between 0.0 and 1.0.
|
||||
*/
|
||||
@@ -112,8 +103,7 @@ class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
|
||||
*
|
||||
* This is intended to be used by servos.
|
||||
*
|
||||
* @pre SetMaxPositivePwm() called.
|
||||
* @pre SetMinNegativePwm() called.
|
||||
* @pre SetBounds() called.
|
||||
*
|
||||
* @return The position the servo is set to between 0.0 and 1.0.
|
||||
*/
|
||||
@@ -124,11 +114,7 @@ class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
|
||||
*
|
||||
* This is intended to be used by motor controllers.
|
||||
*
|
||||
* @pre SetMaxPositivePwm() called.
|
||||
* @pre SetMinPositivePwm() called.
|
||||
* @pre SetCenterPwm() called.
|
||||
* @pre SetMaxNegativePwm() called.
|
||||
* @pre SetMinNegativePwm() called.
|
||||
* @pre SetBounds() called.
|
||||
*
|
||||
* @param speed The speed to set the motor controller between -1.0 and 1.0.
|
||||
*/
|
||||
@@ -139,10 +125,7 @@ class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
|
||||
*
|
||||
* This is intended to be used by motor controllers.
|
||||
*
|
||||
* @pre SetMaxPositivePwm() called.
|
||||
* @pre SetMinPositivePwm() called.
|
||||
* @pre SetMaxNegativePwm() called.
|
||||
* @pre SetMinNegativePwm() called.
|
||||
* @pre SetBounds() called.
|
||||
*
|
||||
* @return The most recently set speed between -1.0 and 1.0.
|
||||
*/
|
||||
@@ -180,46 +163,38 @@ class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
|
||||
* The values determine the upper and lower speeds as well as the deadband
|
||||
* bracket.
|
||||
*
|
||||
* @param max The max PWM pulse width in ms
|
||||
* @param deadbandMax The high end of the deadband range pulse width in ms
|
||||
* @param center The center (off) pulse width in ms
|
||||
* @param deadbandMin The low end of the deadband pulse width in ms
|
||||
* @param min The minimum pulse width in ms
|
||||
* @param max The max PWM pulse width in us
|
||||
* @param deadbandMax The high end of the deadband range pulse width in us
|
||||
* @param center The center (off) pulse width in us
|
||||
* @param deadbandMin The low end of the deadband pulse width in us
|
||||
* @param min The minimum pulse width in us
|
||||
*/
|
||||
void SetBounds(double max, double deadbandMax, double center,
|
||||
double deadbandMin, double min);
|
||||
|
||||
/**
|
||||
* Set the bounds on the PWM values.
|
||||
*
|
||||
* This sets the bounds on the PWM values for a particular each type of
|
||||
* controller. The values determine the upper and lower speeds as well as the
|
||||
* deadband bracket.
|
||||
*
|
||||
* @param max The Minimum pwm value
|
||||
* @param deadbandMax The high end of the deadband range
|
||||
* @param center The center speed (off)
|
||||
* @param deadbandMin The low end of the deadband range
|
||||
* @param min The minimum pwm value
|
||||
*/
|
||||
void SetRawBounds(int max, int deadbandMax, int center, int deadbandMin,
|
||||
int min);
|
||||
void SetBounds(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.
|
||||
*
|
||||
* This Gets the bounds on the PWM values for a particular each type of
|
||||
* This gets the bounds on the PWM values for a particular each type of
|
||||
* controller. The values determine the upper and lower speeds as well as the
|
||||
* deadband bracket.
|
||||
*
|
||||
* @param max The Minimum pwm value
|
||||
* @param max The maximum pwm value
|
||||
* @param deadbandMax The high end of the deadband range
|
||||
* @param center The center speed (off)
|
||||
* @param deadbandMin The low end of the deadband range
|
||||
* @param min The minimum pwm value
|
||||
*/
|
||||
void GetRawBounds(int32_t* max, int32_t* deadbandMax, int32_t* center,
|
||||
int32_t* deadbandMin, int32_t* min);
|
||||
void GetBounds(units::microsecond_t* max, units::microsecond_t* deadbandMax,
|
||||
units::microsecond_t* center,
|
||||
units::microsecond_t* deadbandMin, units::microsecond_t* min);
|
||||
|
||||
/**
|
||||
* Sets the PWM output to be a continuous high signal while enabled.
|
||||
*
|
||||
*/
|
||||
void SetAlwaysHighMode();
|
||||
|
||||
int GetChannel() const;
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <units/angle.h>
|
||||
|
||||
#include "frc/PWM.h"
|
||||
|
||||
namespace frc {
|
||||
@@ -98,11 +100,11 @@ class Servo : public PWM {
|
||||
private:
|
||||
double GetServoAngleRange() const;
|
||||
|
||||
static constexpr double kMaxServoAngle = 180.0;
|
||||
static constexpr double kMaxServoAngle = 180.;
|
||||
static constexpr double kMinServoAngle = 0.0;
|
||||
|
||||
static constexpr double kDefaultMaxServoPWM = 2.4;
|
||||
static constexpr double kDefaultMinServoPWM = 0.6;
|
||||
static constexpr units::millisecond_t kDefaultMaxServoPWM = 2.4_ms;
|
||||
static constexpr units::millisecond_t kDefaultMinServoPWM = 0.6_ms;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
|
||||
@@ -67,29 +67,29 @@ class PWMSim {
|
||||
void SetInitialized(bool initialized);
|
||||
|
||||
/**
|
||||
* Register a callback to be run when the PWM raw value changes.
|
||||
* Register a callback to be run when the PWM pulse microsecond value changes.
|
||||
*
|
||||
* @param callback the callback
|
||||
* @param initialNotify whether to run the callback with the initial value
|
||||
* @return the CallbackStore object associated with this callback
|
||||
*/
|
||||
[[nodiscard]]
|
||||
std::unique_ptr<CallbackStore> RegisterRawValueCallback(
|
||||
std::unique_ptr<CallbackStore> RegisterPulseMicrosecondCallback(
|
||||
NotifyCallback callback, bool initialNotify);
|
||||
|
||||
/**
|
||||
* Get the PWM raw value.
|
||||
* Get the PWM pulse microsecond value.
|
||||
*
|
||||
* @return the PWM raw value
|
||||
* @return the PWM pulse microsecond value
|
||||
*/
|
||||
int GetRawValue() const;
|
||||
int32_t GetPulseMicrosecond() const;
|
||||
|
||||
/**
|
||||
* Set the PWM raw value.
|
||||
* Set the PWM pulse microsecond value.
|
||||
*
|
||||
* @param rawValue the PWM raw value
|
||||
* @param microsecondPulseTime the PWM pulse microsecond value
|
||||
*/
|
||||
void SetRawValue(int rawValue);
|
||||
void SetPulseMicrosecond(int32_t microsecondPulseTime);
|
||||
|
||||
/**
|
||||
* Register a callback to be run when the PWM speed changes.
|
||||
|
||||
Reference in New Issue
Block a user