YAMS
Yet Another Mechanism System
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | List of all members
yams::mechanisms::swerve::SwerveDriveConfig Class Reference

#include <SwerveDriveConfig.hpp>

Public Types

using TelemetryVerbosity = motorcontrollers::SmartMotorControllerConfig::TelemetryVerbosity
 

Public Member Functions

 SwerveDriveConfig ()=default
 
SwerveDriveConfig & WithSubsystem (wpi::cmd::SubsystemBase *subsystem)
 
SwerveDriveConfig & WithModules (std::vector< SwerveModule * > modules)
 
SwerveDriveConfig & WithGyro (std::function< wpi::units::degree_t()> gyroSupplier)
 
SwerveDriveConfig & WithGyro (std::function< wpi::math::Rotation3d()> gyroSupplier)
 
SwerveDriveConfig & WithGyroHeadingAxis (config::GyroAxis axis)
 
SwerveDriveConfig & WithAntiTipping (wpi::units::meters_per_second_t kP, wpi::units::degree_t tippingThreshold, wpi::units::meters_per_second_t maxCorrectionSpeed)
 
SwerveDriveConfig & WithAntiTipping (std::optional< wpi::math::AntiTipping > antiTipping)
 
SwerveDriveConfig & WithModuleStateOptimization (bool enabled)
 
SwerveDriveConfig & WithGyroVelocity (std::function< wpi::units::degrees_per_second_t()> angularVelocitySupplier)
 
SwerveDriveConfig & WithGyroOffset (wpi::units::degree_t offset)
 
SwerveDriveConfig & WithGyroInverted (bool inverted)
 
SwerveDriveConfig & WithStartingPose (wpi::math::Pose2d pose)
 
SwerveDriveConfig & WithMaximumChassisSpeed (wpi::units::meters_per_second_t speed, wpi::units::degrees_per_second_t angularVelocity)
 
SwerveDriveConfig & WithMaximumModuleSpeed (wpi::units::meters_per_second_t speed)
 
SwerveDriveConfig & WithCenterOfRotation (wpi::math::Translation2d center)
 
SwerveDriveConfig & WithCenterOfRotation (wpi::units::meter_t forward, wpi::units::meter_t left)
 
SwerveDriveConfig & WithDiscretizationTime (wpi::units::second_t dt)
 
SwerveDriveConfig & WithSimDiscretizationTime (wpi::units::second_t dt)
 
SwerveDriveConfig & WithGyroAngularVelocityScaleFactor (double scaleFactor)
 
SwerveDriveConfig & WithSimGyroAngularVelocityScaleFactor (double scaleFactor)
 
SwerveDriveConfig & WithTranslationController (wpi::math::PIDController controller)
 
SwerveDriveConfig & WithRotationController (wpi::math::PIDController controller)
 
SwerveDriveConfig & WithSimTranslationController (wpi::math::PIDController controller)
 
SwerveDriveConfig & WithSimRotationController (wpi::math::PIDController controller)
 
SwerveDriveConfig & WithTelemetry (const std::string &name, TelemetryVerbosity verbosity)
 
SwerveDriveConfig & WithTelemetry (const std::string &name, telemetry::SwerveDriveTelemetryConfig telemetryConfig)
 
const std::vector< SwerveModule * > & GetModules () const
 
wpi::cmd::SubsystemBase * GetSubsystem () const
 
wpi::math::Pose2d GetInitialPose () const
 
std::optional< wpi::units::meters_per_second_t > GetMaximumChassisLinearVelocity () const
 
std::optional< wpi::units::degrees_per_second_t > GetMaximumChassisAngularVelocity () const
 
std::optional< wpi::units::meters_per_second_t > GetMaximumModuleLinearVelocity () const
 
std::optional< wpi::math::Translation2d > GetCenterOfRotation () const
 
std::optional< TelemetryVerbosity > GetTelemetryVerbosity () const
 
const std::string & GetTelemetryName () const
 
std::optional< telemetry::SwerveDriveTelemetryConfig > GetSwerveDriveTelemetryConfig ()
 
wpi::units::degree_t GetGyroOffset () const
 
wpi::units::degree_t GetGyroAngle () const
 
wpi::math::Rotation3d GetGyroRotation3d () const
 
wpi::math::ChassisVelocities GetAntiTippingCorrection () const
 
std::optional< bool > GetModuleStateOptimization () const
 
std::optional< wpi::units::second_t > GetDiscretization () const
 
wpi::math::ChassisVelocities OptimizeRobotRelativeChassisSpeeds (wpi::math::ChassisVelocities speeds) const
 
std::optional< std::reference_wrapper< wpi::math::PIDController > > GetTranslationPID ()
 
std::optional< std::reference_wrapper< wpi::math::PIDController > > GetRotationPID ()
 

Static Public Member Functions

static wpi::math::Translation2d CubeTranslation (wpi::math::Translation2d translation)
 
static wpi::math::Translation2d ScaleTranslation (wpi::math::Translation2d translation, double scalar)
 

Detailed Description

Configuration for a SwerveDrive.

Uses a fluent builder pattern; all With* methods return *this for chaining.

Member Typedef Documentation

◆ TelemetryVerbosity

Constructor & Destructor Documentation

◆ SwerveDriveConfig()

yams::mechanisms::swerve::SwerveDriveConfig::SwerveDriveConfig ( )
default

Member Function Documentation

◆ CubeTranslation()

static wpi::math::Translation2d yams::mechanisms::swerve::SwerveDriveConfig::CubeTranslation ( wpi::math::Translation2d  translation)
static

Cube the polar-coordinate magnitude of a Translation2d.

◆ GetAntiTippingCorrection()

wpi::math::ChassisVelocities yams::mechanisms::swerve::SwerveDriveConfig::GetAntiTippingCorrection ( ) const

The anti-tipping correction for the current attitude, robot relative. Zero when anti-tipping or the gyro is not configured.

Returns
The chassis velocity to add.

◆ GetCenterOfRotation()

std::optional< wpi::math::Translation2d > yams::mechanisms::swerve::SwerveDriveConfig::GetCenterOfRotation ( ) const
Returns
Optional override centre of rotation (empty = robot centre).

◆ GetDiscretization()

std::optional< wpi::units::second_t > yams::mechanisms::swerve::SwerveDriveConfig::GetDiscretization ( ) const

The discretization time in use: the simulation one in simulation, the real one otherwise.

Returns
The discretization time, if set.

◆ GetGyroAngle()

wpi::units::degree_t yams::mechanisms::swerve::SwerveDriveConfig::GetGyroAngle ( ) const

Get the gyro angle with inversion and offset applied.

Exceptions
std::runtime_errorif no gyro supplier has been configured.

◆ GetGyroOffset()

wpi::units::degree_t yams::mechanisms::swerve::SwerveDriveConfig::GetGyroOffset ( ) const

Get the stored gyro offset (zero if never set).

◆ GetGyroRotation3d()

wpi::math::Rotation3d yams::mechanisms::swerve::SwerveDriveConfig::GetGyroRotation3d ( ) const

The robot's attitude: the gyro's attitude rotated into the robot's frame (see WithGyroHeadingAxis), with the gyro inversion and offset applied to its yaw. Its yaw is the robot's heading, and its roll and pitch are the robot's tilt.

Returns
The robot's attitude.
Exceptions
std::runtime_errorif no gyro supplier was set with WithGyro.

◆ GetInitialPose()

wpi::math::Pose2d yams::mechanisms::swerve::SwerveDriveConfig::GetInitialPose ( ) const

Get the starting pose.

◆ GetMaximumChassisAngularVelocity()

std::optional< wpi::units::degrees_per_second_t > yams::mechanisms::swerve::SwerveDriveConfig::GetMaximumChassisAngularVelocity ( ) const
Returns
Optional maximum chassis angular speed for wheel-speed desaturation.

◆ GetMaximumChassisLinearVelocity()

std::optional< wpi::units::meters_per_second_t > yams::mechanisms::swerve::SwerveDriveConfig::GetMaximumChassisLinearVelocity ( ) const
Returns
Optional maximum chassis linear speed for wheel-speed desaturation.

◆ GetMaximumModuleLinearVelocity()

std::optional< wpi::units::meters_per_second_t > yams::mechanisms::swerve::SwerveDriveConfig::GetMaximumModuleLinearVelocity ( ) const
Returns
Optional maximum module linear speed for wheel-speed desaturation.

◆ GetModules()

const std::vector< SwerveModule * > & yams::mechanisms::swerve::SwerveDriveConfig::GetModules ( ) const

Get the vector of module pointers.

◆ GetModuleStateOptimization()

std::optional< bool > yams::mechanisms::swerve::SwerveDriveConfig::GetModuleStateOptimization ( ) const

The module state optimization setting from WithModuleStateOptimization, if it was set.

Returns
true or false if set, or std::nullopt to leave each module's own setting.

◆ GetRotationPID()

std::optional< std::reference_wrapper< wpi::math::PIDController > > yams::mechanisms::swerve::SwerveDriveConfig::GetRotationPID ( )

Get the active rotation PID controller used for drive to pose and heading control (sim variant if in simulation and configured).

Returns
Reference to the rotation PID controller, or empty when none is configured.

◆ GetSubsystem()

wpi::cmd::SubsystemBase * yams::mechanisms::swerve::SwerveDriveConfig::GetSubsystem ( ) const

Get the subsystem pointer.

◆ GetSwerveDriveTelemetryConfig()

std::optional< telemetry::SwerveDriveTelemetryConfig > yams::mechanisms::swerve::SwerveDriveConfig::GetSwerveDriveTelemetryConfig ( )

Get the user-specified SwerveDriveTelemetryConfig, if configured via WithTelemetry(name, SwerveDriveTelemetryConfig). Moves the config out of this SwerveDriveConfig; intended to be called exactly once, when the owning SwerveDrive sets up its telemetry.

Returns
SwerveDriveTelemetryConfig if configured.

◆ GetTelemetryName()

const std::string & yams::mechanisms::swerve::SwerveDriveConfig::GetTelemetryName ( ) const
Returns
The telemetry name for the drive (defaults to "swerve").

◆ GetTelemetryVerbosity()

std::optional< TelemetryVerbosity > yams::mechanisms::swerve::SwerveDriveConfig::GetTelemetryVerbosity ( ) const
Returns
Optional telemetry verbosity level.

◆ GetTranslationPID()

std::optional< std::reference_wrapper< wpi::math::PIDController > > yams::mechanisms::swerve::SwerveDriveConfig::GetTranslationPID ( )

Get the active translation PID controller used for drive to pose (sim variant if in simulation and configured).

Returns
Reference to the translation PID controller, or empty when none is configured.

◆ OptimizeRobotRelativeChassisSpeeds()

wpi::math::ChassisVelocities yams::mechanisms::swerve::SwerveDriveConfig::OptimizeRobotRelativeChassisSpeeds ( wpi::math::ChassisVelocities  speeds) const

Apply angular-velocity skew correction and ChassisSpeeds discretization.

Parameters
speedsRaw robot-relative chassis speeds.
Returns
Optimized chassis speeds.

◆ ScaleTranslation()

static wpi::math::Translation2d yams::mechanisms::swerve::SwerveDriveConfig::ScaleTranslation ( wpi::math::Translation2d  translation,
double  scalar 
)
static

Scale the polar-coordinate magnitude of a Translation2d by a scalar.

◆ WithAntiTipping() [1/2]

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithAntiTipping ( std::optional< wpi::math::AntiTipping >  antiTipping)

Push back against tipping with your own controller, or std::nullopt to turn it off.

Parameters
antiTippingThe anti-tipping controller.
Returns
*this for chaining.

◆ WithAntiTipping() [2/2]

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithAntiTipping ( wpi::units::meters_per_second_t  kP,
wpi::units::degree_t  tippingThreshold,
wpi::units::meters_per_second_t  maxCorrectionSpeed 
)

Push back against tipping: when the robot leans by more than the threshold, a chassis velocity is added in the direction it is leaning, so the drive base moves back under the robot. It needs the gyro's full attitude, from the Rotation3d overload of WithGyro.

Parameters
kPCorrection speed per radian of lean. Must be positive.
tippingThresholdLean beyond which the correction applies. Between 0 and 90 degrees.
maxCorrectionSpeedThe largest correction. Must be positive.
Returns
*this for chaining.
Exceptions
std::invalid_argumentif a value is out of range.

◆ WithCenterOfRotation() [1/2]

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithCenterOfRotation ( wpi::math::Translation2d  center)

Override the chassis centre of rotation (default is the robot centre).

Parameters
centerTranslation2d in metres (X forward, Y left).
Returns
*this for chaining.

◆ WithCenterOfRotation() [2/2]

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithCenterOfRotation ( wpi::units::meter_t  forward,
wpi::units::meter_t  left 
)

Override the chassis centre of rotation via forward/left distances.

Parameters
forwardDistance forward from the robot centre.
leftDistance left from the robot centre.
Returns
*this for chaining.

◆ WithDiscretizationTime()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithDiscretizationTime ( wpi::units::second_t  dt)

Set the discretization timestep used to compensate for ChassisSpeeds skew.

Parameters
dtDiscretization period (typically one robot loop period).
Returns
*this for chaining.

◆ WithGyro() [1/2]

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithGyro ( std::function< wpi::math::Rotation3d()>  gyroSupplier)

Supply the gyro's full 3D attitude. See WithGyroHeadingAxis for a gyro that is not mounted flat.

Parameters
gyroSupplierCallable returning the gyro's attitude.
Returns
*this for chaining.

◆ WithGyro() [2/2]

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithGyro ( std::function< wpi::units::degree_t()>  gyroSupplier)

Supply the gyro's heading in degrees, for a gyro mounted flat.

The attitude is taken to be this yaw with no roll or pitch, so anti-tipping does nothing and WithGyroHeadingAxis has nothing to rotate. Use the Rotation3d overload for either.

Parameters
gyroSupplierCallable returning the current gyro angle.
Returns
*this for chaining.

◆ WithGyroAngularVelocityScaleFactor()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithGyroAngularVelocityScaleFactor ( double  scaleFactor)

Set the angular velocity scale factor for skew correction on the real robot.

Parameters
scaleFactorScale applied to the gyro angular velocity [0, 1].
Returns
*this for chaining.

◆ WithGyroHeadingAxis()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithGyroHeadingAxis ( config::GyroAxis  axis)

Set which gyro axis points up through the robot, for a gyro mounted on its side. The attitude from WithGyro is rotated into the robot's frame, so GetGyroRotation3d returns the robot's heading as its yaw and the robot's tilt as its roll and pitch.

Parameters
axisThe gyro axis that points up. GyroAxis::YAW, the default, is a gyro mounted flat.
Returns
*this for chaining.

◆ WithGyroInverted()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithGyroInverted ( bool  inverted)

Invert the gyro direction.

Parameters
invertedtrue to negate the raw gyro angle.
Returns
*this for chaining.

◆ WithGyroOffset()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithGyroOffset ( wpi::units::degree_t  offset)

Set the gyro offset (subtracted from the raw gyro reading in GetGyroAngle).

Parameters
offsetGyro angle offset.
Returns
*this for chaining.

◆ WithGyroVelocity()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithGyroVelocity ( std::function< wpi::units::degrees_per_second_t()>  angularVelocitySupplier)

Supply the gyro angular velocity in degrees per second.

Required for angular-velocity skew correction.

Parameters
angularVelocitySupplierCallable returning the current gyro angular velocity.
Returns
*this for chaining.

◆ WithMaximumChassisSpeed()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithMaximumChassisSpeed ( wpi::units::meters_per_second_t  speed,
wpi::units::degrees_per_second_t  angularVelocity 
)

Set the maximum chassis linear and angular speeds for wheel speed desaturation.

Parameters
speedMaximum chassis linear speed.
angularVelocityMaximum chassis angular speed.
Returns
*this for chaining.

◆ WithMaximumModuleSpeed()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithMaximumModuleSpeed ( wpi::units::meters_per_second_t  speed)

Set the maximum module linear speed for wheel speed desaturation.

Parameters
speedMaximum module speed.
Returns
*this for chaining.

◆ WithModules()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithModules ( std::vector< SwerveModule * >  modules)

Set the swerve modules (must match the NumModules template argument of SwerveDrive).

Parameters
modulesVector of module pointers (must outlive this config).
Returns
*this for chaining.

◆ WithModuleStateOptimization()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithModuleStateOptimization ( bool  enabled)

Turn swerve module state optimization on or off for every module. It is applied to the modules given so far, and to all of them when the SwerveDrive is created.

Parameters
enabledtrue to optimize module states.
Returns
*this for chaining.

◆ WithRotationController()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithRotationController ( wpi::math::PIDController  controller)

Set the rotation PID controller for DriveToPose (input units: radians).

Continuous input over [-π, π] is enabled automatically.

Parameters
controllerPIDController for heading.
Returns
*this for chaining.

◆ WithSimDiscretizationTime()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithSimDiscretizationTime ( wpi::units::second_t  dt)

Set the discretization timestep used during simulation.

Falls back to WithDiscretizationTime if not set.

Parameters
dtDiscretization period for simulation.
Returns
*this for chaining.

◆ WithSimGyroAngularVelocityScaleFactor()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithSimGyroAngularVelocityScaleFactor ( double  scaleFactor)

Set the angular velocity scale factor for skew correction in simulation.

Falls back to WithGyroAngularVelocityScaleFactor if not set.

Parameters
scaleFactorScale applied to the gyro angular velocity [0, 1].
Returns
*this for chaining.

◆ WithSimRotationController()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithSimRotationController ( wpi::math::PIDController  controller)

Set the simulated rotation PID controller for DriveToPose (input units: radians).

Falls back to WithRotationController if not set. Continuous input is enabled automatically.

Parameters
controllerPIDController for heading in simulation.
Returns
*this for chaining.

◆ WithSimTranslationController()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithSimTranslationController ( wpi::math::PIDController  controller)

Set the simulated translation PID controller for DriveToPose (input units: metres).

Falls back to WithTranslationController if not set.

Parameters
controllerPIDController for X/Y translation in simulation.
Returns
*this for chaining.

◆ WithStartingPose()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithStartingPose ( wpi::math::Pose2d  pose)

Set the starting field pose of the robot.

Parameters
poseStarting Pose2d (field-relative, blue-origin).
Returns
*this for chaining.

◆ WithSubsystem()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithSubsystem ( wpi::cmd::SubsystemBase *  subsystem)

Associate a command subsystem with this drive (used for command requirements).

Parameters
subsystemPointer to the subsystem (must outlive this config).
Returns
*this for chaining.

◆ WithTelemetry() [1/2]

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithTelemetry ( const std::string &  name,
telemetry::SwerveDriveTelemetryConfig  telemetryConfig 
)

Configure telemetry for the drive with an explicit SwerveDriveTelemetryConfig, taking precedence over WithTelemetry(name, TelemetryVerbosity).

Parameters
nameTelemetry name (defaults to "swerve").
telemetryConfigConfig that specifies what to log.
Returns
*this for chaining.

◆ WithTelemetry() [2/2]

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithTelemetry ( const std::string &  name,
TelemetryVerbosity  verbosity 
)

Set the telemetry name and verbosity for the drive.

Parameters
nameTelemetry name (defaults to "swerve").
verbosityAmount of data to publish.
Returns
*this for chaining.

◆ WithTranslationController()

SwerveDriveConfig & yams::mechanisms::swerve::SwerveDriveConfig::WithTranslationController ( wpi::math::PIDController  controller)

Set the translation PID controller for DriveToPose (input units: metres).

Parameters
controllerPIDController for X/Y translation.
Returns
*this for chaining.

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