Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ add output velocity setter #13

Merged
merged 4 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions include/hardware/Motor/Motor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,23 @@ class Motor : public Encoder {
* @endcode
*/
Temperature getTemperature() const;
/**
* @brief set the output velocity of the motor
*
* @param outputVelocity the theoretical maximum output velocity of the motor, after gearing, to set
* @return int 0 success
* @return INT_MAX error occurred, setting errno
*
* @b Example:
* @code {.cpp}
* void initialize() {
* lemlib::Motor motor(1, 360_rpm);
* // set the output velocity to 450 rpm
* motor.setOutputVelocity(450_rpm);
* }
* @endcode
*/
int setOutputVelocity(AngularVelocity outputVelocity);
private:
AngularVelocity m_outputVelocity;
Angle m_offset = 0_stDeg;
Expand Down
20 changes: 19 additions & 1 deletion include/hardware/Motor/MotorGroup.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "pros/motor_group.hpp"
#include "hardware/Motor/Motor.hpp"
#include "units/Angle.hpp"
#include <vector>

namespace lemlib {
Expand Down Expand Up @@ -333,6 +334,23 @@ class MotorGroup : Encoder {
* @endcode
*/
std::vector<Temperature> getTemperatures();
/**
* @brief set the output velocity of the motors
*
* @param outputVelocity the theoretical maximum output velocity of the motor group, after gearing, to set
* @return int 0 success
* @return INT_MAX error occurred, setting errno
*
* @b Example:
* @code {.cpp}
* void initialize() {
* lemlib::MotorGroup motorGroup({1, -2, 3}, 360_rpm);
* // set the output velocity to 450 rpm
* motorGroup.setOutputVelocity(450_rpm);
* }
* @endcode
*/
int setOutputVelocity(AngularVelocity outputVelocity);
/**
* @brief Get the number of connected motors in the group
*
Expand Down Expand Up @@ -495,7 +513,7 @@ class MotorGroup : Encoder {
* @return const std::vector<Motor> vector of lemlib::Motor objects
*/
const std::vector<Motor> getMotors();
const AngularVelocity m_outputVelocity;
AngularVelocity m_outputVelocity;
/**
* This member variable is a vector of motor information
*
Expand Down
8 changes: 8 additions & 0 deletions src/hardware/Motor/Motor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,12 @@ Temperature Motor::getTemperature() const {
if (result.internal() == INFINITY) return result; // error checking
return result;
}

// Always returns 0 because the velocity setter is not dependent on hardware and should never fail
int Motor::setOutputVelocity(AngularVelocity outputVelocity) {
Angle angle = getAngle();
m_outputVelocity = outputVelocity;
setAngle(angle);
return 0;
}
} // namespace lemlib
9 changes: 9 additions & 0 deletions src/hardware/Motor/MotorGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "units/Temperature.hpp"
#include <climits>
#include <cmath>
#include <cstdint>
#include <errno.h>

namespace lemlib {
Expand Down Expand Up @@ -144,6 +145,14 @@ std::vector<Temperature> MotorGroup::getTemperatures() {
return temperatures;
}

// Always returns 0 because the velocity setter is not dependent on hardware and should never fail
int MotorGroup::setOutputVelocity(AngularVelocity outputVelocity) {
Angle angle = getAngle();
m_outputVelocity = outputVelocity;
setAngle(angle);
return 0;
}

int MotorGroup::getSize() {
const std::vector<Motor> motors = getMotors();
int size = 0;
Expand Down