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-29 16:48:12 -07:00
|
|
|
|
|
|
|
|
#include "frc/controller/RamseteController.h"
|
|
|
|
|
|
2024-04-29 22:01:42 -07:00
|
|
|
#include <wpi/deprecated.h>
|
|
|
|
|
|
2021-12-03 18:21:30 -08:00
|
|
|
#include "units/angle.h"
|
2021-06-16 17:45:51 +03:00
|
|
|
#include "units/math.h"
|
2020-06-29 22:25:09 -07:00
|
|
|
|
2019-09-29 16:48:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns sin(x) / x.
|
|
|
|
|
*
|
|
|
|
|
* @param x Value of which to take sinc(x).
|
|
|
|
|
*/
|
2021-12-03 18:21:30 -08:00
|
|
|
static decltype(1 / 1_rad) Sinc(units::radian_t x) {
|
|
|
|
|
if (units::math::abs(x) < 1e-9_rad) {
|
|
|
|
|
return decltype(1 / 1_rad){1.0 - 1.0 / 6.0 * x.value() * x.value()};
|
2019-09-29 16:48:12 -07:00
|
|
|
} else {
|
2021-12-03 18:21:30 -08:00
|
|
|
return units::math::sin(x) / x;
|
2019-09-29 16:48:12 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 18:21:30 -08:00
|
|
|
RamseteController::RamseteController(units::unit_t<b_unit> b,
|
|
|
|
|
units::unit_t<zeta_unit> zeta)
|
2019-09-29 16:48:12 -07:00
|
|
|
: m_b{b}, m_zeta{zeta} {}
|
|
|
|
|
|
2024-04-29 22:01:42 -07:00
|
|
|
WPI_IGNORE_DEPRECATED
|
|
|
|
|
|
2021-12-03 18:21:30 -08:00
|
|
|
RamseteController::RamseteController()
|
2022-08-17 13:42:36 -07:00
|
|
|
: RamseteController{units::unit_t<b_unit>{2.0},
|
|
|
|
|
units::unit_t<zeta_unit>{0.7}} {}
|
2021-12-03 18:21:30 -08:00
|
|
|
|
2024-04-29 22:01:42 -07:00
|
|
|
WPI_UNIGNORE_DEPRECATED
|
|
|
|
|
|
2019-09-29 16:48:12 -07:00
|
|
|
bool RamseteController::AtReference() const {
|
|
|
|
|
const auto& eTranslate = m_poseError.Translation();
|
|
|
|
|
const auto& eRotate = m_poseError.Rotation();
|
|
|
|
|
const auto& tolTranslate = m_poseTolerance.Translation();
|
|
|
|
|
const auto& tolRotate = m_poseTolerance.Rotation();
|
|
|
|
|
return units::math::abs(eTranslate.X()) < tolTranslate.X() &&
|
|
|
|
|
units::math::abs(eTranslate.Y()) < tolTranslate.Y() &&
|
|
|
|
|
units::math::abs(eRotate.Radians()) < tolRotate.Radians();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RamseteController::SetTolerance(const Pose2d& poseTolerance) {
|
|
|
|
|
m_poseTolerance = poseTolerance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChassisSpeeds RamseteController::Calculate(
|
|
|
|
|
const Pose2d& currentPose, const Pose2d& poseRef,
|
|
|
|
|
units::meters_per_second_t linearVelocityRef,
|
|
|
|
|
units::radians_per_second_t angularVelocityRef) {
|
2020-01-27 21:53:00 -08:00
|
|
|
if (!m_enabled) {
|
|
|
|
|
return ChassisSpeeds{linearVelocityRef, 0_mps, angularVelocityRef};
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 16:48:12 -07:00
|
|
|
m_poseError = poseRef.RelativeTo(currentPose);
|
|
|
|
|
|
|
|
|
|
// Aliases for equation readability
|
2021-12-03 18:21:30 -08:00
|
|
|
const auto& eX = m_poseError.X();
|
|
|
|
|
const auto& eY = m_poseError.Y();
|
|
|
|
|
const auto& eTheta = m_poseError.Rotation().Radians();
|
|
|
|
|
const auto& vRef = linearVelocityRef;
|
|
|
|
|
const auto& omegaRef = angularVelocityRef;
|
|
|
|
|
|
2022-08-11 07:09:16 -07:00
|
|
|
// k = 2ζ√(ω_ref² + b v_ref²)
|
2021-12-03 18:21:30 -08:00
|
|
|
auto k = 2.0 * m_zeta *
|
|
|
|
|
units::math::sqrt(units::math::pow<2>(omegaRef) +
|
|
|
|
|
m_b * units::math::pow<2>(vRef));
|
2019-09-29 16:48:12 -07:00
|
|
|
|
2022-08-11 07:09:16 -07:00
|
|
|
// v_cmd = v_ref cos(e_θ) + k e_x
|
|
|
|
|
// ω_cmd = ω_ref + k e_θ + b v_ref sinc(e_θ) e_y
|
2021-12-06 12:57:42 -08:00
|
|
|
return ChassisSpeeds{vRef * m_poseError.Rotation().Cos() + k * eX, 0_mps,
|
|
|
|
|
omegaRef + k * eTheta + m_b * vRef * Sinc(eTheta) * eY};
|
2019-09-29 16:48:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChassisSpeeds RamseteController::Calculate(
|
|
|
|
|
const Pose2d& currentPose, const Trajectory::State& desiredState) {
|
|
|
|
|
return Calculate(currentPose, desiredState.pose, desiredState.velocity,
|
|
|
|
|
desiredState.velocity * desiredState.curvature);
|
|
|
|
|
}
|
2020-01-27 21:53:00 -08:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void RamseteController::SetEnabled(bool enabled) {
|
|
|
|
|
m_enabled = enabled;
|
|
|
|
|
}
|