YAMS
Yet Another Mechanism System
Loading...
Searching...
No Matches
SwerveModuleVelocityWithAzimuth.hpp
Go to the documentation of this file.
1// Copyright (c) 2026 Yet Another Software Suite
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#pragma once
5
6#include <optional>
7#include <wpi/math/geometry/Rotation2d.hpp>
8#include <wpi/math/kinematics/SwerveModuleVelocity.hpp>
9#include <wpi/units/angle.hpp>
10#include <wpi/units/angular_velocity.hpp>
11#include <wpi/units/math.hpp>
12#include <wpi/units/velocity.hpp>
13
15
19struct SwerveModuleVelocityWithAzimuth : public wpi::math::SwerveModuleVelocity {
21 wpi::units::radians_per_second_t azimuthVelocity{0};
22
24
30 SwerveModuleVelocityWithAzimuth(wpi::units::meters_per_second_t velocity,
31 wpi::math::Rotation2d angle,
32 wpi::units::radians_per_second_t azimuthVelocity)
33 : wpi::math::SwerveModuleVelocity{velocity, angle}, azimuthVelocity{azimuthVelocity} {}
34
46 static wpi::math::SwerveModuleVelocity Optimize(
47 const wpi::math::SwerveModuleVelocity& desired,
48 const SwerveModuleVelocityWithAzimuth& measured,
49 const std::optional<wpi::math::Rotation2d>& lastCommanded,
50 wpi::units::degree_t hysteresis = wpi::units::degree_t{30}) {
51 wpi::math::SwerveModuleVelocity state = desired.Optimize(measured.angle);
52 if (!lastCommanded || wpi::units::math::abs((state.angle - *lastCommanded).Degrees()) <=
53 wpi::units::degree_t{90}) {
54 return state;
55 }
56 // The other orientation is the one the wheel was last sent to.
57 wpi::math::SwerveModuleVelocity lastOrientation{
58 -state.velocity, state.angle.RotateBy(wpi::math::Rotation2d{wpi::units::degree_t{180}})};
59 return wpi::units::math::abs((lastOrientation.angle - measured.angle).Degrees()) <
60 wpi::units::degree_t{90} + hysteresis
61 ? lastOrientation
62 : state;
63 }
64};
65
66} // namespace yams::mechanisms::swerve
Definition SwerveDrive.hpp:49
Definition SwerveModuleVelocityWithAzimuth.hpp:19
wpi::units::radians_per_second_t azimuthVelocity
Definition SwerveModuleVelocityWithAzimuth.hpp:21
static wpi::math::SwerveModuleVelocity Optimize(const wpi::math::SwerveModuleVelocity &desired, const SwerveModuleVelocityWithAzimuth &measured, const std::optional< wpi::math::Rotation2d > &lastCommanded, wpi::units::degree_t hysteresis=wpi::units::degree_t{30})
Definition SwerveModuleVelocityWithAzimuth.hpp:46
SwerveModuleVelocityWithAzimuth(wpi::units::meters_per_second_t velocity, wpi::math::Rotation2d angle, wpi::units::radians_per_second_t azimuthVelocity)
Definition SwerveModuleVelocityWithAzimuth.hpp:30