![]() |
Robotic Car Controller 1.0
Library to manage a two wheeled robotic vehicle (Stepper Motors)
|
#include <MD_SmartCar2.h>
Classes | |
struct | actionItem_t |
Public Member Functions | |
Class constructor and destructor. | |
MD_SmartCar2 (MD_Stepper *ml, MD_Stepper *mr) | |
~MD_SmartCar2 (void) | |
Methods for core object control. | |
bool | begin (uint16_t ppr, uint16_t ppsMax, uint16_t dWheel, uint16_t lBase) |
void | setVehicleParameters (uint16_t ppr, uint16_t ppsMax, uint16_t dWheel, uint16_t lBase) |
void | run (void) |
bool | isRunning (void) |
bool | isRunning (uint8_t mtr) |
Methods for free running the vehicle. | |
void | drive (int8_t vLinear, int8_t vAngularD) |
void | drive (int8_t vLinear) |
void | drive (int8_t vLinear, float vAngularR) |
void | stop (bool insideSequence=false) |
void | setLinearVelocity (int8_t vel) |
int8_t | getLinearVelocity (void) |
void | setAngularVelocity (float angR) |
void | setAngularVelocity (int8_t angD) |
float | getAngularVelocity (void) |
void | setAccelProfile (uint16_t timebase, uint8_t steps) |
Methods for precision movements of the vehicle. | |
void | move (float angL, float angR) |
void | move (int16_t angL, int16_t angR) |
void | move (int16_t len) |
void | spin (int16_t fraction) |
void | startSequence (const actionItem_t *actionList) |
void | startSequence (actionItem_t *actionList) |
void | stopSequence (void) |
bool | isSequenceComplete (void) |
Methods for Configuration Management. | |
uint16_t | getPulsePerRev () |
float | getDistancePerPulse () |
Utility methods. | |
float | deg2rad (int16_t deg) |
float | len2rad (int16_t len) |
Structures, Enumerated Types and Constants. | |
enum | actionId_t { DRIVE , MOVE , SPIN , PAUSE , STOP , END } |
static const uint8_t | MAX_MOTOR = 2 |
Core object for the MD_SmartCar2 library
Enumerated type for Action Items operation
Specifies which operation is being defined in the actionItem_t
Enumerator | |
---|---|
DRIVE | executes drive(); param 0 lin vel, param 1 angular velocity |
MOVE | executes a move(); param 0 left rotate, param 1 right rotate |
SPIN | executes a spin(); param 0 spin percentage |
PAUSE | executes a pause; param 0 milliseconds pause |
STOP | executes a stop() |
END | marks the end of the action list; should always be last item. |
MD_SmartCar2::MD_SmartCar2 | ( | MD_Stepper * | ml, |
MD_Stepper * | mr | ||
) |
Class Constructor
Instantiate a new instance of the class. This variant is for motor controllers that have a PWM input for speed control.
The main function for the core object is to reset the internal shared variables and timers to default values.
ml | The MD_Stepper object for controlling the left side motor. |
mr | The MD_Stepper object for controlling the right side motor. |
MD_SmartCar2::~MD_SmartCar2 | ( | void | ) |
Class Destructor.
Release any allocated memory and clean up anything else.
bool MD_SmartCar2::begin | ( | uint16_t | ppr, |
uint16_t | ppsMax, | ||
uint16_t | dWheel, | ||
uint16_t | lBase | ||
) |
Initialize the object.
Initialize the object data. This needs to be called during setup() to reset new items that cannot be done during object creation.
Vehicle constants are passed through to the setVehicleParameters() method. See comments for that method for more details.
ppr | Number of encoder pulses per wheel revolution. |
ppsMax | Maximum number of encoder pulses per second at top speed (100% velocity). |
dWheel | Wheel diameter in mm. |
lBase | Base length (distance between wheel centers) in mm |
float MD_SmartCar2::deg2rad | ( | int16_t | deg | ) |
Convert degrees to radians.
Convert the degrees measure specified into radians.
deg | the value in degrees to be converted. |
void MD_SmartCar2::drive | ( | int8_t | vLinear | ) |
Drive the vehicle along a straight path.
Run the vehicle along a straight path with the specified velocity.
The velocity is specified as a percentage of the maximum vehicle velocity [-100..100]. Positive velocity move the vehicle forward, negative moves it in reverse. The speed will be adjusted using the acceleration parameters.
vLinear | the linear velocity as a percentage of full scale [-100..100]. |
void MD_SmartCar2::drive | ( | int8_t | vLinear, |
float | vAngularR | ||
) |
Drive the vehicle along specified path (radians).
Run the vehicle along a path with the specified velocity and angular orientation.
The velocity is specified as a percentage of the maximum vehicle velocity [-100..100]. Positive velocity move the vehicle forward, negative moves it in reverse. The speed will be adjusted using the acceleration parameters.
Angular velocity direction is specified in radians per second [-pi/2..pi/2]. Positive angle is clockwise rotation.
vLinear | the linear velocity as a percentage of full scale [-100..100]. |
vAngularR | the angular velocity in radians per second [-pi/2..pi/2]. |
void MD_SmartCar2::drive | ( | int8_t | vLinear, |
int8_t | vAngularD | ||
) |
Drive the vehicle along specified path (degrees).
Run the vehicle along a path with the specified velocity and angular orientation.
The velocity is specified as a percentage of the maximum vehicle velocity [0..100]. Positive velocity move the vehicle forward, negative moves it in reverse. The speed will be adjusted using the acceleration parameters.
Angular velocity is specified in degrees per second [-90..90]. Positive angle is clockwise rotation.
vLinear | the linear velocity as a percentage of full scale [-100..100]. |
vAngularD | the angular velocity in degrees per second [-90..90]. |
float MD_SmartCar2::getAngularVelocity | ( | void | ) |
Get the current angular velocity.
Angular velocity is expressed in radians relative to the forward direction [-PI/2..PI/2]. Positive angle is turn to the right, negative left.
float MD_SmartCar2::getDistancePerPulse | ( | ) |
Read linear length moved per wheel pulse
Returns the distance traveled for each pulse of the motor.
int8_t MD_SmartCar2::getLinearVelocity | ( | void | ) |
Get the current linear velocity.
Linear velocity is expressed as a percentage of the maximum velocity [-100..100]. The Master velocity is used to regulate all the speed functions for the motors.
uint16_t MD_SmartCar2::getPulsePerRev | ( | ) |
Read pulses per wheel revolution
Returns the number of pulses per encoder revolution. This may be needed to change from number of pulses to revolutions and then distance.
bool MD_SmartCar2::isRunning | ( | uint8_t | mtr | ) |
bool MD_SmartCar2::isRunning | ( | void | ) |
bool MD_SmartCar2::isSequenceComplete | ( | void | ) |
Check if the current action sequence has completed.
Once an action sequqnce is started it will automatically execute to completion unless interrupted. This method checks to see if the action sequqnce has completed.
float MD_SmartCar2::len2rad | ( | int16_t | len | ) |
Convert a length to angle of wheel rotation.
Convert a length in mm to travel into the radian of wheel rotation required for that travel.
len | length in mm to convert. |
void MD_SmartCar2::move | ( | float | angL, |
float | angR | ||
) |
Precisely move the vehicle (radians).
Controls the movement by counting the encoder pulses rather that PID, which should make it more precise and controlled. This is useful for specific movements run at slow speed.
The call to move() specifies the angle each wheels will turn, independently. This method is designed to allow close movements such as spin-in-place or other short precise motions.
The motion for each wheel is specified as speed as the total angle subtended by the turned by the wheel in radians. Negative angle is a reverse wheel rotation.
angL | left wheel angle subtended by the motion in radians. |
angR | right wheel angle subtended by the motion in radians. |
void MD_SmartCar2::move | ( | int16_t | angL, |
int16_t | angR | ||
) |
Precisely move the vehicle (degrees).
Controls the movement by counting the encoder pulses rather that PID, which should make it more precise and controlled. This is useful for specific movements run at slow speed.
The call to move() specifies the precise motion of the motors, independently. This method is designed to allow close movements such as spin-in-place or other short precise motions.
The motion for each wheel is specified as speed as the total angle subtended by the turned by the wheel in degrees. Negative angle is a reverse wheel rotation.
angL | left wheel angle subtended by the motion in degrees. |
angR | right wheel angle subtended by the motion in degrees. |
void MD_SmartCar2::move | ( | int16_t | len | ) |
Precisely move the vehicle (millimeter).
Controls the movement by counting the encoder pulses rather that PID, which should make it more precise and controlled. This is useful for specific movements run at slow speed.
The call to move() specifies the precise motion of the motors, independently. This method is designed to allow close movements such as spin-in-place or other short precise motions.
The motion for each wheel will be identical to move the vehicle the required distance. Negative length is a reverse wheel rotation.
len | distance to move in mm. |
void MD_SmartCar2::run | ( | void | ) |
Run the Robot Management Services.
This is called every iteration through loop() to run all the required Smart Car Management functions.
void MD_SmartCar2::setAccelProfile | ( | uint16_t | timebase, |
uint8_t | steps | ||
) |
Set the acceleration profile parameters
To make the best use of the stepper motor torque, speed changes are adjusted in incremental steps. The number of stepos is given by the steps parameter and the time between each step is given by the timebase parameter.
The library calculates the difference between the 2 speed (S2 - S1) and divides this by steps. During the acceleration/deceleration, the motor speed is changed by this quantity timebase time period until the new speed is reached.
Default values are the class private constants DEF_ACCEL_TIME and DEF_ACCEL_STEPS.
timebase | time between acceleration steps (in milliseconds) |
steps | number of dicrete steps between start and end speeds |
void MD_SmartCar2::setAngularVelocity | ( | float | angR | ) |
Set the angular velocity (radians).
Sets the angular velocity without changing any other parameters. Useful for adjusting turning when already in motion.
Angular velocity is expressed in radians relative to the forward direction [-PI/2..PI/2]. Positive angle is turn to the right, negative left.
angR | the new turning rate in radians. |
void MD_SmartCar2::setAngularVelocity | ( | int8_t | angD | ) |
Set the angular velocity (degrees).
Sets the angular velocity without changing any other parameters. Useful for adjusting turning when already in motion.
Angular velocity is expressed in degrees relative to the forward direction [-90..90]. Positive angle is turn to the right, negative left.
angD | the new turning rate in degrees. |
void MD_SmartCar2::setLinearVelocity | ( | int8_t | vel | ) |
Set the linear velocity
Sets the linear velocity without changing any other parameters. Useful for adjusting the speed when already in motion. The vehicle will accelerate/decelerate to the new speed.
The velocity is specified as a percentage of the maximum vehicle velocity [-100..100]. Positive velocity move the vehicle forward, negative moves it in reverse.
/sa getLinearVelocity(), drive(), setAccelProfile()
vel | the new value for the linear velocity [-100..100]. |
void MD_SmartCar2::setVehicleParameters | ( | uint16_t | ppr, |
uint16_t | ppsMax, | ||
uint16_t | dWheel, | ||
uint16_t | lBase | ||
) |
Set the vehicle constants
Sets the number of pulses per encoder revolution, maximum speed reading and important dimensions for the vehicle. This depends on the hardware and could vary between different vehicle configurations.
For encoder ppr, there is only one value for all whole vehicle, so all encoders need to operate the same way.
ppr | Number of encoder pulses per wheel revolution. |
ppsMax | Maximum number of encoder pulses per second at top speed (100% velocity). |
dWheel | Wheel diameter in mm. |
lBase | Base length (distance between wheel centers) in mm |
void MD_SmartCar2::spin | ( | int16_t | fraction | ) |
Precisely spin the vehicle.
Controls the movement by spinning the vehicle about its central vertical axis. It works similar to move() to spin the wheels in an directions to effect the turning motion.
The call to spin() specifies the percentage (-100 to 100) of the full circle rotation about the central axis passing through the vehicle base length. Positive angle is a turn to the right, negative to the left.
fraction | Percentage fraction of full revolution [-100..100]. Positive spins right; negative pins left. |
void MD_SmartCar2::startSequence | ( | actionItem_t * | actionList | ) |
Start an action sequence stored in RAM.
This method is passed the reference to an action sequence array stored in RAM to the library for background execution. The array must remain in scope (ie, global or static declaration) for the duration of the sequence running.
Details on actions sequences can be found at Action Sequences
actionList | pointer to the array of actionItem_t ending with and END record. |
void MD_SmartCar2::startSequence | ( | const actionItem_t * | actionList | ) |
Start an action sequence stored in PROGMEM.
This method is passed the reference to an action sequence array stored in PROGMEM to the library for background execution.
Details on actions sequences can be found at Action Sequences
actionList | pointer to the array of actionItem_t ending with and END record. |
void MD_SmartCar2::stop | ( | bool | insideSequence = false | ) |
Stop the smart car.
This method brings the SmartCar to an immediate stop.
insideSequence | set true if this was called from inside a sequence (not for end users) |
void MD_SmartCar2::stopSequence | ( | void | ) |
Stop the currently executing sequence.
This method stops the currently executing sequence in the middle of the execution stream. This vehicle will be left in an indetermined state and the sequence will be reported as completed.
|
static |
Maximum number of motors
Define the maximum number of motors that this library will control