2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
#include "frc2/command/PIDSubsystem.h"
|
|
|
|
|
|
2020-12-28 10:12:52 -08:00
|
|
|
#include <utility>
|
|
|
|
|
|
2019-08-25 23:55:59 -04:00
|
|
|
using namespace frc2;
|
|
|
|
|
|
2019-12-23 02:09:25 -05:00
|
|
|
PIDSubsystem::PIDSubsystem(PIDController controller, double initialPosition)
|
2020-12-28 10:12:52 -08:00
|
|
|
: m_controller{std::move(controller)} {
|
2019-12-23 02:09:25 -05:00
|
|
|
SetSetpoint(initialPosition);
|
2020-10-19 20:04:18 -07:00
|
|
|
AddChild("PID Controller", &m_controller);
|
2019-12-23 02:09:25 -05:00
|
|
|
}
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
void PIDSubsystem::Periodic() {
|
|
|
|
|
if (m_enabled) {
|
2022-12-03 11:32:46 -08:00
|
|
|
UseOutput(m_controller.Calculate(GetMeasurement()), GetSetpoint());
|
2019-08-25 23:55:59 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void PIDSubsystem::SetSetpoint(double setpoint) {
|
2022-12-03 11:32:46 -08:00
|
|
|
m_controller.SetSetpoint(setpoint);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2019-11-26 00:46:47 -05:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
double PIDSubsystem::GetSetpoint() const {
|
2022-12-03 11:32:46 -08:00
|
|
|
return m_controller.GetSetpoint();
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-06-20 16:30:21 -07:00
|
|
|
|
2019-08-25 23:55:59 -04:00
|
|
|
void PIDSubsystem::Enable() {
|
|
|
|
|
m_controller.Reset();
|
|
|
|
|
m_enabled = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIDSubsystem::Disable() {
|
2019-11-26 00:46:47 -05:00
|
|
|
UseOutput(0, 0);
|
2019-08-25 23:55:59 -04:00
|
|
|
m_enabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool PIDSubsystem::IsEnabled() {
|
|
|
|
|
return m_enabled;
|
|
|
|
|
}
|
2019-11-26 00:46:47 -05:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
PIDController& PIDSubsystem::GetController() {
|
|
|
|
|
return m_controller;
|
|
|
|
|
}
|