[wpilib] DutyCycleEncoder: add setting of duty cycle range (#3759)

As the sensor needs to maintain an actual duty cycle, it can't go all
the way from 0-100, so provide a way to set the min and max and linearly
map between the two.
This commit is contained in:
Peter Johnson
2021-12-05 14:28:08 -08:00
committed by GitHub
parent a5a56dd067
commit 3ef2dab465
3 changed files with 57 additions and 0 deletions

View File

@@ -121,6 +121,21 @@ class DutyCycleEncoder : public wpi::Sendable,
*/
units::turn_t Get() const;
/**
* Set the encoder duty cycle range. As the encoder needs to maintain a duty
* cycle, the duty cycle cannot go all the way to 0% or all the way to 100%.
* For example, an encoder with a 4096 us period might have a minimum duty
* cycle of 1 us / 4096 us and a maximum duty cycle of 4095 / 4096 us. Setting
* the range will result in an encoder duty cycle less than or equal to the
* minimum being output as 0 rotation, the duty cycle greater than or equal to
* the maximum being output as 1 rotation, and values in between linearly
* scaled from 0 to 1.
*
* @param min minimum duty cycle (0-1 range)
* @param max maximum duty cycle (0-1 range)
*/
void SetDutyCycleRange(double min, double max);
/**
* Set the distance per rotation of the encoder. This sets the multiplier used
* to determine the distance driven based on the rotation value from the
@@ -175,6 +190,8 @@ class DutyCycleEncoder : public wpi::Sendable,
double m_positionOffset = 0;
double m_distancePerRotation = 1.0;
mutable units::turn_t m_lastPosition{0.0};
double m_sensorMin = 0;
double m_sensorMax = 1;
hal::SimDevice m_simDevice;
hal::SimDouble m_simPosition;