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