2019-10-26 12:58:13 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) 2019 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
|
|
|
|
|
|
2019-11-05 20:52:49 -08:00
|
|
|
#include <functional>
|
|
|
|
|
#include <initializer_list>
|
2019-11-20 22:44:18 -08:00
|
|
|
#include <utility>
|
2019-11-05 20:52:49 -08:00
|
|
|
|
|
|
|
|
#include <frc/controller/ProfiledPIDController.h>
|
2019-10-26 12:58:13 -04:00
|
|
|
#include <units/units.h>
|
|
|
|
|
|
|
|
|
|
#include "frc2/command/CommandBase.h"
|
|
|
|
|
#include "frc2/command/CommandHelper.h"
|
|
|
|
|
|
|
|
|
|
namespace frc2 {
|
|
|
|
|
/**
|
|
|
|
|
* A command that controls an output with a ProfiledPIDController. Runs forever
|
|
|
|
|
* by default - to add exit conditions and/or other behavior, subclass this
|
|
|
|
|
* class. The controller calculation and output are performed synchronously in
|
|
|
|
|
* the command's execute() method.
|
|
|
|
|
*
|
2019-11-20 23:11:46 -05:00
|
|
|
* @see ProfiledPIDController<Distance>
|
2019-10-26 12:58:13 -04:00
|
|
|
*/
|
2019-11-20 23:11:46 -05:00
|
|
|
template <class Distance>
|
2019-10-26 12:58:13 -04:00
|
|
|
class ProfiledPIDCommand
|
2019-11-20 23:11:46 -05:00
|
|
|
: public CommandHelper<CommandBase, ProfiledPIDCommand<Distance>> {
|
|
|
|
|
using Distance_t = units::unit_t<Distance>;
|
|
|
|
|
using Velocity =
|
|
|
|
|
units::compound_unit<Distance, units::inverse<units::seconds>>;
|
|
|
|
|
using Velocity_t = units::unit_t<Velocity>;
|
2019-11-21 00:46:33 -05:00
|
|
|
using State = typename frc::TrapezoidProfile<Distance>::State;
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new PIDCommand, which controls the given output with a
|
|
|
|
|
* ProfiledPIDController.
|
|
|
|
|
*
|
|
|
|
|
* @param controller the controller that controls the output.
|
|
|
|
|
* @param measurementSource the measurement of the process variable
|
|
|
|
|
* @param goalSource the controller's goal
|
|
|
|
|
* @param useOutput the controller's output
|
|
|
|
|
* @param requirements the subsystems required by this command
|
|
|
|
|
*/
|
2019-11-20 23:11:46 -05:00
|
|
|
ProfiledPIDCommand(frc::ProfiledPIDController<Distance> controller,
|
2019-12-04 23:40:37 -05:00
|
|
|
std::function<Distance_t()> measurementSource,
|
2019-10-26 12:58:13 -04:00
|
|
|
std::function<State()> goalSource,
|
|
|
|
|
std::function<void(double, State)> useOutput,
|
2019-11-20 23:11:46 -05:00
|
|
|
std::initializer_list<Subsystem*> requirements = {})
|
2019-11-20 22:44:18 -08:00
|
|
|
: m_controller{controller},
|
|
|
|
|
m_measurement{std::move(measurementSource)},
|
|
|
|
|
m_goal{std::move(goalSource)},
|
|
|
|
|
m_useOutput{std::move(useOutput)} {
|
2019-12-04 23:40:37 -05:00
|
|
|
this->AddRequirements(requirements);
|
2019-11-20 23:11:46 -05:00
|
|
|
}
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new PIDCommand, which controls the given output with a
|
|
|
|
|
* ProfiledPIDController.
|
|
|
|
|
*
|
|
|
|
|
* @param controller the controller that controls the output.
|
|
|
|
|
* @param measurementSource the measurement of the process variable
|
|
|
|
|
* @param goalSource the controller's goal
|
|
|
|
|
* @param useOutput the controller's output
|
|
|
|
|
* @param requirements the subsystems required by this command
|
|
|
|
|
*/
|
2019-11-20 23:11:46 -05:00
|
|
|
ProfiledPIDCommand(frc::ProfiledPIDController<Distance> controller,
|
2019-12-04 23:40:37 -05:00
|
|
|
std::function<Distance_t()> measurementSource,
|
|
|
|
|
std::function<Distance_t()> goalSource,
|
2019-10-26 12:58:13 -04:00
|
|
|
std::function<void(double, State)> useOutput,
|
2019-11-20 23:11:46 -05:00
|
|
|
std::initializer_list<Subsystem*> requirements)
|
2019-11-20 22:44:18 -08:00
|
|
|
: ProfiledPIDCommand(controller, measurementSource,
|
|
|
|
|
[&goalSource]() {
|
2019-12-04 23:40:37 -05:00
|
|
|
return State{goalSource(), Velocity_t{0}};
|
2019-11-20 22:44:18 -08:00
|
|
|
},
|
|
|
|
|
useOutput, requirements) {}
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new PIDCommand, which controls the given output with a
|
|
|
|
|
* ProfiledPIDController with a constant goal.
|
|
|
|
|
*
|
|
|
|
|
* @param controller the controller that controls the output.
|
|
|
|
|
* @param measurementSource the measurement of the process variable
|
|
|
|
|
* @param goal the controller's goal
|
|
|
|
|
* @param useOutput the controller's output
|
|
|
|
|
* @param requirements the subsystems required by this command
|
|
|
|
|
*/
|
2019-11-20 23:11:46 -05:00
|
|
|
ProfiledPIDCommand(frc::ProfiledPIDController<Distance> controller,
|
2019-12-04 23:40:37 -05:00
|
|
|
std::function<Distance_t()> measurementSource, State goal,
|
|
|
|
|
std::function<void(double, State)> useOutput,
|
2019-11-20 23:11:46 -05:00
|
|
|
std::initializer_list<Subsystem*> requirements)
|
2019-11-20 22:44:18 -08:00
|
|
|
: ProfiledPIDCommand(controller, measurementSource,
|
|
|
|
|
[goal] { return goal; }, useOutput, requirements) {}
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new PIDCommand, which controls the given output with a
|
|
|
|
|
* ProfiledPIDController with a constant goal.
|
|
|
|
|
*
|
|
|
|
|
* @param controller the controller that controls the output.
|
|
|
|
|
* @param measurementSource the measurement of the process variable
|
|
|
|
|
* @param goal the controller's goal
|
|
|
|
|
* @param useOutput the controller's output
|
|
|
|
|
* @param requirements the subsystems required by this command
|
|
|
|
|
*/
|
2019-11-20 23:11:46 -05:00
|
|
|
ProfiledPIDCommand(frc::ProfiledPIDController<Distance> controller,
|
2019-12-04 23:40:37 -05:00
|
|
|
std::function<Distance_t()> measurementSource,
|
|
|
|
|
Distance_t goal,
|
2019-10-26 12:58:13 -04:00
|
|
|
std::function<void(double, State)> useOutput,
|
2019-11-20 23:11:46 -05:00
|
|
|
std::initializer_list<Subsystem*> requirements)
|
2019-11-20 22:44:18 -08:00
|
|
|
: ProfiledPIDCommand(controller, measurementSource,
|
|
|
|
|
[goal] { return goal; }, useOutput, requirements) {}
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
ProfiledPIDCommand(ProfiledPIDCommand&& other) = default;
|
|
|
|
|
|
|
|
|
|
ProfiledPIDCommand(const ProfiledPIDCommand& other) = default;
|
|
|
|
|
|
2019-11-20 23:11:46 -05:00
|
|
|
void Initialize() override { m_controller.Reset(); }
|
2019-10-26 12:58:13 -04:00
|
|
|
|
2019-11-20 23:11:46 -05:00
|
|
|
void Execute() override {
|
|
|
|
|
m_useOutput(m_controller.Calculate(m_measurement(), m_goal()),
|
|
|
|
|
m_controller.GetSetpoint());
|
|
|
|
|
}
|
2019-10-26 12:58:13 -04:00
|
|
|
|
2019-11-20 23:11:46 -05:00
|
|
|
void End(bool interrupted) override {
|
|
|
|
|
m_useOutput(0, State{Distance_t(0), Velocity_t(0)});
|
|
|
|
|
}
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the ProfiledPIDController used by the command.
|
|
|
|
|
*
|
|
|
|
|
* @return The ProfiledPIDController
|
|
|
|
|
*/
|
2019-11-20 22:44:18 -08:00
|
|
|
frc::ProfiledPIDController<Distance>& GetController() { return m_controller; }
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
protected:
|
2019-11-20 23:11:46 -05:00
|
|
|
frc::ProfiledPIDController<Distance> m_controller;
|
2019-12-04 23:40:37 -05:00
|
|
|
std::function<Distance_t()> m_measurement;
|
2019-10-26 12:58:13 -04:00
|
|
|
std::function<State()> m_goal;
|
|
|
|
|
std::function<void(double, State)> m_useOutput;
|
|
|
|
|
};
|
|
|
|
|
} // namespace frc2
|