mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Improve various subsystem APIs (#2130)
Improves the APIs for various prebuilt subsystems (PIDSubsystem, TrapezoidProfileSubsystem, ProfiledPIDSubsystem). Addresses #2128, and also changes the rather cumbersome getSetpoint API to a more intuitive setSetpoint one. Updates examples to match.
This commit is contained in:
@@ -30,25 +30,11 @@ class PIDSubsystem : public SubsystemBase {
|
||||
void Periodic() override;
|
||||
|
||||
/**
|
||||
* Uses the output from the PIDController.
|
||||
* Sets the setpoint for the subsystem.
|
||||
*
|
||||
* @param output the output of the PIDController
|
||||
* @param setpoint the setpoint for the subsystem
|
||||
*/
|
||||
virtual void UseOutput(double output) = 0;
|
||||
|
||||
/**
|
||||
* Returns the reference (setpoint) used by the PIDController.
|
||||
*
|
||||
* @return the reference (setpoint) to be used by the controller
|
||||
*/
|
||||
virtual double GetSetpoint() = 0;
|
||||
|
||||
/**
|
||||
* Returns the measurement of the process variable used by the PIDController.
|
||||
*
|
||||
* @return the measurement of the process variable
|
||||
*/
|
||||
virtual double GetMeasurement() = 0;
|
||||
void SetSetpoint(double setpoint);
|
||||
|
||||
/**
|
||||
* Enables the PID control. Resets the controller.
|
||||
@@ -60,6 +46,13 @@ class PIDSubsystem : public SubsystemBase {
|
||||
*/
|
||||
virtual void Disable();
|
||||
|
||||
/**
|
||||
* Returns whether the controller is enabled.
|
||||
*
|
||||
* @return Whether the controller is enabled.
|
||||
*/
|
||||
bool IsEnabled();
|
||||
|
||||
/**
|
||||
* Returns the PIDController.
|
||||
*
|
||||
@@ -70,5 +63,23 @@ class PIDSubsystem : public SubsystemBase {
|
||||
protected:
|
||||
PIDController m_controller;
|
||||
bool m_enabled;
|
||||
|
||||
/**
|
||||
* Returns the measurement of the process variable used by the PIDController.
|
||||
*
|
||||
* @return the measurement of the process variable
|
||||
*/
|
||||
virtual double GetMeasurement() = 0;
|
||||
|
||||
/**
|
||||
* Uses the output from the PIDController.
|
||||
*
|
||||
* @param output the output of the PIDController
|
||||
* @param setpoint the setpoint of the PIDController (for feedforward)
|
||||
*/
|
||||
virtual void UseOutput(double output, double setpoint) = 0;
|
||||
|
||||
private:
|
||||
double m_setpoint{0};
|
||||
};
|
||||
} // namespace frc2
|
||||
|
||||
@@ -38,34 +38,24 @@ class ProfiledPIDSubsystem : public SubsystemBase {
|
||||
|
||||
void Periodic() override {
|
||||
if (m_enabled) {
|
||||
UseOutput(m_controller.Calculate(GetMeasurement(), GetGoal()),
|
||||
UseOutput(m_controller.Calculate(GetMeasurement(), m_goal),
|
||||
m_controller.GetSetpoint());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the output from the ProfiledPIDController.
|
||||
* Sets the goal state for the subsystem.
|
||||
*
|
||||
* @param output the output of the ProfiledPIDController
|
||||
* @param setpoint the setpoint state of the ProfiledPIDController, for
|
||||
* feedforward
|
||||
* @param goal The goal state for the subsystem's motion profile.
|
||||
*/
|
||||
virtual void UseOutput(double output, State setpoint) = 0;
|
||||
void SetGoal(State goal) { m_goal = goal; }
|
||||
|
||||
/**
|
||||
* Returns the goal used by the ProfiledPIDController.
|
||||
* Sets the goal state for the subsystem. Goal velocity assumed to be zero.
|
||||
*
|
||||
* @return the goal to be used by the controller
|
||||
* @param goal The goal position for the subsystem's motion profile.
|
||||
*/
|
||||
virtual State GetGoal() = 0;
|
||||
|
||||
/**
|
||||
* Returns the measurement of the process variable used by the
|
||||
* ProfiledPIDController.
|
||||
*
|
||||
* @return the measurement of the process variable
|
||||
*/
|
||||
virtual Distance_t GetMeasurement() = 0;
|
||||
void SetGoal(Distance_t goal) { m_goal = State{goal, Velocity_t(0)}; }
|
||||
|
||||
/**
|
||||
* Enables the PID control. Resets the controller.
|
||||
@@ -83,6 +73,13 @@ class ProfiledPIDSubsystem : public SubsystemBase {
|
||||
m_enabled = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the controller is enabled.
|
||||
*
|
||||
* @return Whether the controller is enabled.
|
||||
*/
|
||||
bool IsEnabled() { return m_enabled; }
|
||||
|
||||
/**
|
||||
* Returns the ProfiledPIDController.
|
||||
*
|
||||
@@ -92,6 +89,26 @@ class ProfiledPIDSubsystem : public SubsystemBase {
|
||||
|
||||
protected:
|
||||
frc::ProfiledPIDController<Distance> m_controller;
|
||||
bool m_enabled;
|
||||
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;
|
||||
};
|
||||
} // namespace frc2
|
||||
|
||||
@@ -41,22 +41,29 @@ class TrapezoidProfileSubsystem : public SubsystemBase {
|
||||
units::second_t period = 20_ms)
|
||||
: m_constraints(constraints),
|
||||
m_state{position, Velocity_t(0)},
|
||||
m_goal{position, Velocity_t{0}},
|
||||
m_period(period) {}
|
||||
|
||||
void Periodic() override {
|
||||
auto profile =
|
||||
frc::TrapezoidProfile<Distance>(m_constraints, GetGoal(), m_state);
|
||||
frc::TrapezoidProfile<Distance>(m_constraints, m_goal, m_state);
|
||||
m_state = profile.Calculate(m_period);
|
||||
UseState(m_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Users should override this to return the goal state for the subsystem's
|
||||
* motion profile.
|
||||
* Sets the goal state for the subsystem.
|
||||
*
|
||||
* @return The goal state for the subsystem's motion profile.
|
||||
* @param goal The goal state for the subsystem's motion profile.
|
||||
*/
|
||||
virtual State GetGoal() = 0;
|
||||
void SetGoal(State goal) { m_goal = goal; }
|
||||
|
||||
/**
|
||||
* Sets the goal state for the subsystem. Goal velocity assumed to be zero.
|
||||
*
|
||||
* @param goal The goal position for the subsystem's motion profile.
|
||||
*/
|
||||
void SetGoal(Distance_t goal) { m_goal = State{goal, Velocity_t(0)}; }
|
||||
|
||||
protected:
|
||||
/**
|
||||
@@ -70,6 +77,7 @@ class TrapezoidProfileSubsystem : public SubsystemBase {
|
||||
private:
|
||||
Constraints m_constraints;
|
||||
State m_state;
|
||||
State m_goal;
|
||||
units::second_t m_period;
|
||||
};
|
||||
} // namespace frc2
|
||||
|
||||
Reference in New Issue
Block a user