From 2b58bbde0b94f7d41ae2e7e1370a1df0f2ad27c3 Mon Sep 17 00:00:00 2001 From: Zhiquan Yeo Date: Fri, 22 Sep 2023 16:03:43 -0400 Subject: [PATCH] [xrp] Add Reflectance sensor and rangefinder classes (#5673) Co-authored-by: Tyler Veness --- .../wpi/first/wpilibj/xrp/XRPRangefinder.java | 31 +++++++++++++++++ .../wpilibj/xrp/XRPReflectanceSensor.java | 31 +++++++++++++++++ .../main/native/cpp/xrp/XRPRangefinder.cpp | 11 ++++++ .../native/cpp/xrp/XRPReflectanceSensor.cpp | 15 ++++++++ .../native/include/frc/xrp/XRPRangefinder.h | 29 ++++++++++++++++ .../include/frc/xrp/XRPReflectanceSensor.h | 34 +++++++++++++++++++ 6 files changed, 151 insertions(+) create mode 100644 xrpVendordep/src/main/java/edu/wpi/first/wpilibj/xrp/XRPRangefinder.java create mode 100644 xrpVendordep/src/main/java/edu/wpi/first/wpilibj/xrp/XRPReflectanceSensor.java create mode 100644 xrpVendordep/src/main/native/cpp/xrp/XRPRangefinder.cpp create mode 100644 xrpVendordep/src/main/native/cpp/xrp/XRPReflectanceSensor.cpp create mode 100644 xrpVendordep/src/main/native/include/frc/xrp/XRPRangefinder.h create mode 100644 xrpVendordep/src/main/native/include/frc/xrp/XRPReflectanceSensor.h diff --git a/xrpVendordep/src/main/java/edu/wpi/first/wpilibj/xrp/XRPRangefinder.java b/xrpVendordep/src/main/java/edu/wpi/first/wpilibj/xrp/XRPRangefinder.java new file mode 100644 index 0000000000..ac2d72c1f8 --- /dev/null +++ b/xrpVendordep/src/main/java/edu/wpi/first/wpilibj/xrp/XRPRangefinder.java @@ -0,0 +1,31 @@ +// 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. + +package edu.wpi.first.wpilibj.xrp; + +import edu.wpi.first.math.util.Units; +import edu.wpi.first.wpilibj.AnalogInput; + +/** This class represents the ultrasonic rangefinder on an XRP robot. */ +public class XRPRangefinder { + private final AnalogInput m_rangefinder = new AnalogInput(2); + + /** + * Get the measured distance in meters. Distance further than 4m will be reported as 4m. + * + * @return distance in meters + */ + public double getDistanceMeters() { + return (m_rangefinder.getVoltage() / 5.0) * 4.0; + } + + /** + * Get the measured distance in inches. + * + * @return distance in inches + */ + public double getDistanceInches() { + return Units.metersToInches(getDistanceMeters()); + } +} diff --git a/xrpVendordep/src/main/java/edu/wpi/first/wpilibj/xrp/XRPReflectanceSensor.java b/xrpVendordep/src/main/java/edu/wpi/first/wpilibj/xrp/XRPReflectanceSensor.java new file mode 100644 index 0000000000..ebd5368db8 --- /dev/null +++ b/xrpVendordep/src/main/java/edu/wpi/first/wpilibj/xrp/XRPReflectanceSensor.java @@ -0,0 +1,31 @@ +// 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. + +package edu.wpi.first.wpilibj.xrp; + +import edu.wpi.first.wpilibj.AnalogInput; + +/** This class represents the reflectance sensor pair on an XRP robot. */ +public class XRPReflectanceSensor { + private final AnalogInput m_leftSensor = new AnalogInput(0); + private final AnalogInput m_rightSensor = new AnalogInput(1); + + /** + * Returns the reflectance value of the left sensor. + * + * @return value between 0.0 (white) and 1.0 (black). + */ + public double getLeftReflectanceValue() { + return m_leftSensor.getVoltage() / 5.0; + } + + /** + * Returns the reflectance value of the right sensor. + * + * @return value between 0.0 (white) and 1.0 (black). + */ + public double getRightReflectanceValue() { + return m_rightSensor.getVoltage() / 5.0; + } +} diff --git a/xrpVendordep/src/main/native/cpp/xrp/XRPRangefinder.cpp b/xrpVendordep/src/main/native/cpp/xrp/XRPRangefinder.cpp new file mode 100644 index 0000000000..c1222bde05 --- /dev/null +++ b/xrpVendordep/src/main/native/cpp/xrp/XRPRangefinder.cpp @@ -0,0 +1,11 @@ +// 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. + +#include "frc/xrp/XRPRangefinder.h" + +using namespace frc; + +units::meter_t XRPRangefinder::GetDistance() const { + return units::meter_t{m_rangefinder.GetVoltage() / 5.0 * 4.0}; +} diff --git a/xrpVendordep/src/main/native/cpp/xrp/XRPReflectanceSensor.cpp b/xrpVendordep/src/main/native/cpp/xrp/XRPReflectanceSensor.cpp new file mode 100644 index 0000000000..5f8177476c --- /dev/null +++ b/xrpVendordep/src/main/native/cpp/xrp/XRPReflectanceSensor.cpp @@ -0,0 +1,15 @@ +// 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. + +#include "frc/xrp/XRPReflectanceSensor.h" + +using namespace frc; + +double XRPReflectanceSensor::GetLeftReflectanceValue() const { + return m_leftSensor.GetVoltage() / 5.0; +} + +double XRPReflectanceSensor::GetRightReflectanceValue() const { + return m_rightSensor.GetVoltage() / 5.0; +} diff --git a/xrpVendordep/src/main/native/include/frc/xrp/XRPRangefinder.h b/xrpVendordep/src/main/native/include/frc/xrp/XRPRangefinder.h new file mode 100644 index 0000000000..5be78e6784 --- /dev/null +++ b/xrpVendordep/src/main/native/include/frc/xrp/XRPRangefinder.h @@ -0,0 +1,29 @@ +// 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. + +#pragma once + +#include + +#include + +namespace frc { + +/** + * This class represents the reflectance sensor pair + * on the XRP robot. + */ +class XRPRangefinder { + public: + /** + * Return the measured distance in meters. Distances further than 4 meters + * will be reported as 4 meters. + */ + units::meter_t GetDistance() const; + + private: + frc::AnalogInput m_rangefinder{2}; +}; + +} // namespace frc diff --git a/xrpVendordep/src/main/native/include/frc/xrp/XRPReflectanceSensor.h b/xrpVendordep/src/main/native/include/frc/xrp/XRPReflectanceSensor.h new file mode 100644 index 0000000000..8b85cd2b64 --- /dev/null +++ b/xrpVendordep/src/main/native/include/frc/xrp/XRPReflectanceSensor.h @@ -0,0 +1,34 @@ +// 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. + +#pragma once + +#include + +namespace frc { + +/** + * This class represents the reflectance sensor pair + * on the XRP robot. + */ +class XRPReflectanceSensor { + public: + /** + * Return the reflectance value of the left sensor. + * Value ranges from 0.0 (white) to 1.0 (black) + */ + double GetLeftReflectanceValue() const; + + /** + * Return the reflectance value of the right sensor. + * Value ranges from 0.0 (white) to 1.0 (black) + */ + double GetRightReflectanceValue() const; + + private: + frc::AnalogInput m_leftSensor{0}; + frc::AnalogInput m_rightSensor{1}; +}; + +} // namespace frc