// 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. #pragma once #include #include #include "frc2/command/SwerveControllerCommand.h" namespace frc2 { template SwerveControllerCommand::SwerveControllerCommand( frc::Trajectory trajectory, std::function pose, frc::SwerveDriveKinematics kinematics, frc2::PIDController xController, frc2::PIDController yController, frc::ProfiledPIDController thetaController, std::function desiredRotation, std::function)> output, std::initializer_list requirements) : m_trajectory(std::move(trajectory)), m_pose(std::move(pose)), m_kinematics(kinematics), m_controller(xController, yController, thetaController), m_desiredRotation(std::move(desiredRotation)), m_outputStates(output) { this->AddRequirements(requirements); } template SwerveControllerCommand::SwerveControllerCommand( frc::Trajectory trajectory, std::function pose, frc::SwerveDriveKinematics kinematics, frc2::PIDController xController, frc2::PIDController yController, frc::ProfiledPIDController thetaController, std::function)> output, std::initializer_list requirements) : m_trajectory(std::move(trajectory)), m_pose(std::move(pose)), m_kinematics(kinematics), m_controller(xController, yController, thetaController), m_outputStates(output) { this->AddRequirements(requirements); } template SwerveControllerCommand::SwerveControllerCommand( frc::Trajectory trajectory, std::function pose, frc::SwerveDriveKinematics kinematics, frc2::PIDController xController, frc2::PIDController yController, frc::ProfiledPIDController thetaController, std::function desiredRotation, std::function)> output, wpi::span requirements) : m_trajectory(std::move(trajectory)), m_pose(std::move(pose)), m_kinematics(kinematics), m_controller(xController, yController, thetaController), m_desiredRotation(std::move(desiredRotation)), m_outputStates(output) { this->AddRequirements(requirements); } template SwerveControllerCommand::SwerveControllerCommand( frc::Trajectory trajectory, std::function pose, frc::SwerveDriveKinematics kinematics, frc2::PIDController xController, frc2::PIDController yController, frc::ProfiledPIDController thetaController, std::function)> output, wpi::span requirements) : m_trajectory(std::move(trajectory)), m_pose(std::move(pose)), m_kinematics(kinematics), m_controller(xController, yController, thetaController), m_outputStates(output) { this->AddRequirements(requirements); } template void SwerveControllerCommand::Initialize() { if (m_desiredRotation == nullptr) { m_desiredRotation = [&] { return m_trajectory.States().back().pose.Rotation(); }; } m_timer.Reset(); m_timer.Start(); } template void SwerveControllerCommand::Execute() { auto curTime = units::second_t(m_timer.Get()); auto m_desiredState = m_trajectory.Sample(curTime); auto targetChassisSpeeds = m_controller.Calculate(m_pose(), m_desiredState, m_desiredRotation()); auto targetModuleStates = m_kinematics.ToSwerveModuleStates(targetChassisSpeeds); m_outputStates(targetModuleStates); } template void SwerveControllerCommand::End(bool interrupted) { m_timer.Stop(); } template bool SwerveControllerCommand::IsFinished() { return m_timer.HasElapsed(m_trajectory.TotalTime()); } } // namespace frc2