wpi-32u4-library
Public Member Functions | Data Fields | Protected Attributes
Chassis Class Reference

#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
 

Detailed Description

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.

Definition at line 18 of file Chassis.h.

Constructor & Destructor Documentation

◆ Chassis()

Chassis::Chassis ( float  wheelDiam = 7,
float  ticksPerRevolution = 1440,
float  wheelTrack = 14.7 
)
inline

Chassis constructor.

Parameters
wheelDiamWheel diameter in cm.
ticksPerRevolutionEnccoder ticks per wheel revolution.
wheelTrackDistance between wheels in cm.

Definition at line 36 of file Chassis.h.

Member Function Documentation

◆ checkMotionComplete()

bool Chassis::checkMotionComplete ( void  )

Checks if the motion commanded by driveFor() or turnFor() is done.

Returns
Returns true if the motion is complete.

Definition at line 118 of file Chassis.cpp.

References Romi32U4Motor::checkComplete(), leftMotor, and rightMotor.

Referenced by driveFor(), and turnFor().

◆ driveFor()

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.

Parameters
forwardDistanceTarget distance in cm
forwardSpeedTarget speed rate in cm/sec
blockIf 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().

◆ idle()

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().

◆ init()

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().

◆ printEncoderCounts()

void Chassis::printEncoderCounts ( void  )

Definition at line 152 of file Chassis.cpp.

References Romi32U4Motor::getCount(), leftMotor, and rightMotor.

◆ printSpeeds()

void Chassis::printSpeeds ( void  )

Definition at line 144 of file Chassis.cpp.

References leftMotor, rightMotor, and Romi32U4Motor::speed.

◆ setMotorEfforts()

void Chassis::setMotorEfforts ( int  leftEffort,
int  rightEffort 
)

Sets motor efforts. Max speed is 420.

Parameters
leftEffortEffort for left motor
rightEffortEffort 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().

◆ setMotorPIDcoeffs()

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().

◆ setTwist()

void Chassis::setTwist ( float  forwardSpeed,
float  turningSpeed 
)

Sets target motion for the chassis.

Parameters
forwardSpeedTarget forward speed in cm/sec
rightSpeedTarget 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().

◆ setWheelSpeeds()

void Chassis::setWheelSpeeds ( float  leftSpeed,
float  rightSpeed 
)

Sets target wheel speeds in cm/sec.

Parameters
leftSpeedTarget speed for left wheel in cm/sec
rightSpeedTarget speed for right wheel in cm/sec

Definition at line 60 of file Chassis.cpp.

References cmPerEncoderTick, ctrlIntervalMS, leftMotor, rightMotor, and Romi32U4Motor::setTargetSpeed().

◆ turnFor()

void Chassis::turnFor ( float  turnAngle,
float  turningSpeed,
bool  block = false 
)

Commands the chassis to turn a set angle.

Parameters
turnAngleTarget angle to turn in degrees
turningSpeedTarget spin rate in deg/sec
blockIf 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().

◆ updateEncoderDeltas()

void Chassis::updateEncoderDeltas ( void  )
inline

Definition at line 135 of file Chassis.cpp.

References Romi32U4Motor::calcEncoderDelta(), leftMotor, rightMotor, and Romi32U4Motor::update().

Referenced by ISR().

Field Documentation

◆ cmPerEncoderTick

const float Chassis::cmPerEncoderTick
protected

Definition at line 25 of file Chassis.h.

Referenced by driveFor(), setTwist(), setWheelSpeeds(), and turnFor().

◆ ctrlIntervalMS

const uint16_t Chassis::ctrlIntervalMS = 16
protected

Definition at line 27 of file Chassis.h.

Referenced by setTwist(), and setWheelSpeeds().

◆ leftMotor

LeftMotor Chassis::leftMotor

◆ rightMotor

RightMotor Chassis::rightMotor

◆ robotRadius

const float Chassis::robotRadius
protected

Definition at line 26 of file Chassis.h.

Referenced by setTwist(), and turnFor().


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