From 2b0250e19a58a85d90bb183cc5bba016c1e20bdd Mon Sep 17 00:00:00 2001 From: Miniongolf Date: Tue, 4 Mar 2025 13:06:58 -0500 Subject: [PATCH 1/4] Add distance sensor header files --- include/hardware/Distance/DistanceSensor.hpp | 64 +++++++++ .../hardware/Distance/V5DistanceSensor.hpp | 127 ++++++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 include/hardware/Distance/DistanceSensor.hpp create mode 100644 include/hardware/Distance/V5DistanceSensor.hpp diff --git a/include/hardware/Distance/DistanceSensor.hpp b/include/hardware/Distance/DistanceSensor.hpp new file mode 100644 index 0000000..429facf --- /dev/null +++ b/include/hardware/Distance/DistanceSensor.hpp @@ -0,0 +1,64 @@ +#pragma once + +#include "hardware/Device.hpp" +#include "units/units.hpp" + +namespace lemlib { +/** + * @brief abstract class for distance sensors + * + * An distance sensor is a device that measures the distance to an external object. This class is abstract, and must be + * implemented by a subclass. + */ +class DistanceSensor : public Device { + public: + /** + * @brief Get the distance measured by the distance sensor + * + * The distance measured by the encoder is the distance from the sensor to the nearest object. + * + * @return Length the distance measured by the encoder + * @return INFINITY if there is an error, setting errno + * + * @b Example: + * @code {.cpp} + * void initialize() { + * DistanceSensor* distanceSensor; + * const Length distance = distanceSensor->getDistance(); + * if (distance == INFINITY) { + * std::cout << "Error getting distance!" << std::endl; + * } else { + * std::cout << "Distance: " << distance.convert(in) << std::endl; + * } + * } + * @endcode + */ + virtual Length getDistance() const = 0; + /** + * @brief Set the offset of the distance sensor + * + * This function sets the offset of the distance sensor. A positive offset increases distance readings while a + * negative offset decreases them. This function is non-blocking. + * + * @param distance the length to set the offset to + * @return 0 on success + * @return INT_MAX on failure, setting errno + * + * @b Example: + * @code {.cpp} + * void initialize() { + * DistanceSensor* distanceSensor; + * if (distanceSensor->setOffset(1_in) == 0) { + * std::cout << "Offset set!" << std::endl; + * // With an object touching the surface of the distance sensor + * std::cout < "Offset distance: " << distanceSensor->getDistance().convert(in) << std::endl; // outputs 1 inch + * } else { + * std::cout << "Error setting offset!" << std::endl; + * } + * } + * @endcode + */ + virtual int32_t setOffset(Length distance) = 0; + virtual ~DistanceSensor() = default; +}; +} // namespace lemlib \ No newline at end of file diff --git a/include/hardware/Distance/V5DistanceSensor.hpp b/include/hardware/Distance/V5DistanceSensor.hpp new file mode 100644 index 0000000..82a8c34 --- /dev/null +++ b/include/hardware/Distance/V5DistanceSensor.hpp @@ -0,0 +1,127 @@ +#pragma once + +#include "hardware/Distance/DistanceSensor.hpp" +#include "hardware/Port.hpp" +#include "pros/distance.hpp" + +namespace lemlib { +/** + * @brief Distance sensor implementation for the V5 Distance sensor + * + */ +class V5DistanceSensor : public DistanceSensor { + public: + /** + * @brief Construct a new V5 Distance Sensor + * + * @param port the unsigned port of the distance sensor. + * + * @b Example: + * @code {.cpp} + * void initialize() { + * // distance sensor on port 1 + * lemlib::V5DistanceSensor distance(1); + * } + * @endcode + */ + V5DistanceSensor(SmartPort port); + /** + * @brief V5DistanceSensor copy constructor + * + * Because pros::Mutex does not have a copy constructor, an explicit + * copy constructor for the V5DistanceSensor is necessary + * + * @param other the V5DistanceSensor to copy + */ + V5DistanceSensor(const V5DistanceSensor& other); + /** + * @brief Create a new V5 Distance Sensor + * + * @param encoder the pros::Distance object to use + * + * @b Example: + * @code {.cpp} + * void initialize() { + * lemlib::V5DistanceSensor distance = lemlib::V5RDistanceSensor::from_pros_dist(pros::Distance(1)); + * } + * @endcode + */ + static V5DistanceSensor from_pros_dist(pros::Distance distanceSensor); + /** + * @brief whether the V5 Distance Sensor is connected + * + * @return 0 if its not connected + * @return 1 if it is connected + * + * @b Example: + * @code {.cpp} + * void initialize() { + * lemlib::V5DistanceSensor distance = pros::Distance(1); + * const int result = distance.isConnected(); + * if (result == 1) { + * std::cout << "Distance sensor is connected!" << std::endl; + * } else if (result == 0) { + * std::cout << "Distance sensor is not connected!" << std::endl; + * } else { + * std::cout << "Error checking if distance sensor is connected!" << std::endl; + * } + * } + * @endcode + */ + int32_t isConnected() const override; + /** + * @brief Get the distance measured by the V5 Distance Sensor + * + * This function uses the following values of errno when an error state is reached: + * + * ENXIO: the port is not within the range of valid ports (1-21) + * ENODEV: the port cannot be configured as an V5 Distance sensor + * + * @return Length the relative distance measured by the distance sensor + * @return INFINITY if there is an error, setting errno + * + * @b Example: + * @code {.cpp} + * void initialize() { + * lemlib::V5DistanceSensor distanceSensor = pros::Distance(1); + * const Length distance = distanceSensor.getDistance(); + * if (angle == INFINITY) { + * std::cout << "Error getting distance!" << std::endl; + * } else { + * std::cout << "Distance: " << distance.convert(in) << std::endl; + * } + * } + * @endcode + */ + Length getDistance() const override; + /** + * @brief Set the offset of the distance sensor + * + * This function sets the offset of the distance sensor. A positive offset increases distance readings while a + * negative offset decreases them. This function is non-blocking. + * + * @param distance the length to set the offset to + * @return 0 on success + * @return INT_MAX on failure, setting errno + * + * @b Example: + * @code {.cpp} + * void initialize() { + * DistanceSensor* distanceSensor; + * if (distanceSensor->setOffset(1_in) == 0) { + * std::cout << "Offset set!" << std::endl; + * // With an object touching the surface of the distance sensor + * std::cout < "Offset distance: " << distanceSensor->getDistance().convert(in) << std::endl; // outputs 1 inch + * } else { + * std::cout << "Error setting offset!" << std::endl; + * } + * } + * @endcode + */ + int32_t setOffset(Length distance) override; + private: + mutable pros::Mutex m_mutex; + Length m_offset = 0_in; + int m_port; +}; +} // namespace lemlib \ No newline at end of file From 155451c0756fef1356d24dcd13a2cbb7f804a1fc Mon Sep 17 00:00:00 2001 From: Miniongolf Date: Tue, 4 Mar 2025 13:16:24 -0500 Subject: [PATCH 2/4] Add src file for v5 distance sensor --- src/hardware/Distance/V5DistanceSensor.cpp | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/hardware/Distance/V5DistanceSensor.cpp diff --git a/src/hardware/Distance/V5DistanceSensor.cpp b/src/hardware/Distance/V5DistanceSensor.cpp new file mode 100644 index 0000000..5c83df6 --- /dev/null +++ b/src/hardware/Distance/V5DistanceSensor.cpp @@ -0,0 +1,38 @@ +#include "hardware/Distance/V5DistanceSensor.hpp" +#include "hardware/util.hpp" +#include +#include + +namespace lemlib { +V5DistanceSensor::V5DistanceSensor(SmartPort port) + : m_port(port) {} + +V5DistanceSensor::V5DistanceSensor(const V5DistanceSensor& other) + : m_port(other.m_port), + m_offset(other.m_offset) {} + +V5DistanceSensor V5DistanceSensor::from_pros_dist(pros::Distance distanceSensor) { + return V5DistanceSensor {{distanceSensor.get_port(), runtime_check_port}}; +} + +int32_t V5DistanceSensor::isConnected() const { + if (pros::c::distance_get(m_port) == INT_MAX) return 0; + else return 1; +} + +Length V5DistanceSensor::getDistance() const { + std::lock_guard lock(m_mutex); + const int32_t raw = pros::c::distance_get(m_port); + if (raw == INT_MAX) return from_in(INFINITY); + // the rotation sensor returns mm + const Length distance = from_mm(raw); + return distance + m_offset; +} + +int32_t V5DistanceSensor::setOffset(Length offset) { + const int32_t raw = pros::c::distance_get(m_port); + if (raw == INT_MAX) return INT_MAX; + m_offset = offset; + return 0; +} +} // namespace lemlib \ No newline at end of file From c057754247f06fbeb1a04712b550b0b232ab9007 Mon Sep 17 00:00:00 2001 From: Miniongolf Date: Tue, 4 Mar 2025 13:24:54 -0500 Subject: [PATCH 3/4] Fix documentations --- include/hardware/Distance/DistanceSensor.hpp | 4 ++-- include/hardware/Distance/V5DistanceSensor.hpp | 10 +++++----- src/hardware/Distance/V5DistanceSensor.cpp | 3 ++- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/include/hardware/Distance/DistanceSensor.hpp b/include/hardware/Distance/DistanceSensor.hpp index 429facf..d208278 100644 --- a/include/hardware/Distance/DistanceSensor.hpp +++ b/include/hardware/Distance/DistanceSensor.hpp @@ -15,7 +15,7 @@ class DistanceSensor : public Device { /** * @brief Get the distance measured by the distance sensor * - * The distance measured by the encoder is the distance from the sensor to the nearest object. + * The distance measured by the distance sensor is the distance from the sensor to the nearest object. * * @return Length the distance measured by the encoder * @return INFINITY if there is an error, setting errno @@ -51,7 +51,7 @@ class DistanceSensor : public Device { * if (distanceSensor->setOffset(1_in) == 0) { * std::cout << "Offset set!" << std::endl; * // With an object touching the surface of the distance sensor - * std::cout < "Offset distance: " << distanceSensor->getDistance().convert(in) << std::endl; // outputs 1 inch + * std::cout << "Offset distance: " << distanceSensor->getDistance().convert(in) << std::endl; // outputs 1 inch * } else { * std::cout << "Error setting offset!" << std::endl; * } diff --git a/include/hardware/Distance/V5DistanceSensor.hpp b/include/hardware/Distance/V5DistanceSensor.hpp index 82a8c34..d7505b8 100644 --- a/include/hardware/Distance/V5DistanceSensor.hpp +++ b/include/hardware/Distance/V5DistanceSensor.hpp @@ -42,7 +42,7 @@ class V5DistanceSensor : public DistanceSensor { * @b Example: * @code {.cpp} * void initialize() { - * lemlib::V5DistanceSensor distance = lemlib::V5RDistanceSensor::from_pros_dist(pros::Distance(1)); + * lemlib::V5DistanceSensor distance = lemlib::V5DistanceSensor::from_pros_dist(pros::Distance(1)); * } * @endcode */ @@ -56,7 +56,7 @@ class V5DistanceSensor : public DistanceSensor { * @b Example: * @code {.cpp} * void initialize() { - * lemlib::V5DistanceSensor distance = pros::Distance(1); + * lemlib::V5DistanceSensor distance = lemlib::V5DistanceSensor(1); * const int result = distance.isConnected(); * if (result == 1) { * std::cout << "Distance sensor is connected!" << std::endl; @@ -83,7 +83,7 @@ class V5DistanceSensor : public DistanceSensor { * @b Example: * @code {.cpp} * void initialize() { - * lemlib::V5DistanceSensor distanceSensor = pros::Distance(1); + * lemlib::V5DistanceSensor distanceSensor = lemlib::V5DistanceSensor(1); * const Length distance = distanceSensor.getDistance(); * if (angle == INFINITY) { * std::cout << "Error getting distance!" << std::endl; @@ -107,11 +107,11 @@ class V5DistanceSensor : public DistanceSensor { * @b Example: * @code {.cpp} * void initialize() { - * DistanceSensor* distanceSensor; + * lemlib::V5DistanceSensor distanceSensor(1); * if (distanceSensor->setOffset(1_in) == 0) { * std::cout << "Offset set!" << std::endl; * // With an object touching the surface of the distance sensor - * std::cout < "Offset distance: " << distanceSensor->getDistance().convert(in) << std::endl; // outputs 1 inch + * std::cout << "Offset distance: " << distanceSensor->getDistance().convert(in) << std::endl; // outputs 1 inch * } else { * std::cout << "Error setting offset!" << std::endl; * } diff --git a/src/hardware/Distance/V5DistanceSensor.cpp b/src/hardware/Distance/V5DistanceSensor.cpp index 5c83df6..20c6b39 100644 --- a/src/hardware/Distance/V5DistanceSensor.cpp +++ b/src/hardware/Distance/V5DistanceSensor.cpp @@ -2,6 +2,7 @@ #include "hardware/util.hpp" #include #include +#include namespace lemlib { V5DistanceSensor::V5DistanceSensor(SmartPort port) @@ -24,7 +25,7 @@ Length V5DistanceSensor::getDistance() const { std::lock_guard lock(m_mutex); const int32_t raw = pros::c::distance_get(m_port); if (raw == INT_MAX) return from_in(INFINITY); - // the rotation sensor returns mm + // the distance sensor returns mm const Length distance = from_mm(raw); return distance + m_offset; } From 93941e6097eb7456daf141671f75db0f9a2c2047 Mon Sep 17 00:00:00 2001 From: Miniongolf Date: Tue, 4 Mar 2025 13:28:26 -0500 Subject: [PATCH 4/4] Fix clang-format --- include/hardware/Distance/DistanceSensor.hpp | 5 ++--- include/hardware/Distance/V5DistanceSensor.hpp | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/include/hardware/Distance/DistanceSensor.hpp b/include/hardware/Distance/DistanceSensor.hpp index d208278..e06f4bf 100644 --- a/include/hardware/Distance/DistanceSensor.hpp +++ b/include/hardware/Distance/DistanceSensor.hpp @@ -51,9 +51,8 @@ class DistanceSensor : public Device { * if (distanceSensor->setOffset(1_in) == 0) { * std::cout << "Offset set!" << std::endl; * // With an object touching the surface of the distance sensor - * std::cout << "Offset distance: " << distanceSensor->getDistance().convert(in) << std::endl; // outputs 1 inch - * } else { - * std::cout << "Error setting offset!" << std::endl; + * std::cout << "Offset distance: " << distanceSensor->getDistance().convert(in) << std::endl; // + * outputs 1 inch } else { std::cout << "Error setting offset!" << std::endl; * } * } * @endcode diff --git a/include/hardware/Distance/V5DistanceSensor.hpp b/include/hardware/Distance/V5DistanceSensor.hpp index d7505b8..32cf6cf 100644 --- a/include/hardware/Distance/V5DistanceSensor.hpp +++ b/include/hardware/Distance/V5DistanceSensor.hpp @@ -111,9 +111,8 @@ class V5DistanceSensor : public DistanceSensor { * if (distanceSensor->setOffset(1_in) == 0) { * std::cout << "Offset set!" << std::endl; * // With an object touching the surface of the distance sensor - * std::cout << "Offset distance: " << distanceSensor->getDistance().convert(in) << std::endl; // outputs 1 inch - * } else { - * std::cout << "Error setting offset!" << std::endl; + * std::cout << "Offset distance: " << distanceSensor->getDistance().convert(in) << std::endl; // + * outputs 1 inch } else { std::cout << "Error setting offset!" << std::endl; * } * } * @endcode