mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31:44 +00:00
[wpilib] Remove PIDController, PIDOutput, PIDSource
Move them to the old commands vendordep so that PIDCommand and PIDSubsystem continue to work. This also removes Filter and LinearDigitalFilter.
This commit is contained in:
@@ -48,10 +48,6 @@ void AnalogAccelerometer::SetZero(double zero) {
|
||||
m_zeroGVoltage = zero;
|
||||
}
|
||||
|
||||
double AnalogAccelerometer::PIDGet() {
|
||||
return GetAcceleration();
|
||||
}
|
||||
|
||||
void AnalogAccelerometer::InitSendable(SendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("Accelerometer");
|
||||
builder.AddDoubleProperty(
|
||||
|
||||
@@ -254,13 +254,6 @@ double AnalogInput::GetSampleRate() {
|
||||
return sampleRate;
|
||||
}
|
||||
|
||||
double AnalogInput::PIDGet() {
|
||||
if (StatusIsFatal()) {
|
||||
return 0.0;
|
||||
}
|
||||
return GetAverageVoltage();
|
||||
}
|
||||
|
||||
void AnalogInput::SetSimDevice(HAL_SimDeviceHandle device) {
|
||||
HAL_SetAnalogInputSimDevice(m_port, device);
|
||||
}
|
||||
|
||||
@@ -42,10 +42,6 @@ double AnalogPotentiometer::Get() const {
|
||||
m_offset;
|
||||
}
|
||||
|
||||
double AnalogPotentiometer::PIDGet() {
|
||||
return Get();
|
||||
}
|
||||
|
||||
void AnalogPotentiometer::InitSendable(SendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("Analog Input");
|
||||
builder.AddDoubleProperty(
|
||||
|
||||
@@ -213,20 +213,6 @@ int Encoder::GetSamplesToAverage() const {
|
||||
return result;
|
||||
}
|
||||
|
||||
double Encoder::PIDGet() {
|
||||
if (StatusIsFatal()) {
|
||||
return 0.0;
|
||||
}
|
||||
switch (GetPIDSourceType()) {
|
||||
case PIDSourceType::kDisplacement:
|
||||
return GetDistance();
|
||||
case PIDSourceType::kRate:
|
||||
return GetRate();
|
||||
default:
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
void Encoder::SetIndexSource(int channel, Encoder::IndexingType type) {
|
||||
// Force digital input if just given an index
|
||||
m_indexSource = std::make_shared<DigitalInput>(channel);
|
||||
|
||||
@@ -9,17 +9,6 @@
|
||||
|
||||
using namespace frc;
|
||||
|
||||
double GyroBase::PIDGet() {
|
||||
switch (GetPIDSourceType()) {
|
||||
case PIDSourceType::kRate:
|
||||
return GetRate();
|
||||
case PIDSourceType::kDisplacement:
|
||||
return GetAngle();
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void GyroBase::InitSendable(SendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("Gyro");
|
||||
builder.AddDoubleProperty(
|
||||
|
||||
@@ -59,10 +59,6 @@ void NidecBrushless::Enable() {
|
||||
m_disabled = false;
|
||||
}
|
||||
|
||||
void NidecBrushless::PIDWrite(double output) {
|
||||
Set(output);
|
||||
}
|
||||
|
||||
void NidecBrushless::StopMotor() {
|
||||
m_dio.UpdateDutyCycle(0.5);
|
||||
m_pwm.SetDisabled();
|
||||
|
||||
@@ -1,358 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#include "frc/PIDBase.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
|
||||
#include "frc/PIDOutput.h"
|
||||
#include "frc/smartdashboard/SendableBuilder.h"
|
||||
#include "frc/smartdashboard/SendableRegistry.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
template <class T>
|
||||
constexpr const T& clamp(const T& value, const T& low, const T& high) {
|
||||
return std::max(low, std::min(value, high));
|
||||
}
|
||||
|
||||
PIDBase::PIDBase(double Kp, double Ki, double Kd, PIDSource& source,
|
||||
PIDOutput& output)
|
||||
: PIDBase(Kp, Ki, Kd, 0.0, source, output) {}
|
||||
|
||||
PIDBase::PIDBase(double Kp, double Ki, double Kd, double Kf, PIDSource& source,
|
||||
PIDOutput& output) {
|
||||
m_P = Kp;
|
||||
m_I = Ki;
|
||||
m_D = Kd;
|
||||
m_F = Kf;
|
||||
|
||||
m_pidInput = &source;
|
||||
m_filter = LinearFilter<double>::MovingAverage(1);
|
||||
|
||||
m_pidOutput = &output;
|
||||
|
||||
m_setpointTimer.Start();
|
||||
|
||||
static int instances = 0;
|
||||
instances++;
|
||||
HAL_Report(HALUsageReporting::kResourceType_PIDController, instances);
|
||||
SendableRegistry::GetInstance().Add(this, "PIDController", instances);
|
||||
}
|
||||
|
||||
double PIDBase::Get() const {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
return m_result;
|
||||
}
|
||||
|
||||
void PIDBase::SetContinuous(bool continuous) {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_continuous = continuous;
|
||||
}
|
||||
|
||||
void PIDBase::SetInputRange(double minimumInput, double maximumInput) {
|
||||
{
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_minimumInput = minimumInput;
|
||||
m_maximumInput = maximumInput;
|
||||
m_inputRange = maximumInput - minimumInput;
|
||||
}
|
||||
|
||||
SetSetpoint(m_setpoint);
|
||||
}
|
||||
|
||||
void PIDBase::SetOutputRange(double minimumOutput, double maximumOutput) {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_minimumOutput = minimumOutput;
|
||||
m_maximumOutput = maximumOutput;
|
||||
}
|
||||
|
||||
void PIDBase::SetPID(double p, double i, double d) {
|
||||
{
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_P = p;
|
||||
m_I = i;
|
||||
m_D = d;
|
||||
}
|
||||
}
|
||||
|
||||
void PIDBase::SetPID(double p, double i, double d, double f) {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_P = p;
|
||||
m_I = i;
|
||||
m_D = d;
|
||||
m_F = f;
|
||||
}
|
||||
|
||||
void PIDBase::SetP(double p) {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_P = p;
|
||||
}
|
||||
|
||||
void PIDBase::SetI(double i) {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_I = i;
|
||||
}
|
||||
|
||||
void PIDBase::SetD(double d) {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_D = d;
|
||||
}
|
||||
|
||||
void PIDBase::SetF(double f) {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_F = f;
|
||||
}
|
||||
|
||||
double PIDBase::GetP() const {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
return m_P;
|
||||
}
|
||||
|
||||
double PIDBase::GetI() const {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
return m_I;
|
||||
}
|
||||
|
||||
double PIDBase::GetD() const {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
return m_D;
|
||||
}
|
||||
|
||||
double PIDBase::GetF() const {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
return m_F;
|
||||
}
|
||||
|
||||
void PIDBase::SetSetpoint(double setpoint) {
|
||||
{
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
|
||||
if (m_maximumInput > m_minimumInput) {
|
||||
if (setpoint > m_maximumInput) {
|
||||
m_setpoint = m_maximumInput;
|
||||
} else if (setpoint < m_minimumInput) {
|
||||
m_setpoint = m_minimumInput;
|
||||
} else {
|
||||
m_setpoint = setpoint;
|
||||
}
|
||||
} else {
|
||||
m_setpoint = setpoint;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double PIDBase::GetSetpoint() const {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
return m_setpoint;
|
||||
}
|
||||
|
||||
double PIDBase::GetDeltaSetpoint() const {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
return (m_setpoint - m_prevSetpoint) / m_setpointTimer.Get();
|
||||
}
|
||||
|
||||
double PIDBase::GetError() const {
|
||||
double setpoint = GetSetpoint();
|
||||
{
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
return GetContinuousError(setpoint - m_pidInput->PIDGet());
|
||||
}
|
||||
}
|
||||
|
||||
double PIDBase::GetAvgError() const {
|
||||
return GetError();
|
||||
}
|
||||
|
||||
void PIDBase::SetPIDSourceType(PIDSourceType pidSource) {
|
||||
m_pidInput->SetPIDSourceType(pidSource);
|
||||
}
|
||||
|
||||
PIDSourceType PIDBase::GetPIDSourceType() const {
|
||||
return m_pidInput->GetPIDSourceType();
|
||||
}
|
||||
|
||||
void PIDBase::SetTolerance(double percent) {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_toleranceType = kPercentTolerance;
|
||||
m_tolerance = percent;
|
||||
}
|
||||
|
||||
void PIDBase::SetAbsoluteTolerance(double absTolerance) {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_toleranceType = kAbsoluteTolerance;
|
||||
m_tolerance = absTolerance;
|
||||
}
|
||||
|
||||
void PIDBase::SetPercentTolerance(double percent) {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_toleranceType = kPercentTolerance;
|
||||
m_tolerance = percent;
|
||||
}
|
||||
|
||||
void PIDBase::SetToleranceBuffer(int bufLength) {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_filter = LinearFilter<double>::MovingAverage(bufLength);
|
||||
}
|
||||
|
||||
bool PIDBase::OnTarget() const {
|
||||
double error = GetError();
|
||||
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
switch (m_toleranceType) {
|
||||
case kPercentTolerance:
|
||||
return std::fabs(error) < m_tolerance / 100 * m_inputRange;
|
||||
break;
|
||||
case kAbsoluteTolerance:
|
||||
return std::fabs(error) < m_tolerance;
|
||||
break;
|
||||
case kNoTolerance:
|
||||
// TODO: this case needs an error
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void PIDBase::Reset() {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_prevError = 0;
|
||||
m_totalError = 0;
|
||||
m_result = 0;
|
||||
}
|
||||
|
||||
void PIDBase::PIDWrite(double output) {
|
||||
SetSetpoint(output);
|
||||
}
|
||||
|
||||
void PIDBase::InitSendable(SendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("PIDController");
|
||||
builder.SetSafeState([=]() { Reset(); });
|
||||
builder.AddDoubleProperty(
|
||||
"p", [=]() { return GetP(); }, [=](double value) { SetP(value); });
|
||||
builder.AddDoubleProperty(
|
||||
"i", [=]() { return GetI(); }, [=](double value) { SetI(value); });
|
||||
builder.AddDoubleProperty(
|
||||
"d", [=]() { return GetD(); }, [=](double value) { SetD(value); });
|
||||
builder.AddDoubleProperty(
|
||||
"f", [=]() { return GetF(); }, [=](double value) { SetF(value); });
|
||||
builder.AddDoubleProperty(
|
||||
"setpoint", [=]() { return GetSetpoint(); },
|
||||
[=](double value) { SetSetpoint(value); });
|
||||
}
|
||||
|
||||
void PIDBase::Calculate() {
|
||||
if (m_pidInput == nullptr || m_pidOutput == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool enabled;
|
||||
{
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
enabled = m_enabled;
|
||||
}
|
||||
|
||||
if (enabled) {
|
||||
double input;
|
||||
|
||||
// Storage for function inputs
|
||||
PIDSourceType pidSourceType;
|
||||
double P;
|
||||
double I;
|
||||
double D;
|
||||
double feedForward = CalculateFeedForward();
|
||||
double minimumOutput;
|
||||
double maximumOutput;
|
||||
|
||||
// Storage for function input-outputs
|
||||
double prevError;
|
||||
double error;
|
||||
double totalError;
|
||||
|
||||
{
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
|
||||
input = m_filter.Calculate(m_pidInput->PIDGet());
|
||||
|
||||
pidSourceType = m_pidInput->GetPIDSourceType();
|
||||
P = m_P;
|
||||
I = m_I;
|
||||
D = m_D;
|
||||
minimumOutput = m_minimumOutput;
|
||||
maximumOutput = m_maximumOutput;
|
||||
|
||||
prevError = m_prevError;
|
||||
error = GetContinuousError(m_setpoint - input);
|
||||
totalError = m_totalError;
|
||||
}
|
||||
|
||||
// Storage for function outputs
|
||||
double result;
|
||||
|
||||
if (pidSourceType == PIDSourceType::kRate) {
|
||||
if (P != 0) {
|
||||
totalError =
|
||||
clamp(totalError + error, minimumOutput / P, maximumOutput / P);
|
||||
}
|
||||
|
||||
result = D * error + P * totalError + feedForward;
|
||||
} else {
|
||||
if (I != 0) {
|
||||
totalError =
|
||||
clamp(totalError + error, minimumOutput / I, maximumOutput / I);
|
||||
}
|
||||
|
||||
result =
|
||||
P * error + I * totalError + D * (error - prevError) + feedForward;
|
||||
}
|
||||
|
||||
result = clamp(result, minimumOutput, maximumOutput);
|
||||
|
||||
{
|
||||
// Ensures m_enabled check and PIDWrite() call occur atomically
|
||||
std::scoped_lock pidWriteLock(m_pidWriteMutex);
|
||||
std::unique_lock mainLock(m_thisMutex);
|
||||
if (m_enabled) {
|
||||
// Don't block other PIDBase operations on PIDWrite()
|
||||
mainLock.unlock();
|
||||
|
||||
m_pidOutput->PIDWrite(result);
|
||||
}
|
||||
}
|
||||
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_prevError = m_error;
|
||||
m_error = error;
|
||||
m_totalError = totalError;
|
||||
m_result = result;
|
||||
}
|
||||
}
|
||||
|
||||
double PIDBase::CalculateFeedForward() {
|
||||
if (m_pidInput->GetPIDSourceType() == PIDSourceType::kRate) {
|
||||
return m_F * GetSetpoint();
|
||||
} else {
|
||||
double temp = m_F * GetDeltaSetpoint();
|
||||
m_prevSetpoint = m_setpoint;
|
||||
m_setpointTimer.Reset();
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
|
||||
double PIDBase::GetContinuousError(double error) const {
|
||||
if (m_continuous && m_inputRange != 0) {
|
||||
error = std::fmod(error, m_inputRange);
|
||||
if (std::fabs(error) > m_inputRange / 2) {
|
||||
if (error > 0) {
|
||||
return error - m_inputRange;
|
||||
} else {
|
||||
return error + m_inputRange;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#include "frc/PIDController.h"
|
||||
|
||||
#include "frc/Notifier.h"
|
||||
#include "frc/PIDOutput.h"
|
||||
#include "frc/smartdashboard/SendableBuilder.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
PIDController::PIDController(double Kp, double Ki, double Kd, PIDSource* source,
|
||||
PIDOutput* output, double period)
|
||||
: PIDController(Kp, Ki, Kd, 0.0, *source, *output, period) {}
|
||||
|
||||
PIDController::PIDController(double Kp, double Ki, double Kd, double Kf,
|
||||
PIDSource* source, PIDOutput* output,
|
||||
double period)
|
||||
: PIDController(Kp, Ki, Kd, Kf, *source, *output, period) {}
|
||||
|
||||
PIDController::PIDController(double Kp, double Ki, double Kd, PIDSource& source,
|
||||
PIDOutput& output, double period)
|
||||
: PIDController(Kp, Ki, Kd, 0.0, source, output, period) {}
|
||||
|
||||
PIDController::PIDController(double Kp, double Ki, double Kd, double Kf,
|
||||
PIDSource& source, PIDOutput& output,
|
||||
double period)
|
||||
: PIDBase(Kp, Ki, Kd, Kf, source, output) {
|
||||
m_controlLoop = std::make_unique<Notifier>(&PIDController::Calculate, this);
|
||||
m_controlLoop->StartPeriodic(units::second_t(period));
|
||||
}
|
||||
|
||||
PIDController::~PIDController() {
|
||||
// Forcefully stopping the notifier so the callback can successfully run.
|
||||
m_controlLoop->Stop();
|
||||
}
|
||||
|
||||
void PIDController::Enable() {
|
||||
{
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
void PIDController::Disable() {
|
||||
{
|
||||
// Ensures m_enabled modification and PIDWrite() call occur atomically
|
||||
std::scoped_lock pidWriteLock(m_pidWriteMutex);
|
||||
{
|
||||
std::scoped_lock mainLock(m_thisMutex);
|
||||
m_enabled = false;
|
||||
}
|
||||
|
||||
m_pidOutput->PIDWrite(0);
|
||||
}
|
||||
}
|
||||
|
||||
void PIDController::SetEnabled(bool enable) {
|
||||
if (enable) {
|
||||
Enable();
|
||||
} else {
|
||||
Disable();
|
||||
}
|
||||
}
|
||||
|
||||
bool PIDController::IsEnabled() const {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
return m_enabled;
|
||||
}
|
||||
|
||||
void PIDController::Reset() {
|
||||
Disable();
|
||||
|
||||
PIDBase::Reset();
|
||||
}
|
||||
|
||||
void PIDController::InitSendable(SendableBuilder& builder) {
|
||||
PIDBase::InitSendable(builder);
|
||||
builder.AddBooleanProperty(
|
||||
"enabled", [=]() { return IsEnabled(); },
|
||||
[=](bool value) { SetEnabled(value); });
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#include "frc/PIDSource.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
void PIDSource::SetPIDSourceType(PIDSourceType pidSource) {
|
||||
m_pidSource = pidSource;
|
||||
}
|
||||
|
||||
PIDSourceType PIDSource::GetPIDSourceType() const {
|
||||
return m_pidSource;
|
||||
}
|
||||
@@ -42,10 +42,6 @@ int PWMSpeedController::GetChannel() const {
|
||||
return m_pwm.GetChannel();
|
||||
}
|
||||
|
||||
void PWMSpeedController::PIDWrite(double output) {
|
||||
Set(output);
|
||||
}
|
||||
|
||||
PWMSpeedController::PWMSpeedController(const wpi::Twine& name, int channel)
|
||||
: m_pwm(channel, false) {
|
||||
SendableRegistry::GetInstance().AddLW(this, name, channel);
|
||||
|
||||
@@ -60,10 +60,6 @@ void SpeedControllerGroup::StopMotor() {
|
||||
}
|
||||
}
|
||||
|
||||
void SpeedControllerGroup::PIDWrite(double output) {
|
||||
Set(output);
|
||||
}
|
||||
|
||||
void SpeedControllerGroup::InitSendable(SendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("Speed Controller");
|
||||
builder.SetActuator(true);
|
||||
|
||||
@@ -26,47 +26,39 @@ std::atomic<bool> Ultrasonic::m_automaticEnabled{false};
|
||||
std::vector<Ultrasonic*> Ultrasonic::m_sensors;
|
||||
std::thread Ultrasonic::m_thread;
|
||||
|
||||
Ultrasonic::Ultrasonic(int pingChannel, int echoChannel, DistanceUnit units)
|
||||
Ultrasonic::Ultrasonic(int pingChannel, int echoChannel)
|
||||
: m_pingChannel(std::make_shared<DigitalOutput>(pingChannel)),
|
||||
m_echoChannel(std::make_shared<DigitalInput>(echoChannel)),
|
||||
m_counter(m_echoChannel) {
|
||||
m_units = units;
|
||||
Initialize();
|
||||
auto& registry = SendableRegistry::GetInstance();
|
||||
registry.AddChild(this, m_pingChannel.get());
|
||||
registry.AddChild(this, m_echoChannel.get());
|
||||
}
|
||||
|
||||
Ultrasonic::Ultrasonic(DigitalOutput* pingChannel, DigitalInput* echoChannel,
|
||||
DistanceUnit units)
|
||||
Ultrasonic::Ultrasonic(DigitalOutput* pingChannel, DigitalInput* echoChannel)
|
||||
: m_pingChannel(pingChannel, NullDeleter<DigitalOutput>()),
|
||||
m_echoChannel(echoChannel, NullDeleter<DigitalInput>()),
|
||||
m_counter(m_echoChannel) {
|
||||
if (pingChannel == nullptr || echoChannel == nullptr) {
|
||||
wpi_setWPIError(NullParameter);
|
||||
m_units = units;
|
||||
return;
|
||||
}
|
||||
m_units = units;
|
||||
Initialize();
|
||||
}
|
||||
|
||||
Ultrasonic::Ultrasonic(DigitalOutput& pingChannel, DigitalInput& echoChannel,
|
||||
DistanceUnit units)
|
||||
Ultrasonic::Ultrasonic(DigitalOutput& pingChannel, DigitalInput& echoChannel)
|
||||
: m_pingChannel(&pingChannel, NullDeleter<DigitalOutput>()),
|
||||
m_echoChannel(&echoChannel, NullDeleter<DigitalInput>()),
|
||||
m_counter(m_echoChannel) {
|
||||
m_units = units;
|
||||
Initialize();
|
||||
}
|
||||
|
||||
Ultrasonic::Ultrasonic(std::shared_ptr<DigitalOutput> pingChannel,
|
||||
std::shared_ptr<DigitalInput> echoChannel,
|
||||
DistanceUnit units)
|
||||
std::shared_ptr<DigitalInput> echoChannel)
|
||||
: m_pingChannel(std::move(pingChannel)),
|
||||
m_echoChannel(std::move(echoChannel)),
|
||||
m_counter(m_echoChannel) {
|
||||
m_units = units;
|
||||
Initialize();
|
||||
}
|
||||
|
||||
@@ -164,31 +156,6 @@ void Ultrasonic::SetEnabled(bool enable) {
|
||||
m_enabled = enable;
|
||||
}
|
||||
|
||||
void Ultrasonic::SetDistanceUnits(DistanceUnit units) {
|
||||
m_units = units;
|
||||
}
|
||||
|
||||
Ultrasonic::DistanceUnit Ultrasonic::GetDistanceUnits() const {
|
||||
return m_units;
|
||||
}
|
||||
|
||||
double Ultrasonic::PIDGet() {
|
||||
switch (m_units) {
|
||||
case Ultrasonic::kInches:
|
||||
return GetRangeInches();
|
||||
case Ultrasonic::kMilliMeters:
|
||||
return GetRangeMM();
|
||||
default:
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
void Ultrasonic::SetPIDSourceType(PIDSourceType pidSource) {
|
||||
if (wpi_assert(pidSource == PIDSourceType::kDisplacement)) {
|
||||
m_pidSource = pidSource;
|
||||
}
|
||||
}
|
||||
|
||||
void Ultrasonic::InitSendable(SendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("Ultrasonic");
|
||||
builder.AddDoubleProperty(
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#include "frc/filters/Filter.h"
|
||||
|
||||
#include "frc/Base.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
Filter::Filter(PIDSource& source)
|
||||
: m_source(std::shared_ptr<PIDSource>(&source, NullDeleter<PIDSource>())) {}
|
||||
|
||||
Filter::Filter(std::shared_ptr<PIDSource> source)
|
||||
: m_source(std::move(source)) {}
|
||||
|
||||
void Filter::SetPIDSourceType(PIDSourceType pidSource) {
|
||||
m_source->SetPIDSourceType(pidSource);
|
||||
}
|
||||
|
||||
PIDSourceType Filter::GetPIDSourceType() const {
|
||||
return m_source->GetPIDSourceType();
|
||||
}
|
||||
|
||||
double Filter::PIDGetSource() {
|
||||
return m_source->PIDGet();
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#include "frc/filters/LinearDigitalFilter.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
|
||||
using namespace frc;
|
||||
|
||||
LinearDigitalFilter::LinearDigitalFilter(PIDSource& source,
|
||||
wpi::ArrayRef<double> ffGains,
|
||||
wpi::ArrayRef<double> fbGains)
|
||||
: Filter(source),
|
||||
m_inputs(ffGains.size()),
|
||||
m_outputs(fbGains.size()),
|
||||
m_inputGains(ffGains),
|
||||
m_outputGains(fbGains) {
|
||||
static int instances = 0;
|
||||
instances++;
|
||||
HAL_Report(HALUsageReporting::kResourceType_LinearFilter, instances);
|
||||
}
|
||||
|
||||
LinearDigitalFilter::LinearDigitalFilter(PIDSource& source,
|
||||
std::initializer_list<double> ffGains,
|
||||
std::initializer_list<double> fbGains)
|
||||
: LinearDigitalFilter(source,
|
||||
wpi::makeArrayRef(ffGains.begin(), ffGains.end()),
|
||||
wpi::makeArrayRef(fbGains.begin(), fbGains.end())) {}
|
||||
|
||||
LinearDigitalFilter::LinearDigitalFilter(std::shared_ptr<PIDSource> source,
|
||||
wpi::ArrayRef<double> ffGains,
|
||||
wpi::ArrayRef<double> fbGains)
|
||||
: Filter(source),
|
||||
m_inputs(ffGains.size()),
|
||||
m_outputs(fbGains.size()),
|
||||
m_inputGains(ffGains),
|
||||
m_outputGains(fbGains) {
|
||||
static int instances = 0;
|
||||
instances++;
|
||||
HAL_Report(HALUsageReporting::kResourceType_LinearFilter, instances);
|
||||
}
|
||||
|
||||
LinearDigitalFilter::LinearDigitalFilter(std::shared_ptr<PIDSource> source,
|
||||
std::initializer_list<double> ffGains,
|
||||
std::initializer_list<double> fbGains)
|
||||
: LinearDigitalFilter(source,
|
||||
wpi::makeArrayRef(ffGains.begin(), ffGains.end()),
|
||||
wpi::makeArrayRef(fbGains.begin(), fbGains.end())) {}
|
||||
|
||||
LinearDigitalFilter LinearDigitalFilter::SinglePoleIIR(PIDSource& source,
|
||||
double timeConstant,
|
||||
double period) {
|
||||
double gain = std::exp(-period / timeConstant);
|
||||
return LinearDigitalFilter(source, {1.0 - gain}, {-gain});
|
||||
}
|
||||
|
||||
LinearDigitalFilter LinearDigitalFilter::HighPass(PIDSource& source,
|
||||
double timeConstant,
|
||||
double period) {
|
||||
double gain = std::exp(-period / timeConstant);
|
||||
return LinearDigitalFilter(source, {gain, -gain}, {-gain});
|
||||
}
|
||||
|
||||
LinearDigitalFilter LinearDigitalFilter::MovingAverage(PIDSource& source,
|
||||
int taps) {
|
||||
assert(taps > 0);
|
||||
|
||||
std::vector<double> gains(taps, 1.0 / taps);
|
||||
return LinearDigitalFilter(source, gains, {});
|
||||
}
|
||||
|
||||
LinearDigitalFilter LinearDigitalFilter::SinglePoleIIR(
|
||||
std::shared_ptr<PIDSource> source, double timeConstant, double period) {
|
||||
double gain = std::exp(-period / timeConstant);
|
||||
return LinearDigitalFilter(std::move(source), {1.0 - gain}, {-gain});
|
||||
}
|
||||
|
||||
LinearDigitalFilter LinearDigitalFilter::HighPass(
|
||||
std::shared_ptr<PIDSource> source, double timeConstant, double period) {
|
||||
double gain = std::exp(-period / timeConstant);
|
||||
return LinearDigitalFilter(std::move(source), {gain, -gain}, {-gain});
|
||||
}
|
||||
|
||||
LinearDigitalFilter LinearDigitalFilter::MovingAverage(
|
||||
std::shared_ptr<PIDSource> source, int taps) {
|
||||
assert(taps > 0);
|
||||
|
||||
std::vector<double> gains(taps, 1.0 / taps);
|
||||
return LinearDigitalFilter(std::move(source), gains, {});
|
||||
}
|
||||
|
||||
double LinearDigitalFilter::Get() const {
|
||||
double retVal = 0.0;
|
||||
|
||||
// Calculate the new value
|
||||
for (size_t i = 0; i < m_inputGains.size(); i++) {
|
||||
retVal += m_inputs[i] * m_inputGains[i];
|
||||
}
|
||||
for (size_t i = 0; i < m_outputGains.size(); i++) {
|
||||
retVal -= m_outputs[i] * m_outputGains[i];
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
void LinearDigitalFilter::Reset() {
|
||||
m_inputs.reset();
|
||||
m_outputs.reset();
|
||||
}
|
||||
|
||||
double LinearDigitalFilter::PIDGet() {
|
||||
double retVal = 0.0;
|
||||
|
||||
// Rotate the inputs
|
||||
m_inputs.push_front(PIDGetSource());
|
||||
|
||||
// Calculate the new value
|
||||
for (size_t i = 0; i < m_inputGains.size(); i++) {
|
||||
retVal += m_inputs[i] * m_inputGains[i];
|
||||
}
|
||||
for (size_t i = 0; i < m_outputGains.size(); i++) {
|
||||
retVal -= m_outputs[i] * m_outputGains[i];
|
||||
}
|
||||
|
||||
// Rotate the outputs
|
||||
m_outputs.push_front(retVal);
|
||||
|
||||
return retVal;
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#include "frc/interfaces/Potentiometer.h"
|
||||
|
||||
#include "frc/Utility.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
void Potentiometer::SetPIDSourceType(PIDSourceType pidSource) {
|
||||
if (wpi_assert(pidSource == PIDSourceType::kDisplacement)) {
|
||||
m_pidSource = pidSource;
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,6 @@
|
||||
|
||||
#include "frc/AnalogInput.h"
|
||||
#include "frc/ErrorBase.h"
|
||||
#include "frc/PIDSource.h"
|
||||
#include "frc/smartdashboard/Sendable.h"
|
||||
#include "frc/smartdashboard/SendableHelper.h"
|
||||
|
||||
@@ -24,7 +23,6 @@ class SendableBuilder;
|
||||
* calibrated by finding the center value over a period of time.
|
||||
*/
|
||||
class AnalogAccelerometer : public ErrorBase,
|
||||
public PIDSource,
|
||||
public Sendable,
|
||||
public SendableHelper<AnalogAccelerometer> {
|
||||
public:
|
||||
@@ -97,13 +95,6 @@ class AnalogAccelerometer : public ErrorBase,
|
||||
*/
|
||||
void SetZero(double zero);
|
||||
|
||||
/**
|
||||
* Get the Acceleration for the PID Source parent.
|
||||
*
|
||||
* @return The current acceleration in Gs.
|
||||
*/
|
||||
double PIDGet() override;
|
||||
|
||||
void InitSendable(SendableBuilder& builder) override;
|
||||
|
||||
private:
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
#include <hal/Types.h>
|
||||
|
||||
#include "frc/ErrorBase.h"
|
||||
#include "frc/PIDSource.h"
|
||||
#include "frc/smartdashboard/Sendable.h"
|
||||
#include "frc/smartdashboard/SendableHelper.h"
|
||||
|
||||
@@ -32,7 +31,6 @@ class DMASample;
|
||||
* stable values.
|
||||
*/
|
||||
class AnalogInput : public ErrorBase,
|
||||
public PIDSource,
|
||||
public Sendable,
|
||||
public SendableHelper<AnalogInput> {
|
||||
friend class AnalogTrigger;
|
||||
@@ -280,13 +278,6 @@ class AnalogInput : public ErrorBase,
|
||||
*/
|
||||
static double GetSampleRate();
|
||||
|
||||
/**
|
||||
* Get the Average value for the PID Source base object.
|
||||
*
|
||||
* @return The average voltage.
|
||||
*/
|
||||
double PIDGet() override;
|
||||
|
||||
/**
|
||||
* Indicates this input is used by a simulated device.
|
||||
*
|
||||
|
||||
@@ -107,13 +107,6 @@ class AnalogPotentiometer : public ErrorBase,
|
||||
*/
|
||||
double Get() const override;
|
||||
|
||||
/**
|
||||
* Implement the PIDSource interface.
|
||||
*
|
||||
* @return The current reading.
|
||||
*/
|
||||
double PIDGet() override;
|
||||
|
||||
void InitSendable(SendableBuilder& builder) override;
|
||||
|
||||
private:
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
#include "frc/Counter.h"
|
||||
#include "frc/CounterBase.h"
|
||||
#include "frc/ErrorBase.h"
|
||||
#include "frc/PIDSource.h"
|
||||
#include "frc/smartdashboard/Sendable.h"
|
||||
#include "frc/smartdashboard/SendableHelper.h"
|
||||
|
||||
@@ -40,7 +39,6 @@ class DMASample;
|
||||
*/
|
||||
class Encoder : public ErrorBase,
|
||||
public CounterBase,
|
||||
public PIDSource,
|
||||
public Sendable,
|
||||
public SendableHelper<Encoder> {
|
||||
friend class DMA;
|
||||
@@ -314,8 +312,6 @@ class Encoder : public ErrorBase,
|
||||
*/
|
||||
int GetSamplesToAverage() const;
|
||||
|
||||
double PIDGet() override;
|
||||
|
||||
/**
|
||||
* Set the index source for the encoder.
|
||||
*
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "frc/ErrorBase.h"
|
||||
#include "frc/PIDSource.h"
|
||||
#include "frc/interfaces/Gyro.h"
|
||||
#include "frc/smartdashboard/Sendable.h"
|
||||
#include "frc/smartdashboard/SendableHelper.h"
|
||||
@@ -18,7 +17,6 @@ namespace frc {
|
||||
*/
|
||||
class GyroBase : public Gyro,
|
||||
public ErrorBase,
|
||||
public PIDSource,
|
||||
public Sendable,
|
||||
public SendableHelper<GyroBase> {
|
||||
public:
|
||||
@@ -26,15 +24,6 @@ class GyroBase : public Gyro,
|
||||
GyroBase(GyroBase&&) = default;
|
||||
GyroBase& operator=(GyroBase&&) = default;
|
||||
|
||||
// PIDSource interface
|
||||
/**
|
||||
* Get the PIDOutput for the PIDSource base object. Can be set to return
|
||||
* angle or rate using SetPIDSourceType(). Defaults to angle.
|
||||
*
|
||||
* @return The PIDOutput (angle or rate, defaults to angle)
|
||||
*/
|
||||
double PIDGet() override;
|
||||
|
||||
void InitSendable(SendableBuilder& builder) override;
|
||||
};
|
||||
|
||||
|
||||
@@ -73,14 +73,6 @@ class NidecBrushless : public SpeedController,
|
||||
*/
|
||||
void Enable();
|
||||
|
||||
// PIDOutput interface
|
||||
/**
|
||||
* Write out the PID value as seen in the PIDOutput base object.
|
||||
*
|
||||
* @param output Write out the PWM value as was found in the PIDController
|
||||
*/
|
||||
void PIDWrite(double output) override;
|
||||
|
||||
// MotorSafety interface
|
||||
void StopMotor() override;
|
||||
void GetDescription(wpi::raw_ostream& desc) const override;
|
||||
|
||||
@@ -1,407 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <wpi/deprecated.h>
|
||||
#include <wpi/mutex.h>
|
||||
|
||||
#include "frc/Base.h"
|
||||
#include "frc/LinearFilter.h"
|
||||
#include "frc/PIDInterface.h"
|
||||
#include "frc/PIDOutput.h"
|
||||
#include "frc/PIDSource.h"
|
||||
#include "frc/Timer.h"
|
||||
#include "frc/smartdashboard/Sendable.h"
|
||||
#include "frc/smartdashboard/SendableHelper.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class SendableBuilder;
|
||||
|
||||
/**
|
||||
* Class implements a PID Control Loop.
|
||||
*
|
||||
* Creates a separate thread which reads the given PIDSource and takes care of
|
||||
* the integral calculations, as well as writing the given PIDOutput.
|
||||
*
|
||||
* This feedback controller runs in discrete time, so time deltas are not used
|
||||
* in the integral and derivative calculations. Therefore, the sample rate
|
||||
* affects the controller's behavior for a given set of PID constants.
|
||||
*
|
||||
* @deprecated All APIs which use this have been deprecated.
|
||||
*/
|
||||
class PIDBase : public PIDInterface,
|
||||
public PIDOutput,
|
||||
public Sendable,
|
||||
public SendableHelper<PIDBase> {
|
||||
public:
|
||||
/**
|
||||
* Allocate a PID object with the given constants for P, I, D.
|
||||
*
|
||||
* @param Kp the proportional coefficient
|
||||
* @param Ki the integral coefficient
|
||||
* @param Kd the derivative coefficient
|
||||
* @param source The PIDSource object that is used to get values
|
||||
* @param output The PIDOutput object that is set to the output value
|
||||
*/
|
||||
WPI_DEPRECATED("All APIs which use this have been deprecated.")
|
||||
PIDBase(double p, double i, double d, PIDSource& source, PIDOutput& output);
|
||||
|
||||
/**
|
||||
* Allocate a PID object with the given constants for P, I, D.
|
||||
*
|
||||
* @param Kp the proportional coefficient
|
||||
* @param Ki the integral coefficient
|
||||
* @param Kd the derivative coefficient
|
||||
* @param source The PIDSource object that is used to get values
|
||||
* @param output The PIDOutput object that is set to the output value
|
||||
*/
|
||||
WPI_DEPRECATED("All APIs which use this have been deprecated.")
|
||||
PIDBase(double p, double i, double d, double f, PIDSource& source,
|
||||
PIDOutput& output);
|
||||
|
||||
~PIDBase() override = default;
|
||||
|
||||
/**
|
||||
* Return the current PID result.
|
||||
*
|
||||
* This is always centered on zero and constrained the the max and min outs.
|
||||
*
|
||||
* @return the latest calculated output
|
||||
*/
|
||||
virtual double Get() const;
|
||||
|
||||
/**
|
||||
* Set the PID controller to consider the input to be continuous,
|
||||
*
|
||||
* Rather then using the max and min input range as constraints, it considers
|
||||
* them to be the same point and automatically calculates the shortest route
|
||||
* to the setpoint.
|
||||
*
|
||||
* @param continuous true turns on continuous, false turns off continuous
|
||||
*/
|
||||
virtual void SetContinuous(bool continuous = true);
|
||||
|
||||
/**
|
||||
* Sets the maximum and minimum values expected from the input.
|
||||
*
|
||||
* @param minimumInput the minimum value expected from the input
|
||||
* @param maximumInput the maximum value expected from the output
|
||||
*/
|
||||
virtual void SetInputRange(double minimumInput, double maximumInput);
|
||||
|
||||
/**
|
||||
* Sets the minimum and maximum values to write.
|
||||
*
|
||||
* @param minimumOutput the minimum value to write to the output
|
||||
* @param maximumOutput the maximum value to write to the output
|
||||
*/
|
||||
virtual void SetOutputRange(double minimumOutput, double maximumOutput);
|
||||
|
||||
/**
|
||||
* Set the PID Controller gain parameters.
|
||||
*
|
||||
* Set the proportional, integral, and differential coefficients.
|
||||
*
|
||||
* @param p Proportional coefficient
|
||||
* @param i Integral coefficient
|
||||
* @param d Differential coefficient
|
||||
*/
|
||||
void SetPID(double p, double i, double d) override;
|
||||
|
||||
/**
|
||||
* Set the PID Controller gain parameters.
|
||||
*
|
||||
* Set the proportional, integral, and differential coefficients.
|
||||
*
|
||||
* @param p Proportional coefficient
|
||||
* @param i Integral coefficient
|
||||
* @param d Differential coefficient
|
||||
* @param f Feed forward coefficient
|
||||
*/
|
||||
virtual void SetPID(double p, double i, double d, double f);
|
||||
|
||||
/**
|
||||
* Set the Proportional coefficient of the PID controller gain.
|
||||
*
|
||||
* @param p proportional coefficient
|
||||
*/
|
||||
void SetP(double p);
|
||||
|
||||
/**
|
||||
* Set the Integral coefficient of the PID controller gain.
|
||||
*
|
||||
* @param i integral coefficient
|
||||
*/
|
||||
void SetI(double i);
|
||||
|
||||
/**
|
||||
* Set the Differential coefficient of the PID controller gain.
|
||||
*
|
||||
* @param d differential coefficient
|
||||
*/
|
||||
void SetD(double d);
|
||||
|
||||
/**
|
||||
* Get the Feed forward coefficient of the PID controller gain.
|
||||
*
|
||||
* @param f Feed forward coefficient
|
||||
*/
|
||||
void SetF(double f);
|
||||
|
||||
/**
|
||||
* Get the Proportional coefficient.
|
||||
*
|
||||
* @return proportional coefficient
|
||||
*/
|
||||
double GetP() const override;
|
||||
|
||||
/**
|
||||
* Get the Integral coefficient.
|
||||
*
|
||||
* @return integral coefficient
|
||||
*/
|
||||
double GetI() const override;
|
||||
|
||||
/**
|
||||
* Get the Differential coefficient.
|
||||
*
|
||||
* @return differential coefficient
|
||||
*/
|
||||
double GetD() const override;
|
||||
|
||||
/**
|
||||
* Get the Feed forward coefficient.
|
||||
*
|
||||
* @return Feed forward coefficient
|
||||
*/
|
||||
virtual double GetF() const;
|
||||
|
||||
/**
|
||||
* Set the setpoint for the PIDBase.
|
||||
*
|
||||
* @param setpoint the desired setpoint
|
||||
*/
|
||||
void SetSetpoint(double setpoint) override;
|
||||
|
||||
/**
|
||||
* Returns the current setpoint of the PIDBase.
|
||||
*
|
||||
* @return the current setpoint
|
||||
*/
|
||||
double GetSetpoint() const override;
|
||||
|
||||
/**
|
||||
* Returns the change in setpoint over time of the PIDBase.
|
||||
*
|
||||
* @return the change in setpoint over time
|
||||
*/
|
||||
double GetDeltaSetpoint() const;
|
||||
|
||||
/**
|
||||
* Returns the current difference of the input from the setpoint.
|
||||
*
|
||||
* @return the current error
|
||||
*/
|
||||
virtual double GetError() const;
|
||||
|
||||
/**
|
||||
* Returns the current average of the error over the past few iterations.
|
||||
*
|
||||
* You can specify the number of iterations to average with
|
||||
* SetToleranceBuffer() (defaults to 1). This is the same value that is used
|
||||
* for OnTarget().
|
||||
*
|
||||
* @return the average error
|
||||
*/
|
||||
WPI_DEPRECATED("Use a LinearFilter as the input and GetError().")
|
||||
virtual double GetAvgError() const;
|
||||
|
||||
/**
|
||||
* Sets what type of input the PID controller will use.
|
||||
*/
|
||||
virtual void SetPIDSourceType(PIDSourceType pidSource);
|
||||
|
||||
/**
|
||||
* Returns the type of input the PID controller is using.
|
||||
*
|
||||
* @return the PID controller input type
|
||||
*/
|
||||
virtual PIDSourceType GetPIDSourceType() const;
|
||||
|
||||
/**
|
||||
* Set the percentage error which is considered tolerable for use with
|
||||
* OnTarget.
|
||||
*
|
||||
* @param percentage error which is tolerable
|
||||
*/
|
||||
WPI_DEPRECATED("Use SetPercentTolerance() instead.")
|
||||
virtual void SetTolerance(double percent);
|
||||
|
||||
/**
|
||||
* Set the absolute error which is considered tolerable for use with
|
||||
* OnTarget.
|
||||
*
|
||||
* @param percentage error which is tolerable
|
||||
*/
|
||||
virtual void SetAbsoluteTolerance(double absValue);
|
||||
|
||||
/**
|
||||
* Set the percentage error which is considered tolerable for use with
|
||||
* OnTarget.
|
||||
*
|
||||
* @param percentage error which is tolerable
|
||||
*/
|
||||
virtual void SetPercentTolerance(double percentValue);
|
||||
|
||||
/**
|
||||
* Set the number of previous error samples to average for tolerancing. When
|
||||
* determining whether a mechanism is on target, the user may want to use a
|
||||
* rolling average of previous measurements instead of a precise position or
|
||||
* velocity. This is useful for noisy sensors which return a few erroneous
|
||||
* measurements when the mechanism is on target. However, the mechanism will
|
||||
* not register as on target for at least the specified bufLength cycles.
|
||||
*
|
||||
* @param bufLength Number of previous cycles to average. Defaults to 1.
|
||||
*/
|
||||
WPI_DEPRECATED("Use a LinearDigitalFilter as the input.")
|
||||
virtual void SetToleranceBuffer(int buf = 1);
|
||||
|
||||
/**
|
||||
* Return true if the error is within the percentage of the total input range,
|
||||
* determined by SetTolerance. This asssumes that the maximum and minimum
|
||||
* input were set using SetInput.
|
||||
*
|
||||
* Currently this just reports on target as the actual value passes through
|
||||
* the setpoint. Ideally it should be based on being within the tolerance for
|
||||
* some period of time.
|
||||
*
|
||||
* This will return false until at least one input value has been computed.
|
||||
*/
|
||||
virtual bool OnTarget() const;
|
||||
|
||||
/**
|
||||
* Reset the previous error, the integral term, and disable the controller.
|
||||
*/
|
||||
void Reset() override;
|
||||
|
||||
/**
|
||||
* Passes the output directly to SetSetpoint().
|
||||
*
|
||||
* PIDControllers can be nested by passing a PIDController as another
|
||||
* PIDController's output. In that case, the output of the parent controller
|
||||
* becomes the input (i.e., the reference) of the child.
|
||||
*
|
||||
* It is the caller's responsibility to put the data into a valid form for
|
||||
* SetSetpoint().
|
||||
*/
|
||||
void PIDWrite(double output) override;
|
||||
|
||||
void InitSendable(SendableBuilder& builder) override;
|
||||
|
||||
protected:
|
||||
// Is the pid controller enabled
|
||||
bool m_enabled = false;
|
||||
|
||||
mutable wpi::mutex m_thisMutex;
|
||||
|
||||
// Ensures when Disable() is called, PIDWrite() won't run if Calculate()
|
||||
// is already running at that time.
|
||||
mutable wpi::mutex m_pidWriteMutex;
|
||||
|
||||
PIDSource* m_pidInput;
|
||||
PIDOutput* m_pidOutput;
|
||||
Timer m_setpointTimer;
|
||||
|
||||
/**
|
||||
* Read the input, calculate the output accordingly, and write to the output.
|
||||
* This should only be called by the Notifier.
|
||||
*/
|
||||
virtual void Calculate();
|
||||
|
||||
/**
|
||||
* Calculate the feed forward term.
|
||||
*
|
||||
* Both of the provided feed forward calculations are velocity feed forwards.
|
||||
* If a different feed forward calculation is desired, the user can override
|
||||
* this function and provide his or her own. This function does no
|
||||
* synchronization because the PIDBase class only calls it in synchronized
|
||||
* code, so be careful if calling it oneself.
|
||||
*
|
||||
* If a velocity PID controller is being used, the F term should be set to 1
|
||||
* over the maximum setpoint for the output. If a position PID controller is
|
||||
* being used, the F term should be set to 1 over the maximum speed for the
|
||||
* output measured in setpoint units per this controller's update period (see
|
||||
* the default period in this class's constructor).
|
||||
*/
|
||||
virtual double CalculateFeedForward();
|
||||
|
||||
/**
|
||||
* Wraps error around for continuous inputs. The original error is returned if
|
||||
* continuous mode is disabled. This is an unsynchronized function.
|
||||
*
|
||||
* @param error The current error of the PID controller.
|
||||
* @return Error for continuous inputs.
|
||||
*/
|
||||
double GetContinuousError(double error) const;
|
||||
|
||||
private:
|
||||
// Factor for "proportional" control
|
||||
double m_P;
|
||||
|
||||
// Factor for "integral" control
|
||||
double m_I;
|
||||
|
||||
// Factor for "derivative" control
|
||||
double m_D;
|
||||
|
||||
// Factor for "feed forward" control
|
||||
double m_F;
|
||||
|
||||
// |maximum output|
|
||||
double m_maximumOutput = 1.0;
|
||||
|
||||
// |minimum output|
|
||||
double m_minimumOutput = -1.0;
|
||||
|
||||
// Maximum input - limit setpoint to this
|
||||
double m_maximumInput = 0;
|
||||
|
||||
// Minimum input - limit setpoint to this
|
||||
double m_minimumInput = 0;
|
||||
|
||||
// input range - difference between maximum and minimum
|
||||
double m_inputRange = 0;
|
||||
|
||||
// Do the endpoints wrap around? eg. Absolute encoder
|
||||
bool m_continuous = false;
|
||||
|
||||
// The prior error (used to compute velocity)
|
||||
double m_prevError = 0;
|
||||
|
||||
// The sum of the errors for use in the integral calc
|
||||
double m_totalError = 0;
|
||||
|
||||
enum {
|
||||
kAbsoluteTolerance,
|
||||
kPercentTolerance,
|
||||
kNoTolerance
|
||||
} m_toleranceType = kNoTolerance;
|
||||
|
||||
// The percetage or absolute error that is considered on target.
|
||||
double m_tolerance = 0.05;
|
||||
|
||||
double m_setpoint = 0;
|
||||
double m_prevSetpoint = 0;
|
||||
double m_error = 0;
|
||||
double m_result = 0;
|
||||
|
||||
LinearFilter<double> m_filter{{}, {}};
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -1,136 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <wpi/deprecated.h>
|
||||
#include <wpi/mutex.h>
|
||||
|
||||
#include "frc/Base.h"
|
||||
#include "frc/Controller.h"
|
||||
#include "frc/Notifier.h"
|
||||
#include "frc/PIDBase.h"
|
||||
#include "frc/PIDSource.h"
|
||||
#include "frc/Timer.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class PIDOutput;
|
||||
|
||||
/**
|
||||
* Class implements a PID Control Loop.
|
||||
*
|
||||
* Creates a separate thread which reads the given PIDSource and takes care of
|
||||
* the integral calculations, as well as writing the given PIDOutput.
|
||||
*
|
||||
* This feedback controller runs in discrete time, so time deltas are not used
|
||||
* in the integral and derivative calculations. Therefore, the sample rate
|
||||
* affects the controller's behavior for a given set of PID constants.
|
||||
*
|
||||
* @deprecated Use frc2::PIDController class instead.
|
||||
*/
|
||||
class PIDController : public PIDBase, public Controller {
|
||||
public:
|
||||
/**
|
||||
* Allocate a PID object with the given constants for P, I, D.
|
||||
*
|
||||
* @param Kp the proportional coefficient
|
||||
* @param Ki the integral coefficient
|
||||
* @param Kd the derivative coefficient
|
||||
* @param source The PIDSource object that is used to get values
|
||||
* @param output The PIDOutput object that is set to the output value
|
||||
* @param period the loop time for doing calculations in seconds. This
|
||||
* particularly affects calculations of the integral and
|
||||
* differential terms. The default is 0.05 (50ms).
|
||||
*/
|
||||
WPI_DEPRECATED("Use frc2::PIDController class instead.")
|
||||
PIDController(double p, double i, double d, PIDSource* source,
|
||||
PIDOutput* output, double period = 0.05);
|
||||
|
||||
/**
|
||||
* Allocate a PID object with the given constants for P, I, D.
|
||||
*
|
||||
* @param Kp the proportional coefficient
|
||||
* @param Ki the integral coefficient
|
||||
* @param Kd the derivative coefficient
|
||||
* @param source The PIDSource object that is used to get values
|
||||
* @param output The PIDOutput object that is set to the output value
|
||||
* @param period the loop time for doing calculations in seconds. This
|
||||
* particularly affects calculations of the integral and
|
||||
* differential terms. The default is 0.05 (50ms).
|
||||
*/
|
||||
WPI_DEPRECATED("Use frc2::PIDController class instead.")
|
||||
PIDController(double p, double i, double d, double f, PIDSource* source,
|
||||
PIDOutput* output, double period = 0.05);
|
||||
|
||||
/**
|
||||
* Allocate a PID object with the given constants for P, I, D.
|
||||
*
|
||||
* @param Kp the proportional coefficient
|
||||
* @param Ki the integral coefficient
|
||||
* @param Kd the derivative coefficient
|
||||
* @param source The PIDSource object that is used to get values
|
||||
* @param output The PIDOutput object that is set to the output value
|
||||
* @param period the loop time for doing calculations in seconds. This
|
||||
* particularly affects calculations of the integral and
|
||||
* differential terms. The default is 0.05 (50ms).
|
||||
*/
|
||||
WPI_DEPRECATED("Use frc2::PIDController class instead.")
|
||||
PIDController(double p, double i, double d, PIDSource& source,
|
||||
PIDOutput& output, double period = 0.05);
|
||||
|
||||
/**
|
||||
* Allocate a PID object with the given constants for P, I, D.
|
||||
*
|
||||
* @param Kp the proportional coefficient
|
||||
* @param Ki the integral coefficient
|
||||
* @param Kd the derivative coefficient
|
||||
* @param source The PIDSource object that is used to get values
|
||||
* @param output The PIDOutput object that is set to the output value
|
||||
* @param period the loop time for doing calculations in seconds. This
|
||||
* particularly affects calculations of the integral and
|
||||
* differential terms. The default is 0.05 (50ms).
|
||||
*/
|
||||
WPI_DEPRECATED("Use frc2::PIDController class instead.")
|
||||
PIDController(double p, double i, double d, double f, PIDSource& source,
|
||||
PIDOutput& output, double period = 0.05);
|
||||
|
||||
~PIDController() override;
|
||||
|
||||
/**
|
||||
* Begin running the PIDController.
|
||||
*/
|
||||
void Enable() override;
|
||||
|
||||
/**
|
||||
* Stop running the PIDController, this sets the output to zero before
|
||||
* stopping.
|
||||
*/
|
||||
void Disable() override;
|
||||
|
||||
/**
|
||||
* Set the enabled state of the PIDController.
|
||||
*/
|
||||
void SetEnabled(bool enable);
|
||||
|
||||
/**
|
||||
* Return true if PIDController is enabled.
|
||||
*/
|
||||
bool IsEnabled() const;
|
||||
|
||||
/**
|
||||
* Reset the previous error, the integral term, and disable the controller.
|
||||
*/
|
||||
void Reset() override;
|
||||
|
||||
void InitSendable(SendableBuilder& builder) override;
|
||||
|
||||
private:
|
||||
std::unique_ptr<Notifier> m_controlLoop;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -1,34 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <wpi/deprecated.h>
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* Interface for PID Control Loop.
|
||||
*
|
||||
* @deprecated All APIs which use this have been deprecated.
|
||||
*/
|
||||
class PIDInterface {
|
||||
public:
|
||||
WPI_DEPRECATED("All APIs which use this have been deprecated.")
|
||||
PIDInterface() = default;
|
||||
PIDInterface(PIDInterface&&) = default;
|
||||
PIDInterface& operator=(PIDInterface&&) = default;
|
||||
|
||||
virtual void SetPID(double p, double i, double d) = 0;
|
||||
virtual double GetP() const = 0;
|
||||
virtual double GetI() const = 0;
|
||||
virtual double GetD() const = 0;
|
||||
|
||||
virtual void SetSetpoint(double setpoint) = 0;
|
||||
virtual double GetSetpoint() const = 0;
|
||||
|
||||
virtual void Reset() = 0;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -1,22 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "frc/Base.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* PIDOutput interface is a generic output for the PID class.
|
||||
*
|
||||
* PWMs use this class. Users implement this interface to allow for a
|
||||
* PIDController to read directly from the inputs.
|
||||
*/
|
||||
class PIDOutput {
|
||||
public:
|
||||
virtual void PIDWrite(double output) = 0;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -1,36 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace frc {
|
||||
|
||||
enum class PIDSourceType { kDisplacement, kRate };
|
||||
|
||||
/**
|
||||
* PIDSource interface is a generic sensor source for the PID class.
|
||||
*
|
||||
* All sensors that can be used with the PID class will implement the PIDSource
|
||||
* that returns a standard value that will be used in the PID code.
|
||||
*/
|
||||
class PIDSource {
|
||||
public:
|
||||
virtual ~PIDSource() = default;
|
||||
|
||||
/**
|
||||
* Set which parameter you are using as a process control variable.
|
||||
*
|
||||
* @param pidSource An enum to select the parameter.
|
||||
*/
|
||||
virtual void SetPIDSourceType(PIDSourceType pidSource);
|
||||
|
||||
virtual PIDSourceType GetPIDSourceType() const;
|
||||
|
||||
virtual double PIDGet() = 0;
|
||||
|
||||
protected:
|
||||
PIDSourceType m_pidSource = PIDSourceType::kDisplacement;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -60,13 +60,6 @@ class PWMSpeedController : public SpeedController,
|
||||
|
||||
int GetChannel() const;
|
||||
|
||||
/**
|
||||
* Write out the PID value as seen in the PIDOutput base object.
|
||||
*
|
||||
* @param output Write out the PWM value as was found in the PIDController
|
||||
*/
|
||||
void PIDWrite(double output) override;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Constructor for a PWM Speed Controller connected via PWM.
|
||||
|
||||
@@ -6,14 +6,12 @@
|
||||
|
||||
#include <units/voltage.h>
|
||||
|
||||
#include "frc/PIDOutput.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* Interface for speed controlling devices.
|
||||
*/
|
||||
class SpeedController : public PIDOutput {
|
||||
class SpeedController {
|
||||
public:
|
||||
virtual ~SpeedController() = default;
|
||||
|
||||
|
||||
@@ -32,7 +32,6 @@ class SpeedControllerGroup : public Sendable,
|
||||
bool GetInverted() const override;
|
||||
void Disable() override;
|
||||
void StopMotor() override;
|
||||
void PIDWrite(double output) override;
|
||||
|
||||
void InitSendable(SendableBuilder& builder) override;
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#include "frc/Counter.h"
|
||||
#include "frc/ErrorBase.h"
|
||||
#include "frc/PIDSource.h"
|
||||
#include "frc/smartdashboard/Sendable.h"
|
||||
#include "frc/smartdashboard/SendableHelper.h"
|
||||
|
||||
@@ -36,11 +35,8 @@ class DigitalOutput;
|
||||
*/
|
||||
class Ultrasonic : public ErrorBase,
|
||||
public Sendable,
|
||||
public PIDSource,
|
||||
public SendableHelper<Ultrasonic> {
|
||||
public:
|
||||
enum DistanceUnit { kInches = 0, kMilliMeters = 1 };
|
||||
|
||||
/**
|
||||
* Create an instance of the Ultrasonic Sensor.
|
||||
*
|
||||
@@ -51,9 +47,8 @@ class Ultrasonic : public ErrorBase,
|
||||
* @param echoChannel The digital input channel that receives the echo. The
|
||||
* length of time that the echo is high represents the
|
||||
* round trip time of the ping, and the distance.
|
||||
* @param units The units returned in either kInches or kMilliMeters
|
||||
*/
|
||||
Ultrasonic(int pingChannel, int echoChannel, DistanceUnit units = kInches);
|
||||
Ultrasonic(int pingChannel, int echoChannel);
|
||||
|
||||
/**
|
||||
* Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo
|
||||
@@ -63,10 +58,8 @@ class Ultrasonic : public ErrorBase,
|
||||
* ping. Requires a 10uS pulse to start.
|
||||
* @param echoChannel The digital input object that times the return pulse to
|
||||
* determine the range.
|
||||
* @param units The units returned in either kInches or kMilliMeters
|
||||
*/
|
||||
Ultrasonic(DigitalOutput* pingChannel, DigitalInput* echoChannel,
|
||||
DistanceUnit units = kInches);
|
||||
Ultrasonic(DigitalOutput* pingChannel, DigitalInput* echoChannel);
|
||||
|
||||
/**
|
||||
* Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo
|
||||
@@ -76,10 +69,8 @@ class Ultrasonic : public ErrorBase,
|
||||
* ping. Requires a 10uS pulse to start.
|
||||
* @param echoChannel The digital input object that times the return pulse to
|
||||
* determine the range.
|
||||
* @param units The units returned in either kInches or kMilliMeters
|
||||
*/
|
||||
Ultrasonic(DigitalOutput& pingChannel, DigitalInput& echoChannel,
|
||||
DistanceUnit units = kInches);
|
||||
Ultrasonic(DigitalOutput& pingChannel, DigitalInput& echoChannel);
|
||||
|
||||
/**
|
||||
* Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo
|
||||
@@ -89,11 +80,9 @@ class Ultrasonic : public ErrorBase,
|
||||
* ping. Requires a 10uS pulse to start.
|
||||
* @param echoChannel The digital input object that times the return pulse to
|
||||
* determine the range.
|
||||
* @param units The units returned in either kInches or kMilliMeters
|
||||
*/
|
||||
Ultrasonic(std::shared_ptr<DigitalOutput> pingChannel,
|
||||
std::shared_ptr<DigitalInput> echoChannel,
|
||||
DistanceUnit units = kInches);
|
||||
std::shared_ptr<DigitalInput> echoChannel);
|
||||
|
||||
~Ultrasonic() override;
|
||||
|
||||
@@ -156,30 +145,6 @@ class Ultrasonic : public ErrorBase,
|
||||
|
||||
void SetEnabled(bool enable);
|
||||
|
||||
/**
|
||||
* Set the current DistanceUnit that should be used for the PIDSource base
|
||||
* object.
|
||||
*
|
||||
* @param units The DistanceUnit that should be used.
|
||||
*/
|
||||
void SetDistanceUnits(DistanceUnit units);
|
||||
|
||||
/**
|
||||
* Get the current DistanceUnit that is used for the PIDSource base object.
|
||||
*
|
||||
* @return The type of DistanceUnit that is being used.
|
||||
*/
|
||||
DistanceUnit GetDistanceUnits() const;
|
||||
|
||||
/**
|
||||
* Get the range in the current DistanceUnit for the PIDSource base object.
|
||||
*
|
||||
* @return The range in DistanceUnit
|
||||
*/
|
||||
double PIDGet() override;
|
||||
|
||||
void SetPIDSourceType(PIDSourceType pidSource) override;
|
||||
|
||||
void InitSendable(SendableBuilder& builder) override;
|
||||
|
||||
private:
|
||||
@@ -228,7 +193,6 @@ class Ultrasonic : public ErrorBase,
|
||||
std::shared_ptr<DigitalInput> m_echoChannel;
|
||||
bool m_enabled = false;
|
||||
Counter m_counter;
|
||||
DistanceUnit m_units;
|
||||
|
||||
hal::SimDevice m_simDevice;
|
||||
hal::SimBoolean m_simRangeValid;
|
||||
|
||||
@@ -239,3 +239,9 @@ class PIDController : public frc::Sendable,
|
||||
};
|
||||
|
||||
} // namespace frc2
|
||||
|
||||
namespace frc {
|
||||
|
||||
using frc2::PIDController;
|
||||
|
||||
} // namespace frc
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <wpi/deprecated.h>
|
||||
|
||||
#include "frc/PIDSource.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* Interface for filters
|
||||
*
|
||||
* @deprecated only used by the deprecated LinearDigitalFilter
|
||||
*/
|
||||
class Filter : public PIDSource {
|
||||
public:
|
||||
WPI_DEPRECATED("This class is no longer used.")
|
||||
explicit Filter(PIDSource& source);
|
||||
WPI_DEPRECATED("This class is no longer used.")
|
||||
explicit Filter(std::shared_ptr<PIDSource> source);
|
||||
~Filter() override = default;
|
||||
|
||||
Filter(Filter&&) = default;
|
||||
Filter& operator=(Filter&&) = default;
|
||||
|
||||
// PIDSource interface
|
||||
void SetPIDSourceType(PIDSourceType pidSource) override;
|
||||
PIDSourceType GetPIDSourceType() const override;
|
||||
double PIDGet() override = 0;
|
||||
|
||||
/**
|
||||
* Returns the current filter estimate without also inserting new data as
|
||||
* PIDGet() would do.
|
||||
*
|
||||
* @return The current filter estimate
|
||||
*/
|
||||
virtual double Get() const = 0;
|
||||
|
||||
/**
|
||||
* Reset the filter state
|
||||
*/
|
||||
virtual void Reset() = 0;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Calls PIDGet() of source
|
||||
*
|
||||
* @return Current value of source
|
||||
*/
|
||||
double PIDGetSource();
|
||||
|
||||
private:
|
||||
std::shared_ptr<PIDSource> m_source;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -1,223 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <initializer_list>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include <wpi/ArrayRef.h>
|
||||
#include <wpi/circular_buffer.h>
|
||||
#include <wpi/deprecated.h>
|
||||
|
||||
#include "frc/filters/Filter.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* This class implements a linear, digital filter. All types of FIR and IIR
|
||||
* filters are supported. Static factory methods are provided to create commonly
|
||||
* used types of filters.
|
||||
*
|
||||
* Filters are of the form:<br>
|
||||
* y[n] = (b0 * x[n] + b1 * x[n-1] + ... + bP * x[n-P]) -
|
||||
* (a0 * y[n-1] + a2 * y[n-2] + ... + aQ * y[n-Q])
|
||||
*
|
||||
* Where:<br>
|
||||
* y[n] is the output at time "n"<br>
|
||||
* x[n] is the input at time "n"<br>
|
||||
* y[n-1] is the output from the LAST time step ("n-1")<br>
|
||||
* x[n-1] is the input from the LAST time step ("n-1")<br>
|
||||
* b0...bP are the "feedforward" (FIR) gains<br>
|
||||
* a0...aQ are the "feedback" (IIR) gains<br>
|
||||
* IMPORTANT! Note the "-" sign in front of the feedback term! This is a common
|
||||
* convention in signal processing.
|
||||
*
|
||||
* What can linear filters do? Basically, they can filter, or diminish, the
|
||||
* effects of undesirable input frequencies. High frequencies, or rapid changes,
|
||||
* can be indicative of sensor noise or be otherwise undesirable. A "low pass"
|
||||
* filter smooths out the signal, reducing the impact of these high frequency
|
||||
* components. Likewise, a "high pass" filter gets rid of slow-moving signal
|
||||
* components, letting you detect large changes more easily.
|
||||
*
|
||||
* Example FRC applications of filters:
|
||||
* - Getting rid of noise from an analog sensor input (note: the roboRIO's FPGA
|
||||
* can do this faster in hardware)
|
||||
* - Smoothing out joystick input to prevent the wheels from slipping or the
|
||||
* robot from tipping
|
||||
* - Smoothing motor commands so that unnecessary strain isn't put on
|
||||
* electrical or mechanical components
|
||||
* - If you use clever gains, you can make a PID controller out of this class!
|
||||
*
|
||||
* For more on filters, I highly recommend the following articles:<br>
|
||||
* http://en.wikipedia.org/wiki/Linear_filter<br>
|
||||
* http://en.wikipedia.org/wiki/Iir_filter<br>
|
||||
* http://en.wikipedia.org/wiki/Fir_filter<br>
|
||||
*
|
||||
* Note 1: PIDGet() should be called by the user on a known, regular period.
|
||||
* You can set up a Notifier to do this (look at the WPILib PIDController
|
||||
* class), or do it "inline" with code in a periodic function.
|
||||
*
|
||||
* Note 2: For ALL filters, gains are necessarily a function of frequency. If
|
||||
* you make a filter that works well for you at, say, 100Hz, you will most
|
||||
* definitely need to adjust the gains if you then want to run it at 200Hz!
|
||||
* Combining this with Note 1 - the impetus is on YOU as a developer to make
|
||||
* sure PIDGet() gets called at the desired, constant frequency!
|
||||
*
|
||||
* @deprecated Use LinearFilter class instead
|
||||
*/
|
||||
class LinearDigitalFilter : public Filter {
|
||||
public:
|
||||
/**
|
||||
* Create a linear FIR or IIR filter.
|
||||
*
|
||||
* @param source The PIDSource object that is used to get values
|
||||
* @param ffGains The "feed forward" or FIR gains
|
||||
* @param fbGains The "feed back" or IIR gains
|
||||
*/
|
||||
WPI_DEPRECATED("Use LinearFilter class instead.")
|
||||
LinearDigitalFilter(PIDSource& source, wpi::ArrayRef<double> ffGains,
|
||||
wpi::ArrayRef<double> fbGains);
|
||||
|
||||
/**
|
||||
* Create a linear FIR or IIR filter.
|
||||
*
|
||||
* @param source The PIDSource object that is used to get values
|
||||
* @param ffGains The "feed forward" or FIR gains
|
||||
* @param fbGains The "feed back" or IIR gains
|
||||
*/
|
||||
WPI_DEPRECATED("Use LinearFilter class instead.")
|
||||
LinearDigitalFilter(PIDSource& source, std::initializer_list<double> ffGains,
|
||||
std::initializer_list<double> fbGains);
|
||||
|
||||
/**
|
||||
* Create a linear FIR or IIR filter.
|
||||
*
|
||||
* @param source The PIDSource object that is used to get values
|
||||
* @param ffGains The "feed forward" or FIR gains
|
||||
* @param fbGains The "feed back" or IIR gains
|
||||
*/
|
||||
WPI_DEPRECATED("Use LinearFilter class instead.")
|
||||
LinearDigitalFilter(std::shared_ptr<PIDSource> source,
|
||||
wpi::ArrayRef<double> ffGains,
|
||||
wpi::ArrayRef<double> fbGains);
|
||||
|
||||
/**
|
||||
* Create a linear FIR or IIR filter.
|
||||
*
|
||||
* @param source The PIDSource object that is used to get values
|
||||
* @param ffGains The "feed forward" or FIR gains
|
||||
* @param fbGains The "feed back" or IIR gains
|
||||
*/
|
||||
WPI_DEPRECATED("Use LinearFilter class instead.")
|
||||
LinearDigitalFilter(std::shared_ptr<PIDSource> source,
|
||||
std::initializer_list<double> ffGains,
|
||||
std::initializer_list<double> fbGains);
|
||||
|
||||
LinearDigitalFilter(LinearDigitalFilter&&) = default;
|
||||
LinearDigitalFilter& operator=(LinearDigitalFilter&&) = default;
|
||||
|
||||
// Static methods to create commonly used filters
|
||||
/**
|
||||
* Creates a one-pole IIR low-pass filter of the form:<br>
|
||||
* y[n] = (1 - gain) * x[n] + gain * y[n-1]<br>
|
||||
* where gain = e<sup>-dt / T</sup>, T is the time constant in seconds
|
||||
*
|
||||
* This filter is stable for time constants greater than zero.
|
||||
*
|
||||
* @param source The PIDSource object that is used to get values
|
||||
* @param timeConstant The discrete-time time constant in seconds
|
||||
* @param period The period in seconds between samples taken by the user
|
||||
*/
|
||||
static LinearDigitalFilter SinglePoleIIR(PIDSource& source,
|
||||
double timeConstant, double period);
|
||||
|
||||
/**
|
||||
* Creates a first-order high-pass filter of the form:<br>
|
||||
* y[n] = gain * x[n] + (-gain) * x[n-1] + gain * y[n-1]<br>
|
||||
* where gain = e<sup>-dt / T</sup>, T is the time constant in seconds
|
||||
*
|
||||
* This filter is stable for time constants greater than zero.
|
||||
*
|
||||
* @param source The PIDSource object that is used to get values
|
||||
* @param timeConstant The discrete-time time constant in seconds
|
||||
* @param period The period in seconds between samples taken by the user
|
||||
*/
|
||||
static LinearDigitalFilter HighPass(PIDSource& source, double timeConstant,
|
||||
double period);
|
||||
|
||||
/**
|
||||
* Creates a K-tap FIR moving average filter of the form:<br>
|
||||
* y[n] = 1/k * (x[k] + x[k-1] + … + x[0])
|
||||
*
|
||||
* This filter is always stable.
|
||||
*
|
||||
* @param source The PIDSource object that is used to get values
|
||||
* @param taps The number of samples to average over. Higher = smoother but
|
||||
* slower
|
||||
*/
|
||||
static LinearDigitalFilter MovingAverage(PIDSource& source, int taps);
|
||||
|
||||
/**
|
||||
* Creates a one-pole IIR low-pass filter of the form:<br>
|
||||
* y[n] = (1 - gain) * x[n] + gain * y[n-1]<br>
|
||||
* where gain = e<sup>-dt / T</sup>, T is the time constant in seconds
|
||||
*
|
||||
* This filter is stable for time constants greater than zero.
|
||||
*
|
||||
* @param source The PIDSource object that is used to get values
|
||||
* @param timeConstant The discrete-time time constant in seconds
|
||||
* @param period The period in seconds between samples taken by the user
|
||||
*/
|
||||
static LinearDigitalFilter SinglePoleIIR(std::shared_ptr<PIDSource> source,
|
||||
double timeConstant, double period);
|
||||
|
||||
/**
|
||||
* Creates a first-order high-pass filter of the form:<br>
|
||||
* y[n] = gain * x[n] + (-gain) * x[n-1] + gain * y[n-1]<br>
|
||||
* where gain = e<sup>-dt / T</sup>, T is the time constant in seconds
|
||||
*
|
||||
* This filter is stable for time constants greater than zero.
|
||||
*
|
||||
* @param source The PIDSource object that is used to get values
|
||||
* @param timeConstant The discrete-time time constant in seconds
|
||||
* @param period The period in seconds between samples taken by the user
|
||||
*/
|
||||
static LinearDigitalFilter HighPass(std::shared_ptr<PIDSource> source,
|
||||
double timeConstant, double period);
|
||||
|
||||
/**
|
||||
* Creates a K-tap FIR moving average filter of the form:<br>
|
||||
* y[n] = 1/k * (x[k] + x[k-1] + … + x[0])
|
||||
*
|
||||
* This filter is always stable.
|
||||
*
|
||||
* @param source The PIDSource object that is used to get values
|
||||
* @param taps The number of samples to average over. Higher = smoother but
|
||||
* slower
|
||||
*/
|
||||
static LinearDigitalFilter MovingAverage(std::shared_ptr<PIDSource> source,
|
||||
int taps);
|
||||
|
||||
// Filter interface
|
||||
double Get() const override;
|
||||
void Reset() override;
|
||||
|
||||
// PIDSource interface
|
||||
/**
|
||||
* Calculates the next value of the filter
|
||||
*
|
||||
* @return The filtered value at this step
|
||||
*/
|
||||
double PIDGet() override;
|
||||
|
||||
private:
|
||||
wpi::circular_buffer<double> m_inputs;
|
||||
wpi::circular_buffer<double> m_outputs;
|
||||
std::vector<double> m_inputGains;
|
||||
std::vector<double> m_outputGains;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -4,17 +4,15 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "frc/PIDSource.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* Interface for potentiometers.
|
||||
*/
|
||||
class Potentiometer : public PIDSource {
|
||||
class Potentiometer {
|
||||
public:
|
||||
Potentiometer() = default;
|
||||
~Potentiometer() override = default;
|
||||
virtual ~Potentiometer() = default;
|
||||
|
||||
Potentiometer(Potentiometer&&) = default;
|
||||
Potentiometer& operator=(Potentiometer&&) = default;
|
||||
@@ -25,8 +23,6 @@ class Potentiometer : public PIDSource {
|
||||
* @return The current set speed. Value is between -1.0 and 1.0.
|
||||
*/
|
||||
virtual double Get() const = 0;
|
||||
|
||||
void SetPIDSourceType(PIDSourceType pidSource) override;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
|
||||
Reference in New Issue
Block a user