WPILibC++ 2027.0.0-alpha-2
Loading...
Searching...
No Matches
MecanumDriveKinematics.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <Eigen/QR>
8#include <wpi/SymbolExports.h>
9
10#include "frc/EigenCore.h"
17#include "wpimath/MathShared.h"
18
19namespace frc {
20
21/**
22 * Helper class that converts a chassis velocity (dx, dy, and dtheta components)
23 * into individual wheel speeds.
24 *
25 * The inverse kinematics (converting from a desired chassis velocity to
26 * individual wheel speeds) uses the relative locations of the wheels with
27 * respect to the center of rotation. The center of rotation for inverse
28 * kinematics is also variable. This means that you can set your set your center
29 * of rotation in a corner of the robot to perform special evasion maneuvers.
30 *
31 * Forward kinematics (converting an array of wheel speeds into the overall
32 * chassis motion) is performs the exact opposite of what inverse kinematics
33 * does. Since this is an overdetermined system (more equations than variables),
34 * we use a least-squares approximation.
35 *
36 * The inverse kinematics: [wheelSpeeds] = [wheelLocations] * [chassisSpeeds]
37 * We take the Moore-Penrose pseudoinverse of [wheelLocations] and then
38 * multiply by [wheelSpeeds] to get our chassis speeds.
39 *
40 * Forward kinematics is also used for odometry -- determining the position of
41 * the robot on the field using encoders and a gyro.
42 */
44 : public Kinematics<MecanumDriveWheelSpeeds, MecanumDriveWheelPositions> {
45 public:
46 /**
47 * Constructs a mecanum drive kinematics object.
48 *
49 * @param frontLeftWheel The location of the front-left wheel relative to the
50 * physical center of the robot.
51 * @param frontRightWheel The location of the front-right wheel relative to
52 * the physical center of the robot.
53 * @param rearLeftWheel The location of the rear-left wheel relative to the
54 * physical center of the robot.
55 * @param rearRightWheel The location of the rear-right wheel relative to the
56 * physical center of the robot.
57 */
58 explicit MecanumDriveKinematics(Translation2d frontLeftWheel,
59 Translation2d frontRightWheel,
60 Translation2d rearLeftWheel,
61 Translation2d rearRightWheel)
62 : m_frontLeftWheel{frontLeftWheel},
63 m_frontRightWheel{frontRightWheel},
64 m_rearLeftWheel{rearLeftWheel},
65 m_rearRightWheel{rearRightWheel} {
66 SetInverseKinematics(frontLeftWheel, frontRightWheel, rearLeftWheel,
67 rearRightWheel);
68 m_forwardKinematics = m_inverseKinematics.householderQr();
69 wpi::math::MathSharedStore::ReportUsage("MecanumDriveKinematics", "");
70 }
71
73
74 /**
75 * Performs inverse kinematics to return the wheel speeds from a desired
76 * chassis velocity. This method is often used to convert joystick values into
77 * wheel speeds.
78 *
79 * This function also supports variable centers of rotation. During normal
80 * operations, the center of rotation is usually the same as the physical
81 * center of the robot; therefore, the argument is defaulted to that use case.
82 * However, if you wish to change the center of rotation for evasive
83 * maneuvers, vision alignment, or for any other use case, you can do so.
84 *
85 * @param chassisSpeeds The desired chassis speed.
86 * @param centerOfRotation The center of rotation. For example, if you set the
87 * center of rotation at one corner of the robot and
88 * provide a chassis speed that only has a dtheta
89 * component, the robot will rotate around that
90 * corner.
91 *
92 * @return The wheel speeds. Use caution because they are not normalized.
93 * Sometimes, a user input may cause one of the wheel speeds to go
94 * above the attainable max velocity. Use the
95 * MecanumDriveWheelSpeeds::Normalize() function to rectify this
96 * issue. In addition, you can leverage the power of C++17 to directly
97 * assign the wheel speeds to variables:
98 *
99 * @code{.cpp}
100 * auto [fl, fr, bl, br] = kinematics.ToWheelSpeeds(chassisSpeeds);
101 * @endcode
102 */
104 const ChassisSpeeds& chassisSpeeds,
105 const Translation2d& centerOfRotation) const;
106
108 const ChassisSpeeds& chassisSpeeds) const override {
109 return ToWheelSpeeds(chassisSpeeds, {});
110 }
111
112 /**
113 * Performs forward kinematics to return the resulting chassis state from the
114 * given wheel speeds. This method is often used for odometry -- determining
115 * the robot's position on the field using data from the real-world speed of
116 * each wheel on the robot.
117 *
118 * @param wheelSpeeds The current mecanum drive wheel speeds.
119 *
120 * @return The resulting chassis speed.
121 */
123 const MecanumDriveWheelSpeeds& wheelSpeeds) const override;
124
126 const MecanumDriveWheelPositions& end) const override;
127
128 /**
129 * Performs forward kinematics to return the resulting Twist2d from the
130 * given wheel position deltas. This method is often used for odometry --
131 * determining the robot's position on the field using data from the
132 * distance driven by each wheel on the robot.
133 *
134 * @param wheelDeltas The change in distance driven by each wheel.
135 *
136 * @return The resulting chassis speed.
137 */
139
140 /**
141 * Returns the front-left wheel translation.
142 *
143 * @return The front-left wheel translation.
144 */
145 const Translation2d& GetFrontLeft() const { return m_frontLeftWheel; }
146
147 /**
148 * Returns the front-right wheel translation.
149 *
150 * @return The front-right wheel translation.
151 */
152 const Translation2d& GetFrontRight() const { return m_frontRightWheel; }
153
154 /**
155 * Returns the rear-left wheel translation.
156 *
157 * @return The rear-left wheel translation.
158 */
159 const Translation2d& GetRearLeft() const { return m_rearLeftWheel; }
160
161 /**
162 * Returns the rear-right wheel translation.
163 *
164 * @return The rear-right wheel translation.
165 */
166 const Translation2d& GetRearRight() const { return m_rearRightWheel; }
167
169 const MecanumDriveWheelPositions& start,
170 const MecanumDriveWheelPositions& end, double t) const override {
171 return start.Interpolate(end, t);
172 }
173
174 private:
175 mutable Matrixd<4, 3> m_inverseKinematics;
176 Eigen::HouseholderQR<Matrixd<4, 3>> m_forwardKinematics;
177 Translation2d m_frontLeftWheel;
178 Translation2d m_frontRightWheel;
179 Translation2d m_rearLeftWheel;
180 Translation2d m_rearRightWheel;
181
182 mutable Translation2d m_previousCoR;
183
184 /**
185 * Construct inverse kinematics matrix from wheel locations.
186 *
187 * @param fl The location of the front-left wheel relative to the physical
188 * center of the robot.
189 * @param fr The location of the front-right wheel relative to the physical
190 * center of the robot.
191 * @param rl The location of the rear-left wheel relative to the physical
192 * center of the robot.
193 * @param rr The location of the rear-right wheel relative to the physical
194 * center of the robot.
195 */
196 void SetInverseKinematics(Translation2d fl, Translation2d fr,
197 Translation2d rl, Translation2d rr) const;
198};
199
200} // namespace frc
201
#define WPILIB_DLLEXPORT
Definition SymbolExports.h:36
Helper class that converts a chassis velocity (dx, dy, and dtheta components) into individual wheel s...
Definition Kinematics.h:25
Helper class that converts a chassis velocity (dx, dy, and dtheta components) into individual wheel s...
Definition MecanumDriveKinematics.h:44
MecanumDriveWheelSpeeds ToWheelSpeeds(const ChassisSpeeds &chassisSpeeds, const Translation2d &centerOfRotation) const
Performs inverse kinematics to return the wheel speeds from a desired chassis velocity.
Twist2d ToTwist2d(const MecanumDriveWheelPositions &wheelDeltas) const
Performs forward kinematics to return the resulting Twist2d from the given wheel position deltas.
const Translation2d & GetRearRight() const
Returns the rear-right wheel translation.
Definition MecanumDriveKinematics.h:166
const Translation2d & GetFrontRight() const
Returns the front-right wheel translation.
Definition MecanumDriveKinematics.h:152
Twist2d ToTwist2d(const MecanumDriveWheelPositions &start, const MecanumDriveWheelPositions &end) const override
Performs forward kinematics to return the resulting Twist2d from the given change in wheel positions.
MecanumDriveKinematics(const MecanumDriveKinematics &)=default
MecanumDriveKinematics(Translation2d frontLeftWheel, Translation2d frontRightWheel, Translation2d rearLeftWheel, Translation2d rearRightWheel)
Constructs a mecanum drive kinematics object.
Definition MecanumDriveKinematics.h:58
const Translation2d & GetFrontLeft() const
Returns the front-left wheel translation.
Definition MecanumDriveKinematics.h:145
const Translation2d & GetRearLeft() const
Returns the rear-left wheel translation.
Definition MecanumDriveKinematics.h:159
MecanumDriveWheelSpeeds ToWheelSpeeds(const ChassisSpeeds &chassisSpeeds) const override
Performs inverse kinematics to return the wheel speeds from a desired chassis velocity.
Definition MecanumDriveKinematics.h:107
MecanumDriveWheelPositions Interpolate(const MecanumDriveWheelPositions &start, const MecanumDriveWheelPositions &end, double t) const override
Performs interpolation between two values.
Definition MecanumDriveKinematics.h:168
ChassisSpeeds ToChassisSpeeds(const MecanumDriveWheelSpeeds &wheelSpeeds) const override
Performs forward kinematics to return the resulting chassis state from the given wheel speeds.
Represents a translation in 2D space.
Definition Translation2d.h:29
static void ReportUsage(std::string_view resource, std::string_view data)
Definition MathShared.h:61
Definition SystemServer.h:9
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition EigenCore.h:21
Represents the speed of a robot chassis.
Definition ChassisSpeeds.h:25
Represents the wheel positions for a mecanum drive drivetrain.
Definition MecanumDriveWheelPositions.h:16
Represents the wheel speeds for a mecanum drive drivetrain.
Definition MecanumDriveWheelSpeeds.h:20
A change in distance along a 2D arc since the last pose update.
Definition Twist2d.h:22