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 <frc/controller/ProfiledPIDController.h>
|
2019-10-26 12:58:13 -04:00
|
|
|
#include <units/units.h>
|
|
|
|
|
|
|
|
|
|
#include "frc2/command/SubsystemBase.h"
|
|
|
|
|
|
|
|
|
|
namespace frc2 {
|
|
|
|
|
/**
|
|
|
|
|
* A subsystem that uses a ProfiledPIDController to control an output. The
|
|
|
|
|
* controller is run synchronously from the subsystem's periodic() method.
|
|
|
|
|
*
|
|
|
|
|
* @see ProfiledPIDController
|
|
|
|
|
*/
|
2019-11-20 23:11:46 -05:00
|
|
|
template <class Distance>
|
2019-10-26 12:58:13 -04:00
|
|
|
class ProfiledPIDSubsystem : public SubsystemBase {
|
2019-11-20 23:11:46 -05:00
|
|
|
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 ProfiledPIDSubsystem.
|
|
|
|
|
*
|
|
|
|
|
* @param controller the ProfiledPIDController to use
|
|
|
|
|
*/
|
2019-11-20 23:11:46 -05:00
|
|
|
explicit ProfiledPIDSubsystem(frc::ProfiledPIDController<Distance> controller)
|
|
|
|
|
: m_controller{controller} {}
|
2019-10-26 12:58:13 -04:00
|
|
|
|
2019-11-20 23:11:46 -05:00
|
|
|
void Periodic() override {
|
|
|
|
|
if (m_enabled) {
|
|
|
|
|
UseOutput(m_controller.Calculate(GetMeasurement(), GetGoal()),
|
|
|
|
|
m_controller.GetSetpoint());
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Uses the output from the ProfiledPIDController.
|
|
|
|
|
*
|
|
|
|
|
* @param output the output of the ProfiledPIDController
|
2019-11-09 12:16:50 -05:00
|
|
|
* @param setpoint the setpoint state of the ProfiledPIDController, for
|
|
|
|
|
* feedforward
|
2019-10-26 12:58:13 -04:00
|
|
|
*/
|
2019-11-09 12:16:50 -05:00
|
|
|
virtual void UseOutput(double output, State setpoint) = 0;
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the goal used by the ProfiledPIDController.
|
|
|
|
|
*
|
|
|
|
|
* @return the goal to be used by the controller
|
|
|
|
|
*/
|
|
|
|
|
virtual State GetGoal() = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the measurement of the process variable used by the
|
|
|
|
|
* ProfiledPIDController.
|
|
|
|
|
*
|
|
|
|
|
* @return the measurement of the process variable
|
|
|
|
|
*/
|
2019-11-21 00:46:33 -05:00
|
|
|
virtual Distance_t GetMeasurement() = 0;
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enables the PID control. Resets the controller.
|
|
|
|
|
*/
|
2019-11-20 23:11:46 -05:00
|
|
|
virtual void Enable() {
|
|
|
|
|
m_controller.Reset();
|
|
|
|
|
m_enabled = true;
|
|
|
|
|
}
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disables the PID control. Sets output to zero.
|
|
|
|
|
*/
|
2019-11-20 23:11:46 -05:00
|
|
|
virtual void Disable() {
|
|
|
|
|
UseOutput(0, State{Distance_t(0), Velocity_t(0)});
|
|
|
|
|
m_enabled = false;
|
|
|
|
|
}
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the ProfiledPIDController.
|
|
|
|
|
*
|
|
|
|
|
* @return The controller.
|
|
|
|
|
*/
|
2019-11-20 23:11:46 -05: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-10-26 12:58:13 -04:00
|
|
|
bool m_enabled;
|
|
|
|
|
};
|
|
|
|
|
} // namespace frc2
|