WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
ChassisVelocities.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
9#include "wpi/units/angular_velocity.hpp"
10#include "wpi/units/velocity.hpp"
12
13namespace wpi::math {
14/**
15 * Represents robot chassis velocities.
16 *
17 * Although this struct contains similar members compared to a Twist2d, they do
18 * NOT represent the same thing. Whereas a Twist2d represents a change in pose
19 * w.r.t to the robot frame of reference, a ChassisVelocities struct represents
20 * a robot's velocities.
21 *
22 * A strictly non-holonomic drivetrain, such as a differential drive, should
23 * never have a dy component because it can never move sideways. Holonomic
24 * drivetrains such as swerve and mecanum will often have all three components.
25 */
27 /**
28 * Velocity along the x-axis. (Fwd is +)
29 */
30 wpi::units::meters_per_second_t vx = 0_mps;
31
32 /**
33 * Velocity along the y-axis. (Left is +)
34 */
35 wpi::units::meters_per_second_t vy = 0_mps;
36
37 /**
38 * Represents the angular velocity of the robot frame. (CCW is +)
39 */
40 wpi::units::radians_per_second_t omega = 0_rad_per_s;
41
42 /**
43 * Creates a Twist2d from ChassisVelocities.
44 *
45 * @param dt The duration of the timestep.
46 *
47 * @return Twist2d.
48 */
49 constexpr Twist2d ToTwist2d(wpi::units::second_t dt) const {
50 return Twist2d{vx * dt, vy * dt, omega * dt};
51 }
52
53 /**
54 * Discretizes continuous-time chassis velocities.
55 *
56 * This function converts continuous-time chassis velocities into
57 * discrete-time ones such that when the discrete-time chassis velocities are
58 * applied for one timestep, the robot moves as if the velocity components are
59 * independent (i.e., the robot moves v_x * dt along the x-axis, v_y * dt
60 * along the y-axis, and omega * dt around the z-axis).
61 *
62 * This is useful for compensating for translational skew when translating and
63 * rotating a holonomic (swerve or mecanum) drivetrain. However, scaling down
64 * the ChassisVelocities after discretizing (e.g., when desaturating swerve
65 * module velocities) rotates the direction of net motion in the opposite
66 * direction of rotational velocity, introducing a different translational
67 * skew which is not accounted for by discretization.
68 *
69 * @param dt The duration of the timestep the velocities should be applied
70 * for.
71 * @return Discretized ChassisVelocities.
72 */
73 constexpr ChassisVelocities Discretize(wpi::units::second_t dt) const {
74 // Construct the desired pose after a timestep, relative to the current
75 // pose. The desired pose has decoupled translation and rotation.
76 Transform2d desiredTransform{vx * dt, vy * dt, omega * dt};
77
78 // Find the chassis translation/rotation deltas in the robot frame that move
79 // the robot from its current pose to the desired pose
80 auto twist = desiredTransform.Log();
81
82 // Turn the chassis translation/rotation deltas into average velocities
83 return {twist.dx / dt, twist.dy / dt, twist.dtheta / dt};
84 }
85
86 /**
87 * Converts this field-relative set of velocities into a robot-relative
88 * ChassisVelocities object.
89 *
90 * @param robotAngle The angle of the robot as measured by a gyroscope. The
91 * robot's angle is considered to be zero when it is facing directly away
92 * from your alliance station wall. Remember that this should be CCW
93 * positive.
94 * @return ChassisVelocities object representing the velocities in the robot's
95 * frame of reference.
96 */
98 const Rotation2d& robotAngle) const {
99 // CW rotation into chassis frame
100 auto rotated = Translation2d{wpi::units::meter_t{vx.value()},
101 wpi::units::meter_t{vy.value()}}
102 .RotateBy(-robotAngle);
103 return {wpi::units::meters_per_second_t{rotated.X().value()},
104 wpi::units::meters_per_second_t{rotated.Y().value()}, omega};
105 }
106
107 /**
108 * Converts this robot-relative set of velocities into a field-relative
109 * ChassisVelocities object.
110 *
111 * @param robotAngle The angle of the robot as measured by a gyroscope. The
112 * robot's angle is considered to be zero when it is facing directly away
113 * from your alliance station wall. Remember that this should be CCW
114 * positive.
115 * @return ChassisVelocities object representing the velocities in the field's
116 * frame of reference.
117 */
119 const Rotation2d& robotAngle) const {
120 // CCW rotation out of chassis frame
121 auto rotated = Translation2d{wpi::units::meter_t{vx.value()},
122 wpi::units::meter_t{vy.value()}}
123 .RotateBy(robotAngle);
124 return {wpi::units::meters_per_second_t{rotated.X().value()},
125 wpi::units::meters_per_second_t{rotated.Y().value()}, omega};
126 }
127
128 /**
129 * Adds two ChassisVelocities and returns the sum.
130 *
131 * For example, ChassisVelocities{1.0, 0.5, 1.5} +
132 * ChassisVelocities{2.0, 1.5, 0.5} = ChassisVelocities{3.0, 2.0, 2.0}
133 *
134 * @param other The ChassisVelocities to add.
135 *
136 * @return The sum of the ChassisVelocities.
137 */
138 constexpr ChassisVelocities operator+(const ChassisVelocities& other) const {
139 return {vx + other.vx, vy + other.vy, omega + other.omega};
140 }
141
142 /**
143 * Subtracts the other ChassisVelocities from the current ChassisVelocities
144 * and returns the difference.
145 *
146 * For example, ChassisVelocities{5.0, 4.0, 2.0} -
147 * ChassisVelocities{1.0, 2.0, 1.0} = ChassisVelocities{4.0, 2.0, 1.0}
148 *
149 * @param other The ChassisVelocities to subtract.
150 *
151 * @return The difference between the two ChassisVelocities.
152 */
153 constexpr ChassisVelocities operator-(const ChassisVelocities& other) const {
154 return *this + -other;
155 }
156
157 /**
158 * Returns the inverse of the current ChassisVelocities.
159 * This is equivalent to negating all components of the ChassisVelocities.
160 *
161 * @return The inverse of the current ChassisVelocities.
162 */
163 constexpr ChassisVelocities operator-() const { return {-vx, -vy, -omega}; }
164
165 /**
166 * Multiplies the ChassisVelocities by a scalar and returns the new
167 * ChassisVelocities.
168 *
169 * For example, ChassisVelocities{2.0, 2.5, 1.0} * 2
170 * = ChassisVelocities{4.0, 5.0, 1.0}
171 *
172 * @param scalar The scalar to multiply by.
173 *
174 * @return The scaled ChassisVelocities.
175 */
176 constexpr ChassisVelocities operator*(double scalar) const {
177 return {scalar * vx, scalar * vy, scalar * omega};
178 }
179
180 /**
181 * Divides the ChassisVelocities by a scalar and returns the new
182 * ChassisVelocities.
183 *
184 * For example, ChassisVelocities{2.0, 2.5, 1.0} / 2
185 * = ChassisVelocities{1.0, 1.25, 0.5}
186 *
187 * @param scalar The scalar to divide by.
188 *
189 * @return The scaled ChassisVelocities.
190 */
191 constexpr ChassisVelocities operator/(double scalar) const {
192 return operator*(1.0 / scalar);
193 }
194
195 /**
196 * Compares the ChassisVelocities with another ChassisVelocity.
197 *
198 * @param other The other ChassisVelocities.
199 *
200 * @return The result of the comparison. Is true if they are the same.
201 */
202 constexpr bool operator==(const ChassisVelocities& other) const = default;
203};
204} // namespace wpi::math
205
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
A rotation in a 2D coordinate frame represented by a point on the unit circle (cosine and sine).
Definition Rotation2d.hpp:26
Represents a transformation for a Pose2d in the pose's frame.
Definition Transform2d.hpp:21
constexpr Twist2d Log() const
Returns a Twist2d of the current transform (pose delta).
Definition Transform2d.hpp:192
Represents a translation in 2D space.
Definition Translation2d.hpp:30
constexpr Translation2d RotateBy(const Rotation2d &other) const
Applies a rotation to the translation in 2D space.
Definition Translation2d.hpp:167
Definition LinearSystem.hpp:20
Represents robot chassis velocities.
Definition ChassisVelocities.hpp:26
constexpr ChassisVelocities Discretize(wpi::units::second_t dt) const
Discretizes continuous-time chassis velocities.
Definition ChassisVelocities.hpp:73
wpi::units::meters_per_second_t vx
Velocity along the x-axis.
Definition ChassisVelocities.hpp:30
constexpr ChassisVelocities ToRobotRelative(const Rotation2d &robotAngle) const
Converts this field-relative set of velocities into a robot-relative ChassisVelocities object.
Definition ChassisVelocities.hpp:97
constexpr ChassisVelocities operator-(const ChassisVelocities &other) const
Subtracts the other ChassisVelocities from the current ChassisVelocities and returns the difference.
Definition ChassisVelocities.hpp:153
constexpr ChassisVelocities operator-() const
Returns the inverse of the current ChassisVelocities.
Definition ChassisVelocities.hpp:163
constexpr ChassisVelocities operator*(double scalar) const
Multiplies the ChassisVelocities by a scalar and returns the new ChassisVelocities.
Definition ChassisVelocities.hpp:176
constexpr Twist2d ToTwist2d(wpi::units::second_t dt) const
Creates a Twist2d from ChassisVelocities.
Definition ChassisVelocities.hpp:49
constexpr ChassisVelocities ToFieldRelative(const Rotation2d &robotAngle) const
Converts this robot-relative set of velocities into a field-relative ChassisVelocities object.
Definition ChassisVelocities.hpp:118
wpi::units::radians_per_second_t omega
Represents the angular velocity of the robot frame.
Definition ChassisVelocities.hpp:40
constexpr ChassisVelocities operator+(const ChassisVelocities &other) const
Adds two ChassisVelocities and returns the sum.
Definition ChassisVelocities.hpp:138
wpi::units::meters_per_second_t vy
Velocity along the y-axis.
Definition ChassisVelocities.hpp:35
constexpr bool operator==(const ChassisVelocities &other) const =default
Compares the ChassisVelocities with another ChassisVelocity.
constexpr ChassisVelocities operator/(double scalar) const
Divides the ChassisVelocities by a scalar and returns the new ChassisVelocities.
Definition ChassisVelocities.hpp:191
A change in distance along a 2D arc since the last pose update.
Definition Twist2d.hpp:23