PicoLowLevel
|
Class used to control DC motors at a constant speed. More...
#include <SmartMotor.h>
Public Member Functions | |
SmartMotor (byte pwm, byte dir, byte enc_a, byte enc_b, bool invert=false, PIO pio=pio0) | |
Create SmartMotor object, creating all necessary objects. | |
void | begin () |
Initialize SmartMotor and necessary components. | |
void | update () |
Update routine, updating the PID and the motor speed. | |
void | setSpeed (float value) |
Set the desired speed of the motor. | |
float | getSpeed () |
Get the current speed of the motor. | |
void | stop () |
Stop the motor. | |
void | calibrate (float target=45.f) |
Calibrate the PID controller. | |
Class used to control DC motors at a constant speed.
Relies on a PID controller receiving data from a rotary encoder that uses a PIO state machine to reduce CPU usage and increase reliability.
SmartMotor::SmartMotor | ( | byte | pwm, |
byte | dir, | ||
byte | enc_a, | ||
byte | enc_b, | ||
bool | invert = false , |
||
PIO | pio = pio0 |
||
) |
Create SmartMotor object, creating all necessary objects.
pwm | PWM pin. |
dir | Direction pin. |
enc_a | Pin A of the encoder. |
enc_b | Pin B of the encoder. |
invert | Invert motor direction, usuful when motors are mounted opposite to one another. |
pio | PIO to use for the encoder. Each PIO can handle up to 4 encoders. |
void SmartMotor::begin | ( | ) |
Initialize SmartMotor and necessary components.
void SmartMotor::calibrate | ( | float | target = 45.f | ) |
Calibrate the PID controller.
This function will set the PID parameters to values that should work for the motor. The method used is based on Åström–Hägglund tuning method, while using Ziegler-Nichols formulas to compute the gains. Only the Kp and Ki gains are computed while the Kd gain is set to 0 since it doesn't have a positive effect on controlling the motor.
target | Target speed to use for calibration. |
float SmartMotor::getSpeed | ( | ) |
Get the current speed of the motor.
The value is only updated at a fixed rate, defined by DT_ENC, to avoid losing precision.
void SmartMotor::setSpeed | ( | float | value | ) |
Set the desired speed of the motor.
value | Desired motor speed between -MAX_SPEED and MAX_SPEED. |
void SmartMotor::stop | ( | ) |
Stop the motor.
This function will stop the motor and reset the PID.
void SmartMotor::update | ( | ) |
Update routine, updating the PID and the motor speed.
This function will be executed at a fixed rate, defined by DT_PID, and should therefore be called as often as possible.