2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2011-2018 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
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2016-05-25 22:40:15 -07:00
|
|
|
#pragma once
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-09-05 13:55:31 -07:00
|
|
|
#include <memory>
|
2017-12-01 20:50:35 -08:00
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/Twine.h>
|
2016-09-05 13:55:31 -07:00
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "Commands/Subsystem.h"
|
|
|
|
|
#include "PIDController.h"
|
|
|
|
|
#include "PIDOutput.h"
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "PIDSource.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
namespace frc {
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
2017-11-16 00:33:51 -08:00
|
|
|
* This class is designed to handle the case where there is a Subsystem which
|
|
|
|
|
* uses a single PIDController almost constantly (for instance, an elevator
|
|
|
|
|
* which attempts to stay at a constant height).
|
2013-12-15 18:30:16 -05:00
|
|
|
*
|
2017-11-16 00:33:51 -08:00
|
|
|
* It provides some convenience methods to run an internal PIDController. It
|
|
|
|
|
* also allows access to the internal PIDController in order to give total
|
|
|
|
|
* control to the programmer.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
class PIDSubsystem : public Subsystem, public PIDOutput, public PIDSource {
|
|
|
|
|
public:
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Instantiates a PIDSubsystem that will use the given P, I, and D values.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name
|
|
|
|
|
* @param p the proportional value
|
|
|
|
|
* @param i the integral value
|
|
|
|
|
* @param d the derivative value
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
PIDSubsystem(const wpi::Twine& name, double p, double i, double d);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Instantiates a PIDSubsystem that will use the given P, I, and D values.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name
|
|
|
|
|
* @param p the proportional value
|
|
|
|
|
* @param i the integral value
|
|
|
|
|
* @param d the derivative value
|
|
|
|
|
* @param f the feedforward value
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
PIDSubsystem(const wpi::Twine& name, double p, double i, double d, double f);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Instantiates a PIDSubsystem that will use the given P, I, and D values.
|
|
|
|
|
*
|
|
|
|
|
* It will also space the time between PID loop calculations to be equal to
|
|
|
|
|
* the given period.
|
|
|
|
|
*
|
|
|
|
|
* @param name the name
|
|
|
|
|
* @param p the proportional value
|
|
|
|
|
* @param i the integral value
|
|
|
|
|
* @param d the derivative value
|
|
|
|
|
* @param f the feedfoward value
|
|
|
|
|
* @param period the time (in seconds) between calculations
|
|
|
|
|
*/
|
2018-04-29 23:33:19 -07:00
|
|
|
PIDSubsystem(const wpi::Twine& name, double p, double i, double d, double f,
|
2015-06-25 15:07:55 -04:00
|
|
|
double period);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Instantiates a PIDSubsystem that will use the given P, I, and D values.
|
|
|
|
|
*
|
|
|
|
|
* It will use the class name as its name.
|
|
|
|
|
*
|
|
|
|
|
* @param p the proportional value
|
|
|
|
|
* @param i the integral value
|
|
|
|
|
* @param d the derivative value
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
PIDSubsystem(double p, double i, double d);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Instantiates a PIDSubsystem that will use the given P, I, and D values.
|
|
|
|
|
*
|
|
|
|
|
* It will use the class name as its name.
|
|
|
|
|
*
|
|
|
|
|
* @param p the proportional value
|
|
|
|
|
* @param i the integral value
|
|
|
|
|
* @param d the derivative value
|
|
|
|
|
* @param f the feedforward value
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
PIDSubsystem(double p, double i, double d, double f);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Instantiates a PIDSubsystem that will use the given P, I, and D values.
|
|
|
|
|
*
|
|
|
|
|
* It will use the class name as its name. It will also space the time
|
|
|
|
|
* between PID loop calculations to be equal to the given period.
|
|
|
|
|
*
|
|
|
|
|
* @param p the proportional value
|
|
|
|
|
* @param i the integral value
|
|
|
|
|
* @param d the derivative value
|
|
|
|
|
* @param f the feedforward value
|
|
|
|
|
* @param period the time (in seconds) between calculations
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
PIDSubsystem(double p, double i, double d, double f, double period);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
~PIDSubsystem() override = default;
|
2014-08-05 14:02:11 -04:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Enables the internal PIDController.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Enable();
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disables the internal PIDController.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Disable();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// PIDOutput interface
|
2017-08-20 09:46:28 -07:00
|
|
|
void PIDWrite(double output) override;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// PIDSource interface
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2017-08-20 09:46:28 -07:00
|
|
|
double PIDGet() override;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the setpoint to the given value.
|
|
|
|
|
*
|
|
|
|
|
* If SetRange() was called, then the given setpoint will be trimmed to fit
|
|
|
|
|
* within the range.
|
|
|
|
|
*
|
|
|
|
|
* @param setpoint the new setpoint
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void SetSetpoint(double setpoint);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds the given value to the setpoint.
|
|
|
|
|
*
|
|
|
|
|
* If SetRange() was used, then the bounds will still be honored by this
|
|
|
|
|
* method.
|
|
|
|
|
*
|
|
|
|
|
* @param deltaSetpoint the change in the setpoint
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void SetSetpointRelative(double deltaSetpoint);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
void SetInputRange(double minimumInput, double maximumInput);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the maximum and minimum values to write.
|
|
|
|
|
*
|
|
|
|
|
* @param minimumOutput the minimum value to write to the output
|
|
|
|
|
* @param maximumOutput the maximum value to write to the output
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
void SetOutputRange(double minimumOutput, double maximumOutput);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the current setpoint.
|
|
|
|
|
*
|
|
|
|
|
* @return The current setpoint
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double GetSetpoint();
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the current position.
|
|
|
|
|
*
|
|
|
|
|
* @return the current position
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double GetPosition();
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the current rate.
|
|
|
|
|
*
|
|
|
|
|
* @return the current rate
|
|
|
|
|
*/
|
2015-07-14 20:37:52 -07:00
|
|
|
double GetRate();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Set the absolute error which is considered tolerable for use with
|
|
|
|
|
* OnTarget.
|
|
|
|
|
*
|
|
|
|
|
* @param absValue absolute error which is tolerable
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
virtual void SetAbsoluteTolerance(double absValue);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the percentage error which is considered tolerable for use with
|
|
|
|
|
* OnTarget().
|
|
|
|
|
*
|
|
|
|
|
* @param percent percentage error which is tolerable
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
virtual void SetPercentTolerance(double percent);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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().
|
|
|
|
|
* Use OnTarget() in the IsFinished() method of commands that use this
|
|
|
|
|
* subsystem.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* @return True if the error is within the percentage tolerance of the input
|
|
|
|
|
* range
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
virtual bool OnTarget() const;
|
2014-08-05 14:02:11 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
protected:
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Returns the PIDController used by this PIDSubsystem.
|
|
|
|
|
*
|
|
|
|
|
* Use this if you would like to fine tune the PID loop.
|
|
|
|
|
*
|
|
|
|
|
* @return The PIDController used by this PIDSubsystem
|
|
|
|
|
*/
|
2015-07-24 19:19:40 -04:00
|
|
|
std::shared_ptr<PIDController> GetPIDController();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
Revert changes preventing old user code from compiling.
I'm not 100% sure whether we want these, but they are a quick
find and replace to do.
Basically, there are two primary things that we have done
this summer that break existing user code:
-Changing GetInstance() calls to return references instead
of pointers. This forces users to change from doing something
like LiveWindow::GetInstance()->AddSensor() to LiveWindow::GetInstance().AddSensor().
-Making PIDGet() and related calls const, forcing users to change
the function signatures wherever they override them.
The GetInstance() calls don't really matter to me either way,
especially since there are no real ownership issues going on there,
unlike the rest of the smart pointer-related changes.
For the const stuff, it is certainly more correct to mandate that
user PIDGet() functions be const and the such, but at the same time,
I'm not sure that there is any strong need for it, and the errors
generated are not the most helpful. While this wouldn't necessarily
be an issue for more experienced teams or completely new teams (who
don't have any old code to be reusing), it may cause issues for more
average teams who aren't familiar with the intricacies of C++ anything.
Change-Id: I6e7007982069292ea70e6d0fc8ca40203340df1b
2015-07-24 19:19:40 -04:00
|
|
|
virtual double ReturnPIDInput() = 0;
|
2015-06-25 15:07:55 -04:00
|
|
|
virtual void UsePIDOutput(double output) = 0;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
private:
|
2017-11-16 00:33:51 -08:00
|
|
|
// The internal PIDController
|
2015-07-24 19:19:40 -04:00
|
|
|
std::shared_ptr<PIDController> m_controller;
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|
2016-11-01 22:33:12 -07:00
|
|
|
|
|
|
|
|
} // namespace frc
|