okapi::ChassisControllerPID class

Base classes

class ChassisController

Constructors, destructors, conversion operators

ChassisControllerPID(TimeUtil itimeUtil, std::shared_ptr<ChassisModel> imodel, std::unique_ptr<IterativePosPIDController> idistanceController, std::unique_ptr<IterativePosPIDController> iturnController, std::unique_ptr<IterativePosPIDController> iangleController, const AbstractMotor::GearsetRatioPair& igearset = AbstractMotor::gearset::green, const ChassisScales& iscales = ChassisScales({1, 1}, imev5GreenTPR), std::shared_ptr<Logger> ilogger = Logger::getDefaultLogger())
ChassisController using PID control.
ChassisControllerPID(const ChassisControllerPID&) deleted
ChassisControllerPID(ChassisControllerPID&& other) deleted
~ChassisControllerPID() override

Public functions

auto operator=(const ChassisControllerPID& other) -> ChassisControllerPID& deleted
auto operator=(ChassisControllerPID&& other) -> ChassisControllerPID& deleted
void moveDistance(QLength itarget) override
Drives the robot straight for a distance (using closed-loop control).
void moveRaw(double itarget) override
Drives the robot straight for a distance (using closed-loop control).
void moveDistanceAsync(QLength itarget) override
Sets the target distance for the robot to drive straight (using closed-loop control).
void moveRawAsync(double itarget) override
Sets the target distance for the robot to drive straight (using closed-loop control).
void turnAngle(QAngle idegTarget) override
Turns the robot clockwise in place (using closed-loop control).
void turnRaw(double idegTarget) override
Turns the robot clockwise in place (using closed-loop control).
void turnAngleAsync(QAngle idegTarget) override
Sets the target angle for the robot to turn clockwise in place (using closed-loop control).
void turnRawAsync(double idegTarget) override
Sets the target angle for the robot to turn clockwise in place (using closed-loop control).
void setTurnsMirrored(bool ishouldMirror) override
Sets whether turns should be mirrored.
auto isSettled() -> bool override
Checks whether the internal controllers are currently settled.
void waitUntilSettled() override
Delays until the currently executing movement completes.
auto getChassisScales() const -> ChassisScales override
Gets the ChassisScales.
auto getGearsetRatioPair() const -> AbstractMotor::GearsetRatioPair override
Gets the GearsetRatioPair.
void setVelocityMode(bool ivelocityMode)
Sets the velocity mode flag.
void setGains(const IterativePosPIDController::Gains& idistanceGains, const IterativePosPIDController::Gains& iturnGains, const IterativePosPIDController::Gains& iangleGains)
Sets the gains for all controllers.
auto getGains() const -> std::tuple<IterativePosPIDController::Gains, IterativePosPIDController::Gains, IterativePosPIDController::Gains>
Gets the current controller gains.
void startThread()
Starts the internal thread.
auto getThread() const -> CrossplatformThread*
Returns the underlying thread handle.
void stop() override
Interrupts the current movement to stop the robot.
void setMaxVelocity(double imaxVelocity) override
Sets a new maximum velocity in RPM [0-600].
auto getMaxVelocity() const -> double override
auto getModel() -> std::shared_ptr<ChassisModel> override
auto model() -> ChassisModel& override

Protected types

enum modeType { distance, angle, none }

Protected static functions

static void trampoline(void* context)

Protected functions

void loop()
auto waitForDistanceSettled() -> bool
Wait for the distance setup (distancePid and anglePid) to settle.
auto waitForAngleSettled() -> bool
Wait for the angle setup (anglePid) to settle.
void stopAfterSettled()
Stops all the controllers and the ChassisModel.

Protected variables

std::shared_ptr<Logger> logger
bool normalTurns
std::shared_ptr<ChassisModel> chassisModel
TimeUtil timeUtil
std::unique_ptr<IterativePosPIDController> distancePid
std::unique_ptr<IterativePosPIDController> turnPid
std::unique_ptr<IterativePosPIDController> anglePid
ChassisScales scales
AbstractMotor::GearsetRatioPair gearsetRatioPair
bool velocityMode
std::atomic_bool doneLooping
std::atomic_bool doneLoopingSeen
std::atomic_bool newMovement
std::atomic_bool dtorCalled
QTime threadSleepTime
modeType mode
CrossplatformThread* task

Function documentation

okapi::ChassisControllerPID::ChassisControllerPID(TimeUtil itimeUtil, std::shared_ptr<ChassisModel> imodel, std::unique_ptr<IterativePosPIDController> idistanceController, std::unique_ptr<IterativePosPIDController> iturnController, std::unique_ptr<IterativePosPIDController> iangleController, const AbstractMotor::GearsetRatioPair& igearset = AbstractMotor::gearset::green, const ChassisScales& iscales = ChassisScales({1, 1}, imev5GreenTPR), std::shared_ptr<Logger> ilogger = Logger::getDefaultLogger())

ChassisController using PID control.

Parameters
itimeUtil The TimeUtil.
imodel The ChassisModel used to read from sensors/write to motors.
idistanceController The PID controller that controls chassis distance for driving straight.
iturnController The PID controller that controls chassis angle for turning.
iangleController The PID controller that controls chassis angle for driving straight.
igearset The internal gearset and external ratio used on the drive motors.
iscales The ChassisScales.
ilogger The logger this instance will log to.

Puts the motors into encoder count units. Throws a std::invalid_argument exception if the gear ratio is zero.

void okapi::ChassisControllerPID::moveDistance(QLength itarget) override

Drives the robot straight for a distance (using closed-loop control).

Parameters
itarget distance to travel
// Drive forward 6 inches
chassis->moveDistance(6_in);

// Drive backward 0.2 meters
chassis->moveDistance(-0.2_m);

void okapi::ChassisControllerPID::moveRaw(double itarget) override

Drives the robot straight for a distance (using closed-loop control).

Parameters
itarget distance to travel in motor degrees
// Drive forward by spinning the motors 400 degrees
chassis->moveRaw(400);

void okapi::ChassisControllerPID::moveDistanceAsync(QLength itarget) override

Sets the target distance for the robot to drive straight (using closed-loop control).

Parameters
itarget distance to travel

void okapi::ChassisControllerPID::moveRawAsync(double itarget) override

Sets the target distance for the robot to drive straight (using closed-loop control).

Parameters
itarget distance to travel in motor degrees

void okapi::ChassisControllerPID::turnAngle(QAngle idegTarget) override

Turns the robot clockwise in place (using closed-loop control).

Parameters
idegTarget angle to turn for
// Turn 90 degrees clockwise
chassis->turnAngle(90_deg);

void okapi::ChassisControllerPID::turnRaw(double idegTarget) override

Turns the robot clockwise in place (using closed-loop control).

Parameters
idegTarget angle to turn for in motor degrees
// Turn clockwise by spinning the motors 200 degrees
chassis->turnRaw(200);

void okapi::ChassisControllerPID::turnAngleAsync(QAngle idegTarget) override

Sets the target angle for the robot to turn clockwise in place (using closed-loop control).

Parameters
idegTarget angle to turn for

void okapi::ChassisControllerPID::turnRawAsync(double idegTarget) override

Sets the target angle for the robot to turn clockwise in place (using closed-loop control).

Parameters
idegTarget angle to turn for in motor degrees

void okapi::ChassisControllerPID::setTurnsMirrored(bool ishouldMirror) override

Sets whether turns should be mirrored.

Parameters
ishouldMirror whether turns should be mirrored

bool okapi::ChassisControllerPID::isSettled() override

Checks whether the internal controllers are currently settled.

Returns Whether this ChassisController is settled.

void okapi::ChassisControllerPID::setVelocityMode(bool ivelocityMode)

Sets the velocity mode flag.

Parameters
ivelocityMode Whether the controller should be in velocity or voltage mode.

When the controller is in velocity mode, the control loop will set motor velocities. When the controller is in voltage mode (ivelocityMode = false), the control loop will set motor voltages. Additionally, when the controller is in voltage mode, it will not obey maximum velocity limits.

void okapi::ChassisControllerPID::setGains(const IterativePosPIDController::Gains& idistanceGains, const IterativePosPIDController::Gains& iturnGains, const IterativePosPIDController::Gains& iangleGains)

Sets the gains for all controllers.

Parameters
idistanceGains The distance controller gains.
iturnGains The turn controller gains.
iangleGains The angle controller gains.

std::tuple<IterativePosPIDController::Gains, IterativePosPIDController::Gains, IterativePosPIDController::Gains> okapi::ChassisControllerPID::getGains() const

Gets the current controller gains.

Returns The current controller gains in the order: distance, turn, angle.

void okapi::ChassisControllerPID::startThread()

Starts the internal thread.

This method is called by the ChassisControllerBuilder when making a new instance of this class.

CrossplatformThread* okapi::ChassisControllerPID::getThread() const

Returns the underlying thread handle.

Returns The underlying thread handle.

void okapi::ChassisControllerPID::setMaxVelocity(double imaxVelocity) override

Sets a new maximum velocity in RPM [0-600].

Parameters
imaxVelocity The new maximum velocity.

In voltage mode, the max velocity is ignored and a max voltage should be set on the underlying ChassisModel instead.

double okapi::ChassisControllerPID::getMaxVelocity() const override

Returns The maximum velocity in RPM [0-600].

std::shared_ptr<ChassisModel> okapi::ChassisControllerPID::getModel() override

Returns The internal ChassisModel.

ChassisModel& okapi::ChassisControllerPID::model() override

Returns The internal ChassisModel.

void okapi::ChassisControllerPID::loop() protected

doneLooping is set to false by moveDistanceAsync and turnAngleAsync and then set to true by waitUntilSettled

bool okapi::ChassisControllerPID::waitForDistanceSettled() protected

Wait for the distance setup (distancePid and anglePid) to settle.

Returns true if done settling; false if settling should be tried again

bool okapi::ChassisControllerPID::waitForAngleSettled() protected

Wait for the angle setup (anglePid) to settle.

Returns true if done settling; false if settling should be tried again