2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-07-07 19:17:14 -07:00
|
|
|
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/PIDController.h"
|
2016-07-14 13:51:32 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Notifier.h"
|
|
|
|
|
#include "frc/PIDOutput.h"
|
|
|
|
|
#include "frc/smartdashboard/SendableBuilder.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
PIDController::PIDController(double Kp, double Ki, double Kd, PIDSource* source,
|
|
|
|
|
PIDOutput* output, double period)
|
2018-05-16 19:51:37 -07:00
|
|
|
: PIDController(Kp, Ki, Kd, 0.0, *source, *output, period) {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
PIDController::PIDController(double Kp, double Ki, double Kd, double Kf,
|
2016-05-20 17:30:37 -07:00
|
|
|
PIDSource* source, PIDOutput* output,
|
2017-12-04 23:28:33 -08:00
|
|
|
double period)
|
2018-05-16 19:51:37 -07:00
|
|
|
: PIDController(Kp, Ki, Kd, Kf, *source, *output, period) {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-11-19 19:06:00 -08:00
|
|
|
PIDController::PIDController(double Kp, double Ki, double Kd, PIDSource& source,
|
|
|
|
|
PIDOutput& output, double period)
|
2018-05-16 19:51:37 -07:00
|
|
|
: PIDController(Kp, Ki, Kd, 0.0, source, output, period) {}
|
2017-11-19 19:06:00 -08:00
|
|
|
|
|
|
|
|
PIDController::PIDController(double Kp, double Ki, double Kd, double Kf,
|
|
|
|
|
PIDSource& source, PIDOutput& output,
|
|
|
|
|
double period)
|
2018-05-16 19:51:37 -07:00
|
|
|
: PIDBase(Kp, Ki, Kd, Kf, source, output) {
|
|
|
|
|
m_controlLoop = std::make_unique<Notifier>(&PIDController::Calculate, this);
|
|
|
|
|
m_controlLoop->StartPeriodic(period);
|
|
|
|
|
}
|
2017-11-19 19:06:00 -08:00
|
|
|
|
2015-08-13 23:17:19 -07:00
|
|
|
PIDController::~PIDController() {
|
2018-05-16 19:51:37 -07:00
|
|
|
// Forcefully stopping the notifier so the callback can successfully run.
|
2016-05-22 23:24:10 -07:00
|
|
|
m_controlLoop->Stop();
|
2015-08-13 23:17:19 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void PIDController::Enable() {
|
2015-06-25 01:54:20 -07:00
|
|
|
{
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2015-06-25 01:54:20 -07:00
|
|
|
m_enabled = true;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void PIDController::Disable() {
|
2015-06-25 01:54:20 -07:00
|
|
|
{
|
2017-11-24 00:55:35 -08:00
|
|
|
// Ensures m_enabled modification and PIDWrite() call occur atomically
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock pidWriteLock(m_pidWriteMutex);
|
2017-11-24 00:55:35 -08:00
|
|
|
{
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock mainLock(m_thisMutex);
|
2017-11-24 00:55:35 -08:00
|
|
|
m_enabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
m_pidOutput->PIDWrite(0);
|
|
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void PIDController::SetEnabled(bool enable) {
|
|
|
|
|
if (enable) {
|
|
|
|
|
Enable();
|
|
|
|
|
} else {
|
|
|
|
|
Disable();
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
bool PIDController::IsEnabled() const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2015-06-25 01:54:20 -07:00
|
|
|
return m_enabled;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void PIDController::Reset() {
|
|
|
|
|
Disable();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-16 19:51:37 -07:00
|
|
|
PIDBase::Reset();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void PIDController::InitSendable(SendableBuilder& builder) {
|
2018-05-16 19:51:37 -07:00
|
|
|
PIDBase::InitSendable(builder);
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.AddBooleanProperty("enabled", [=]() { return IsEnabled(); },
|
|
|
|
|
[=](bool value) { SetEnabled(value); });
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|