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.
|
2020-07-12 21:03:25 -04:00
|
|
|
|
|
|
|
|
#include "frc/controller/HolonomicDriveController.h"
|
|
|
|
|
|
2020-12-28 10:12:52 -08:00
|
|
|
#include <utility>
|
|
|
|
|
|
2021-06-16 17:45:51 +03:00
|
|
|
#include "units/angular_velocity.h"
|
2020-07-12 21:03:25 -04:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
HolonomicDriveController::HolonomicDriveController(
|
2023-09-15 19:57:31 -07:00
|
|
|
PIDController xController, PIDController yController,
|
2020-12-28 10:12:52 -08:00
|
|
|
ProfiledPIDController<units::radian> thetaController)
|
|
|
|
|
: m_xController(std::move(xController)),
|
|
|
|
|
m_yController(std::move(yController)),
|
2022-10-07 01:46:22 +03:00
|
|
|
m_thetaController(std::move(thetaController)) {
|
|
|
|
|
m_thetaController.EnableContinuousInput(0_deg, 360.0_deg);
|
|
|
|
|
}
|
2020-07-12 21:03:25 -04:00
|
|
|
|
|
|
|
|
bool HolonomicDriveController::AtReference() const {
|
|
|
|
|
const auto& eTranslate = m_poseError.Translation();
|
2021-02-13 01:11:57 -05:00
|
|
|
const auto& eRotate = m_rotationError;
|
2020-07-12 21:03:25 -04:00
|
|
|
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 HolonomicDriveController::SetTolerance(const Pose2d& tolerance) {
|
|
|
|
|
m_poseTolerance = tolerance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChassisSpeeds HolonomicDriveController::Calculate(
|
2022-11-24 09:13:50 +02:00
|
|
|
const Pose2d& currentPose, const Pose2d& trajectoryPose,
|
|
|
|
|
units::meters_per_second_t desiredLinearVelocity,
|
|
|
|
|
const Rotation2d& desiredHeading) {
|
2021-04-19 00:00:11 -04:00
|
|
|
// If this is the first run, then we need to reset the theta controller to the
|
|
|
|
|
// current pose's heading.
|
|
|
|
|
if (m_firstRun) {
|
|
|
|
|
m_thetaController.Reset(currentPose.Rotation().Radians());
|
|
|
|
|
m_firstRun = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-12 21:03:25 -04:00
|
|
|
// Calculate feedforward velocities (field-relative)
|
2022-11-24 09:13:50 +02:00
|
|
|
auto xFF = desiredLinearVelocity * trajectoryPose.Rotation().Cos();
|
|
|
|
|
auto yFF = desiredLinearVelocity * trajectoryPose.Rotation().Sin();
|
2022-08-17 13:42:36 -07:00
|
|
|
auto thetaFF = units::radians_per_second_t{m_thetaController.Calculate(
|
2022-11-24 09:13:50 +02:00
|
|
|
currentPose.Rotation().Radians(), desiredHeading.Radians())};
|
2020-07-12 21:03:25 -04:00
|
|
|
|
2022-11-24 09:13:50 +02:00
|
|
|
m_poseError = trajectoryPose.RelativeTo(currentPose);
|
|
|
|
|
m_rotationError = desiredHeading - currentPose.Rotation();
|
2020-07-12 21:03:25 -04:00
|
|
|
|
|
|
|
|
if (!m_enabled) {
|
|
|
|
|
return ChassisSpeeds::FromFieldRelativeSpeeds(xFF, yFF, thetaFF,
|
|
|
|
|
currentPose.Rotation());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate feedback velocities (based on position error).
|
2022-11-24 09:13:50 +02:00
|
|
|
auto xFeedback = units::meters_per_second_t{m_xController.Calculate(
|
|
|
|
|
currentPose.X().value(), trajectoryPose.X().value())};
|
|
|
|
|
auto yFeedback = units::meters_per_second_t{m_yController.Calculate(
|
|
|
|
|
currentPose.Y().value(), trajectoryPose.Y().value())};
|
2020-07-12 21:03:25 -04:00
|
|
|
|
|
|
|
|
// Return next output.
|
|
|
|
|
return ChassisSpeeds::FromFieldRelativeSpeeds(
|
|
|
|
|
xFF + xFeedback, yFF + yFeedback, thetaFF, currentPose.Rotation());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChassisSpeeds HolonomicDriveController::Calculate(
|
|
|
|
|
const Pose2d& currentPose, const Trajectory::State& desiredState,
|
2022-11-24 09:13:50 +02:00
|
|
|
const Rotation2d& desiredHeading) {
|
2020-07-12 21:03:25 -04:00
|
|
|
return Calculate(currentPose, desiredState.pose, desiredState.velocity,
|
2022-11-24 09:13:50 +02:00
|
|
|
desiredHeading);
|
2020-07-12 21:03:25 -04:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void HolonomicDriveController::SetEnabled(bool enabled) {
|
|
|
|
|
m_enabled = enabled;
|
|
|
|
|
}
|
2023-01-17 03:33:15 +11:00
|
|
|
|
2024-11-05 08:50:17 -08:00
|
|
|
PIDController& HolonomicDriveController::getXController() {
|
|
|
|
|
return m_xController;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PIDController& HolonomicDriveController::getYController() {
|
|
|
|
|
return m_yController;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-17 03:33:15 +11:00
|
|
|
ProfiledPIDController<units::radian>&
|
|
|
|
|
HolonomicDriveController::getThetaController() {
|
|
|
|
|
return m_thetaController;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-05 08:50:17 -08:00
|
|
|
PIDController& HolonomicDriveController::GetXController() {
|
2023-01-17 03:33:15 +11:00
|
|
|
return m_xController;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-05 08:50:17 -08:00
|
|
|
PIDController& HolonomicDriveController::GetYController() {
|
2023-01-17 03:33:15 +11:00
|
|
|
return m_yController;
|
|
|
|
|
}
|
2024-11-05 08:50:17 -08:00
|
|
|
|
|
|
|
|
ProfiledPIDController<units::radian>&
|
|
|
|
|
HolonomicDriveController::GetThetaController() {
|
|
|
|
|
return m_thetaController;
|
|
|
|
|
}
|