WPILibC++ 2024.3.2
frc::LTVDifferentialDriveController Class Reference

The linear time-varying differential drive controller has a similar form to the LQR, but the model used to compute the controller gain is the nonlinear differential drive model linearized around the drivetrain's current state. More...

#include <frc/controller/LTVDifferentialDriveController.h>

Public Member Functions

 LTVDifferentialDriveController (const frc::LinearSystem< 2, 2, 2 > &plant, units::meter_t trackwidth, const wpi::array< double, 5 > &Qelems, const wpi::array< double, 2 > &Relems, units::second_t dt)
 Constructs a linear time-varying differential drive controller. More...
 
 LTVDifferentialDriveController (LTVDifferentialDriveController &&)=default
 Move constructor. More...
 
LTVDifferentialDriveControlleroperator= (LTVDifferentialDriveController &&)=default
 Move assignment operator. More...
 
bool AtReference () const
 Returns true if the pose error is within tolerance of the reference. More...
 
void SetTolerance (const Pose2d &poseTolerance, units::meters_per_second_t leftVelocityTolerance, units::meters_per_second_t rightVelocityTolerance)
 Sets the pose error which is considered tolerable for use with AtReference(). More...
 
DifferentialDriveWheelVoltages Calculate (const Pose2d &currentPose, units::meters_per_second_t leftVelocity, units::meters_per_second_t rightVelocity, const Pose2d &poseRef, units::meters_per_second_t leftVelocityRef, units::meters_per_second_t rightVelocityRef)
 Returns the left and right output voltages of the LTV controller. More...
 
DifferentialDriveWheelVoltages Calculate (const Pose2d &currentPose, units::meters_per_second_t leftVelocity, units::meters_per_second_t rightVelocity, const Trajectory::State &desiredState)
 Returns the left and right output voltages of the LTV controller. More...
 

Detailed Description

The linear time-varying differential drive controller has a similar form to the LQR, but the model used to compute the controller gain is the nonlinear differential drive model linearized around the drivetrain's current state.

We precompute gains for important places in our state-space, then interpolate between them with a lookup table to save computational resources.

This controller has a flat hierarchy with pose and wheel velocity references and voltage outputs. This is different from a Ramsete controller's nested hierarchy where the top-level controller has a pose reference and chassis velocity command outputs, and the low-level controller has wheel velocity references and voltage outputs. Flat hierarchies are easier to tune in one shot. Furthermore, this controller is more optimal in the "least-squares error" sense than a controller based on Ramsete.

See section 8.7 in Controls Engineering in FRC for a derivation of the control law we used shown in theorem 8.7.4.

Constructor & Destructor Documentation

◆ LTVDifferentialDriveController() [1/2]

frc::LTVDifferentialDriveController::LTVDifferentialDriveController ( const frc::LinearSystem< 2, 2, 2 > &  plant,
units::meter_t  trackwidth,
const wpi::array< double, 5 > &  Qelems,
const wpi::array< double, 2 > &  Relems,
units::second_t  dt 
)

Constructs a linear time-varying differential drive controller.

See https://docs.wpilib.org/en/stable/docs/software/advanced-controls/state-space/state-space-intro.html#lqr-tuning for how to select the tolerances.

Parameters
plantThe differential drive velocity plant.
trackwidthThe distance between the differential drive's left and right wheels.
QelemsThe maximum desired error tolerance for each state.
RelemsThe maximum desired control effort for each input.
dtDiscretization timestep.
Exceptions
std::domain_errorif max velocity of plant with 12 V input <= 0 m/s or >= 15 m/s.

◆ LTVDifferentialDriveController() [2/2]

frc::LTVDifferentialDriveController::LTVDifferentialDriveController ( LTVDifferentialDriveController &&  )
default

Move constructor.

Member Function Documentation

◆ AtReference()

bool frc::LTVDifferentialDriveController::AtReference ( ) const

Returns true if the pose error is within tolerance of the reference.

◆ Calculate() [1/2]

DifferentialDriveWheelVoltages frc::LTVDifferentialDriveController::Calculate ( const Pose2d currentPose,
units::meters_per_second_t  leftVelocity,
units::meters_per_second_t  rightVelocity,
const Pose2d poseRef,
units::meters_per_second_t  leftVelocityRef,
units::meters_per_second_t  rightVelocityRef 
)

Returns the left and right output voltages of the LTV controller.

The reference pose, linear velocity, and angular velocity should come from a drivetrain trajectory.

Parameters
currentPoseThe current pose.
leftVelocityThe current left velocity.
rightVelocityThe current right velocity.
poseRefThe desired pose.
leftVelocityRefThe desired left velocity.
rightVelocityRefThe desired right velocity.

◆ Calculate() [2/2]

DifferentialDriveWheelVoltages frc::LTVDifferentialDriveController::Calculate ( const Pose2d currentPose,
units::meters_per_second_t  leftVelocity,
units::meters_per_second_t  rightVelocity,
const Trajectory::State desiredState 
)

Returns the left and right output voltages of the LTV controller.

The reference pose, linear velocity, and angular velocity should come from a drivetrain trajectory.

Parameters
currentPoseThe current pose.
leftVelocityThe left velocity.
rightVelocityThe right velocity.
desiredStateThe desired pose, linear velocity, and angular velocity from a trajectory.

◆ operator=()

LTVDifferentialDriveController & frc::LTVDifferentialDriveController::operator= ( LTVDifferentialDriveController &&  )
default

Move assignment operator.

◆ SetTolerance()

void frc::LTVDifferentialDriveController::SetTolerance ( const Pose2d poseTolerance,
units::meters_per_second_t  leftVelocityTolerance,
units::meters_per_second_t  rightVelocityTolerance 
)

Sets the pose error which is considered tolerable for use with AtReference().

Parameters
poseTolerancePose error which is tolerable.
leftVelocityToleranceLeft velocity error which is tolerable.
rightVelocityToleranceRight velocity error which is tolerable.

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