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)
|
2019-11-20 22:44:18 -08:00
|
|
|
: 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) {
|
2019-11-26 00:46:47 -05:00
|
|
|
UseOutput(m_controller.Calculate(GetMeasurement(), m_goal),
|
2019-11-20 23:11:46 -05:00
|
|
|
m_controller.GetSetpoint());
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
/**
|
2019-11-26 00:46:47 -05:00
|
|
|
* Sets the goal state for the subsystem.
|
2019-10-26 12:58:13 -04:00
|
|
|
*
|
2019-11-26 00:46:47 -05:00
|
|
|
* @param goal The goal state for the subsystem's motion profile.
|
2019-10-26 12:58:13 -04:00
|
|
|
*/
|
2019-11-26 00:46:47 -05:00
|
|
|
void SetGoal(State goal) { m_goal = goal; }
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
/**
|
2019-11-26 00:46:47 -05:00
|
|
|
* Sets the goal state for the subsystem. Goal velocity assumed to be zero.
|
2019-10-26 12:58:13 -04:00
|
|
|
*
|
2019-11-26 00:46:47 -05:00
|
|
|
* @param goal The goal position for the subsystem's motion profile.
|
2019-10-26 12:58:13 -04:00
|
|
|
*/
|
2019-11-26 00:46:47 -05:00
|
|
|
void SetGoal(Distance_t goal) { m_goal = State{goal, Velocity_t(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() {
|
2019-11-20 22:44:18 -08:00
|
|
|
UseOutput(0, State{Distance_t(0), Velocity_t(0)});
|
|
|
|
|
m_enabled = false;
|
|
|
|
|
}
|
2019-10-26 12:58:13 -04:00
|
|
|
|
2019-11-26 00:46:47 -05:00
|
|
|
/**
|
|
|
|
|
* Returns whether the controller is enabled.
|
|
|
|
|
*
|
|
|
|
|
* @return Whether the controller is enabled.
|
|
|
|
|
*/
|
|
|
|
|
bool IsEnabled() { return m_enabled; }
|
|
|
|
|
|
2019-10-26 12:58:13 -04:00
|
|
|
/**
|
|
|
|
|
* Returns the ProfiledPIDController.
|
|
|
|
|
*
|
|
|
|
|
* @return The controller.
|
|
|
|
|
*/
|
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-11-26 00:46:47 -05:00
|
|
|
bool m_enabled{false};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the measurement of the process variable used by the
|
|
|
|
|
* ProfiledPIDController.
|
|
|
|
|
*
|
|
|
|
|
* @return the measurement of the process variable
|
|
|
|
|
*/
|
|
|
|
|
virtual Distance_t GetMeasurement() = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Uses the output from the ProfiledPIDController.
|
|
|
|
|
*
|
|
|
|
|
* @param output the output of the ProfiledPIDController
|
|
|
|
|
* @param setpoint the setpoint state of the ProfiledPIDController, for
|
|
|
|
|
* feedforward
|
|
|
|
|
*/
|
|
|
|
|
virtual void UseOutput(double output, State setpoint) = 0;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
State m_goal;
|
2019-10-26 12:58:13 -04:00
|
|
|
};
|
|
|
|
|
} // namespace frc2
|