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