Skip to content

Commit

Permalink
Merge branch 'abstract_motor_pr' into refactor-master
Browse files Browse the repository at this point in the history
  • Loading branch information
UvuvDev committed Mar 13, 2024
2 parents 62acf5e + e62cfc8 commit c9be17b
Show file tree
Hide file tree
Showing 16 changed files with 623 additions and 41 deletions.
2 changes: 1 addition & 1 deletion include/lemlib/devices/encoder/encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Encoder {
* @return true encoder calibration failed
* @return false encoder calibration succeeded
*/
virtual bool reset() = 0;
virtual bool tare() = 0;
protected:
float lastAngle = 0;
};
Expand Down
8 changes: 4 additions & 4 deletions include/lemlib/devices/encoder/motor.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <memory>
#include "pros/motor_group.hpp"
#include "lemlib/devices/motor/abstractgroup.hpp"
#include "lemlib/devices/encoder/encoder.hpp"

namespace lemlib {
Expand All @@ -15,7 +15,7 @@ class MotorEncoder : public Encoder {
* @param motors pointer to the motor group to be used
* @param rpm output rpm
*/
MotorEncoder(std::shared_ptr<pros::MotorGroup> motors, float rpm);
MotorEncoder(std::shared_ptr<Abstract_MotorGroup> motors, float rpm);

/**
* @brief Get the angle rotated by the motor encoders, in radians
Expand All @@ -29,9 +29,9 @@ class MotorEncoder : public Encoder {
* @return true calibration failed
* @return false calibration succeeded
*/
bool reset() override;
bool tare() override;
private:
std::shared_ptr<pros::MotorGroup> motors;
std::shared_ptr<Abstract_MotorGroup> motors;
const float rpm;
};
} // namespace lemlib
2 changes: 1 addition & 1 deletion include/lemlib/devices/encoder/optical.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class OpticalEncoder : public Encoder {
* @return true calibration failed
* @return false calibration succeeded
*/
bool reset() override;
bool tare() override;
private:
pros::adi::Encoder optical;
const float ratio;
Expand Down
2 changes: 1 addition & 1 deletion include/lemlib/devices/encoder/rotation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RotationEncoder : public Encoder {
* @return true calibration failed
* @return false calibration succeeded
*/
bool reset() override;
bool tare() override;
private:
pros::Rotation rotation;
const float ratio;
Expand Down
41 changes: 41 additions & 0 deletions include/lemlib/devices/motor/abstractgroup.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once
#include "lemlib/devices/motor/abstractmotor.hpp"

namespace lemlib {

class Abstract_MotorGroup {
protected:
bool isBroken = false;

std::vector<std::unique_ptr<Abstract_Motor>> motorContainer;
public:
virtual void spinAtVoltage(int voltage) = 0;

virtual void spinPerc(int percent) = 0;

virtual void spinJoystick(int joystickValue) = 0;

virtual void spinAtRPM(int RPM) = 0;

virtual void spinUntilDegree(int degree, int speedInVoltage) = 0;

virtual void spinFor(float seconds, int speedInVoltage) = 0;

virtual void logMotorPerformance() = 0; // Stuff like wattage and temperature, RPM, etc, along with Port Number

virtual void shutDown() = 0;

virtual void revive() = 0;

virtual bool isOverheated() = 0;

virtual void set_zero_position(int position) = 0;

virtual std::vector<double> getPositions() = 0;

virtual std::vector<int> getBaseRPMs() = 0;

virtual std::vector<std::unique_ptr<Abstract_Motor>>& getMotorContainer() = 0;
};

} // namespace lemlib
87 changes: 87 additions & 0 deletions include/lemlib/devices/motor/abstractmotor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#pragma once
#include "main.h"
#include "pros/motors.h"
#include "lemlib/pid.hpp"
#include <memory>

namespace lemlib {

struct MotorInfo {
int port;
bool reversed;
float gearRatio;
int cartrpm;

MotorInfo(int port, bool reversed, float gearRatio, int cartrpm)
: port(port),
reversed(reversed),
gearRatio(gearRatio),
cartrpm(cartrpm) {}
};

class Abstract_Motor {
protected:
static constexpr int overheatTempCelsius = 55;

bool isBroken = false;
bool isReversed = false;
bool hasPairMotor = false;

int voltage = 0;

float gearRatio = 0;

float baseRPM = 0;
public:
virtual void spinAtVoltage(int voltage) = 0;

virtual void spinPerc(int percent) = 0;

virtual void spinJoystick(int joystickValue) = 0;

virtual void spinAtRPM(int RPM) = 0;

virtual void spinUntilDegree(int degree, int speedInVoltage) = 0;

virtual void spinFor(float seconds, int speedInVoltage) = 0;

virtual void logMotorPerformance() = 0; // Stuff like wattage and temperature, RPM, etc, along with Port Number

virtual void set_zero_position(int position) = 0;

virtual int getPosition() = 0;

virtual void shutDown() = 0;

virtual void revive() = 0;

virtual bool isOverheated() = 0;

virtual bool getIsBroken() = 0;

virtual float getRPM() = 0;

virtual float getVoltage() = 0;

virtual int getPort() = 0;

virtual void setGearset(int gearset) = 0;

virtual int getGearset() = 0;

virtual void setReversed(bool isReversed) = 0;

virtual bool getIsReversed() = 0;

virtual std::shared_ptr<Abstract_Motor> getPairMotor() = 0;

virtual void setPID(std::shared_ptr<FAPID> pid) = 0;

virtual std::shared_ptr<FAPID> getPID() = 0;

virtual void setGearRatio(float gearRatio) = 0;

virtual float getGearRatio() = 0;
};

} // namespace lemlib
97 changes: 97 additions & 0 deletions include/lemlib/devices/motor/prosmotor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#pragma once

#include "lemlib/pid.hpp"
#include "lemlib/devices/motor/abstractmotor.hpp"
#include "pros/motors.h"
#include <memory>

namespace lemlib {

class PROSMotor : public Abstract_Motor {
private:
std::unique_ptr<pros::Motor> motor;
std::shared_ptr<PROSMotor> pairMotor;

std::shared_ptr<FAPID> pid;

static constexpr int overheatTempCelsius = 55;

bool isBroken = false;
bool isReversed = false;
bool hasPairMotor = false;

int voltage = 0;

float gearRatio;

pros::motor_gearset_e gearset = pros::E_MOTOR_GEAR_GREEN;
public:
/**
* @brief Construct a new Motor object
*
* @param port V5 Smart Port number
* @param isReversedArg Defines direction of the motor spinning. True is reversed, false is normal.
* @param gearRatioArg Gear ratio of the motors mechanism. 3:4 = .75
* @param gearset The PROS motor gearset.
* @param pairMotorArg The motor paired with this motor. For example, leftFront and rightFront motors.
* @param pid Built-in PID Controller for the motor.
*/
PROSMotor(const uint8_t port, const bool isReversed, const float gearRatio,
const int cartRPM = 600,
std::shared_ptr<PROSMotor> pairMotor = nullptr, std::shared_ptr<FAPID> pid = nullptr);

void spinAtVoltage(int voltage) override;

void spinPerc(int percent) override;

void spinJoystick(int joystickValue) override;

void spinAtRPM(int RPM) override;

void spinUntilDegree(int degree, int speedInVoltage) override;

void spinFor(float seconds, int speedInVoltage) override;

void logMotorPerformance() override; // Stuff like wattage and temperature, RPM, etc, along with Port Number

void set_zero_position(int position) override;

int getPosition() override;

void shutDown() override;

void revive() override;

bool isOverheated() override;

bool getIsBroken() override;

float getRPM() override;

float getVoltage() override;

int getPort() override;

void setGearset(int gearset) override;

int getGearset() override;

void setReversed(bool isReversed) override;

bool getIsReversed() override;

std::shared_ptr<Abstract_Motor> getPairMotor() override;

void setPID(std::shared_ptr<FAPID> pid) override;

std::shared_ptr<FAPID> getPID() override;

void setGearRatio(float gearRatio) override;

float getGearRatio() override;



};

} // namespace lemlib
55 changes: 55 additions & 0 deletions include/lemlib/devices/motor/prosmotorgroup.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

#include "abstractgroup.hpp"
#include "lemlib/devices/motor/abstractmotor.hpp"
#include "prosmotor.hpp"
#include <memory>

namespace lemlib {

class PROSMotorGroup : public Abstract_MotorGroup {
private:

public:
PROSMotorGroup(std::vector<std::unique_ptr<PROSMotor>> motorContainerArg);

PROSMotorGroup(std::vector<MotorInfo> motorParameters);

void spinAtVoltage(int voltage) override;

void spinPerc(int percent) override;

void spinJoystick(int joystickValue) override;

void spinAtRPM(int RPM) override;

void spinUntilDegree(int degree, int speedInVoltage) override;

void spinFor(float seconds, int speedInVoltage) override;

void logMotorPerformance() override; // Stuff like wattage and temperature, RPM, etc, along with Port Number

void set_zero_position(int position) override;

void shutDown() override;

void revive() override;

bool isOverheated() override;

std::vector<bool> getIsBroken();

std::vector<float> getAllRPM();

std::vector<int> getBaseRPMs() override;

std::vector<double> getPositions() override;

float getAverageRPM();

float getVoltage();

std::vector<std::unique_ptr<Abstract_Motor>>& getMotorContainer() override;
};

} // namespace lemlib
4 changes: 2 additions & 2 deletions include/lemlib/devices/trackingWheel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include <memory>
#include <cstdint>
#include "pros/motor_group.hpp"
#include "lemlib/devices/motor/abstractgroup.hpp"
#include "pros/adi.hpp"
#include "pros/rotation.hpp"
#include "lemlib/devices/encoder/encoder.hpp"
Expand Down Expand Up @@ -56,7 +56,7 @@ class TrackingWheel {
* @param offset distance between the wheel and the tracking center, in inches
* @param rpm of the rpm of the wheels the motor group is driving
*/
TrackingWheel(std::shared_ptr<pros::MotorGroup> motors, float diameter, float offset, float rpm);
TrackingWheel(std::shared_ptr<Abstract_MotorGroup> motors, float diameter, float offset, float rpm);
/**
* @brief Create a new optical encoder tracking wheel
*
Expand Down
Loading

0 comments on commit c9be17b

Please sign in to comment.