WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
MecanumDriveWheelVelocities.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 <algorithm>
8#include <array>
9
10#include "wpi/units/math.hpp"
11#include "wpi/units/velocity.hpp"
13
14namespace wpi::math {
15/**
16 * Represents the wheel velocities for a mecanum drive drivetrain.
17 */
19 /**
20 * Velocity of the front-left wheel.
21 */
22 wpi::units::meters_per_second_t frontLeft = 0_mps;
23
24 /**
25 * Velocity of the front-right wheel.
26 */
27 wpi::units::meters_per_second_t frontRight = 0_mps;
28
29 /**
30 * Velocity of the rear-left wheel.
31 */
32 wpi::units::meters_per_second_t rearLeft = 0_mps;
33
34 /**
35 * Velocity of the rear-right wheel.
36 */
37 wpi::units::meters_per_second_t rearRight = 0_mps;
38
39 /**
40 * Renormalizes the wheel velocities if any individual velocity is above the
41 * specified maximum.
42 *
43 * Sometimes, after inverse kinematics, the requested velocity from one or
44 * more wheels may be above the max attainable velocity for the driving motor
45 * on that wheel. To fix this issue, one can reduce all the wheel velocities
46 * to make sure that all requested module velocities are at-or-below the
47 * absolute threshold, while maintaining the ratio of velocities between
48 * wheels.
49 *
50 * @param attainableMaxVelocity The absolute max velocity that a wheel can
51 * reach.
52 * @return Desaturated MecanumDriveWheelVelocities.
53 */
55 wpi::units::meters_per_second_t attainableMaxVelocity) const {
56 std::array<wpi::units::meters_per_second_t, 4> wheelVelocities{
58 wpi::units::meters_per_second_t realMaxVelocity =
59 wpi::units::math::abs(*std::max_element(
60 wheelVelocities.begin(), wheelVelocities.end(),
61 [](const auto& a, const auto& b) {
62 return wpi::units::math::abs(a) < wpi::units::math::abs(b);
63 }));
64
65 if (realMaxVelocity > attainableMaxVelocity) {
66 for (int i = 0; i < 4; ++i) {
67 wheelVelocities[i] =
68 wheelVelocities[i] / realMaxVelocity * attainableMaxVelocity;
69 }
70 return MecanumDriveWheelVelocities{wheelVelocities[0], wheelVelocities[1],
71 wheelVelocities[2],
72 wheelVelocities[3]};
73 }
74
75 return *this;
76 }
77
78 /**
79 * Adds two MecanumDriveWheelVelocities and returns the sum.
80 *
81 * For example, MecanumDriveWheelVelocities{1.0, 0.5, 2.0, 1.5} +
82 * MecanumDriveWheelVelocities{2.0, 1.5, 0.5, 1.0} =
83 * MecanumDriveWheelVelocities{3.0, 2.0, 2.5, 2.5}
84 *
85 * @param other The MecanumDriveWheelVelocities to add.
86 * @return The sum of the MecanumDriveWheelVelocities.
87 */
89 const MecanumDriveWheelVelocities& other) const {
90 return {frontLeft + other.frontLeft, frontRight + other.frontRight,
91 rearLeft + other.rearLeft, rearRight + other.rearRight};
92 }
93
94 /**
95 * Subtracts the other MecanumDriveWheelVelocities from the current
96 * MecanumDriveWheelVelocities and returns the difference.
97 *
98 * For example, MecanumDriveWheelVelocities{5.0, 4.0, 6.0, 2.5} -
99 * MecanumDriveWheelVelocities{1.0, 2.0, 3.0, 0.5} =
100 * MecanumDriveWheelVelocities{4.0, 2.0, 3.0, 2.0}
101 *
102 * @param other The MecanumDriveWheelVelocities to subtract.
103 * @return The difference between the two MecanumDriveWheelVelocities.
104 */
106 const MecanumDriveWheelVelocities& other) const {
107 return *this + -other;
108 }
109
110 /**
111 * Returns the inverse of the current MecanumDriveWheelVelocities.
112 * This is equivalent to negating all components of the
113 * MecanumDriveWheelVelocities.
114 *
115 * @return The inverse of the current MecanumDriveWheelVelocities.
116 */
118 return {-frontLeft, -frontRight, -rearLeft, -rearRight};
119 }
120
121 /**
122 * Multiplies the MecanumDriveWheelVelocities by a scalar and returns the new
123 * MecanumDriveWheelVelocities.
124 *
125 * For example, MecanumDriveWheelVelocities{2.0, 2.5, 3.0, 3.5} * 2 =
126 * MecanumDriveWheelVelocities{4.0, 5.0, 6.0, 7.0}
127 *
128 * @param scalar The scalar to multiply by.
129 * @return The scaled MecanumDriveWheelVelocities.
130 */
131 constexpr MecanumDriveWheelVelocities operator*(double scalar) const {
132 return {scalar * frontLeft, scalar * frontRight, scalar * rearLeft,
133 scalar * rearRight};
134 }
135
136 /**
137 * Divides the MecanumDriveWheelVelocities by a scalar and returns the new
138 * MecanumDriveWheelVelocities.
139 *
140 * For example, MecanumDriveWheelVelocities{2.0, 2.5, 1.5, 1.0} / 2 =
141 * MecanumDriveWheelVelocities{1.0, 1.25, 0.75, 0.5}
142 *
143 * @param scalar The scalar to divide by.
144 * @return The scaled MecanumDriveWheelVelocities.
145 */
146 constexpr MecanumDriveWheelVelocities operator/(double scalar) const {
147 return operator*(1.0 / scalar);
148 }
149};
150} // namespace wpi::math
151
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
Definition LinearSystem.hpp:20
Represents the wheel velocities for a mecanum drive drivetrain.
Definition MecanumDriveWheelVelocities.hpp:18
wpi::units::meters_per_second_t frontRight
Velocity of the front-right wheel.
Definition MecanumDriveWheelVelocities.hpp:27
constexpr MecanumDriveWheelVelocities operator+(const MecanumDriveWheelVelocities &other) const
Adds two MecanumDriveWheelVelocities and returns the sum.
Definition MecanumDriveWheelVelocities.hpp:88
constexpr MecanumDriveWheelVelocities operator-(const MecanumDriveWheelVelocities &other) const
Subtracts the other MecanumDriveWheelVelocities from the current MecanumDriveWheelVelocities and retu...
Definition MecanumDriveWheelVelocities.hpp:105
wpi::units::meters_per_second_t rearLeft
Velocity of the rear-left wheel.
Definition MecanumDriveWheelVelocities.hpp:32
constexpr MecanumDriveWheelVelocities Desaturate(wpi::units::meters_per_second_t attainableMaxVelocity) const
Renormalizes the wheel velocities if any individual velocity is above the specified maximum.
Definition MecanumDriveWheelVelocities.hpp:54
constexpr MecanumDriveWheelVelocities operator/(double scalar) const
Divides the MecanumDriveWheelVelocities by a scalar and returns the new MecanumDriveWheelVelocities.
Definition MecanumDriveWheelVelocities.hpp:146
wpi::units::meters_per_second_t frontLeft
Velocity of the front-left wheel.
Definition MecanumDriveWheelVelocities.hpp:22
constexpr MecanumDriveWheelVelocities operator*(double scalar) const
Multiplies the MecanumDriveWheelVelocities by a scalar and returns the new MecanumDriveWheelVelocitie...
Definition MecanumDriveWheelVelocities.hpp:131
wpi::units::meters_per_second_t rearRight
Velocity of the rear-right wheel.
Definition MecanumDriveWheelVelocities.hpp:37
constexpr MecanumDriveWheelVelocities operator-() const
Returns the inverse of the current MecanumDriveWheelVelocities.
Definition MecanumDriveWheelVelocities.hpp:117