PicoLowLevel
Loading...
Searching...
No Matches
SmartMotor Class Reference

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.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ SmartMotor()

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.

Parameters
pwmPWM pin.
dirDirection pin.
enc_aPin A of the encoder.
enc_bPin B of the encoder.
invertInvert motor direction, usuful when motors are mounted opposite to one another.
pioPIO to use for the encoder. Each PIO can handle up to 4 encoders.

Member Function Documentation

◆ begin()

void SmartMotor::begin ( )

Initialize SmartMotor and necessary components.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ calibrate()

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.

Parameters
targetTarget speed to use for calibration.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getSpeed()

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.

Returns
float Current speed of the motor between -MAX_SPEED and MAX_SPEED.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setSpeed()

void SmartMotor::setSpeed ( float  value)

Set the desired speed of the motor.

Parameters
valueDesired motor speed between -MAX_SPEED and MAX_SPEED.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop()

void SmartMotor::stop ( )

Stop the motor.

This function will stop the motor and reset the PID.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update()

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.

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: