WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
ChassisAccelerations.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/acceleration.hpp"
10#include "wpi/units/angular_acceleration.hpp"
12
13namespace wpi::math {
14/**
15 * Represents robot chassis accelerations.
16 *
17 * A strictly non-holonomic drivetrain, such as a differential drive, should
18 * never have an ay component because it can never move sideways. Holonomic
19 * drivetrains such as swerve and mecanum will often have all three components.
20 */
22 /**
23 * Acceleration along the x-axis. (Fwd is +)
24 */
25 units::meters_per_second_squared_t ax = 0_mps_sq;
26
27 /**
28 * Acceleration along the y-axis. (Left is +)
29 */
30 units::meters_per_second_squared_t ay = 0_mps_sq;
31
32 /**
33 * Angular acceleration of the robot frame. (CCW is +)
34 */
35 units::radians_per_second_squared_t alpha = 0_rad_per_s_sq;
36
37 /**
38 * Converts this field-relative set of accelerations into a robot-relative
39 * ChassisAccelerations object.
40 *
41 * @param robotAngle The angle of the robot as measured by a gyroscope. The
42 * robot's angle is considered to be zero when it is facing directly away
43 * from your alliance station wall. Remember that this should be CCW
44 * positive.
45 * @return ChassisAccelerations object representing the accelerations in the
46 * robot's frame of reference.
47 */
49 const Rotation2d& robotAngle) const {
50 // CW rotation into chassis frame
51 auto rotated =
52 Translation2d{units::meter_t{ax.value()}, units::meter_t{ay.value()}}
53 .RotateBy(-robotAngle);
54 return {units::meters_per_second_squared_t{rotated.X().value()},
55 units::meters_per_second_squared_t{rotated.Y().value()}, alpha};
56 }
57
58 /**
59 * Converts this robot-relative set of accelerations into a field-relative
60 * ChassisAccelerations object.
61 *
62 * @param robotAngle The angle of the robot as measured by a gyroscope. The
63 * robot's angle is considered to be zero when it is facing directly away
64 * from your alliance station wall. Remember that this should be CCW
65 * positive.
66 * @return ChassisAccelerations object representing the accelerations in the
67 * field's frame of reference.
68 */
70 const Rotation2d& robotAngle) const {
71 // CCW rotation out of chassis frame
72 auto rotated =
73 Translation2d{units::meter_t{ax.value()}, units::meter_t{ay.value()}}
74 .RotateBy(robotAngle);
75 return {units::meters_per_second_squared_t{rotated.X().value()},
76 units::meters_per_second_squared_t{rotated.Y().value()}, alpha};
77 }
78
79 /**
80 * Adds two ChassisAccelerations and returns the sum.
81 *
82 * For example, ChassisAccelerations{1.0, 0.5, 1.5} +
83 * ChassisAccelerations{2.0, 1.5, 0.5} = ChassisAccelerations{3.0, 2.0, 2.0}
84 *
85 * @param other The ChassisAccelerations to add.
86 *
87 * @return The sum of the ChassisAccelerations.
88 */
90 const ChassisAccelerations& other) const {
91 return {ax + other.ax, ay + other.ay, alpha + other.alpha};
92 }
93
94 /**
95 * Subtracts the other ChassisAccelerations from the current
96 * ChassisAccelerations and returns the difference.
97 *
98 * For example, ChassisAccelerations{5.0, 4.0, 2.0} -
99 * ChassisAccelerations{1.0, 2.0, 1.0} = ChassisAccelerations{4.0, 2.0, 1.0}
100 *
101 * @param other The ChassisAccelerations to subtract.
102 *
103 * @return The difference between the two ChassisAccelerations.
104 */
106 const ChassisAccelerations& other) const {
107 return *this + -other;
108 }
109
110 /**
111 * Returns the inverse of the current ChassisAccelerations.
112 * This is equivalent to negating all components of the ChassisAccelerations.
113 *
114 * @return The inverse of the current ChassisAccelerations.
115 */
116 constexpr ChassisAccelerations operator-() const {
117 return {-ax, -ay, -alpha};
118 }
119
120 /**
121 * Multiplies the ChassisAccelerations by a scalar and returns the new
122 * ChassisAccelerations.
123 *
124 * For example, ChassisAccelerations{2.0, 2.5, 1.0} * 2
125 * = ChassisAccelerations{4.0, 5.0, 2.0}
126 *
127 * @param scalar The scalar to multiply by.
128 *
129 * @return The scaled ChassisAccelerations.
130 */
131 constexpr ChassisAccelerations operator*(double scalar) const {
132 return {scalar * ax, scalar * ay, scalar * alpha};
133 }
134
135 /**
136 * Divides the ChassisAccelerations by a scalar and returns the new
137 * ChassisAccelerations.
138 *
139 * For example, ChassisAccelerations{2.0, 2.5, 1.0} / 2
140 * = ChassisAccelerations{1.0, 1.25, 0.5}
141 *
142 * @param scalar The scalar to divide by.
143 *
144 * @return The scaled ChassisAccelerations.
145 */
146 constexpr ChassisAccelerations operator/(double scalar) const {
147 return operator*(1.0 / scalar);
148 }
149
150 /**
151 * Compares the ChassisAccelerations with another ChassisAccelerations.
152 *
153 * @param other The other ChassisAccelerations.
154 *
155 * @return The result of the comparison. Is true if they are the same.
156 */
157 constexpr bool operator==(const ChassisAccelerations& other) const = default;
158};
159} // namespace wpi::math
160
#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 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 accelerations.
Definition ChassisAccelerations.hpp:21
units::meters_per_second_squared_t ax
Acceleration along the x-axis.
Definition ChassisAccelerations.hpp:25
constexpr ChassisAccelerations ToRobotRelative(const Rotation2d &robotAngle) const
Converts this field-relative set of accelerations into a robot-relative ChassisAccelerations object.
Definition ChassisAccelerations.hpp:48
constexpr ChassisAccelerations operator-(const ChassisAccelerations &other) const
Subtracts the other ChassisAccelerations from the current ChassisAccelerations and returns the differ...
Definition ChassisAccelerations.hpp:105
units::radians_per_second_squared_t alpha
Angular acceleration of the robot frame.
Definition ChassisAccelerations.hpp:35
units::meters_per_second_squared_t ay
Acceleration along the y-axis.
Definition ChassisAccelerations.hpp:30
constexpr ChassisAccelerations operator-() const
Returns the inverse of the current ChassisAccelerations.
Definition ChassisAccelerations.hpp:116
constexpr ChassisAccelerations operator*(double scalar) const
Multiplies the ChassisAccelerations by a scalar and returns the new ChassisAccelerations.
Definition ChassisAccelerations.hpp:131
constexpr ChassisAccelerations operator/(double scalar) const
Divides the ChassisAccelerations by a scalar and returns the new ChassisAccelerations.
Definition ChassisAccelerations.hpp:146
constexpr bool operator==(const ChassisAccelerations &other) const =default
Compares the ChassisAccelerations with another ChassisAccelerations.
constexpr ChassisAccelerations ToFieldRelative(const Rotation2d &robotAngle) const
Converts this robot-relative set of accelerations into a field-relative ChassisAccelerations object.
Definition ChassisAccelerations.hpp:69
constexpr ChassisAccelerations operator+(const ChassisAccelerations &other) const
Adds two ChassisAccelerations and returns the sum.
Definition ChassisAccelerations.hpp:89