Files
allwpilib/wpilibNewCommands/src/main/native/include/frc2/command/ProfiledPIDSubsystem.h

115 lines
3.2 KiB
C
Raw Normal View History

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
#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
*/
template <class Distance>
2019-10-26 12:58:13 -04:00
class ProfiledPIDSubsystem : public SubsystemBase {
using Distance_t = units::unit_t<Distance>;
using Velocity =
units::compound_unit<Distance, units::inverse<units::seconds>>;
using Velocity_t = units::unit_t<Velocity>;
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
*/
explicit ProfiledPIDSubsystem(frc::ProfiledPIDController<Distance> controller)
: m_controller{controller} {}
2019-10-26 12:58:13 -04:00
void Periodic() override {
if (m_enabled) {
UseOutput(m_controller.Calculate(GetMeasurement(), m_goal),
m_controller.GetSetpoint());
}
}
2019-10-26 12:58:13 -04:00
/**
* Sets the goal state for the subsystem.
2019-10-26 12:58:13 -04:00
*
* @param goal The goal state for the subsystem's motion profile.
2019-10-26 12:58:13 -04:00
*/
void SetGoal(State goal) { m_goal = goal; }
2019-10-26 12:58:13 -04:00
/**
* Sets the goal state for the subsystem. Goal velocity assumed to be zero.
2019-10-26 12:58:13 -04:00
*
* @param goal The goal position for the subsystem's motion profile.
2019-10-26 12:58:13 -04: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.
*/
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.
*/
virtual void Disable() {
UseOutput(0, State{Distance_t(0), Velocity_t(0)});
m_enabled = false;
}
2019-10-26 12:58:13 -04: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.
*/
frc::ProfiledPIDController<Distance>& GetController() { return m_controller; }
2019-10-26 12:58:13 -04:00
protected:
frc::ProfiledPIDController<Distance> m_controller;
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