WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
SwerveModuleAcceleration.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
8#include "wpi/units/acceleration.hpp"
9#include "wpi/units/math.hpp"
11
12namespace wpi::math {
13/**
14 * Represents the accelerations of one swerve module.
15 */
17 /**
18 * Acceleration of the wheel of the module.
19 */
20 units::meters_per_second_squared_t acceleration = 0_mps_sq;
21
22 /**
23 * Angle of the acceleration vector.
24 */
26
27 /**
28 * Checks equality between this SwerveModuleAccelerations and another object.
29 *
30 * @param other The other object.
31 * @return Whether the two objects are equal.
32 */
33 constexpr bool operator==(const SwerveModuleAcceleration& other) const {
34 return units::math::abs(acceleration - other.acceleration) < 1E-9_mps_sq &&
35 angle == other.angle;
36 }
37
38 /**
39 * Adds two SwerveModuleAccelerations and returns the sum.
40 * Note: This performs vector addition of the accelerations.
41 *
42 * @param other The SwerveModuleAccelerations to add.
43 * @return The sum of the SwerveModuleAccelerations.
44 */
46 const SwerveModuleAcceleration& other) const {
47 // Convert to Cartesian coordinates, add, then convert back
48 auto thisX = acceleration * angle.Cos();
49 auto thisY = acceleration * angle.Sin();
50 auto otherX = other.acceleration * other.angle.Cos();
51 auto otherY = other.acceleration * other.angle.Sin();
52
53 auto sumX = thisX + otherX;
54 auto sumY = thisY + otherY;
55
56 auto resultAcceleration = units::math::hypot(sumX, sumY);
57 auto resultAngle = Rotation2d{sumX.value(), sumY.value()};
58
59 return {resultAcceleration, resultAngle};
60 }
61
62 /**
63 * Subtracts the other SwerveModuleAccelerations from the current
64 * SwerveModuleAccelerations and returns the difference.
65 *
66 * @param other The SwerveModuleAccelerations to subtract.
67 * @return The difference between the two SwerveModuleAccelerations.
68 */
70 const SwerveModuleAcceleration& other) const {
71 return *this + (-other);
72 }
73
74 /**
75 * Returns the inverse of the current SwerveModuleAccelerations.
76 * This is equivalent to rotating the acceleration by pi (or 180 degrees).
77 *
78 * @return The inverse of the current SwerveModuleAccelerations.
79 */
81 return {acceleration, angle + Rotation2d{180_deg}};
82 }
83
84 /**
85 * Multiplies the SwerveModuleAccelerations by a scalar and returns the new
86 * SwerveModuleAccelerations.
87 *
88 * @param scalar The scalar to multiply by.
89 * @return The scaled SwerveModuleAccelerations.
90 */
91 constexpr SwerveModuleAcceleration operator*(double scalar) const {
92 if (scalar < 0) {
93 return {-scalar * acceleration, angle + Rotation2d{180_deg}};
94 }
95 return {scalar * acceleration, angle};
96 }
97
98 /**
99 * Divides the SwerveModuleAccelerations by a scalar and returns the new
100 * SwerveModuleAccelerations.
101 *
102 * @param scalar The scalar to divide by.
103 * @return The scaled SwerveModuleAccelerations.
104 */
105 constexpr SwerveModuleAcceleration operator/(double scalar) const {
106 return operator*(1.0 / scalar);
107 }
108};
109} // namespace wpi::math
110
#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
constexpr double Cos() const
Returns the cosine of the rotation.
Definition Rotation2d.hpp:235
constexpr double Sin() const
Returns the sine of the rotation.
Definition Rotation2d.hpp:242
Definition LinearSystem.hpp:20
Represents the accelerations of one swerve module.
Definition SwerveModuleAcceleration.hpp:16
units::meters_per_second_squared_t acceleration
Acceleration of the wheel of the module.
Definition SwerveModuleAcceleration.hpp:20
constexpr SwerveModuleAcceleration operator-() const
Returns the inverse of the current SwerveModuleAccelerations.
Definition SwerveModuleAcceleration.hpp:80
constexpr bool operator==(const SwerveModuleAcceleration &other) const
Checks equality between this SwerveModuleAccelerations and another object.
Definition SwerveModuleAcceleration.hpp:33
constexpr SwerveModuleAcceleration operator*(double scalar) const
Multiplies the SwerveModuleAccelerations by a scalar and returns the new SwerveModuleAccelerations.
Definition SwerveModuleAcceleration.hpp:91
SwerveModuleAcceleration operator+(const SwerveModuleAcceleration &other) const
Adds two SwerveModuleAccelerations and returns the sum.
Definition SwerveModuleAcceleration.hpp:45
constexpr SwerveModuleAcceleration operator/(double scalar) const
Divides the SwerveModuleAccelerations by a scalar and returns the new SwerveModuleAccelerations.
Definition SwerveModuleAcceleration.hpp:105
Rotation2d angle
Angle of the acceleration vector.
Definition SwerveModuleAcceleration.hpp:25
SwerveModuleAcceleration operator-(const SwerveModuleAcceleration &other) const
Subtracts the other SwerveModuleAccelerations from the current SwerveModuleAccelerations and returns ...
Definition SwerveModuleAcceleration.hpp:69