[wpilib] Tachometer: Add function to return RPS (#3833)

This commit is contained in:
Oblarg
2021-12-26 18:52:18 -05:00
committed by GitHub
parent 84b15f0883
commit baacbc8e24
5 changed files with 77 additions and 8 deletions

View File

@@ -64,17 +64,21 @@ void Tachometer::SetEdgesPerRevolution(int edges) {
m_edgesPerRevolution = edges;
}
units::revolutions_per_minute_t Tachometer::GetRevolutionsPerMinute() const {
units::turns_per_second_t Tachometer::GetRevolutionsPerSecond() const {
auto period = GetPeriod();
if (period.to<double>() == 0) {
return units::revolutions_per_minute_t{0.0};
return units::turns_per_second_t{0.0};
}
int edgesPerRevolution = GetEdgesPerRevolution();
if (edgesPerRevolution == 0) {
return units::revolutions_per_minute_t{0.0};
return units::turns_per_second_t{0.0};
}
auto rotationHz = ((1.0 / edgesPerRevolution) / period);
return units::revolutions_per_minute_t{rotationHz.to<double>() * 60};
return units::turns_per_second_t{rotationHz.to<double>()};
}
units::revolutions_per_minute_t Tachometer::GetRevolutionsPerMinute() const {
return units::revolutions_per_minute_t{GetRevolutionsPerSecond()};
}
bool Tachometer::GetStopped() const {
@@ -111,6 +115,8 @@ void Tachometer::SetUpdateWhenEmpty(bool updateWhenEmpty) {
void Tachometer::InitSendable(wpi::SendableBuilder& builder) {
builder.SetSmartDashboardType("Tachometer");
builder.AddDoubleProperty(
"RPS", [&] { return GetRevolutionsPerSecond().to<double>(); }, nullptr);
builder.AddDoubleProperty(
"RPM", [&] { return GetRevolutionsPerMinute().to<double>(); }, nullptr);
}

View File

@@ -75,6 +75,15 @@ class Tachometer : public wpi::Sendable,
*/
void SetEdgesPerRevolution(int edges);
/**
* Gets the current tachometer revolutions per second.
*
* SetEdgesPerRevolution must be set with a non 0 value for this to work.
*
* @return Current RPS.
*/
units::turns_per_second_t GetRevolutionsPerSecond() const;
/**
* Gets the current tachometer revolutions per minute.
*

View File

@@ -99,13 +99,13 @@ public class Tachometer implements Sendable, AutoCloseable {
}
/**
* Gets the current tachometer revolution per minute.
* Gets the current tachometer revolutions per second.
*
* <p>setEdgesPerRevolution must be set with a non 0 value for this to return valid values.
*
* @return Current RPM.
* @return Current RPS.
*/
public double getRevolutionsPerMinute() {
public double getRevolutionsPerSecond() {
double period = getPeriod();
if (period == 0) {
return 0;
@@ -114,7 +114,18 @@ public class Tachometer implements Sendable, AutoCloseable {
if (edgesPerRevolution == 0) {
return 0;
}
return ((1.0 / edgesPerRevolution) / period) * 60;
return (1.0 / edgesPerRevolution) / period;
}
/**
* Gets the current tachometer revolutions per minute.
*
* <p>setEdgesPerRevolution must be set with a non 0 value for this to return valid values.
*
* @return Current RPM.
*/
public double getRevolutionsPerMinute() {
return getRevolutionsPerSecond() * 60;
}
/**
@@ -165,6 +176,7 @@ public class Tachometer implements Sendable, AutoCloseable {
@Override
public void initSendable(SendableBuilder builder) {
builder.setSmartDashboardType("Tachometer");
builder.addDoubleProperty("RPS", this::getRevolutionsPerSecond, null);
builder.addDoubleProperty("RPM", this::getRevolutionsPerMinute, null);
}
}

View File

@@ -77,6 +77,46 @@ public final class Units {
return Math.toDegrees(radians);
}
/**
* Converts given radians to rotations.
*
* @param radians The radians to convert.
* @return rotations Converted from radians.
*/
public static double radiansToRotations(double radians) {
return radians / (Math.PI * 2);
}
/**
* Converts given degrees to rotations.
*
* @param degrees The degrees to convert.
* @return rotations Converted from radians.
*/
public static double degreesToRotations(double degrees) {
return degrees / 360;
}
/**
* Converts given rotations to degrees.
*
* @param rotations The rotations to convert.
* @return degrees Converted from rotations.
*/
public static double rotationsToDegrees(double rotations) {
return rotations * 360;
}
/**
* Converts given rotations to radians.
*
* @param rotations The rotations to convert.
* @return radians Converted from rotations.
*/
public static double rotationsToRadians(double rotations) {
return rotations * 2 * Math.PI;
}
/**
* Converts rotations per minute to radians per second.
*

View File

@@ -46,6 +46,8 @@ UNIT_ADD(angular_velocity, radians_per_second, radians_per_second, rad_per_s,
unit<std::ratio<1>, units::category::angular_velocity_unit>)
UNIT_ADD(angular_velocity, degrees_per_second, degrees_per_second, deg_per_s,
compound_unit<angle::degrees, inverse<time::seconds>>)
UNIT_ADD(angular_velocity, turns_per_second, turns_per_second, tps,
compound_unit<angle::turns, inverse<time::seconds>>)
UNIT_ADD(angular_velocity, revolutions_per_minute, revolutions_per_minute, rpm,
unit<std::ratio<2, 60>, radians_per_second, std::ratio<1>>)
UNIT_ADD(angular_velocity, milliarcseconds_per_year, milliarcseconds_per_year,