Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpilib] Counter: fix default distance per pulse, give c++ parity #5796

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions wpilibc/src/main/native/cpp/Counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 29 additions & 0 deletions wpilibc/src/main/native/include/frc/Counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
};
Expand Down
2 changes: 1 addition & 1 deletion wpilibj/src/main/java/edu/wpi/first/wpilibj/Counter.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public enum Mode {
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.
Expand Down
Loading