WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
wpi::math::SwerveDriveKinematics< NumModules > Class Template Reference

Helper class that converts a chassis velocity (dx, dy, and dtheta components) into individual module states (velocity and angle). More...

#include <wpi/math/kinematics/SwerveDriveKinematics.hpp>

Inheritance diagram for wpi::math::SwerveDriveKinematics< NumModules >:
wpi::math::Kinematics< wpi::util::array< SwerveModulePosition, NumModules >, wpi::util::array< SwerveModuleVelocity, NumModules >, wpi::util::array< SwerveModuleAcceleration, NumModules > >

Public Member Functions

template<std::convertible_to< Translation2d >... ModuleTranslations>
requires (sizeof...(ModuleTranslations) == NumModules)
 SwerveDriveKinematics (ModuleTranslations &&... moduleTranslations)
 Constructs a swerve drive kinematics object.
 SwerveDriveKinematics (const wpi::util::array< Translation2d, NumModules > &modules)
 SwerveDriveKinematics (const SwerveDriveKinematics &)=default
template<std::convertible_to< Rotation2d >... ModuleHeadings>
requires (sizeof...(ModuleHeadings) == NumModules)
void ResetHeadings (ModuleHeadings &&... moduleHeadings)
 Reset the internal swerve module headings.
void ResetHeadings (wpi::util::array< Rotation2d, NumModules > moduleHeadings)
 Reset the internal swerve module headings.
wpi::util::array< SwerveModuleVelocity, NumModules > ToSwerveModuleVelocities (const ChassisVelocities &chassisVelocities, const Translation2d &centerOfRotation=Translation2d{}) const
 Performs inverse kinematics to return the module states from a desired chassis velocity.
wpi::util::array< SwerveModuleVelocity, NumModules > ToWheelVelocities (const ChassisVelocities &chassisVelocities) const override
 Performs inverse kinematics to return the wheel velocities from a desired chassis velocity.
template<std::convertible_to< SwerveModuleVelocity >... ModuleVelocities>
requires (sizeof...(ModuleVelocities) == NumModules)
ChassisVelocities ToChassisVelocities (ModuleVelocities &&... moduleVelocities) const
 Performs forward kinematics to return the resulting chassis state from the given module states.
ChassisVelocities ToChassisVelocities (const wpi::util::array< SwerveModuleVelocity, NumModules > &moduleVelocities) const override
 Performs forward kinematics to return the resulting chassis state from the given module states.
template<std::convertible_to< SwerveModulePosition >... ModuleDeltas>
requires (sizeof...(ModuleDeltas) == NumModules)
Twist2d ToTwist2d (ModuleDeltas &&... moduleDeltas) const
 Performs forward kinematics to return the resulting Twist2d from the given module position deltas.
Twist2d ToTwist2d (wpi::util::array< SwerveModulePosition, NumModules > moduleDeltas) const
 Performs forward kinematics to return the resulting Twist2d from the given module position deltas.
Twist2d ToTwist2d (const wpi::util::array< SwerveModulePosition, NumModules > &start, const wpi::util::array< SwerveModulePosition, NumModules > &end) const override
 Performs forward kinematics to return the resulting Twist2d from the given change in wheel positions.
wpi::util::array< SwerveModulePosition, NumModules > Interpolate (const wpi::util::array< SwerveModulePosition, NumModules > &start, const wpi::util::array< SwerveModulePosition, NumModules > &end, double t) const override
 Performs interpolation between two values.
const wpi::util::array< Translation2d, NumModules > & GetModules () const
wpi::util::array< SwerveModuleAcceleration, NumModules > ToSwerveModuleAccelerations (const ChassisAccelerations &chassisAccelerations, const units::radians_per_second_t angularVelocity=0.0_rad_per_s, const Translation2d &centerOfRotation=Translation2d{}) const
 Performs inverse kinematics to return the module accelerations from a desired chassis acceleration.
wpi::util::array< SwerveModuleAcceleration, NumModules > ToWheelAccelerations (const ChassisAccelerations &chassisAccelerations) const override
 Performs inverse kinematics to return the module accelerations from a desired chassis acceleration.
template<std::convertible_to< SwerveModuleAcceleration >... ModuleAccelerations>
requires (sizeof...(ModuleAccelerations) == NumModules)
ChassisAccelerations ToChassisAccelerations (ModuleAccelerations &&... moduleAccelerations) const
 Performs forward kinematics to return the resulting chassis accelerations from the given module accelerations.
ChassisAccelerations ToChassisAccelerations (const wpi::util::array< SwerveModuleAcceleration, NumModules > &moduleAccelerations) const override
 Performs forward kinematics to return the resulting chassis accelerations from the given module accelerations.
Public Member Functions inherited from wpi::math::Kinematics< wpi::util::array< SwerveModulePosition, NumModules >, wpi::util::array< SwerveModuleVelocity, NumModules >, wpi::util::array< SwerveModuleAcceleration, NumModules > >
virtual ~Kinematics () noexcept=default

Static Public Member Functions

static void DesaturateWheelVelocities (wpi::util::array< SwerveModuleVelocity, NumModules > *moduleVelocities, wpi::units::meters_per_second_t attainableMaxVelocity)
 Renormalizes the wheel velocities if any individual velocity is above the specified maximum.
static void DesaturateWheelVelocities (wpi::util::array< SwerveModuleVelocity, NumModules > *moduleVelocities, ChassisVelocities desiredChassisVelocity, wpi::units::meters_per_second_t attainableMaxModuleVelocity, wpi::units::meters_per_second_t attainableMaxRobotTranslationVelocity, wpi::units::radians_per_second_t attainableMaxRobotRotationVelocity)
 Renormalizes the wheel velocities if any individual velocity is above the specified maximum, as well as getting rid of joystick saturation at edges of joystick.

Detailed Description

template<size_t NumModules>
class wpi::math::SwerveDriveKinematics< NumModules >

Helper class that converts a chassis velocity (dx, dy, and dtheta components) into individual module states (velocity and angle).

The inverse kinematics (converting from a desired chassis velocity to individual module states) uses the relative locations of the modules with respect to the center of rotation. The center of rotation for inverse kinematics is also variable. This means that you can set your set your center of rotation in a corner of the robot to perform special evasion maneuvers.

Forward kinematics (converting an array of module states into the overall chassis motion) is performs the exact opposite of what inverse kinematics does. Since this is an overdetermined system (more equations than variables), we use a least-squares approximation.

The inverse kinematics: [moduleVelocities] = [moduleLocations] * [chassisVelocities] We take the Moore-Penrose pseudoinverse of [moduleLocations] and then multiply by [moduleVelocities] to get our chassis velocities.

Forward kinematics is also used for odometry – determining the position of the robot on the field using encoders and a gyro.

Constructor & Destructor Documentation

◆ SwerveDriveKinematics() [1/3]

template<size_t NumModules>
template<std::convertible_to< Translation2d >... ModuleTranslations>
requires (sizeof...(ModuleTranslations) == NumModules)
wpi::math::SwerveDriveKinematics< NumModules >::SwerveDriveKinematics ( ModuleTranslations &&... moduleTranslations)
inlineexplicit

Constructs a swerve drive kinematics object.

This takes in a variable number of module locations as Translation2ds. The order in which you pass in the module locations is the same order that you will receive the module states when performing inverse kinematics. It is also expected that you pass in the module states in the same order when calling the forward kinematics methods.

Parameters
moduleTranslationsThe locations of the modules relative to the physical center of the robot.

◆ SwerveDriveKinematics() [2/3]

template<size_t NumModules>
wpi::math::SwerveDriveKinematics< NumModules >::SwerveDriveKinematics ( const wpi::util::array< Translation2d, NumModules > & modules)
inlineexplicit

◆ SwerveDriveKinematics() [3/3]

template<size_t NumModules>
wpi::math::SwerveDriveKinematics< NumModules >::SwerveDriveKinematics ( const SwerveDriveKinematics< NumModules > & )
default

Member Function Documentation

◆ DesaturateWheelVelocities() [1/2]

template<size_t NumModules>
void wpi::math::SwerveDriveKinematics< NumModules >::DesaturateWheelVelocities ( wpi::util::array< SwerveModuleVelocity, NumModules > * moduleVelocities,
ChassisVelocities desiredChassisVelocity,
wpi::units::meters_per_second_t attainableMaxModuleVelocity,
wpi::units::meters_per_second_t attainableMaxRobotTranslationVelocity,
wpi::units::radians_per_second_t attainableMaxRobotRotationVelocity )
inlinestatic

Renormalizes the wheel velocities if any individual velocity is above the specified maximum, as well as getting rid of joystick saturation at edges of joystick.

Sometimes, after inverse kinematics, the requested velocity from one or more modules may be above the max attainable velocity for the driving motor on that module. To fix this issue, one can reduce all the wheel velocities to make sure that all requested module velocities are at-or-below the absolute threshold, while maintaining the ratio of velocities between modules.

Scaling down the module velocities rotates the direction of net motion in the opposite direction of rotational velocity, which makes discretizing the chassis velocities inaccurate because the discretization did not account for this translational skew.

Parameters
moduleVelocitiesReference to array of module states. The array will be mutated with the normalized velocities!
desiredChassisVelocityThe desired velocity of the robot
attainableMaxModuleVelocityThe absolute max velocity a module can reach
attainableMaxRobotTranslationVelocityThe absolute max velocity the robot can reach while translating
attainableMaxRobotRotationVelocityThe absolute max velocity the robot can reach while rotating

◆ DesaturateWheelVelocities() [2/2]

template<size_t NumModules>
void wpi::math::SwerveDriveKinematics< NumModules >::DesaturateWheelVelocities ( wpi::util::array< SwerveModuleVelocity, NumModules > * moduleVelocities,
wpi::units::meters_per_second_t attainableMaxVelocity )
inlinestatic

Renormalizes the wheel velocities if any individual velocity is above the specified maximum.

Sometimes, after inverse kinematics, the requested velocity from one or more modules may be above the max attainable velocity for the driving motor on that module. To fix this issue, one can reduce all the wheel velocities to make sure that all requested module velocities are at-or-below the absolute threshold, while maintaining the ratio of velocities between modules.

Scaling down the module velocities rotates the direction of net motion in the opposite direction of rotational velocity, which makes discretizing the chassis velocities inaccurate because the discretization did not account for this translational skew.

Parameters
moduleVelocitiesReference to array of module states. The array will be mutated with the normalized velocities!
attainableMaxVelocityThe absolute max velocity that a module can reach.

◆ GetModules()

template<size_t NumModules>
const wpi::util::array< Translation2d, NumModules > & wpi::math::SwerveDriveKinematics< NumModules >::GetModules ( ) const
inline

◆ Interpolate()

template<size_t NumModules>
wpi::util::array< SwerveModulePosition, NumModules > wpi::math::SwerveDriveKinematics< NumModules >::Interpolate ( const wpi::util::array< SwerveModulePosition, NumModules > & start,
const wpi::util::array< SwerveModulePosition, NumModules > & end,
double t ) const
inlineoverridevirtual

Performs interpolation between two values.

Parameters
startThe value to start at.
endThe value to end at.
tHow far between the two values to interpolate. This should be bounded to [0, 1].
Returns
The interpolated value.

Implements wpi::math::Kinematics< wpi::util::array< SwerveModulePosition, NumModules >, wpi::util::array< SwerveModuleVelocity, NumModules >, wpi::util::array< SwerveModuleAcceleration, NumModules > >.

◆ ResetHeadings() [1/2]

template<size_t NumModules>
template<std::convertible_to< Rotation2d >... ModuleHeadings>
requires (sizeof...(ModuleHeadings) == NumModules)
void wpi::math::SwerveDriveKinematics< NumModules >::ResetHeadings ( ModuleHeadings &&... moduleHeadings)
inline

Reset the internal swerve module headings.

Parameters
moduleHeadingsThe swerve module headings. The order of the module headings should be same as passed into the constructor of this class.

◆ ResetHeadings() [2/2]

template<size_t NumModules>
void wpi::math::SwerveDriveKinematics< NumModules >::ResetHeadings ( wpi::util::array< Rotation2d, NumModules > moduleHeadings)
inline

Reset the internal swerve module headings.

Parameters
moduleHeadingsThe swerve module headings. The order of the module headings should be same as passed into the constructor of this class.

◆ ToChassisAccelerations() [1/2]

template<size_t NumModules>
ChassisAccelerations wpi::math::SwerveDriveKinematics< NumModules >::ToChassisAccelerations ( const wpi::util::array< SwerveModuleAcceleration, NumModules > & moduleAccelerations) const
inlineoverridevirtual

Performs forward kinematics to return the resulting chassis accelerations from the given module accelerations.

This method is often used for dynamics calculations – determining the robot's acceleration on the field using data from the real-world acceleration of each module on the robot.

Parameters
moduleAccelerationsThe accelerations of the modules as measured from respective encoders and gyros. The order of the swerve module accelerations should be same as passed into the constructor of this class.
Returns
The resulting chassis accelerations.

Implements wpi::math::Kinematics< wpi::util::array< SwerveModulePosition, NumModules >, wpi::util::array< SwerveModuleVelocity, NumModules >, wpi::util::array< SwerveModuleAcceleration, NumModules > >.

◆ ToChassisAccelerations() [2/2]

template<size_t NumModules>
template<std::convertible_to< SwerveModuleAcceleration >... ModuleAccelerations>
requires (sizeof...(ModuleAccelerations) == NumModules)
ChassisAccelerations wpi::math::SwerveDriveKinematics< NumModules >::ToChassisAccelerations ( ModuleAccelerations &&... moduleAccelerations) const
inline

Performs forward kinematics to return the resulting chassis accelerations from the given module accelerations.

This method is often used for dynamics calculations – determining the robot's acceleration on the field using data from the real-world acceleration of each module on the robot.

Parameters
moduleAccelerationsThe accelerations of the modules as measured from respective encoders and gyros. The order of the swerve module accelerations should be same as passed into the constructor of this class.
Returns
The resulting chassis accelerations.

◆ ToChassisVelocities() [1/2]

template<size_t NumModules>
ChassisVelocities wpi::math::SwerveDriveKinematics< NumModules >::ToChassisVelocities ( const wpi::util::array< SwerveModuleVelocity, NumModules > & moduleVelocities) const
inlineoverridevirtual

Performs forward kinematics to return the resulting chassis state from the given module states.

This method is often used for odometry – determining the robot's position on the field using data from the real-world velocity and angle of each module on the robot.

Parameters
moduleVelocitiesThe state of the modules as an wpi::util::array of type SwerveModuleVelocity, NumModules long as measured from respective encoders and gyros. The order of the swerve module states should be same as passed into the constructor of this class.
Returns
The resulting chassis velocity.

Implements wpi::math::Kinematics< wpi::util::array< SwerveModulePosition, NumModules >, wpi::util::array< SwerveModuleVelocity, NumModules >, wpi::util::array< SwerveModuleAcceleration, NumModules > >.

◆ ToChassisVelocities() [2/2]

template<size_t NumModules>
template<std::convertible_to< SwerveModuleVelocity >... ModuleVelocities>
requires (sizeof...(ModuleVelocities) == NumModules)
ChassisVelocities wpi::math::SwerveDriveKinematics< NumModules >::ToChassisVelocities ( ModuleVelocities &&... moduleVelocities) const
inline

Performs forward kinematics to return the resulting chassis state from the given module states.

This method is often used for odometry – determining the robot's position on the field using data from the real-world velocity and angle of each module on the robot.

Parameters
moduleVelocitiesThe state of the modules (as a SwerveModuleVelocity type) as measured from respective encoders and gyros. The order of the swerve module states should be same as passed into the constructor of this class.
Returns
The resulting chassis velocity.

◆ ToSwerveModuleAccelerations()

template<size_t NumModules>
wpi::util::array< SwerveModuleAcceleration, NumModules > wpi::math::SwerveDriveKinematics< NumModules >::ToSwerveModuleAccelerations ( const ChassisAccelerations & chassisAccelerations,
const units::radians_per_second_t angularVelocity = 0.0_rad_per_s,
const Translation2d & centerOfRotation = Translation2d{} ) const
inline

Performs inverse kinematics to return the module accelerations from a desired chassis acceleration.

This method is often used for dynamics calculations – converting desired robot accelerations into individual module accelerations.

This function also supports variable centers of rotation. During normal operations, the center of rotation is usually the same as the physical center of the robot; therefore, the argument is defaulted to that use case. However, if you wish to change the center of rotation for evasive maneuvers, vision alignment, or for any other use case, you can do so.

Parameters
chassisAccelerationsThe desired chassis accelerations.
angularVelocityThe desired robot angular velocity.
centerOfRotationThe center of rotation. For example, if you set the center of rotation at one corner of the robot and provide a chassis acceleration that only has a dtheta component, the robot will rotate around that corner.
Returns
An array containing the module accelerations.

◆ ToSwerveModuleVelocities()

template<size_t NumModules>
wpi::util::array< SwerveModuleVelocity, NumModules > wpi::math::SwerveDriveKinematics< NumModules >::ToSwerveModuleVelocities ( const ChassisVelocities & chassisVelocities,
const Translation2d & centerOfRotation = Translation2d{} ) const
inline

Performs inverse kinematics to return the module states from a desired chassis velocity.

This method is often used to convert joystick values into module velocities and angles.

This function also supports variable centers of rotation. During normal operations, the center of rotation is usually the same as the physical center of the robot; therefore, the argument is defaulted to that use case. However, if you wish to change the center of rotation for evasive maneuvers, vision alignment, or for any other use case, you can do so.

In the case that the desired chassis velocities are zero (i.e. the robot will be stationary), the previously calculated module angle will be maintained.

Parameters
chassisVelocitiesThe desired chassis velocity.
centerOfRotationThe center of rotation. For example, if you set the center of rotation at one corner of the robot and provide a chassis velocity that only has a dtheta component, the robot will rotate around that corner.
Returns
An array containing the module states. Use caution because these module states are not normalized. Sometimes, a user input may cause one of the module velocities to go above the attainable max velocity. Use the DesaturateWheelVelocities(wpi::util::array<SwerveModuleVelocity, NumModules>*, wpi::units::meters_per_second_t) function to rectify this issue. In addition, you can leverage the power of C++17 to directly assign the module states to variables:
auto [fl, fr, bl, br] =
kinematics.ToSwerveModuleVelocities(chassisVelocities);

◆ ToTwist2d() [1/3]

template<size_t NumModules>
Twist2d wpi::math::SwerveDriveKinematics< NumModules >::ToTwist2d ( const wpi::util::array< SwerveModulePosition, NumModules > & start,
const wpi::util::array< SwerveModulePosition, NumModules > & end ) const
inlineoverridevirtual

Performs forward kinematics to return the resulting Twist2d from the given change in wheel positions.

This method is often used for odometry – determining the robot's position on the field using changes in the distance driven by each wheel on the robot.

Parameters
startThe starting distances driven by the wheels.
endThe ending distances driven by the wheels.
Returns
The resulting Twist2d in the robot's movement.

Implements wpi::math::Kinematics< wpi::util::array< SwerveModulePosition, NumModules >, wpi::util::array< SwerveModuleVelocity, NumModules >, wpi::util::array< SwerveModuleAcceleration, NumModules > >.

◆ ToTwist2d() [2/3]

template<size_t NumModules>
template<std::convertible_to< SwerveModulePosition >... ModuleDeltas>
requires (sizeof...(ModuleDeltas) == NumModules)
Twist2d wpi::math::SwerveDriveKinematics< NumModules >::ToTwist2d ( ModuleDeltas &&... moduleDeltas) const
inline

Performs forward kinematics to return the resulting Twist2d from the given module position deltas.

This method is often used for odometry – determining the robot's position on the field using data from the real-world position delta and angle of each module on the robot.

Parameters
moduleDeltasThe latest change in position of the modules (as a SwerveModulePosition type) as measured from respective encoders and gyros. The order of the swerve module states should be same as passed into the constructor of this class.
Returns
The resulting Twist2d.

◆ ToTwist2d() [3/3]

template<size_t NumModules>
Twist2d wpi::math::SwerveDriveKinematics< NumModules >::ToTwist2d ( wpi::util::array< SwerveModulePosition, NumModules > moduleDeltas) const
inline

Performs forward kinematics to return the resulting Twist2d from the given module position deltas.

This method is often used for odometry – determining the robot's position on the field using data from the real-world position delta and angle of each module on the robot.

Parameters
moduleDeltasThe latest change in position of the modules (as a SwerveModulePosition type) as measured from respective encoders and gyros. The order of the swerve module states should be same as passed into the constructor of this class.
Returns
The resulting Twist2d.

◆ ToWheelAccelerations()

template<size_t NumModules>
wpi::util::array< SwerveModuleAcceleration, NumModules > wpi::math::SwerveDriveKinematics< NumModules >::ToWheelAccelerations ( const ChassisAccelerations & chassisAccelerations) const
inlineoverridevirtual

Performs inverse kinematics to return the module accelerations from a desired chassis acceleration.

This method is often used for dynamics calculations – converting desired robot accelerations into individual module accelerations.

Parameters
chassisAccelerationsThe desired chassis accelerations.
Returns
An array containing the module accelerations.

Implements wpi::math::Kinematics< wpi::util::array< SwerveModulePosition, NumModules >, wpi::util::array< SwerveModuleVelocity, NumModules >, wpi::util::array< SwerveModuleAcceleration, NumModules > >.

◆ ToWheelVelocities()

template<size_t NumModules>
wpi::util::array< SwerveModuleVelocity, NumModules > wpi::math::SwerveDriveKinematics< NumModules >::ToWheelVelocities ( const ChassisVelocities & chassisVelocities) const
inlineoverridevirtual

Performs inverse kinematics to return the wheel velocities from a desired chassis velocity.

This method is often used to convert joystick values into wheel velocities.

Parameters
chassisVelocitiesThe desired chassis velocity.
Returns
The wheel velocities.

Implements wpi::math::Kinematics< wpi::util::array< SwerveModulePosition, NumModules >, wpi::util::array< SwerveModuleVelocity, NumModules >, wpi::util::array< SwerveModuleAcceleration, NumModules > >.


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