WPILibC++ 2027.0.0-alpha-4
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 */
47 constexpr void Optimize(const Rotation2d& currentAngle) {
48 auto delta = angle - currentAngle;
49 if (wpi::units::math::abs(delta.Degrees()) > 90_deg) {
50 velocity *= -1;
51 angle = angle + Rotation2d{180_deg};
52 }
53 }
54
55 /**
56 * Minimize the change in heading the desired swerve module velocity would
57 * require by potentially reversing the direction the wheel spins. If this is
58 * used with the PIDController class's continuous input functionality, the
59 * furthest a wheel will ever rotate is 90 degrees.
60 *
61 * @param desiredVelocity The desired velocity.
62 * @param currentAngle The current module angle.
63 */
64 [[deprecated("Use instance method instead.")]]
66 const SwerveModuleVelocity& desiredVelocity,
67 const Rotation2d& currentAngle) {
68 auto delta = desiredVelocity.angle - currentAngle;
69 if (wpi::units::math::abs(delta.Degrees()) > 90_deg) {
70 return {-desiredVelocity.velocity,
71 desiredVelocity.angle + Rotation2d{180_deg}};
72 } else {
73 return {desiredVelocity.velocity, desiredVelocity.angle};
74 }
75 }
76
77 /**
78 * Scales velocity by cosine of angle error. This scales down movement
79 * perpendicular to the desired direction of travel that can occur when
80 * modules change directions. This results in smoother driving.
81 *
82 * @param currentAngle The current module angle.
83 */
84 constexpr void CosineScale(const Rotation2d& currentAngle) {
85 velocity *= (angle - currentAngle).Cos();
86 }
87};
88} // namespace wpi::math
89
#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:26
Definition LinearSystem.hpp:20
Represents the velocity of one swerve module.
Definition SwerveModuleVelocity.hpp:17
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 void Optimize(const Rotation2d &currentAngle)
Minimize the change in the heading this swerve module velocity would require by potentially reversing...
Definition SwerveModuleVelocity.hpp:47
constexpr void CosineScale(const Rotation2d &currentAngle)
Scales velocity by cosine of angle error.
Definition SwerveModuleVelocity.hpp:84
static constexpr SwerveModuleVelocity Optimize(const SwerveModuleVelocity &desiredVelocity, const Rotation2d &currentAngle)
Minimize the change in heading the desired swerve module velocity would require by potentially revers...
Definition SwerveModuleVelocity.hpp:65
constexpr bool operator==(const SwerveModuleVelocity &other) const
Checks equality between this SwerveModuleVelocity and another object.
Definition SwerveModuleVelocity.hpp:34