From 7ba7c85bd5a1a5a52823344f4616ec004a71f2a6 Mon Sep 17 00:00:00 2001 From: Liam Teale Date: Sat, 18 Jan 2025 14:50:58 -0800 Subject: [PATCH] make some encoder methods const --- include/hardware/Encoder/ADIEncoder.hpp | 6 ++-- include/hardware/Encoder/Encoder.hpp | 6 ++-- include/hardware/Encoder/V5RotationSensor.hpp | 10 +++---- include/hardware/Motor/Motor.hpp | 28 ++++++++--------- include/hardware/Motor/MotorGroup.hpp | 26 ++++++++-------- src/hardware/Encoder/ADIEncoder.cpp | 6 ++-- src/hardware/Encoder/V5RotationSensor.cpp | 10 +++---- src/hardware/Motor/Motor.cpp | 30 ++++++++++--------- src/hardware/Motor/MotorGroup.cpp | 26 ++++++++-------- 9 files changed, 75 insertions(+), 73 deletions(-) diff --git a/include/hardware/Encoder/ADIEncoder.hpp b/include/hardware/Encoder/ADIEncoder.hpp index dcc8afe..12d6fb8 100644 --- a/include/hardware/Encoder/ADIEncoder.hpp +++ b/include/hardware/Encoder/ADIEncoder.hpp @@ -82,7 +82,7 @@ class ADIEncoder : public Encoder { * @return INT_MAX if there is an error, setting errno */ [[deprecated("This function is not implemented due to hardware limitations")]] - int isConnected() override; + int32_t isConnected() const override; /** * @brief Get the relative angle measured by the encoder * @@ -109,7 +109,7 @@ class ADIEncoder : public Encoder { * } * @endcode */ - Angle getAngle() override; + Angle getAngle() const override; /** * @brief Set the relative angle of the encoder * @@ -138,7 +138,7 @@ class ADIEncoder : public Encoder { * } * @endcode */ - int setAngle(Angle angle) override; + int32_t setAngle(Angle angle) override; private: mutable pros::Mutex m_mutex; pros::adi::Encoder m_encoder; diff --git a/include/hardware/Encoder/Encoder.hpp b/include/hardware/Encoder/Encoder.hpp index 2c9baae..3789cbb 100644 --- a/include/hardware/Encoder/Encoder.hpp +++ b/include/hardware/Encoder/Encoder.hpp @@ -33,7 +33,7 @@ class Encoder { * } * @endcode */ - virtual int isConnected() = 0; + virtual int32_t isConnected() const = 0; /** * @brief Get the relative angle measured by the encoder * @@ -56,7 +56,7 @@ class Encoder { * } * @endcode */ - virtual Angle getAngle() = 0; + virtual Angle getAngle() const = 0; /** * @brief Set the relative angle of the encoder * @@ -80,7 +80,7 @@ class Encoder { * } * @endcode */ - virtual int setAngle(Angle angle) = 0; + virtual int32_t setAngle(Angle angle) = 0; virtual ~Encoder() = default; }; } // namespace lemlib \ No newline at end of file diff --git a/include/hardware/Encoder/V5RotationSensor.hpp b/include/hardware/Encoder/V5RotationSensor.hpp index 32a5c08..c36a370 100644 --- a/include/hardware/Encoder/V5RotationSensor.hpp +++ b/include/hardware/Encoder/V5RotationSensor.hpp @@ -68,7 +68,7 @@ class V5RotationSensor : public Encoder { * } * @endcode */ - int isConnected() override; + int32_t isConnected() const override; /** * @brief Get the relative angle measured by the V5 Rotation Sensor * @@ -96,7 +96,7 @@ class V5RotationSensor : public Encoder { * } * @endcode */ - Angle getAngle() override; + Angle getAngle() const override; /** * @brief Set the relative angle of the V5 Rotation Sensor * @@ -125,7 +125,7 @@ class V5RotationSensor : public Encoder { * } * @endcode */ - int setAngle(Angle angle) override; + int32_t setAngle(Angle angle) override; /** * @brief returns whether the V5 Rotation Sensor is reversed or not * @@ -145,7 +145,7 @@ class V5RotationSensor : public Encoder { * } * @endcode */ - int isReversed() const; + int32_t isReversed() const; /** * @brief Set whether the V5 Rotation Sensor is reversed or not * @@ -166,7 +166,7 @@ class V5RotationSensor : public Encoder { * } * @endcode */ - int setReversed(bool reversed); + int32_t setReversed(bool reversed); private: mutable pros::Mutex m_mutex; Angle m_offset = 0_stRot; diff --git a/include/hardware/Motor/Motor.hpp b/include/hardware/Motor/Motor.hpp index 7efa2bf..cda20ff 100644 --- a/include/hardware/Motor/Motor.hpp +++ b/include/hardware/Motor/Motor.hpp @@ -78,7 +78,7 @@ class Motor : public Encoder { * } * @endcode */ - int move(Number percent); + int32_t move(Number percent); /** * @brief move the motor at a given angular velocity * @@ -103,7 +103,7 @@ class Motor : public Encoder { * } * @endcode */ - int moveVelocity(AngularVelocity velocity); + int32_t moveVelocity(AngularVelocity velocity); /** * @brief brake the motor * @@ -127,7 +127,7 @@ class Motor : public Encoder { * } * @endcode */ - int brake(); + int32_t brake(); /** * @brief set the brake mode of the motor * @@ -153,7 +153,7 @@ class Motor : public Encoder { * } * @endcode */ - int setBrakeMode(BrakeMode mode); + int32_t setBrakeMode(BrakeMode mode); /** * @brief get the brake mode of the motor * @@ -203,7 +203,7 @@ class Motor : public Encoder { * } * @endcode */ - int isConnected() override; + int32_t isConnected() const override; /** * @brief Get the relative angle measured by the motor * @@ -230,7 +230,7 @@ class Motor : public Encoder { * } * @endcode */ - Angle getAngle() override; + Angle getAngle() const override; /** * @brief Set the relative angle of the motor * @@ -261,7 +261,7 @@ class Motor : public Encoder { * } * @endcode */ - int setAngle(Angle angle) override; + int32_t setAngle(Angle angle) override; /** * @brief Get the offset of the motor encoder * @@ -312,7 +312,7 @@ class Motor : public Encoder { * } * @endcode */ - int setOffset(Angle offset); + int32_t setOffset(Angle offset); /** * @brief Get the type of the motor * @@ -343,7 +343,7 @@ class Motor : public Encoder { * } * @endcode */ - MotorType getType(); + MotorType getType() const; /** * @brief get whether the motor is reversed * @@ -364,7 +364,7 @@ class Motor : public Encoder { * } * @endcode */ - int isReversed() const; + int32_t isReversed() const; /** * @brief set whether the motor should be reversed or not * @@ -380,7 +380,7 @@ class Motor : public Encoder { * } * @endcode */ - int setReversed(bool reversed); + int32_t setReversed(bool reversed); /** * @brief Get the port the motor is connected to * @@ -445,7 +445,7 @@ class Motor : public Encoder { * } * @endcode */ - int setCurrentLimit(Current limit); + int32_t setCurrentLimit(Current limit); /** * @brief Get the temperature of the motor * @@ -488,7 +488,7 @@ class Motor : public Encoder { * } * @endcode */ - int setOutputVelocity(AngularVelocity outputVelocity); + int32_t setOutputVelocity(AngularVelocity outputVelocity); /** * @brief Get the output velocity of the motor * @@ -503,7 +503,7 @@ class Motor : public Encoder { * } * @endcode */ - AngularVelocity getOutputVelocity(); + AngularVelocity getOutputVelocity() const; private: mutable pros::Mutex m_mutex; AngularVelocity m_outputVelocity; diff --git a/include/hardware/Motor/MotorGroup.hpp b/include/hardware/Motor/MotorGroup.hpp index 87de14a..6ffcc50 100644 --- a/include/hardware/Motor/MotorGroup.hpp +++ b/include/hardware/Motor/MotorGroup.hpp @@ -91,7 +91,7 @@ class MotorGroup : public Encoder { * } * @endcode */ - int move(Number percent); + int32_t move(Number percent); /** * @brief move the motors at a given angular velocity * @@ -117,7 +117,7 @@ class MotorGroup : public Encoder { * } * @endcode */ - int moveVelocity(AngularVelocity velocity); + int32_t moveVelocity(AngularVelocity velocity); /** * @brief brake the motors * @@ -142,7 +142,7 @@ class MotorGroup : public Encoder { * } * @endcode */ - int brake(); + int32_t brake(); /** * @brief set the brake mode of the motors * @@ -163,7 +163,7 @@ class MotorGroup : public Encoder { * } * @endcode */ - int setBrakeMode(BrakeMode mode); + int32_t setBrakeMode(BrakeMode mode); /** * @brief get the brake mode of the motor group * @@ -210,7 +210,7 @@ class MotorGroup : public Encoder { * } * @endcode */ - int isConnected() override; + int32_t isConnected() const override; /** * @brief Get the average relative angle measured by the motors * @@ -238,7 +238,7 @@ class MotorGroup : public Encoder { * } * @endcode */ - Angle getAngle() override; + Angle getAngle() const override; /** * @brief Set the relative angle of all the motors * @@ -267,7 +267,7 @@ class MotorGroup : public Encoder { * } * @endcode */ - int setAngle(Angle angle) override; + int32_t setAngle(Angle angle) override; /** * @brief Get the combined current limit of all motors in the group * @@ -318,7 +318,7 @@ class MotorGroup : public Encoder { * } * @endcode */ - int setCurrentLimit(Current limit); + int32_t setCurrentLimit(Current limit); /** * @brief Get the temperatures of the motors in the motor group * @@ -363,7 +363,7 @@ class MotorGroup : public Encoder { * } * @endcode */ - int setOutputVelocity(AngularVelocity outputVelocity); + int32_t setOutputVelocity(AngularVelocity outputVelocity); /** * @brief Get the output velocity of the motor group * @@ -392,7 +392,7 @@ class MotorGroup : public Encoder { * } * @endcode */ - int getSize() const; + int32_t getSize() const; /** * @brief Add a motor to the motor group * @@ -421,7 +421,7 @@ class MotorGroup : public Encoder { * } * @endcode */ - int addMotor(ReversibleSmartPort port); + int32_t addMotor(ReversibleSmartPort port); /** * @brief Add a motor to the motor group * @@ -446,7 +446,7 @@ class MotorGroup : public Encoder { * } * @endcode */ - int addMotor(Motor motor); + int32_t addMotor(Motor motor); /** * @brief Add a motor to the motor group * @@ -472,7 +472,7 @@ class MotorGroup : public Encoder { * } * @endcode */ - int addMotor(Motor motor, bool reversed); + int32_t addMotor(Motor motor, bool reversed); /** * @brief Remove a motor from the motor group * diff --git a/src/hardware/Encoder/ADIEncoder.cpp b/src/hardware/Encoder/ADIEncoder.cpp index d9d9ce7..9622cdc 100644 --- a/src/hardware/Encoder/ADIEncoder.cpp +++ b/src/hardware/Encoder/ADIEncoder.cpp @@ -18,7 +18,7 @@ ADIEncoder::ADIEncoder(const ADIEncoder& other) : m_encoder(other.m_encoder), m_offset(other.m_offset) {} -int ADIEncoder::isConnected() { +int32_t ADIEncoder::isConnected() const { // it's not possible to check if the ADIEncoder is connected, so we just return 1 to indicate that it is // we do run a simple test however to check if the ports are valid if (m_encoder.get_value() == INT_MAX) { @@ -29,7 +29,7 @@ int ADIEncoder::isConnected() { return 1; } -Angle ADIEncoder::getAngle() { +Angle ADIEncoder::getAngle() const { std::unique_lock lock(m_mutex); const int raw = m_encoder.get_value(); // check for errors @@ -41,7 +41,7 @@ Angle ADIEncoder::getAngle() { return from_stDeg(raw) + m_offset; } -int ADIEncoder::setAngle(Angle angle) { +int32_t ADIEncoder::setAngle(Angle angle) { std::unique_lock lock(m_mutex); // the Vex SDK does not support setting the relative angle of an ADI encoder to a specific value // but we can overcome this limitation by resetting the relative angle to zero and saving an offset diff --git a/src/hardware/Encoder/V5RotationSensor.cpp b/src/hardware/Encoder/V5RotationSensor.cpp index cef475d..925815a 100644 --- a/src/hardware/Encoder/V5RotationSensor.cpp +++ b/src/hardware/Encoder/V5RotationSensor.cpp @@ -22,13 +22,13 @@ V5RotationSensor V5RotationSensor::from_pros_rot(pros::Rotation encoder) { else return V5RotationSensor {{encoder.get_port(), runtime_check_port}}; } -int V5RotationSensor::isConnected() { +int32_t V5RotationSensor::isConnected() const { if (pros::c::rotation_set_reversed(m_port, m_reversed) == INT_MAX) return 0; if (pros::c::rotation_get_angle(m_port) == INT_MAX) return 0; else return 1; } -Angle V5RotationSensor::getAngle() { +Angle V5RotationSensor::getAngle() const { std::lock_guard lock(m_mutex); if (pros::c::rotation_set_reversed(m_port, m_reversed) == INT_MAX) return from_stRot(INFINITY); const int32_t raw = pros::c::rotation_get_position(m_port); @@ -38,7 +38,7 @@ Angle V5RotationSensor::getAngle() { return angle + m_offset; } -int V5RotationSensor::setAngle(Angle angle) { +int32_t V5RotationSensor::setAngle(Angle angle) { std::lock_guard lock(m_mutex); if (pros::c::rotation_set_reversed(m_port, m_reversed) == INT_MAX) return INT_MAX; // requestedAngle = pos + offset @@ -51,12 +51,12 @@ int V5RotationSensor::setAngle(Angle angle) { return 0; } -int V5RotationSensor::isReversed() const { +int32_t V5RotationSensor::isReversed() const { std::lock_guard lock(m_mutex); return m_reversed; } -int V5RotationSensor::setReversed(bool reversed) { +int32_t V5RotationSensor::setReversed(bool reversed) { std::lock_guard lock(m_mutex); m_reversed = reversed; return convertStatus(pros::c::rotation_set_reversed(m_port, m_reversed)); diff --git a/src/hardware/Motor/Motor.cpp b/src/hardware/Motor/Motor.cpp index 60d3eb8..94ab1f8 100644 --- a/src/hardware/Motor/Motor.cpp +++ b/src/hardware/Motor/Motor.cpp @@ -45,7 +45,7 @@ BrakeMode motorBrakeToBrakeMode(pros::motor_brake_mode_e_t mode) { } } -int Motor::move(Number percent) { +int32_t Motor::move(Number percent) { // the V5 and EXP motors have different voltage caps, so we need to scale based on the motor type // V5 motors have their voltage capped at 12v, while EXP motors have their voltage capped at 7.2v // but they have the same max velocity, so we can scale the percent power based on the motor type @@ -56,7 +56,7 @@ int Motor::move(Number percent) { } } -int Motor::moveVelocity(AngularVelocity velocity) { +int32_t Motor::moveVelocity(AngularVelocity velocity) { std::lock_guard lock(m_mutex); // vexos will behave differently depending on the cartridge of the motor // pros uses an integer value to represent the rpm of the motor @@ -81,12 +81,12 @@ int Motor::moveVelocity(AngularVelocity velocity) { return convertStatus(pros::c::motor_move_velocity(m_port, out)); } -int Motor::brake() { +int32_t Motor::brake() { std::lock_guard lock(m_mutex); return convertStatus(pros::c::motor_brake(m_port)); } -int Motor::setBrakeMode(BrakeMode mode) { +int32_t Motor::setBrakeMode(BrakeMode mode) { std::lock_guard lock(m_mutex); if (mode == BrakeMode::INVALID) { errno = EINVAL; @@ -100,9 +100,11 @@ BrakeMode Motor::getBrakeMode() const { return motorBrakeToBrakeMode(pros::c::motor_get_brake_mode(m_port)); } -int Motor::isConnected() { return pros::c::get_plugged_type(m_port) == pros::c::v5_device_e_t::E_DEVICE_MOTOR; } +int32_t Motor::isConnected() const { + return pros::c::get_plugged_type(m_port) == pros::c::v5_device_e_t::E_DEVICE_MOTOR; +} -Angle Motor::getAngle() { +Angle Motor::getAngle() const { std::lock_guard lock(m_mutex); // get the number of encoder ticks const int ticks = pros::c::motor_get_raw_position(m_port, NULL); @@ -115,7 +117,7 @@ Angle Motor::getAngle() { return position + m_offset; } -int Motor::setAngle(Angle angle) { +int32_t Motor::setAngle(Angle angle) { std::lock_guard lock(m_mutex); // get the raw position const int ticks = pros::c::motor_get_raw_position(m_port, NULL); @@ -134,13 +136,13 @@ Angle Motor::getOffset() const { return m_offset; } -int Motor::setOffset(Angle offset) { +int32_t Motor::setOffset(Angle offset) { std::lock_guard lock(m_mutex); m_offset = offset; return 0; } -MotorType Motor::getType() { +MotorType Motor::getType() const { std::lock_guard lock(m_mutex); // there is no exposed api to get the motor type // while the memory address of the function has been found through reverse engineering, @@ -161,14 +163,14 @@ MotorType Motor::getType() { } else return MotorType::EXP; } -int Motor::isReversed() const { +int32_t Motor::isReversed() const { std::lock_guard lock(m_mutex); // technically this returns an int, but as long as you only pass 0 to the index its impossible for it to return an // error. This is because we keep track of whether the motor is reversed or not through the sign of its port return m_port < 0; } -int Motor::setReversed(bool reversed) { +int32_t Motor::setReversed(bool reversed) { std::lock_guard lock(m_mutex); // technically this returns an int, but as long as you only pass 0 to the index its impossible for it to return an // error. This is because we keep track of whether the motor is reversed or not through the sign of its port @@ -188,7 +190,7 @@ Current Motor::getCurrentLimit() const { return result; } -int Motor::setCurrentLimit(Current limit) { +int32_t Motor::setCurrentLimit(Current limit) { std::lock_guard lock(m_mutex); return pros::c::motor_set_current_limit(m_port, to_amp(limit) * 1000); } @@ -200,7 +202,7 @@ Temperature Motor::getTemperature() const { } // Always returns 0 because the velocity setter is not dependent on hardware and should never fail -int Motor::setOutputVelocity(AngularVelocity outputVelocity) { +int32_t Motor::setOutputVelocity(AngularVelocity outputVelocity) { std::lock_guard lock(m_mutex); Angle angle = getAngle(); m_outputVelocity = outputVelocity; @@ -208,7 +210,7 @@ int Motor::setOutputVelocity(AngularVelocity outputVelocity) { return 0; } -AngularVelocity Motor::getOutputVelocity() { +AngularVelocity Motor::getOutputVelocity() const { std::lock_guard lock(m_mutex); return m_outputVelocity; } diff --git a/src/hardware/Motor/MotorGroup.cpp b/src/hardware/Motor/MotorGroup.cpp index 207918b..105bdb7 100644 --- a/src/hardware/Motor/MotorGroup.cpp +++ b/src/hardware/Motor/MotorGroup.cpp @@ -30,7 +30,7 @@ MotorGroup MotorGroup::from_pros_group(pros::MotorGroup group, AngularVelocity o return motor_group; } -int MotorGroup::move(Number percent) { +int32_t MotorGroup::move(Number percent) { std::lock_guard lock(m_mutex); const std::vector motors = getMotors(); bool success = false; @@ -42,7 +42,7 @@ int MotorGroup::move(Number percent) { return success ? 0 : INT_MAX; } -int MotorGroup::moveVelocity(AngularVelocity velocity) { +int32_t MotorGroup::moveVelocity(AngularVelocity velocity) { std::lock_guard lock(m_mutex); const std::vector motors = getMotors(); bool success = false; @@ -54,7 +54,7 @@ int MotorGroup::moveVelocity(AngularVelocity velocity) { return success ? 0 : INT_MAX; } -int MotorGroup::brake() { +int32_t MotorGroup::brake() { std::lock_guard lock(m_mutex); const std::vector motors = getMotors(); bool success = false; @@ -66,7 +66,7 @@ int MotorGroup::brake() { return success ? 0 : INT_MAX; } -int MotorGroup::setBrakeMode(BrakeMode mode) { +int32_t MotorGroup::setBrakeMode(BrakeMode mode) { std::lock_guard lock(m_mutex); m_brakeMode = mode; getMotors(); // even though we don't use this, we call it anyway for brake mode setting and disconnect handling @@ -79,7 +79,7 @@ BrakeMode MotorGroup::getBrakeMode() const { return m_brakeMode; } -int MotorGroup::isConnected() { +int32_t MotorGroup::isConnected() const { std::lock_guard lock(m_mutex); const std::vector motors = getMotors(); for (Motor motor : motors) { @@ -90,7 +90,7 @@ int MotorGroup::isConnected() { return 0; } -Angle MotorGroup::getAngle() { +Angle MotorGroup::getAngle() const { std::lock_guard lock(m_mutex); const std::vector motors = getMotors(); // get the average angle of all motors in the group @@ -112,7 +112,7 @@ Angle MotorGroup::getAngle() { return angle / (getSize() - errors); } -int MotorGroup::setAngle(Angle angle) { +int32_t MotorGroup::setAngle(Angle angle) { std::lock_guard lock(m_mutex); const std::vector motors = getMotors(); bool success = false; @@ -147,7 +147,7 @@ Current MotorGroup::getCurrentLimit() const { return total; } -int MotorGroup::setCurrentLimit(Current limit) { +int32_t MotorGroup::setCurrentLimit(Current limit) { std::lock_guard lock(m_mutex); const std::vector motors = getMotors(); if (motors.size() == 0) return INT_MAX; // error handling @@ -168,7 +168,7 @@ std::vector MotorGroup::getTemperatures() const { } // Always returns 0 because the velocity setter is not dependent on hardware and should never fail -int MotorGroup::setOutputVelocity(AngularVelocity outputVelocity) { +int32_t MotorGroup::setOutputVelocity(AngularVelocity outputVelocity) { std::lock_guard lock(m_mutex); Angle angle = getAngle(); m_outputVelocity = outputVelocity; @@ -181,7 +181,7 @@ AngularVelocity MotorGroup::getOutputVelocity() const { return m_outputVelocity; } -int MotorGroup::getSize() const { +int32_t MotorGroup::getSize() const { std::lock_guard lock(m_mutex); const std::vector motors = getMotors(); int size = 0; @@ -190,7 +190,7 @@ int MotorGroup::getSize() const { return size; } -int MotorGroup::addMotor(ReversibleSmartPort port) { +int32_t MotorGroup::addMotor(ReversibleSmartPort port) { std::lock_guard lock(m_mutex); // check that the motor isn't already part of the group for (const MotorInfo& info : m_motors) { @@ -209,12 +209,12 @@ int MotorGroup::addMotor(ReversibleSmartPort port) { return 0; } -int MotorGroup::addMotor(Motor motor) { +int32_t MotorGroup::addMotor(Motor motor) { std::lock_guard lock(m_mutex); return addMotor(motor.getPort()); } -int MotorGroup::addMotor(Motor motor, bool reversed) { +int32_t MotorGroup::addMotor(Motor motor, bool reversed) { std::lock_guard lock(m_mutex); // set the motor reversal motor.setReversed(reversed);