2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-09-08 00:11:49 -04:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2021-08-20 09:02:01 -07:00
|
|
|
#include <wpi/SymbolExports.h>
|
|
|
|
|
|
2019-09-08 00:11:49 -04:00
|
|
|
#include "frc/geometry/Rotation2d.h"
|
2021-01-10 22:49:46 -08:00
|
|
|
#include "units/angle.h"
|
|
|
|
|
#include "units/math.h"
|
2020-08-06 23:57:39 -07:00
|
|
|
#include "units/velocity.h"
|
2019-09-08 00:11:49 -04:00
|
|
|
|
|
|
|
|
namespace frc {
|
|
|
|
|
/**
|
|
|
|
|
* Represents the state of one swerve module.
|
|
|
|
|
*/
|
2021-08-20 09:02:01 -07:00
|
|
|
struct WPILIB_DLLEXPORT SwerveModuleState {
|
2019-09-08 00:11:49 -04:00
|
|
|
/**
|
|
|
|
|
* Speed of the wheel of the module.
|
|
|
|
|
*/
|
|
|
|
|
units::meters_per_second_t speed = 0_mps;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Angle of the module.
|
|
|
|
|
*/
|
|
|
|
|
Rotation2d angle;
|
2021-01-10 22:49:46 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Minimize the change in heading the desired swerve module state would
|
|
|
|
|
* require by potentially reversing the direction the wheel spins. If this is
|
|
|
|
|
* used with the PIDController class's continuous input functionality, the
|
|
|
|
|
* furthest a wheel will ever rotate is 90 degrees.
|
|
|
|
|
*
|
|
|
|
|
* @param desiredState The desired state.
|
|
|
|
|
* @param currentAngle The current module angle.
|
|
|
|
|
*/
|
|
|
|
|
static SwerveModuleState Optimize(const SwerveModuleState& desiredState,
|
|
|
|
|
const Rotation2d& currentAngle) {
|
|
|
|
|
auto delta = desiredState.angle - currentAngle;
|
|
|
|
|
if (units::math::abs(delta.Degrees()) > 90_deg) {
|
|
|
|
|
return {-desiredState.speed, desiredState.angle + Rotation2d{180_deg}};
|
|
|
|
|
} else {
|
|
|
|
|
return {desiredState.speed, desiredState.angle};
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-08 00:11:49 -04:00
|
|
|
};
|
|
|
|
|
} // namespace frc
|