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-11-21 19:52:56 -08:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
2020-12-31 11:26:53 -08:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
#include "frc2/command/SwerveControllerCommand.h"
|
2019-11-21 19:52:56 -08:00
|
|
|
|
|
|
|
|
namespace frc2 {
|
|
|
|
|
|
|
|
|
|
template <size_t NumModules>
|
|
|
|
|
SwerveControllerCommand<NumModules>::SwerveControllerCommand(
|
|
|
|
|
frc::Trajectory trajectory, std::function<frc::Pose2d()> pose,
|
|
|
|
|
frc::SwerveDriveKinematics<NumModules> kinematics,
|
|
|
|
|
frc2::PIDController xController, frc2::PIDController yController,
|
|
|
|
|
frc::ProfiledPIDController<units::radians> thetaController,
|
2020-07-12 22:04:21 -04:00
|
|
|
std::function<frc::Rotation2d()> desiredRotation,
|
2019-11-21 19:52:56 -08:00
|
|
|
std::function<void(std::array<frc::SwerveModuleState, NumModules>)> output,
|
|
|
|
|
std::initializer_list<Subsystem*> requirements)
|
2020-12-31 11:26:53 -08:00
|
|
|
: m_trajectory(std::move(trajectory)),
|
|
|
|
|
m_pose(std::move(pose)),
|
2020-01-01 20:09:17 -08:00
|
|
|
m_kinematics(kinematics),
|
2020-07-12 22:04:21 -04:00
|
|
|
m_controller(xController, yController, thetaController),
|
2020-12-31 11:26:53 -08:00
|
|
|
m_desiredRotation(std::move(desiredRotation)),
|
2020-01-01 20:09:17 -08:00
|
|
|
m_outputStates(output) {
|
|
|
|
|
this->AddRequirements(requirements);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <size_t NumModules>
|
|
|
|
|
SwerveControllerCommand<NumModules>::SwerveControllerCommand(
|
|
|
|
|
frc::Trajectory trajectory, std::function<frc::Pose2d()> pose,
|
|
|
|
|
frc::SwerveDriveKinematics<NumModules> kinematics,
|
|
|
|
|
frc2::PIDController xController, frc2::PIDController yController,
|
|
|
|
|
frc::ProfiledPIDController<units::radians> thetaController,
|
|
|
|
|
std::function<void(std::array<frc::SwerveModuleState, NumModules>)> output,
|
2020-07-12 22:04:21 -04:00
|
|
|
std::initializer_list<Subsystem*> requirements)
|
2020-12-31 11:26:53 -08:00
|
|
|
: m_trajectory(std::move(trajectory)),
|
|
|
|
|
m_pose(std::move(pose)),
|
2020-07-12 22:04:21 -04:00
|
|
|
m_kinematics(kinematics),
|
|
|
|
|
m_controller(xController, yController, thetaController),
|
|
|
|
|
m_outputStates(output) {
|
|
|
|
|
this->AddRequirements(requirements);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <size_t NumModules>
|
|
|
|
|
SwerveControllerCommand<NumModules>::SwerveControllerCommand(
|
|
|
|
|
frc::Trajectory trajectory, std::function<frc::Pose2d()> pose,
|
|
|
|
|
frc::SwerveDriveKinematics<NumModules> kinematics,
|
|
|
|
|
frc2::PIDController xController, frc2::PIDController yController,
|
|
|
|
|
frc::ProfiledPIDController<units::radians> thetaController,
|
|
|
|
|
std::function<frc::Rotation2d()> desiredRotation,
|
|
|
|
|
std::function<void(std::array<frc::SwerveModuleState, NumModules>)> output,
|
2021-06-06 19:51:14 -07:00
|
|
|
wpi::span<Subsystem* const> requirements)
|
2020-12-31 11:26:53 -08:00
|
|
|
: m_trajectory(std::move(trajectory)),
|
|
|
|
|
m_pose(std::move(pose)),
|
2019-11-21 19:52:56 -08:00
|
|
|
m_kinematics(kinematics),
|
2020-07-12 22:04:21 -04:00
|
|
|
m_controller(xController, yController, thetaController),
|
2020-12-31 11:26:53 -08:00
|
|
|
m_desiredRotation(std::move(desiredRotation)),
|
2019-11-21 19:52:56 -08:00
|
|
|
m_outputStates(output) {
|
|
|
|
|
this->AddRequirements(requirements);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <size_t NumModules>
|
2020-07-12 22:04:21 -04:00
|
|
|
SwerveControllerCommand<NumModules>::SwerveControllerCommand(
|
|
|
|
|
frc::Trajectory trajectory, std::function<frc::Pose2d()> pose,
|
|
|
|
|
frc::SwerveDriveKinematics<NumModules> kinematics,
|
|
|
|
|
frc2::PIDController xController, frc2::PIDController yController,
|
|
|
|
|
frc::ProfiledPIDController<units::radians> thetaController,
|
|
|
|
|
std::function<void(std::array<frc::SwerveModuleState, NumModules>)> output,
|
2021-06-06 19:51:14 -07:00
|
|
|
wpi::span<Subsystem* const> requirements)
|
2020-12-31 11:26:53 -08:00
|
|
|
: m_trajectory(std::move(trajectory)),
|
|
|
|
|
m_pose(std::move(pose)),
|
2020-07-12 22:04:21 -04:00
|
|
|
m_kinematics(kinematics),
|
|
|
|
|
m_controller(xController, yController, thetaController),
|
|
|
|
|
m_outputStates(output) {
|
|
|
|
|
this->AddRequirements(requirements);
|
|
|
|
|
}
|
2019-11-21 19:52:56 -08:00
|
|
|
|
2020-07-12 22:04:21 -04:00
|
|
|
template <size_t NumModules>
|
|
|
|
|
void SwerveControllerCommand<NumModules>::Initialize() {
|
2021-12-19 20:08:28 -08:00
|
|
|
if (m_desiredRotation == nullptr) {
|
|
|
|
|
m_desiredRotation = [&] {
|
|
|
|
|
return m_trajectory.States().back().pose.Rotation();
|
|
|
|
|
};
|
|
|
|
|
}
|
2019-11-21 19:52:56 -08:00
|
|
|
m_timer.Reset();
|
|
|
|
|
m_timer.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <size_t NumModules>
|
|
|
|
|
void SwerveControllerCommand<NumModules>::Execute() {
|
|
|
|
|
auto curTime = units::second_t(m_timer.Get());
|
|
|
|
|
auto m_desiredState = m_trajectory.Sample(curTime);
|
|
|
|
|
|
|
|
|
|
auto targetChassisSpeeds =
|
2020-07-12 22:04:21 -04:00
|
|
|
m_controller.Calculate(m_pose(), m_desiredState, m_desiredRotation());
|
2019-11-21 19:52:56 -08:00
|
|
|
auto targetModuleStates =
|
|
|
|
|
m_kinematics.ToSwerveModuleStates(targetChassisSpeeds);
|
|
|
|
|
|
|
|
|
|
m_outputStates(targetModuleStates);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <size_t NumModules>
|
|
|
|
|
void SwerveControllerCommand<NumModules>::End(bool interrupted) {
|
|
|
|
|
m_timer.Stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <size_t NumModules>
|
|
|
|
|
bool SwerveControllerCommand<NumModules>::IsFinished() {
|
2020-02-08 13:23:29 -05:00
|
|
|
return m_timer.HasElapsed(m_trajectory.TotalTime());
|
2019-11-21 19:52:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace frc2
|