wpi-32u4-library
|
#include <Chassis.h>
Public Member Functions | |
Chassis (float wheelDiam=7, float ticksPerRevolution=1440, float wheelTrack=14.7) | |
Chassis constructor. More... | |
void | init (void) |
Initializes the chassis. Must be called in setup(). More... | |
void | setMotorPIDcoeffs (float kp, float ki) |
Sets PID coefficients for the motors. Not independent. More... | |
void | idle (void) |
Idles chassis. Motors will stop. More... | |
void | setMotorEfforts (int leftEffort, int rightEffort) |
Sets motor efforts. Max speed is 420. More... | |
void | setWheelSpeeds (float leftSpeed, float rightSpeed) |
Sets target wheel speeds in cm/sec. More... | |
void | setTwist (float forwardSpeed, float turningSpeed) |
Sets target motion for the chassis. More... | |
void | driveFor (float forwardDistance, float forwardSpeed, bool block=false) |
Commands the robot to drive at a distance and speed. More... | |
void | turnFor (float turnAngle, float turningSpeed, bool block=false) |
Commands the chassis to turn a set angle. More... | |
bool | checkMotionComplete (void) |
Checks if the motion commanded by driveFor() or turnFor() is done. More... | |
void | printSpeeds (void) |
void | printEncoderCounts (void) |
void | updateEncoderDeltas () |
Data Fields | |
LeftMotor | leftMotor |
RightMotor | rightMotor |
Protected Attributes | |
const float | cmPerEncoderTick |
const float | robotRadius |
const uint16_t | ctrlIntervalMS = 16 |
The Chassis class manages the motors and encoders.
Chassis sets up a hardware-based timer on a 16ms interval. At each interrupt, it reads the current encoder counts and, if controlling for speed, calculates the effort using a PID controller for each motor, which can be adjusted by the user.
The encoders are attached automatically and the encoders will count regardless of the state of the robot.
Several methods are provided for low level control to commands for driving or turning.
|
inline |
bool Chassis::checkMotionComplete | ( | void | ) |
Checks if the motion commanded by driveFor() or turnFor() is done.
Definition at line 118 of file Chassis.cpp.
References Romi32U4Motor::checkComplete(), leftMotor, and rightMotor.
Referenced by driveFor(), and turnFor().
void Chassis::driveFor | ( | float | forwardDistance, |
float | forwardSpeed, | ||
bool | block = false |
||
) |
Commands the robot to drive at a distance and speed.
The chassis will stop when the distance is reached.
forwardDistance | Target distance in cm |
forwardSpeed | Target speed rate in cm/sec |
block | If true, the function blocks until the motion is complete |
Definition at line 80 of file Chassis.cpp.
References checkMotionComplete(), cmPerEncoderTick, leftMotor, Romi32U4Motor::moveFor(), rightMotor, and setTwist().
void Chassis::idle | ( | void | ) |
Idles chassis. Motors will stop.
Stops the motors. It calls setMotorEfforts() so that the wheels won't lock. Use setSpeeds() if you want the wheels to 'lock' in place.
Definition at line 46 of file Chassis.cpp.
References setMotorEfforts().
Referenced by idle().
void Chassis::init | ( | void | ) |
Initializes the chassis. Must be called in setup().
Call init() in your setup() routine. It sets up some internal timers so that the speed controllers for the wheels will work properly.
Here's how it works: Motor::init() starts a hardware timer on a 16 ms loop. Every time the timer "rolls over," an interrupt service routine (ISR) is called that updates the motor speeds and sets a flag to notify Chassis that it is time to calculate the control inputs.
When set up this way, pins 6, 12, and 13 cannot be used with analogWrite()
Definition at line 13 of file Chassis.cpp.
References Romi32U4Motor::init().
Referenced by setup().
void Chassis::printEncoderCounts | ( | void | ) |
Definition at line 152 of file Chassis.cpp.
References Romi32U4Motor::getCount(), leftMotor, and rightMotor.
void Chassis::printSpeeds | ( | void | ) |
Definition at line 144 of file Chassis.cpp.
References leftMotor, rightMotor, and Romi32U4Motor::speed.
void Chassis::setMotorEfforts | ( | int | leftEffort, |
int | rightEffort | ||
) |
Sets motor efforts. Max speed is 420.
leftEffort | Effort for left motor |
rightEffort | Effort for right motor |
Sets the motor efforts.
Definition at line 54 of file Chassis.cpp.
References leftMotor, rightMotor, LeftMotor::setMotorEffort(), and RightMotor::setMotorEffort().
Referenced by idle().
void Chassis::setMotorPIDcoeffs | ( | float | kp, |
float | ki | ||
) |
Sets PID coefficients for the motors. Not independent.
Definition at line 35 of file Chassis.cpp.
References leftMotor, rightMotor, and Romi32U4Motor::setPIDCoeffients().
Referenced by setup().
void Chassis::setTwist | ( | float | forwardSpeed, |
float | turningSpeed | ||
) |
Sets target motion for the chassis.
forwardSpeed | Target forward speed in cm/sec |
rightSpeed | Target spin rate in deg/sec |
Definition at line 70 of file Chassis.cpp.
References cmPerEncoderTick, ctrlIntervalMS, leftMotor, rightMotor, robotRadius, and Romi32U4Motor::setTargetSpeed().
Referenced by driveFor(), and turnFor().
void Chassis::setWheelSpeeds | ( | float | leftSpeed, |
float | rightSpeed | ||
) |
Sets target wheel speeds in cm/sec.
leftSpeed | Target speed for left wheel in cm/sec |
rightSpeed | Target speed for right wheel in cm/sec |
Definition at line 60 of file Chassis.cpp.
References cmPerEncoderTick, ctrlIntervalMS, leftMotor, rightMotor, and Romi32U4Motor::setTargetSpeed().
void Chassis::turnFor | ( | float | turnAngle, |
float | turningSpeed, | ||
bool | block = false |
||
) |
Commands the chassis to turn a set angle.
turnAngle | Target angle to turn in degrees |
turningSpeed | Target spin rate in deg/sec |
block | If true, the function blocks until the motion is complete |
Definition at line 99 of file Chassis.cpp.
References checkMotionComplete(), cmPerEncoderTick, leftMotor, Romi32U4Motor::moveFor(), rightMotor, robotRadius, and setTwist().
|
inline |
Definition at line 135 of file Chassis.cpp.
References Romi32U4Motor::calcEncoderDelta(), leftMotor, rightMotor, and Romi32U4Motor::update().
Referenced by ISR().
|
protected |
Definition at line 25 of file Chassis.h.
Referenced by driveFor(), setTwist(), setWheelSpeeds(), and turnFor().
|
protected |
Definition at line 27 of file Chassis.h.
Referenced by setTwist(), and setWheelSpeeds().
LeftMotor Chassis::leftMotor |
Definition at line 21 of file Chassis.h.
Referenced by checkMotionComplete(), driveFor(), Romi32U4Motor::initEncoders(), leftISR(), printEncoderCounts(), printSpeeds(), setMotorEfforts(), setMotorPIDcoeffs(), setTwist(), setWheelSpeeds(), turnFor(), and updateEncoderDeltas().
RightMotor Chassis::rightMotor |
Definition at line 22 of file Chassis.h.
Referenced by checkMotionComplete(), driveFor(), Romi32U4Motor::initEncoders(), printEncoderCounts(), printSpeeds(), rightISR(), setMotorEfforts(), setMotorPIDcoeffs(), setTwist(), setWheelSpeeds(), turnFor(), and updateEncoderDeltas().
|
protected |
Definition at line 26 of file Chassis.h.
Referenced by setTwist(), and turnFor().