[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);
}