WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
DifferentialDriveWheelVelocities.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 "wpi/units/math.hpp"
8#include "wpi/units/velocity.hpp"
10
11namespace wpi::math {
12/**
13 * Represents the wheel velocities for a differential drive drivetrain.
14 */
16 /**
17 * Velocity of the left side of the robot.
18 */
19 wpi::units::meters_per_second_t left = 0_mps;
20
21 /**
22 * Velocity of the right side of the robot.
23 */
24 wpi::units::meters_per_second_t right = 0_mps;
25
26 /**
27 * Renormalizes the wheel velocities if either side is above the specified
28 * maximum.
29 *
30 * Sometimes, after inverse kinematics, the requested velocity from one or
31 * more wheels may be above the max attainable velocity for the driving motor
32 * on that wheel. To fix this issue, one can reduce all the wheel velocities
33 * to make sure that all requested module velocities are at-or-below the
34 * absolute threshold, while maintaining the ratio of velocities between
35 * wheels.
36 *
37 * @param attainableMaxVelocity The absolute max velocity that a wheel can
38 * reach.
39 */
40 constexpr void Desaturate(
41 wpi::units::meters_per_second_t attainableMaxVelocity) {
42 auto realMaxVelocity = wpi::units::math::max(wpi::units::math::abs(left),
43 wpi::units::math::abs(right));
44
45 if (realMaxVelocity > attainableMaxVelocity) {
46 left = left / realMaxVelocity * attainableMaxVelocity;
47 right = right / realMaxVelocity * attainableMaxVelocity;
48 }
49 }
50
51 /**
52 * Adds two DifferentialDriveWheelVelocities and returns the sum.
53 *
54 * For example, DifferentialDriveWheelVelocities{1.0, 0.5} +
55 * DifferentialDriveWheelVelocities{2.0, 1.5} =
56 * DifferentialDriveWheelVelocities{3.0, 2.0}
57 *
58 * @param other The DifferentialDriveWheelVelocities to add.
59 * @return The sum of the DifferentialDriveWheelVelocities.
60 */
62 const DifferentialDriveWheelVelocities& other) const {
63 return {left + other.left, right + other.right};
64 }
65
66 /**
67 * Subtracts the other DifferentialDriveWheelVelocities from the current
68 * DifferentialDriveWheelVelocities and returns the difference.
69 *
70 * For example, DifferentialDriveWheelVelocities{5.0, 4.0} -
71 * DifferentialDriveWheelVelocities{1.0, 2.0} =
72 * DifferentialDriveWheelVelocities{4.0, 2.0}
73 *
74 * @param other The DifferentialDriveWheelVelocities to subtract.
75 * @return The difference between the two DifferentialDriveWheelVelocities.
76 */
78 const DifferentialDriveWheelVelocities& other) const {
79 return *this + -other;
80 }
81
82 /**
83 * Returns the inverse of the current DifferentialDriveWheelVelocities.
84 * This is equivalent to negating all components of the
85 * DifferentialDriveWheelVelocities.
86 *
87 * @return The inverse of the current DifferentialDriveWheelVelocities.
88 */
90 return {-left, -right};
91 }
92
93 /**
94 * Multiplies the DifferentialDriveWheelVelocities by a scalar and returns the
95 * new DifferentialDriveWheelVelocities.
96 *
97 * For example, DifferentialDriveWheelVelocities{2.0, 2.5} * 2
98 * = DifferentialDriveWheelVelocities{4.0, 5.0}
99 *
100 * @param scalar The scalar to multiply by.
101 * @return The scaled DifferentialDriveWheelVelocities.
102 */
103 constexpr DifferentialDriveWheelVelocities operator*(double scalar) const {
104 return {scalar * left, scalar * right};
105 }
106
107 /**
108 * Divides the DifferentialDriveWheelVelocities by a scalar and returns the
109 * new DifferentialDriveWheelVelocities.
110 *
111 * For example, DifferentialDriveWheelVelocities{2.0, 2.5} / 2
112 * = DifferentialDriveWheelVelocities{1.0, 1.25}
113 *
114 * @param scalar The scalar to divide by.
115 * @return The scaled DifferentialDriveWheelVelocities.
116 */
117 constexpr DifferentialDriveWheelVelocities operator/(double scalar) const {
118 return operator*(1.0 / scalar);
119 }
120};
121} // namespace wpi::math
122
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
Definition LinearSystem.hpp:20
Represents the wheel velocities for a differential drive drivetrain.
Definition DifferentialDriveWheelVelocities.hpp:15
constexpr void Desaturate(wpi::units::meters_per_second_t attainableMaxVelocity)
Renormalizes the wheel velocities if either side is above the specified maximum.
Definition DifferentialDriveWheelVelocities.hpp:40
constexpr DifferentialDriveWheelVelocities operator-(const DifferentialDriveWheelVelocities &other) const
Subtracts the other DifferentialDriveWheelVelocities from the current DifferentialDriveWheelVelocitie...
Definition DifferentialDriveWheelVelocities.hpp:77
constexpr DifferentialDriveWheelVelocities operator+(const DifferentialDriveWheelVelocities &other) const
Adds two DifferentialDriveWheelVelocities and returns the sum.
Definition DifferentialDriveWheelVelocities.hpp:61
constexpr DifferentialDriveWheelVelocities operator-() const
Returns the inverse of the current DifferentialDriveWheelVelocities.
Definition DifferentialDriveWheelVelocities.hpp:89
constexpr DifferentialDriveWheelVelocities operator*(double scalar) const
Multiplies the DifferentialDriveWheelVelocities by a scalar and returns the new DifferentialDriveWhee...
Definition DifferentialDriveWheelVelocities.hpp:103
wpi::units::meters_per_second_t right
Velocity of the right side of the robot.
Definition DifferentialDriveWheelVelocities.hpp:24
wpi::units::meters_per_second_t left
Velocity of the left side of the robot.
Definition DifferentialDriveWheelVelocities.hpp:19
constexpr DifferentialDriveWheelVelocities operator/(double scalar) const
Divides the DifferentialDriveWheelVelocities by a scalar and returns the new DifferentialDriveWheelVe...
Definition DifferentialDriveWheelVelocities.hpp:117