RBE1001Lib
|
A PID Motor class using FreeRTOS threads, ESP32Encoder and ESP32PWM. More...
#include <Motor.h>
Public Member Functions | |
float | getInterpolationUnitIncrement () |
Motor (int pwmPin, int dirPin, int encAPin, int encBPin) | |
A PID Motor class using FreeRTOS threads, ESP32Encoder and ESP32PWM. More... | |
virtual | ~Motor () |
void | attach () |
Attach the motors hardware. More... | |
void | setEffort (float effort) |
void | setEffortPercent (float percent) |
float | getEffort () |
float | getEffortPercent () |
float | getDegreesPerSecond () |
float | getCurrentDegrees () |
void | loop () |
void | setSetpointWithTime (float newTargetInDegrees, long miliseconds, interpolateMode mode) |
void | setSpeed (float newDegreesPerSecond) |
void | setSpeed (float newDegreesPerSecond, long miliseconds) |
void | moveTo (float newTargetInDegrees, float speedDegPerSec) |
void | moveFor (float deltaTargetInDegrees, float speedDegPerSec) |
void | blockUntilMoveIsDone () |
wait for the motor to arrive at a setpoint More... | |
float | startMoveFor (float deltaTargetInDegrees, float speedDegPerSec) |
void | setSetpoint (float newTargetInDegrees) |
void | setSetpointWithLinearInterpolation (float newTargetInDegrees, long miliseconds) |
void | setSetpointWithSinusoidalInterpolation (float newTargetInDegrees, long miliseconds) |
void | setSetpointWithBezierInterpolation (float newTargetInDegrees, long miliseconds, float Control_0=0.5, float Control_1=1.0) |
void | setSetpointWithTrapezoidalInterpolation (float newTargetInDegrees, long miliseconds, float trapazoidalTime) |
void | setGains (float p, float i, float d) |
void | setGainsP (float p) |
void | setGainsI (float i) |
void | setGainsD (float d) |
float | getGainsP () |
float | getGainsI () |
float | getGainsD () |
bool | isMotorDoneWithMove () |
Check to see if the motor is done with a move. More... | |
Data Fields | |
int | MotorPWMPin = -1 |
int | directionFlag = -1 |
int | MotorEncAPin = -1 |
int | MotorEncBPin = -1 |
int64_t | nowEncoder = 0 |
Static Public Attributes | |
static bool | timersAllocated = false |
static Motor * | list [MAX_POSSIBLE_MOTORS] |
Private Member Functions | |
void | setEffortLocal (float effort) |
Static Private Member Functions | |
static void | allocateTimer (int PWMgenerationTimer) |
Private Attributes | |
ESP32PWM * | pwm |
ESP32Encoder * | encoder |
bool | isAttached = false |
int | interruptCountForVelocity = 0 |
int | prevousCount = 0 |
float | cachedSpeed = 0 |
float | setpoint = 0 |
float | kP = 0.02 |
float | kI = 0.01 |
float | kD = 0 |
float | runntingITerm = 0 |
bool | closedLoopControl = true |
float | targetEffort = 0 |
float | currentEffort = 0 |
float | duration = 0 |
float | startTime = 0 |
float | endSetpoint = 0 |
float | startSetpoint = 0 |
float | unitDuration = 1 |
interpolateMode | mode = LINEAR_INTERPOLATION |
float | milisecondPosIncrementForVelocity |
float | BEZIER_P0 = 0.25 |
BEZIER Control Point 0. More... | |
float | BEZIER_P1 = 0.75 |
BEZIER Control Point 1. More... | |
float | TRAPEZOIDAL_time = 0 |
the amount of time to ramp up and ramp down the speed More... | |
A PID Motor class using FreeRTOS threads, ESP32Encoder and ESP32PWM.
This Motor class is intended to be used by RBE 1001 in the WPI Robotics Department.
Motor objects can be instantiated statically. The attach method must be called before using the motor.
The motor uses one timer for the ESP32PWM objects. That means the static method
Motor::allocateTimer (int PWMgenerationTimer)
must be called before any motor objects can be attached. This method will also start the PID thread.
Motor::Motor | ( | int | pwmPin, |
int | dirPin, | ||
int | encAPin, | ||
int | encBPin | ||
) |
A PID Motor class using FreeRTOS threads, ESP32Encoder and ESP32PWM.
This Motor class is intended to be used by RBE 1001 in the WPI Robotics Department.
Motor objects can be instantiated statically. The attach method must be called before using the motor.
The motor uses one timer for the ESP32PWM objects. That means the static method
Motor::allocateTimer (int PWMgenerationTimer)
must be called before any motor objects can be attached. This method will also start the PID thread.
Definition at line 126 of file Motor.cpp.
References directionFlag, encoder, MotorEncAPin, MotorEncBPin, MotorPWMPin, and pwm.
|
virtual |
Definition at line 136 of file Motor.cpp.
References ESP32PWM::detachPin(), encoder, MotorPWMPin, ESP32Encoder::pauseCount(), and pwm.
|
staticprivate |
PWMgenerationTimer | the timer to be used to generate the 20khz PWM |
PWMgenerationTimer | the timer to be used to generate the 20khx PWM |
controllerTimer | a timer running at 1khz for PID, velocity measurment, and trajectory planning |
Definition at line 115 of file Motor.cpp.
References ESP32PWM::allocateTimer(), complexHandlerTask, onMotorTimer(), and timersAllocated.
Referenced by attach().
void Motor::attach | ( | ) |
Attach the motors hardware.
This attaches the motors to the hardware ports that were saved in the constructor
Definition at line 456 of file Motor.cpp.
References allocateTimer(), ESP32Encoder::attachFullQuad(), ESP32PWM::attachPin(), directionFlag, encoder, ESP32Encoder, isAttached, MAX_POSSIBLE_MOTORS, MotorEncAPin, MotorEncBPin, MotorPWMPin, pwm, timersAllocated, UP, and ESP32Encoder::useInternalWeakPullResistors.
Referenced by blockUntilMoveIsDone(), getCurrentDegrees(), getDegreesPerSecond(), getEffort(), getEffortPercent(), getGainsD(), getGainsI(), getGainsP(), getInterpolationUnitIncrement(), isMotorDoneWithMove(), moveFor(), moveTo(), setEffort(), setEffortLocal(), setEffortPercent(), setGains(), setGainsD(), setGainsI(), setGainsP(), setSetpoint(), setSetpointWithBezierInterpolation(), setSetpointWithLinearInterpolation(), setSetpointWithSinusoidalInterpolation(), setSetpointWithTime(), setSetpointWithTrapezoidalInterpolation(), setSpeed(), and startMoveFor().
void Motor::blockUntilMoveIsDone | ( | ) |
wait for the motor to arrive at a setpoint
Definition at line 249 of file Motor.cpp.
References attach(), isAttached, and isMotorDoneWithMove().
Referenced by getEffortPercent(), and moveFor().
float Motor::getCurrentDegrees | ( | ) |
getTicks
This function returns the current count of encoders
Definition at line 568 of file Motor.cpp.
References attach(), isAttached, nowEncoder, and TICKS_TO_DEGREES.
Referenced by getEffortPercent(), isMotorDoneWithMove(), loop(), moveTo(), WebPage::SendSetpoint(), setSpeed(), startMoveFor(), and updateDashboard().
float Motor::getDegreesPerSecond | ( | ) |
getDegreesPerSecond
This function returns the current speed of the motor
Definition at line 551 of file Motor.cpp.
References attach(), cachedSpeed, isAttached, and TICKS_TO_DEGREES.
Referenced by getEffortPercent(), isMotorDoneWithMove(), loop(), and updateDashboard().
float Motor::getEffort | ( | ) |
Definition at line 516 of file Motor.cpp.
References attach(), currentEffort, and isAttached.
Referenced by getEffortPercent(), and setEffortPercent().
|
inline |
Definition at line 265 of file Motor.h.
References attach(), blockUntilMoveIsDone(), getCurrentDegrees(), getDegreesPerSecond(), getEffort(), loop(), moveFor(), moveTo(), setSetpointWithTime(), setSpeed(), and startMoveFor().
Referenced by loop(), and updateDashboard().
|
inline |
Definition at line 467 of file Motor.h.
References attach(), isMotorDoneWithMove(), and kD.
Referenced by WebPage::SendPIDValues().
|
inline |
Definition at line 461 of file Motor.h.
Referenced by WebPage::SendPIDValues().
|
inline |
Definition at line 455 of file Motor.h.
Referenced by WebPage::SendPIDValues().
float Motor::getInterpolationUnitIncrement | ( | ) |
use the internal state and current time to comput where along the path from start to finish the interpolation is
getInterpolationUnitIncrement A unit vector from 0 to 1 representing the state of the interpolation.
Definition at line 32 of file Motor.cpp.
References attach(), BEZIER, BEZIER_P0, BEZIER_P1, duration, isAttached, mode, SINUSOIDAL_INTERPOLATION, startTime, TRAPEZOIDAL, TRAPEZOIDAL_time, and unitDuration.
Referenced by isMotorDoneWithMove(), and loop().
bool Motor::isMotorDoneWithMove | ( | ) |
Check to see if the motor is done with a move.
isMotorDoneWithMove
This checks that the interpolation is done, that the position is within 1 degree and that the velocity is close to zero
Definition at line 213 of file Motor.cpp.
References attach(), getCurrentDegrees(), getDegreesPerSecond(), getInterpolationUnitIncrement(), isAttached, setpoint, TAG, and TICKS_TO_DEGREES.
Referenced by blockUntilMoveIsDone(), and getGainsD().
void Motor::loop | ( | ) |
Loop function this method is called by the timer to run the PID control of the motors and ensure strict timing
Definition at line 347 of file Motor.cpp.
References cachedSpeed, closedLoopControl, currentEffort, DELTA_EFFORT, encoder, endSetpoint, ESP32Encoder::getCount(), getInterpolationUnitIncrement(), I_TERM_SIZE, interruptCountForVelocity, kI, kP, milisecondPosIncrementForVelocity, mode, nowEncoder, prevousCount, runntingITerm, setEffortLocal(), setpoint, startSetpoint, targetEffort, unitDuration, and VELOCITY_MODE.
Referenced by getEffortPercent(), and onMotorTimer().
void Motor::moveFor | ( | float | deltaTargetInDegrees, |
float | speedDegPerSec | ||
) |
MoveFor a relative amount in degrees with speed Set the setpoint for the motor in degrees and the speed you want to get there Bascially, a wrapper function for SetSetpointWithTime that takes speed as an argument
deltaTargetInDegrees | the new relative setpoint for the closed loop controller |
speedDegPerSec | is the speed in degrees per second |
moveFor a relative amount in degrees with speed Set the setpoint for the motor in degrees and the speed you want to get there Bascially, a wrapper function for setSetpointWithTime that takes speed as an argument
deltaTargetInDegrees | the new relative setpoint for the closed loop controller |
speedDegPerSec | is the speed in degrees per second |
Definition at line 269 of file Motor.cpp.
References attach(), blockUntilMoveIsDone(), isAttached, and startMoveFor().
Referenced by getEffortPercent().
void Motor::moveTo | ( | float | newTargetInDegrees, |
float | speedDegPerSec | ||
) |
MoveTo in degrees with speed Set the setpoint for the motor in degrees and the speed you want to get there Bascially, a wrapper function for SetSetpointWithTime that takes speed as an argument
newTargetInDegrees | the new setpoint for the closed loop controller |
speedDegPerSec | is the speed in degrees per second |
moveTo in degrees with speed Set the setpoint for the motor in degrees and the speed you want to get there Bascially, a wrapper function for setSetpointWithTime that takes speed as an argument
newTargetInDegrees | the new setpoint for the closed loop controller |
speedDegPerSec | is the speed in degrees per second |
Definition at line 182 of file Motor.cpp.
References attach(), getCurrentDegrees(), isAttached, and setSetpointWithBezierInterpolation().
Referenced by getEffortPercent(), and loop().
void Motor::setEffort | ( | float | effort | ) |
Definition at line 495 of file Motor.cpp.
References attach(), closedLoopControl, isAttached, and targetEffort.
Referenced by setEffortPercent().
|
private |
Definition at line 529 of file Motor.cpp.
References attach(), directionFlag, isAttached, pwm, and ESP32PWM::writeScaled().
Referenced by loop().
|
inline |
Definition at line 244 of file Motor.h.
References attach(), getEffort(), and setEffort().
void Motor::setGains | ( | float | p, |
float | i, | ||
float | d | ||
) |
PID gains for the PID controller
Definition at line 418 of file Motor.cpp.
References attach(), isAttached, kD, kI, kP, and runntingITerm.
Referenced by setSetpointWithTrapezoidalInterpolation().
void Motor::setGainsD | ( | float | d | ) |
Definition at line 447 of file Motor.cpp.
References attach(), isAttached, kD, and runntingITerm.
Referenced by WebPage::initalize(), setSetpointWithTrapezoidalInterpolation(), and WebPage::UpdatePIDValues().
void Motor::setGainsI | ( | float | i | ) |
Definition at line 439 of file Motor.cpp.
References attach(), isAttached, kI, and runntingITerm.
Referenced by WebPage::initalize(), setSetpointWithTrapezoidalInterpolation(), and WebPage::UpdatePIDValues().
void Motor::setGainsP | ( | float | p | ) |
Definition at line 431 of file Motor.cpp.
References attach(), isAttached, kP, and runntingITerm.
Referenced by WebPage::initalize(), setSetpointWithTrapezoidalInterpolation(), and WebPage::UpdatePIDValues().
|
inline |
SetSetpoint in degrees with time Set the setpoint for the motor in degrees
newTargetInDegrees | the new setpoint for the closed loop controller |
Definition at line 366 of file Motor.h.
References attach(), LINEAR_INTERPOLATION, and setSetpointWithTime().
Referenced by setSpeed(), and WebPage::UpdateSetpoint().
|
inline |
SetSetpoint in degrees with time Set the setpoint for the motor in degrees
newTargetInDegrees | the new setpoint for the closed loop controller |
miliseconds | the number of miliseconds to get from current position to the new setpoint |
Control_0 | On a scale of 0 to 1, where should the first control point in the equation go |
Control_1 | On a scale of 0 to 1, where should the second control point in the equation go use Bezier interpolation |
Definition at line 413 of file Motor.h.
References attach(), BEZIER, and setSetpointWithTime().
Referenced by moveTo(), and startMoveFor().
|
inline |
SetSetpoint in degrees with time Set the setpoint for the motor in degrees
newTargetInDegrees | the new setpoint for the closed loop controller |
miliseconds | the number of miliseconds to get from current position to the new setpoint use linear interoplation |
Definition at line 380 of file Motor.h.
References attach(), LINEAR_INTERPOLATION, and setSetpointWithTime().
Referenced by loop().
|
inline |
SetSetpoint in degrees with time Set the setpoint for the motor in degrees
newTargetInDegrees | the new setpoint for the closed loop controller |
miliseconds | the number of miliseconds to get from current position to the new setpoint use sinusoidal interpolation |
Definition at line 396 of file Motor.h.
References attach(), setSetpointWithTime(), and SINUSOIDAL_INTERPOLATION.
Referenced by loop(), and setSetpointWithTrapezoidalInterpolation().
void Motor::setSetpointWithTime | ( | float | newTargetInDegrees, |
long | msTimeDuration, | ||
interpolateMode | mode | ||
) |
SetSetpoint in degrees with time Set the setpoint for the motor in degrees
newTargetInDegrees | the new setpoint for the closed loop controller |
miliseconds | the number of miliseconds to get from current position to the new setpoint param mode the interpolation mode |
setSetpoint in degrees with time Set the setpoint for the motor in degrees
newTargetInDegrees | the new setpoint for the closed loop controller |
msTimeDuration | the number of miliseconds to get from current position to the new setpoint |
Definition at line 150 of file Motor.cpp.
References attach(), closedLoopControl, duration, encoder, endSetpoint, ESP32Encoder::getCount(), isAttached, mode, setpoint, startSetpoint, startTime, TAG, and TICKS_TO_DEGREES.
Referenced by getEffortPercent(), setSetpoint(), setSetpointWithBezierInterpolation(), setSetpointWithLinearInterpolation(), setSetpointWithSinusoidalInterpolation(), setSetpointWithTrapezoidalInterpolation(), and setSpeed().
|
inline |
SetSetpoint in degrees with time Set the setpoint for the motor in degrees
newTargetInDegrees | the new setpoint for the closed loop controller |
miliseconds | the number of miliseconds to get from current position to the new setpoint |
trapazoidalTime | miliseconds for the ramping to take at the beginning and end. |
use sinusoidal interpolation
Definition at line 433 of file Motor.h.
References attach(), setGains(), setGainsD(), setGainsI(), setGainsP(), setSetpointWithSinusoidalInterpolation(), setSetpointWithTime(), and TRAPEZOIDAL.
void Motor::setSpeed | ( | float | newDegreesPerSecond | ) |
SetSpeed in degrees with time Set the setpoint for the motor in degrees This implements "Red Queen" mode running interpolation in the PID controller.
"Now, here, you see, it takes all the running you can do, to keep in the same place.
If you want to get somewhere else, you must run at least twice as fast as that!"
— The Red Queen, Alice In Wonderland, Lewis Carroll
The way this velocity mode works is that the position target is moved forward every iteration of the PID loop. The position runs away continuously, in order to keep the velocity stable. A position increment is calculated, and added to the Position every 1ms of the loop()
newDegreesPerSecond | the new speed in degrees per second |
setSpeed in degrees with time Set the setpoint for the motor in degrees This implements "Red Queen" mode running interpolation in the PID controller.
"Now, here, you see, it takes all the running you can do, to keep in the same place.
If you want to get somewhere else, you must run at least twice as fast as that!"
— The Red Queen, Alice In Wonderland, Lewis Carroll
The way this velocity mode works is that the position target is moved forward every iteration of the PID loop. The position runs away continuously, in order to keep the velocity stable. A position increment is calculated, and added to the Position every 1ms of the loop()
newDegreesPerSecond | the new speed in degrees per second |
Definition at line 295 of file Motor.cpp.
References attach(), closedLoopControl, getCurrentDegrees(), isAttached, milisecondPosIncrementForVelocity, mode, setSetpoint(), TICKS_TO_DEGREES, and VELOCITY_MODE.
Referenced by getEffortPercent(), loop(), runStateMachine(), and setSpeed().
void Motor::setSpeed | ( | float | newDegreesPerSecond, |
long | miliseconds | ||
) |
SetSpeed in degrees with time Set the setpoint for the motor in degrees
newDegreesPerSecond | the new speed in degrees per second |
miliseconds | the number of miliseconds to run for |
setSpeed in degrees with time Set the setpoint for the motor in degrees
newDegreesPerSecond | the new speed in degrees per second |
miliseconds | the number of miliseconds to run for |
Definition at line 326 of file Motor.cpp.
References attach(), getCurrentDegrees(), isAttached, LINEAR_INTERPOLATION, setSetpointWithTime(), and setSpeed().
float Motor::startMoveFor | ( | float | deltaTargetInDegrees, |
float | speedDegPerSec | ||
) |
StartMoveFor a relative amount in degrees with speed Set the setpoint for the motor in degrees and the speed you want to get there Bascially, a wrapper function for SetSetpointWithTime that takes speed as an argument
deltaTargetInDegrees | the new relative setpoint for the closed loop controller |
speedDegPerSec | is the speed in degrees per second |
Definition at line 192 of file Motor.cpp.
References attach(), getCurrentDegrees(), isAttached, and setSetpointWithBezierInterpolation().
Referenced by getEffortPercent(), and moveFor().
|
private |
BEZIER Control Point 0.
https://stackoverflow.com/a/43071667
Definition at line 152 of file Motor.h.
Referenced by getInterpolationUnitIncrement().
|
private |
BEZIER Control Point 1.
https://stackoverflow.com/a/43071667
Definition at line 158 of file Motor.h.
Referenced by getInterpolationUnitIncrement().
|
private |
this variable is the most recently calculated speed
Definition at line 70 of file Motor.h.
Referenced by getDegreesPerSecond(), and loop().
|
private |
this is a flag to switch between using the PID controller, or allowing the user to set effort 'directly'
Definition at line 108 of file Motor.h.
Referenced by loop(), setEffort(), setSetpointWithTime(), and setSpeed().
|
private |
variable for caching the current effort being sent to the PWM/direction pins
Definition at line 116 of file Motor.h.
Referenced by getEffort(), and loop().
int Motor::directionFlag = -1 |
GPIO pin number of the motor direction output flag
Definition at line 173 of file Motor.h.
Referenced by attach(), Motor(), and setEffortLocal().
|
private |
PID controller Interpolation duration in miliseconds
Definition at line 120 of file Motor.h.
Referenced by getInterpolationUnitIncrement(), and setSetpointWithTime().
|
private |
|
private |
PID controller Interpolation setpoint for the interpolation to arrive at
Definition at line 128 of file Motor.h.
Referenced by loop(), and setSetpointWithTime().
|
private |
|
private |
True if the motor has been attached
Definition at line 57 of file Motor.h.
Referenced by attach(), blockUntilMoveIsDone(), getCurrentDegrees(), getDegreesPerSecond(), getEffort(), getInterpolationUnitIncrement(), isMotorDoneWithMove(), moveFor(), moveTo(), setEffort(), setEffortLocal(), setGains(), setGainsD(), setGainsI(), setGainsP(), setSetpointWithTime(), setSpeed(), and startMoveFor().
|
private |
PID controller derivitive constant
Definition at line 86 of file Motor.h.
Referenced by getGainsD(), setGains(), and setGainsD().
|
private |
PID controller integral constant
Definition at line 82 of file Motor.h.
Referenced by getGainsI(), loop(), setGains(), and setGainsI().
|
private |
PID controller proportional constant
Definition at line 78 of file Motor.h.
Referenced by getGainsP(), loop(), setGains(), and setGainsP().
|
static |
This is a list of all of the Motor objects that have been attached. As a motor is attahed, it adds itself to this list of Motor pointers. This list is read by the PID thread and each object in the list has loop() called. once every milisecond.
Definition at line 200 of file Motor.h.
Referenced by WebPage::initalize(), WebPage::SendPIDValues(), WebPage::SendSetpoint(), WebPage::UpdatePIDValues(), and WebPage::UpdateSetpoint().
|
private |
when using Red Queen mode for velocity interpolation, this is the amount of setpoint to add to the current setpoint every milisecond to maintain a smooth velocity trajectory.
Definition at line 146 of file Motor.h.
Referenced by loop(), and setSpeed().
|
private |
Current interpolation mode, linear, sinusoidal or velocity
Definition at line 140 of file Motor.h.
Referenced by getInterpolationUnitIncrement(), loop(), setSetpointWithTime(), and setSpeed().
int Motor::MotorEncAPin = -1 |
int Motor::MotorEncBPin = -1 |
int Motor::MotorPWMPin = -1 |
int64_t Motor::nowEncoder = 0 |
Variable to store the latest encoder read from the encoder hardware as read by the PID thread. This variable is set inside the PID thread, and read outside.
Definition at line 190 of file Motor.h.
Referenced by getCurrentDegrees(), and loop().
|
private |
|
private |
a variable to store the running avarage for the integral term
Definition at line 90 of file Motor.h.
Referenced by loop(), setGains(), setGainsD(), setGainsI(), and setGainsP().
|
private |
PID controller setpoint in encoder ticks
Definition at line 74 of file Motor.h.
Referenced by isMotorDoneWithMove(), loop(), and setSetpointWithTime().
|
private |
PID controller Interpolation setpoint at the start of interpolation
Definition at line 132 of file Motor.h.
Referenced by loop(), and setSetpointWithTime().
|
private |
PID controller Interpolation time in miliseconds that the interplation began
Definition at line 124 of file Motor.h.
Referenced by getInterpolationUnitIncrement(), and setSetpointWithTime().
|
private |
variable for storing target effort to allow for smoother accelerations
Definition at line 112 of file Motor.h.
Referenced by loop(), and setEffort().
|
static |
the is a flag to check if the timer has been allocated and the thread started.
Definition at line 194 of file Motor.h.
Referenced by allocateTimer(), and attach().
|
private |
the amount of time to ramp up and ramp down the speed
Definition at line 163 of file Motor.h.
Referenced by getInterpolationUnitIncrement().
|
private |
Duration of the interpolation mode, 1 equals done, 0 starting
Definition at line 136 of file Motor.h.
Referenced by getInterpolationUnitIncrement(), and loop().