[wpilibc] Add units to Ultrasonic class API (#3403)

This commit is contained in:
Tyler Veness
2021-06-01 21:54:56 -07:00
committed by GitHub
parent fe570e000c
commit d9eeb45b03
2 changed files with 11 additions and 25 deletions

View File

@@ -137,23 +137,17 @@ void Ultrasonic::SetAutomaticMode(bool enabling) {
}
}
double Ultrasonic::GetRangeInches() const {
units::meter_t Ultrasonic::GetRange() const {
if (IsRangeValid()) {
if (m_simRange) {
return m_simRange.Get();
return units::meter_t{m_simRange.Get()};
}
return units::inch_t{m_counter.GetPeriod() * kSpeedOfSound / 2.0}
.to<double>();
return m_counter.GetPeriod() * kSpeedOfSound / 2.0;
} else {
return 0;
return 0_m;
}
}
double Ultrasonic::GetRangeMM() const {
return units::millimeter_t{m_counter.GetPeriod() * kSpeedOfSound / 2.0}
.to<double>();
}
bool Ultrasonic::IsEnabled() const {
return m_enabled;
}
@@ -165,7 +159,7 @@ void Ultrasonic::SetEnabled(bool enable) {
void Ultrasonic::InitSendable(SendableBuilder& builder) {
builder.SetSmartDashboardType("Ultrasonic");
builder.AddDoubleProperty(
"Value", [=]() { return GetRangeInches(); }, nullptr);
"Value", [=] { return units::inch_t{GetRange()}.to<double>(); }, nullptr);
}
void Ultrasonic::Initialize() {

View File

@@ -10,6 +10,7 @@
#include <vector>
#include <hal/SimDevice.h>
#include <units/length.h>
#include <units/time.h>
#include <units/velocity.h>
@@ -123,22 +124,13 @@ class Ultrasonic : public Sendable, public SendableHelper<Ultrasonic> {
static void SetAutomaticMode(bool enabling);
/**
* Get the range in inches from the ultrasonic sensor.
* Get the range from the ultrasonic sensor.
*
* @return Range in inches of the target returned from the ultrasonic sensor.
* If there is no valid value yet, i.e. at least one measurement
* hasn't completed, then return 0.
* @return Range of the target returned from the ultrasonic sensor. If there
* is no valid value yet, i.e. at least one measurement hasn't
* completed, then return 0.
*/
double GetRangeInches() const;
/**
* Get the range in millimeters from the ultrasonic sensor.
*
* @return Range in millimeters of the target returned by the ultrasonic
* sensor. If there is no valid value yet, i.e. at least one
* measurement hasn't completed, then return 0.
*/
double GetRangeMM() const;
units::meter_t GetRange() const;
bool IsEnabled() const;