Skip to content

Commit

Permalink
Merge pull request #18 from LemLib/refactor/const
Browse files Browse the repository at this point in the history
♻️ Refactored abstract classes
  • Loading branch information
SizzinSeal authored Jan 18, 2025
2 parents 50f8ad7 + 5b93434 commit 630a570
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 204 deletions.
15 changes: 15 additions & 0 deletions include/hardware/Device.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <cstdint>

namespace lemlib {
/**
* @brief abstract Device class
*
* used to have a generic interface for devices
*/
class Device {
public:
virtual int32_t isConnected() const = 0;
};
} // namespace lemlib
6 changes: 3 additions & 3 deletions include/hardware/Encoder/ADIEncoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -109,7 +109,7 @@ class ADIEncoder : public Encoder {
* }
* @endcode
*/
Angle getAngle() override;
Angle getAngle() const override;
/**
* @brief Set the relative angle of the encoder
*
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 4 additions & 26 deletions include/hardware/Encoder/Encoder.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "hardware/Device.hpp"
#include "units/Angle.hpp"

namespace lemlib {
Expand All @@ -9,31 +10,8 @@ namespace lemlib {
* An encoder is a device that measures the angle of a rotating object. This class is abstract, and must be implemented
* by a subclass.
*/
class Encoder {
class Encoder : public Device {
public:
/**
* @brief whether the encoder is connected
*
* @return 0 if its not connected
* @return 1 if it is connected
* @return INT_MAX if there is an error, setting errno
*
* @b Example:
* @code {.cpp}
* void initialize() {
* Encoder* encoder;
* const int result = encoder->isConnected();
* if (result == 1) {
* std::cout << "Encoder is connected!" << std::endl;
* } else if (result == 0) {
* std::cout << "Encoder is not connected!" << std::endl;
* } else {
* std::cout << "Error checking if encoder is connected!" << std::endl;
* }
* }
* @endcode
*/
virtual int isConnected() = 0;
/**
* @brief Get the relative angle measured by the encoder
*
Expand All @@ -56,7 +34,7 @@ class Encoder {
* }
* @endcode
*/
virtual Angle getAngle() = 0;
virtual Angle getAngle() const = 0;
/**
* @brief Set the relative angle of the encoder
*
Expand All @@ -80,7 +58,7 @@ class Encoder {
* }
* @endcode
*/
virtual int setAngle(Angle angle) = 0;
virtual int32_t setAngle(Angle angle) = 0;
virtual ~Encoder() = default;
};
} // namespace lemlib
10 changes: 5 additions & 5 deletions include/hardware/Encoder/V5RotationSensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand All @@ -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
*
Expand All @@ -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;
Expand Down
43 changes: 26 additions & 17 deletions include/hardware/IMU/IMU.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
#pragma once

#include "hardware/Device.hpp"
#include "units/Angle.hpp"
#include "pros/rtos.hpp"

namespace lemlib {
class IMU {
class IMU : public Device {
public:
/**
* @brief Construct a new IMU object
*
* @param scalar the scalar to apply to the gyro readings. Defaults to 1.
*/
IMU(Number scalar = 1.0);
/**
* @brief IMU copy constructor
*
* since pros::Mutex does not have a copy constructor, we need an explicit copy constructor
*
* @param other the imu to copy
*/
IMU(const IMU& other);
/**
* @brief calibrate the IMU
*
Expand All @@ -14,31 +30,23 @@ class IMU {
* @return 0 success
* @return INT_MAX error occurred, setting errno
*/
virtual int calibrate() = 0;
virtual int32_t calibrate() = 0;
/**
* @brief check if the IMU is calibrated
*
* @return true the IMU is calibrated
* @return false the IMU is not calibrated
* @return INT_MAX error occurred, setting errno
*/
virtual int isCalibrated() = 0;
virtual int32_t isCalibrated() const = 0;
/**
* @brief check if the IMU is calibrating
*
* @return true the IMU is calibrating
* @return false the IMU is not calibrating
* @return INT_MAX error occurred, setting errno
*/
virtual int isCalibrating() = 0;
/**
* @brief whether the IMU is connected
*
* @return true the IMU is connected
* @return false the IMU is not connected
* @return INT_MAX error occurred, setting errno
*/
virtual int isConnected() = 0;
virtual int32_t isCalibrating() const = 0;
/**
* @brief Get the rotation of the IMU
*
Expand All @@ -47,7 +55,7 @@ class IMU {
* @return Angle the rotation of the IMU
* @return INFINITY error occurred, setting errno
*/
virtual Angle getRotation() = 0;
virtual Angle getRotation() const = 0;
/**
* @brief Set the rotation of the IMU
*
Expand All @@ -57,7 +65,7 @@ class IMU {
* @return int 0 success
* @return INT_MAX error occurred, setting errno
*/
virtual int setRotation(Angle rotation) = 0;
virtual int32_t setRotation(Angle rotation) = 0;
/**
* @brief Set the gyro scalar for the IMU
*
Expand All @@ -68,7 +76,7 @@ class IMU {
* @return int 0 success
* @return INT_MAX error occurred, setting errno
*/
virtual int setGyroScalar(Number scalar);
virtual int32_t setGyroScalar(Number scalar);
/**
* @brief Get the gyro scalar for the IMU
*
Expand All @@ -78,9 +86,10 @@ class IMU {
* @return Number gyro scalar
* @return INFINITY error occurred, setting errno
*/
virtual Number getGyroScalar();
virtual Number getGyroScalar() const;
virtual ~IMU() = default;
protected:
Number m_gyroScalar = 1.0;
mutable pros::Mutex m_mutex;
Number m_gyroScalar;
};
} // namespace lemlib
Loading

0 comments on commit 630a570

Please sign in to comment.