[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

@@ -89,6 +89,14 @@ units::turn_t DutyCycleEncoder::Get() const {
auto counter2 = m_counter->Get();
auto pos2 = m_dutyCycle->GetOutput();
if (counter == counter2 && pos == pos2) {
// map sensor range
if (pos < m_sensorMin) {
pos = m_sensorMin;
}
if (pos > m_sensorMax) {
pos = m_sensorMax;
}
pos = (pos - m_sensorMin) / (m_sensorMax - m_sensorMin);
units::turn_t turns{counter + pos - m_positionOffset};
m_lastPosition = turns;
return turns;
@@ -102,6 +110,11 @@ units::turn_t DutyCycleEncoder::Get() const {
return m_lastPosition;
}
void DutyCycleEncoder::SetDutyCycleRange(double min, double max) {
m_sensorMin = std::clamp(min, 0.0, 1.0);
m_sensorMax = std::clamp(max, 0.0, 1.0);
}
void DutyCycleEncoder::SetDistancePerRotation(double distancePerRotation) {
m_distancePerRotation = distancePerRotation;
m_simDistancePerRotation.Set(distancePerRotation);