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-10-27 00:33:41 -04:00
|
|
|
|
|
|
|
|
#include "frc2/command/RamseteCommand.h"
|
|
|
|
|
|
2020-12-28 10:12:52 -08:00
|
|
|
#include <utility>
|
|
|
|
|
|
2019-10-27 00:33:41 -04:00
|
|
|
using namespace frc2;
|
|
|
|
|
using namespace units;
|
|
|
|
|
|
|
|
|
|
RamseteCommand::RamseteCommand(
|
|
|
|
|
frc::Trajectory trajectory, std::function<frc::Pose2d()> pose,
|
2019-11-09 23:16:42 -05:00
|
|
|
frc::RamseteController controller,
|
|
|
|
|
frc::SimpleMotorFeedforward<units::meters> feedforward,
|
2019-10-27 00:33:41 -04:00
|
|
|
frc::DifferentialDriveKinematics kinematics,
|
2019-11-19 01:11:05 -05:00
|
|
|
std::function<frc::DifferentialDriveWheelSpeeds()> wheelSpeeds,
|
2019-10-27 00:33:41 -04:00
|
|
|
frc2::PIDController leftController, frc2::PIDController rightController,
|
|
|
|
|
std::function<void(volt_t, volt_t)> output,
|
|
|
|
|
std::initializer_list<Subsystem*> requirements)
|
2020-12-28 10:12:52 -08:00
|
|
|
: m_trajectory(std::move(trajectory)),
|
|
|
|
|
m_pose(std::move(pose)),
|
2019-10-27 00:33:41 -04:00
|
|
|
m_controller(controller),
|
2019-11-09 23:16:42 -05:00
|
|
|
m_feedforward(feedforward),
|
2019-10-27 00:33:41 -04:00
|
|
|
m_kinematics(kinematics),
|
2020-12-28 10:12:52 -08:00
|
|
|
m_speeds(std::move(wheelSpeeds)),
|
2019-10-27 00:33:41 -04:00
|
|
|
m_leftController(std::make_unique<frc2::PIDController>(leftController)),
|
|
|
|
|
m_rightController(std::make_unique<frc2::PIDController>(rightController)),
|
2020-12-28 10:12:52 -08:00
|
|
|
m_outputVolts(std::move(output)),
|
2019-11-01 12:26:48 -04:00
|
|
|
m_usePID(true) {
|
2019-10-27 00:33:41 -04:00
|
|
|
AddRequirements(requirements);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-01 20:09:17 -08:00
|
|
|
RamseteCommand::RamseteCommand(
|
|
|
|
|
frc::Trajectory trajectory, std::function<frc::Pose2d()> pose,
|
|
|
|
|
frc::RamseteController controller,
|
|
|
|
|
frc::SimpleMotorFeedforward<units::meters> feedforward,
|
|
|
|
|
frc::DifferentialDriveKinematics kinematics,
|
|
|
|
|
std::function<frc::DifferentialDriveWheelSpeeds()> wheelSpeeds,
|
|
|
|
|
frc2::PIDController leftController, frc2::PIDController rightController,
|
|
|
|
|
std::function<void(volt_t, volt_t)> output,
|
|
|
|
|
wpi::ArrayRef<Subsystem*> requirements)
|
2020-12-28 10:12:52 -08:00
|
|
|
: m_trajectory(std::move(trajectory)),
|
|
|
|
|
m_pose(std::move(pose)),
|
2020-01-01 20:09:17 -08:00
|
|
|
m_controller(controller),
|
|
|
|
|
m_feedforward(feedforward),
|
|
|
|
|
m_kinematics(kinematics),
|
2020-12-28 10:12:52 -08:00
|
|
|
m_speeds(std::move(wheelSpeeds)),
|
2020-01-01 20:09:17 -08:00
|
|
|
m_leftController(std::make_unique<frc2::PIDController>(leftController)),
|
|
|
|
|
m_rightController(std::make_unique<frc2::PIDController>(rightController)),
|
2020-12-28 10:12:52 -08:00
|
|
|
m_outputVolts(std::move(output)),
|
2020-01-01 20:09:17 -08:00
|
|
|
m_usePID(true) {
|
|
|
|
|
AddRequirements(requirements);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-27 00:33:41 -04:00
|
|
|
RamseteCommand::RamseteCommand(
|
|
|
|
|
frc::Trajectory trajectory, std::function<frc::Pose2d()> pose,
|
|
|
|
|
frc::RamseteController controller,
|
|
|
|
|
frc::DifferentialDriveKinematics kinematics,
|
|
|
|
|
std::function<void(units::meters_per_second_t, units::meters_per_second_t)>
|
|
|
|
|
output,
|
|
|
|
|
std::initializer_list<Subsystem*> requirements)
|
2020-12-28 10:12:52 -08:00
|
|
|
: m_trajectory(std::move(trajectory)),
|
|
|
|
|
m_pose(std::move(pose)),
|
2019-10-27 00:33:41 -04:00
|
|
|
m_controller(controller),
|
|
|
|
|
m_kinematics(kinematics),
|
2020-12-28 10:12:52 -08:00
|
|
|
m_outputVel(std::move(output)),
|
2019-11-01 12:26:48 -04:00
|
|
|
m_usePID(false) {
|
2019-10-27 00:33:41 -04:00
|
|
|
AddRequirements(requirements);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-01 20:09:17 -08:00
|
|
|
RamseteCommand::RamseteCommand(
|
|
|
|
|
frc::Trajectory trajectory, std::function<frc::Pose2d()> pose,
|
|
|
|
|
frc::RamseteController controller,
|
|
|
|
|
frc::DifferentialDriveKinematics kinematics,
|
|
|
|
|
std::function<void(units::meters_per_second_t, units::meters_per_second_t)>
|
|
|
|
|
output,
|
|
|
|
|
wpi::ArrayRef<Subsystem*> requirements)
|
2020-12-28 10:12:52 -08:00
|
|
|
: m_trajectory(std::move(trajectory)),
|
|
|
|
|
m_pose(std::move(pose)),
|
2020-01-01 20:09:17 -08:00
|
|
|
m_controller(controller),
|
|
|
|
|
m_kinematics(kinematics),
|
2020-12-28 10:12:52 -08:00
|
|
|
m_outputVel(std::move(output)),
|
2020-01-01 20:09:17 -08:00
|
|
|
m_usePID(false) {
|
|
|
|
|
AddRequirements(requirements);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-27 00:33:41 -04:00
|
|
|
void RamseteCommand::Initialize() {
|
2020-11-21 13:03:01 -05:00
|
|
|
m_prevTime = -1_s;
|
2019-10-27 00:33:41 -04:00
|
|
|
auto initialState = m_trajectory.Sample(0_s);
|
|
|
|
|
m_prevSpeeds = m_kinematics.ToWheelSpeeds(
|
|
|
|
|
frc::ChassisSpeeds{initialState.velocity, 0_mps,
|
|
|
|
|
initialState.velocity * initialState.curvature});
|
|
|
|
|
m_timer.Reset();
|
|
|
|
|
m_timer.Start();
|
2019-11-01 12:26:48 -04:00
|
|
|
if (m_usePID) {
|
|
|
|
|
m_leftController->Reset();
|
|
|
|
|
m_rightController->Reset();
|
|
|
|
|
}
|
2019-10-27 00:33:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RamseteCommand::Execute() {
|
|
|
|
|
auto curTime = m_timer.Get();
|
|
|
|
|
auto dt = curTime - m_prevTime;
|
|
|
|
|
|
2020-11-21 13:03:01 -05:00
|
|
|
if (m_prevTime < 0_s) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_usePID) {
|
2020-11-21 13:03:01 -05:00
|
|
|
m_outputVolts(0_V, 0_V);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2020-11-21 13:03:01 -05:00
|
|
|
m_outputVel(0_mps, 0_mps);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-11-21 13:03:01 -05:00
|
|
|
|
|
|
|
|
m_prevTime = curTime;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-27 00:33:41 -04:00
|
|
|
auto targetWheelSpeeds = m_kinematics.ToWheelSpeeds(
|
|
|
|
|
m_controller.Calculate(m_pose(), m_trajectory.Sample(curTime)));
|
|
|
|
|
|
2019-11-01 12:26:48 -04:00
|
|
|
if (m_usePID) {
|
2019-11-20 22:44:18 -08:00
|
|
|
auto leftFeedforward = m_feedforward.Calculate(
|
|
|
|
|
targetWheelSpeeds.left,
|
|
|
|
|
(targetWheelSpeeds.left - m_prevSpeeds.left) / dt);
|
2019-10-27 00:33:41 -04:00
|
|
|
|
2019-11-20 22:44:18 -08:00
|
|
|
auto rightFeedforward = m_feedforward.Calculate(
|
|
|
|
|
targetWheelSpeeds.right,
|
|
|
|
|
(targetWheelSpeeds.right - m_prevSpeeds.right) / dt);
|
2019-10-27 00:33:41 -04:00
|
|
|
|
2019-11-20 22:44:18 -08:00
|
|
|
auto leftOutput = volt_t(m_leftController->Calculate(
|
|
|
|
|
m_speeds().left.to<double>(),
|
|
|
|
|
targetWheelSpeeds.left.to<double>())) +
|
|
|
|
|
leftFeedforward;
|
2019-10-27 00:33:41 -04:00
|
|
|
|
|
|
|
|
auto rightOutput = volt_t(m_rightController->Calculate(
|
2019-11-19 01:11:05 -05:00
|
|
|
m_speeds().right.to<double>(),
|
2019-10-27 00:33:41 -04:00
|
|
|
targetWheelSpeeds.right.to<double>())) +
|
|
|
|
|
rightFeedforward;
|
|
|
|
|
|
|
|
|
|
m_outputVolts(leftOutput, rightOutput);
|
|
|
|
|
} else {
|
|
|
|
|
m_outputVel(targetWheelSpeeds.left, targetWheelSpeeds.right);
|
|
|
|
|
}
|
|
|
|
|
m_prevSpeeds = targetWheelSpeeds;
|
2020-11-21 13:03:01 -05:00
|
|
|
m_prevTime = curTime;
|
2019-10-27 00:33:41 -04:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void RamseteCommand::End(bool interrupted) {
|
|
|
|
|
m_timer.Stop();
|
2021-03-01 08:06:34 +02:00
|
|
|
|
|
|
|
|
if (interrupted) {
|
|
|
|
|
if (m_usePID) {
|
|
|
|
|
m_outputVolts(0_V, 0_V);
|
|
|
|
|
} else {
|
|
|
|
|
m_outputVel(0_mps, 0_mps);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2019-10-27 00:33:41 -04:00
|
|
|
|
|
|
|
|
bool RamseteCommand::IsFinished() {
|
2020-02-08 13:23:29 -05:00
|
|
|
return m_timer.HasElapsed(m_trajectory.TotalTime());
|
2019-10-27 00:33:41 -04:00
|
|
|
}
|