/*----------------------------------------------------------------------------*/ /* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ /*----------------------------------------------------------------------------*/ #pragma once #include 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(trajectory), m_pose(pose), m_kinematics(kinematics), m_controller(xController, yController, thetaController), m_desiredRotation(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(trajectory), m_pose(pose), m_kinematics(kinematics), m_controller(xController, yController, thetaController), m_outputStates(output) { this->AddRequirements(requirements); m_desiredRotation = [&] { return m_trajectory.States().back().pose.Rotation(); }; } 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::ArrayRef requirements) : m_trajectory(trajectory), m_pose(pose), m_kinematics(kinematics), m_controller(xController, yController, thetaController), m_desiredRotation(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::ArrayRef requirements) : m_trajectory(trajectory), m_pose(pose), m_kinematics(kinematics), m_controller(xController, yController, thetaController), m_outputStates(output) { this->AddRequirements(requirements); m_desiredRotation = [&] { return m_trajectory.States().back().pose.Rotation(); }; } template void SwerveControllerCommand::Initialize() { 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