2024-05-24 15:55:30 -04:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hardware/range/SharpIR.hpp"
|
2024-05-24 15:55:30 -04:00
|
|
|
|
2024-09-20 17:43:39 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/UsageReporting.h"
|
2025-11-07 19:57:55 -05:00
|
|
|
#include "wpi/hardware/discrete/AnalogInput.hpp"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/units/length.hpp"
|
|
|
|
|
#include "wpi/util/sendable/SendableBuilder.hpp"
|
|
|
|
|
#include "wpi/util/sendable/SendableRegistry.hpp"
|
2024-05-24 15:55:30 -04:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
SharpIR SharpIR::GP2Y0A02YK0F(int channel) {
|
2025-02-10 07:23:04 -08:00
|
|
|
return SharpIR(channel, 62.28, -1.092, 20_cm, 150_cm);
|
2024-05-24 15:55:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SharpIR SharpIR::GP2Y0A21YK0F(int channel) {
|
2025-02-10 07:23:04 -08:00
|
|
|
return SharpIR(channel, 26.449, -1.226, 10_cm, 80_cm);
|
2024-05-24 15:55:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SharpIR SharpIR::GP2Y0A41SK0F(int channel) {
|
2025-02-10 07:23:04 -08:00
|
|
|
return SharpIR(channel, 12.354, -1.07, 4_cm, 30_cm);
|
2024-05-24 15:55:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SharpIR SharpIR::GP2Y0A51SK0F(int channel) {
|
2025-02-10 07:23:04 -08:00
|
|
|
return SharpIR(channel, 5.2819, -1.161, 2_cm, 15_cm);
|
2024-05-24 15:55:30 -04:00
|
|
|
}
|
|
|
|
|
|
2025-02-10 07:23:04 -08:00
|
|
|
SharpIR::SharpIR(int channel, double a, double b, units::meter_t min,
|
|
|
|
|
units::meter_t max)
|
|
|
|
|
: m_sensor(channel), m_A(a), m_B(b), m_min(min), m_max(max) {
|
2025-02-07 12:37:23 -08:00
|
|
|
HAL_ReportUsage("IO", channel, "SharpIR");
|
2025-01-25 10:52:19 -08:00
|
|
|
wpi::SendableRegistry::Add(this, "SharpIR", channel);
|
2024-05-24 15:55:30 -04:00
|
|
|
|
|
|
|
|
m_simDevice = hal::SimDevice("SharpIR", m_sensor.GetChannel());
|
|
|
|
|
if (m_simDevice) {
|
2025-02-10 07:23:04 -08:00
|
|
|
m_simRange = m_simDevice.CreateDouble("Range (m)", false, 0.0);
|
2024-05-24 15:55:30 -04:00
|
|
|
m_sensor.SetSimDevice(m_simDevice);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SharpIR::GetChannel() const {
|
|
|
|
|
return m_sensor.GetChannel();
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-10 07:23:04 -08:00
|
|
|
units::meter_t SharpIR::GetRange() const {
|
2024-05-24 15:55:30 -04:00
|
|
|
if (m_simRange) {
|
2025-02-10 07:23:04 -08:00
|
|
|
return std::clamp(units::meter_t{m_simRange.Get()}, m_min, m_max);
|
2024-05-24 15:55:30 -04:00
|
|
|
} else {
|
|
|
|
|
// Don't allow zero/negative values
|
|
|
|
|
auto v = std::max(m_sensor.GetVoltage(), 0.00001);
|
|
|
|
|
|
2025-02-10 07:23:04 -08:00
|
|
|
return std::clamp(units::meter_t{m_A * std::pow(v, m_B) * 1e-2}, m_min,
|
|
|
|
|
m_max);
|
|
|
|
|
}
|
2024-05-24 15:55:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SharpIR::InitSendable(wpi::SendableBuilder& builder) {
|
|
|
|
|
builder.SetSmartDashboardType("Ultrasonic");
|
|
|
|
|
builder.AddDoubleProperty(
|
|
|
|
|
"Value", [=, this] { return GetRange().value(); }, nullptr);
|
|
|
|
|
}
|