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