[wpilib] Counter: Fix default distance per pulse, add distance and rate to C++ (#5796)

- Default distance per pulse in java was 0; 1 is a more reasonable default
- C++ was missing this functionality
This commit is contained in:
Ryan Blue
2023-10-20 02:42:53 -04:00
committed by GitHub
parent b14a61e1c0
commit abb2857e03
3 changed files with 42 additions and 1 deletions

View File

@@ -260,6 +260,18 @@ int Counter::GetFPGAIndex() const {
return m_index;
}
void Counter::SetDistancePerPulse(double distancePerPulse) {
m_distancePerPulse = distancePerPulse;
}
double Counter::GetDistance() const {
return Get() * m_distancePerPulse;
}
double Counter::GetRate() const {
return m_distancePerPulse / GetPeriod().value();
}
int Counter::Get() const {
int32_t status = 0;
int value = HAL_GetCounter(m_counter, &status);

View File

@@ -343,6 +343,34 @@ class Counter : public CounterBase,
int GetFPGAIndex() const;
/**
* Set the distance per pulse for this counter. This sets the multiplier used
* to determine the distance driven based on the count value from the encoder.
* Set this value based on the Pulses per Revolution and factor in any gearing
* reductions. This distance can be in any units you like, linear or angular.
*
* @param distancePerPulse The scale factor that will be used to convert
* pulses to useful units.
*/
void SetDistancePerPulse(double distancePerPulse);
/**
* Read the current scaled counter value. Read the value at this instant,
* scaled by the distance per pulse (defaults to 1).
*
* @return The distance since the last reset
*/
double GetDistance() const;
/**
* Get the current rate of the Counter. Read the current rate of the counter
* accounting for the distance per pulse value. The default value for distance
* per pulse (1) yields units of pulses per second.
*
* @return The rate in units/sec
*/
double GetRate() const;
// CounterBase interface
/**
* Read the current counter value.
@@ -434,6 +462,7 @@ class Counter : public CounterBase,
private:
int m_index = 0; // The index of this counter.
double m_distancePerPulse = 1;
friend class DigitalGlitchFilter;
};

View File

@@ -51,7 +51,7 @@ public class Counter implements CounterBase, Sendable, AutoCloseable {
private boolean m_allocatedDownSource;
int m_counter; // /< The FPGA counter object.
private int m_index; // /< The index of this counter.
private double m_distancePerPulse; // distance of travel for each tick
private double m_distancePerPulse = 1; // distance of travel for each tick
/**
* Create an instance of a counter with the given mode.