WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
SwerveModuleVelocity.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/angle.hpp"
9#include "wpi/units/math.hpp"
10#include "wpi/units/velocity.hpp"
12
13namespace wpi::math {
14/**
15 * Represents the velocity of one swerve module.
16 */
18 /**
19 * Velocity of the wheel of the module.
20 */
21 wpi::units::meters_per_second_t velocity = 0_mps;
22
23 /**
24 * Angle of the module.
25 */
27
28 /**
29 * Checks equality between this SwerveModuleVelocity and another object.
30 *
31 * @param other The other object.
32 * @return Whether the two objects are equal.
33 */
34 constexpr bool operator==(const SwerveModuleVelocity& other) const {
35 return wpi::units::math::abs(velocity - other.velocity) < 1E-9_mps &&
36 angle == other.angle;
37 }
38
39 /**
40 * Minimize the change in the heading this swerve module velocity would
41 * require by potentially reversing the direction the wheel spins. If this is
42 * used with the PIDController class's continuous input functionality, the
43 * furthest a wheel will ever rotate is 90 degrees.
44 *
45 * @param currentAngle The current module angle.
46 * @return The optimized SwerveModuleVelocity.
47 */
48 [[nodiscard]]
49 constexpr SwerveModuleVelocity Optimize(const Rotation2d& currentAngle) {
50 auto delta = angle - currentAngle;
51 if (wpi::units::math::abs(delta.Degrees()) > 90_deg) {
52 return {-velocity, angle + Rotation2d{180_deg}};
53 } else {
54 return {velocity, angle};
55 }
56 }
57
58 /**
59 * Scales velocity by cosine of angle error. This scales down movement
60 * perpendicular to the desired direction of travel that can occur when
61 * modules change directions. This results in smoother driving.
62 *
63 * @param currentAngle The current module angle.
64 * @return The scaled SwerveModuleVelocity.
65 */
66 [[nodiscard]]
67 constexpr SwerveModuleVelocity CosineScale(const Rotation2d& currentAngle) {
68 return {velocity * (angle - currentAngle).Cos(), angle};
69 }
70};
71} // namespace wpi::math
72
#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:29
Definition LinearSystem.hpp:20
Represents the velocity of one swerve module.
Definition SwerveModuleVelocity.hpp:17
constexpr SwerveModuleVelocity Optimize(const Rotation2d &currentAngle)
Minimize the change in the heading this swerve module velocity would require by potentially reversing...
Definition SwerveModuleVelocity.hpp:49
wpi::units::meters_per_second_t velocity
Velocity of the wheel of the module.
Definition SwerveModuleVelocity.hpp:21
Rotation2d angle
Angle of the module.
Definition SwerveModuleVelocity.hpp:26
constexpr SwerveModuleVelocity CosineScale(const Rotation2d &currentAngle)
Scales velocity by cosine of angle error.
Definition SwerveModuleVelocity.hpp:67
constexpr bool operator==(const SwerveModuleVelocity &other) const
Checks equality between this SwerveModuleVelocity and another object.
Definition SwerveModuleVelocity.hpp:34