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
|
|
|
|
2019-11-05 20:52:49 -08:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-10-27 00:33:41 -04:00
|
|
|
#include <functional>
|
2019-11-05 20:52:49 -08:00
|
|
|
#include <initializer_list>
|
2019-10-27 00:33:41 -04:00
|
|
|
#include <memory>
|
|
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
#include <frc/Timer.h>
|
2019-11-05 20:52:49 -08:00
|
|
|
#include <frc/controller/PIDController.h>
|
|
|
|
|
#include <frc/controller/RamseteController.h>
|
2019-11-09 23:16:42 -05:00
|
|
|
#include <frc/controller/SimpleMotorFeedforward.h>
|
2019-11-05 20:52:49 -08:00
|
|
|
#include <frc/geometry/Pose2d.h>
|
|
|
|
|
#include <frc/kinematics/DifferentialDriveKinematics.h>
|
|
|
|
|
#include <frc/trajectory/Trajectory.h>
|
2020-06-29 22:25:09 -07:00
|
|
|
#include <units/length.h>
|
|
|
|
|
#include <units/voltage.h>
|
2021-06-06 19:51:14 -07:00
|
|
|
#include <wpi/span.h>
|
2019-10-27 00:33:41 -04:00
|
|
|
|
2019-11-05 20:52:49 -08:00
|
|
|
#include "frc2/command/CommandBase.h"
|
|
|
|
|
#include "frc2/command/CommandHelper.h"
|
2019-10-27 00:33:41 -04:00
|
|
|
|
|
|
|
|
namespace frc2 {
|
|
|
|
|
/**
|
|
|
|
|
* A command that uses a RAMSETE controller to follow a trajectory
|
|
|
|
|
* with a differential drive.
|
|
|
|
|
*
|
|
|
|
|
* <p>The command handles trajectory-following, PID calculations, and
|
|
|
|
|
* feedforwards internally. This is intended to be a more-or-less "complete
|
|
|
|
|
* solution" that can be used by teams without a great deal of controls
|
|
|
|
|
* expertise.
|
|
|
|
|
*
|
|
|
|
|
* <p>Advanced teams seeking more flexibility (for example, those who wish to
|
|
|
|
|
* use the onboard PID functionality of a "smart" motor controller) may use the
|
|
|
|
|
* secondary constructor that omits the PID and feedforward functionality,
|
|
|
|
|
* returning only the raw wheel speeds from the RAMSETE controller.
|
|
|
|
|
*
|
2022-01-08 11:11:34 -08:00
|
|
|
* This class is provided by the NewCommands VendorDep
|
|
|
|
|
*
|
2019-10-27 00:33:41 -04:00
|
|
|
* @see RamseteController
|
|
|
|
|
* @see Trajectory
|
|
|
|
|
*/
|
|
|
|
|
class RamseteCommand : public CommandHelper<CommandBase, RamseteCommand> {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a new RamseteCommand that, when executed, will follow the
|
|
|
|
|
* provided trajectory. PID control and feedforward are handled internally,
|
2019-12-02 00:27:02 -05:00
|
|
|
* and outputs are scaled -12 to 12 representing units of volts.
|
2019-10-27 00:33:41 -04:00
|
|
|
*
|
|
|
|
|
* <p>Note: The controller will *not* set the outputVolts to zero upon
|
|
|
|
|
* completion of the path - this is left to the user, since it is not
|
|
|
|
|
* appropriate for paths with nonstationary endstates.
|
|
|
|
|
*
|
|
|
|
|
* @param trajectory The trajectory to follow.
|
|
|
|
|
* @param pose A function that supplies the robot pose - use one of
|
|
|
|
|
* the odometry classes to provide this.
|
|
|
|
|
* @param controller The RAMSETE controller used to follow the
|
|
|
|
|
* trajectory.
|
2019-11-20 22:44:18 -08:00
|
|
|
* @param feedforward A component for calculating the feedforward for the
|
|
|
|
|
* drive.
|
2019-10-27 00:33:41 -04:00
|
|
|
* @param kinematics The kinematics for the robot drivetrain.
|
2019-11-19 01:11:05 -05:00
|
|
|
* @param wheelSpeeds A function that supplies the speeds of the left
|
|
|
|
|
* and right sides of the robot drive.
|
2019-10-27 00:33:41 -04:00
|
|
|
* @param leftController The PIDController for the left side of the robot
|
|
|
|
|
* drive.
|
|
|
|
|
* @param rightController The PIDController for the right side of the robot
|
|
|
|
|
* drive.
|
|
|
|
|
* @param output A function that consumes the computed left and right
|
|
|
|
|
* outputs (in volts) for the robot drive.
|
|
|
|
|
* @param requirements The subsystems to require.
|
|
|
|
|
*/
|
|
|
|
|
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(units::volt_t, units::volt_t)> output,
|
2020-01-01 20:09:17 -08:00
|
|
|
std::initializer_list<Subsystem*> requirements);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a new RamseteCommand that, when executed, will follow the
|
|
|
|
|
* provided trajectory. PID control and feedforward are handled internally,
|
|
|
|
|
* and outputs are scaled -12 to 12 representing units of volts.
|
|
|
|
|
*
|
|
|
|
|
* <p>Note: The controller will *not* set the outputVolts to zero upon
|
|
|
|
|
* completion of the path - this is left to the user, since it is not
|
|
|
|
|
* appropriate for paths with nonstationary endstates.
|
|
|
|
|
*
|
|
|
|
|
* @param trajectory The trajectory to follow.
|
|
|
|
|
* @param pose A function that supplies the robot pose - use one of
|
|
|
|
|
* the odometry classes to provide this.
|
|
|
|
|
* @param controller The RAMSETE controller used to follow the
|
|
|
|
|
* trajectory.
|
|
|
|
|
* @param feedforward A component for calculating the feedforward for the
|
|
|
|
|
* drive.
|
|
|
|
|
* @param kinematics The kinematics for the robot drivetrain.
|
|
|
|
|
* @param wheelSpeeds A function that supplies the speeds of the left
|
|
|
|
|
* and right sides of the robot drive.
|
|
|
|
|
* @param leftController The PIDController for the left side of the robot
|
|
|
|
|
* drive.
|
|
|
|
|
* @param rightController The PIDController for the right side of the robot
|
|
|
|
|
* drive.
|
|
|
|
|
* @param output A function that consumes the computed left and right
|
|
|
|
|
* outputs (in volts) for the robot drive.
|
|
|
|
|
* @param requirements The subsystems to require.
|
|
|
|
|
*/
|
|
|
|
|
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(units::volt_t, units::volt_t)> output,
|
2021-06-06 19:51:14 -07:00
|
|
|
wpi::span<Subsystem* const> requirements = {});
|
2019-10-27 00:33:41 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a new RamseteCommand that, when executed, will follow the
|
|
|
|
|
* provided trajectory. Performs no PID control and calculates no
|
|
|
|
|
* feedforwards; outputs are the raw wheel speeds from the RAMSETE controller,
|
|
|
|
|
* and will need to be converted into a usable form by the user.
|
|
|
|
|
*
|
|
|
|
|
* @param trajectory The trajectory to follow.
|
|
|
|
|
* @param pose A function that supplies the robot pose - use one of
|
|
|
|
|
* the odometry classes to provide this.
|
|
|
|
|
* @param controller The RAMSETE controller used to follow the
|
|
|
|
|
* trajectory.
|
|
|
|
|
* @param kinematics The kinematics for the robot drivetrain.
|
|
|
|
|
* @param output A function that consumes the computed left and right
|
2019-12-05 23:54:32 -08:00
|
|
|
* wheel speeds.
|
2019-10-27 00:33:41 -04:00
|
|
|
* @param requirements The subsystems to require.
|
|
|
|
|
*/
|
|
|
|
|
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-01-01 20:09:17 -08:00
|
|
|
/**
|
|
|
|
|
* Constructs a new RamseteCommand that, when executed, will follow the
|
|
|
|
|
* provided trajectory. Performs no PID control and calculates no
|
|
|
|
|
* feedforwards; outputs are the raw wheel speeds from the RAMSETE controller,
|
|
|
|
|
* and will need to be converted into a usable form by the user.
|
|
|
|
|
*
|
|
|
|
|
* @param trajectory The trajectory to follow.
|
|
|
|
|
* @param pose A function that supplies the robot pose - use one of
|
|
|
|
|
* the odometry classes to provide this.
|
|
|
|
|
* @param controller The RAMSETE controller used to follow the
|
|
|
|
|
* trajectory.
|
|
|
|
|
* @param kinematics The kinematics for the robot drivetrain.
|
|
|
|
|
* @param output A function that consumes the computed left and right
|
|
|
|
|
* wheel speeds.
|
|
|
|
|
* @param requirements The subsystems to require.
|
|
|
|
|
*/
|
|
|
|
|
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,
|
2021-06-06 19:51:14 -07:00
|
|
|
wpi::span<Subsystem* const> requirements = {});
|
2020-01-01 20:09:17 -08:00
|
|
|
|
2019-10-27 00:33:41 -04:00
|
|
|
void Initialize() override;
|
|
|
|
|
|
|
|
|
|
void Execute() override;
|
|
|
|
|
|
|
|
|
|
void End(bool interrupted) override;
|
|
|
|
|
|
|
|
|
|
bool IsFinished() override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
frc::Trajectory m_trajectory;
|
|
|
|
|
std::function<frc::Pose2d()> m_pose;
|
|
|
|
|
frc::RamseteController m_controller;
|
2019-11-09 23:16:42 -05:00
|
|
|
frc::SimpleMotorFeedforward<units::meters> m_feedforward;
|
2019-10-27 00:33:41 -04:00
|
|
|
frc::DifferentialDriveKinematics m_kinematics;
|
2019-11-19 01:11:05 -05:00
|
|
|
std::function<frc::DifferentialDriveWheelSpeeds()> m_speeds;
|
2019-10-27 00:33:41 -04:00
|
|
|
std::unique_ptr<frc2::PIDController> m_leftController;
|
|
|
|
|
std::unique_ptr<frc2::PIDController> m_rightController;
|
|
|
|
|
std::function<void(units::volt_t, units::volt_t)> m_outputVolts;
|
|
|
|
|
std::function<void(units::meters_per_second_t, units::meters_per_second_t)>
|
|
|
|
|
m_outputVel;
|
|
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
frc::Timer m_timer;
|
2019-10-27 00:33:41 -04:00
|
|
|
units::second_t m_prevTime;
|
|
|
|
|
frc::DifferentialDriveWheelSpeeds m_prevSpeeds;
|
2019-11-01 12:26:48 -04:00
|
|
|
bool m_usePID;
|
2019-10-27 00:33:41 -04:00
|
|
|
};
|
|
|
|
|
} // namespace frc2
|