Files
allwpilib/wpilibc/wpilibC++/include/PIDController.h
James Kuszmaul 534ea134a4 artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.

There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
  which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
  for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
  the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
  existing.
-This doesn't fall directly under raw pointer stuff,
  but move syntax and rvalue references could be introduced
  in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
  in wpilibC++). Someone should figure out a more
  permanent fix (eg, just renaming them), then doing
  what I did (making a new namespace for one of them,
  essentially the same as renaming it).

A few other things:
-I created a NullDeleter class which is marked as deprecated.
  What this does is it can be passed as the deleter to a
  std::shared_ptr so that when you are converting raw pointers
  to shared_ptrs the shared_ptr doesn't do any deletion if
  someone else owns the raw pointer. This should only be
  used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
  emit errors when deprecated functions called deprecated
  functions. Unfortunately, gradle doesn't appear to be
  actually printing out gcc warnigns for some reason.
  The best way I have found to fix this is to patch
  the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
  so that a deprecated function calling a deprecated
  function is fine but a non-deprecated function calling
  a deprecated function will throw a warning (which we
  then elevate with -Werror). I believe that clang
  deals with this properly, although I have not
  tried it myself.

Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-07-20 13:18:29 -04:00

117 lines
4.2 KiB
C++

/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved.
*/
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#pragma once
#include "Base.h"
#include "Controller.h"
#include "LiveWindow/LiveWindow.h"
#include "PIDInterface.h"
#include "HAL/cpp/priority_mutex.h"
#include "Notifier.h"
#include <memory>
class PIDOutput;
class PIDSource;
/**
* 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
*/
class PIDController : public LiveWindowSendable,
public PIDInterface,
public ITableListener {
public:
PIDController(float p, float i, float d, PIDSource *source, PIDOutput *output,
float period = 0.05);
PIDController(float p, float i, float d, float f, PIDSource *source,
PIDOutput *output, float period = 0.05);
virtual ~PIDController() = default;
virtual float Get() const;
virtual void SetContinuous(bool continuous = true);
virtual void SetInputRange(float minimumInput, float maximumInput);
virtual void SetOutputRange(float minimumOutput, float maximumOutput);
virtual void SetPID(double p, double i, double d) override;
virtual void SetPID(double p, double i, double d, double f);
virtual double GetP() const override;
virtual double GetI() const override;
virtual double GetD() const override;
virtual double GetF() const;
virtual void SetSetpoint(float setpoint) override;
virtual double GetSetpoint() const override;
virtual float GetError() const;
virtual void SetTolerance(float percent);
virtual void SetAbsoluteTolerance(float absValue);
virtual void SetPercentTolerance(float percentValue);
virtual bool OnTarget() const;
virtual void Enable() override;
virtual void Disable() override;
virtual bool IsEnabled() const override;
virtual void Reset() override;
virtual void InitTable(::std::shared_ptr<ITable> table) override;
private:
float m_P; // factor for "proportional" control
float m_I; // factor for "integral" control
float m_D; // factor for "derivative" control
float m_F; // factor for "feed forward" control
float m_maximumOutput = 1.0; // |maximum output|
float m_minimumOutput = -1.0; // |minimum output|
float m_maximumInput = 0; // maximum input - limit setpoint to this
float m_minimumInput = 0; // minimum input - limit setpoint to this
bool m_continuous = false; // do the endpoints wrap around? eg. Absolute encoder
bool m_enabled = false; // is the pid controller enabled
bool m_destruct; // should the calculate thread stop running
float m_prevError = 0; // the prior sensor input (used to compute velocity)
double m_totalError = 0; // the sum of the errors for use in the integral calc
enum {
kAbsoluteTolerance,
kPercentTolerance,
kNoTolerance
} m_toleranceType = kNoTolerance;
float m_tolerance = 0.05; // the percetage or absolute error that is considered on
// target
float m_setpoint = 0;
float m_error;
float m_result = 0;
float m_period;
mutable priority_mutex m_mutex;
PIDSource *m_pidInput;
PIDOutput *m_pidOutput;
std::unique_ptr<Notifier> m_controlLoop;
void Initialize(float p, float i, float d, float f, PIDSource *source,
PIDOutput *output, float period = 0.05);
static void CallCalculate(void *controller);
virtual ::std::shared_ptr<ITable> GetTable() const override;
virtual std::string GetSmartDashboardType() const override;
virtual void ValueChanged(::std::shared_ptr<ITable> source, const std::string &key,
EntryValue value, bool isNew) override;
virtual void UpdateTable() override;
virtual void StartLiveWindowMode() override;
virtual void StopLiveWindowMode() override;
protected:
::std::shared_ptr<ITable> m_table = nullptr;
virtual void Calculate();
DISALLOW_COPY_AND_ASSIGN(PIDController);
};