Thanks to visit codestin.com
Credit goes to github.com

Skip to content

RobTillaart/PID_RT

Repository files navigation

Arduino CI Arduino-lint JSON check GitHub issues

License: MIT GitHub release PlatformIO Registry

PID_RT

Arduino library for PID controller.

Description

The PID_RT class allows the user to instantiate a PID controller.

This library allows one to

  • adjust the K parameters (pid) runtime.
  • stop / start computing runtime.

(to be elaborated)

Some PID background

E-book

WOKWI

Interface

#include "PID_RT.h"

Constructor

  • PID_RT() minimal constructor.
  • PID_RT(float sp, float Kp, float Ki, float Kd) constructor that sets minimal parameters to get started.

Core

  • void reset() resets internals to startup (Kp == Ki == Kd == 0).
  • void setPoint(float sp) sets the setPoint, that needs to be reached.
  • float getSetPoint() read back the setPoint.
  • bool compute(float input) does one iteration of the PID controller. See below. Returns true after a calculation is done. Returns false if not computed, either due to stop flag or not yet time to do the calculation. See PID_ENABLE_INTERVAL_CHECK below.
  • float getOutput() get the last calculated output value.
  • bool setK(float Kp, float Ki, float Kd) Set the initial P I D parameters as a group. Overwrites the values set in the constructor.

PID_ENABLE_INTERVAL_CHECK

Since 0.1.8 a compile time flag PID_ENABLE_INTERVAL_CHECK has been added. (See #8). This flag allows the user to enable or disable the interval check in compute(). Default the flag is enabled, and the function will check if the interval set has passed, if not, it will return false, true otherwise.

If the PID_ENABLE_INTERVAL_CHECK is set to false, the user has to ensure the interval periodicity, e.g. by using a hardware timer, external interrupt, RTC or otherwise.

Note that using a hardware timer can improve the accuracy of the PID control.

See the PID_RT.h file.

Start Stop

  • void start() enable the PID controller to compute() new output values.
  • void stop() disable the PID controller, see compute().
  • bool isRunning() return the enable/disable flag.

Behaviour parameters

  • bool setInterval(uint32_t interval) set the interval between two compute() calls. Returns true if changed, otherwise false.
  • uint32_t getInterval() read back interval set.
  • void setOutputRange(float rangeMin, float rangeMax) tune the output range, default 0..100
  • float getOutputMin() read back setting rangeMin.
  • float getOutputMax() read back setting rangeMax.
  • void setReverse(bool reverse) reverse behaviour, seldom needed.
  • bool getReverse() read back the setting.

K-parameters

  • bool setKp(float Kp) runtime updates are allowed - at your own risk.
  • bool setKi(float Ki) runtime updates are allowed - at your own risk.
  • bool setKd(float Kd) runtime updates are allowed - at your own risk.
  • float getKp() read back setting.
  • float getKi() read back setting.
  • float getKd() read back setting.

Proportional

  • void setPropOnInput() this is default behaviour.
  • void setPropOnError() alternative.
  • bool isPropOnInput() read back setting.
  • bool isPropOnError() read back setting.

Debugging

  • float getInput() read last input.
  • float getLastError() read the last error.
  • uint32_t getLastTime() get the last time compute() was called.

Note the lastTime value is incremented with interval every iteration so it may have an offset with respect to the actual time. This method is chosen as it is almost guarantees that no iterations will be missed. To improve the timing of compute() calls, the user could use e.g. a hardware timer to call compute() at very regular intervals. See also PID_ENABLE_INTERVAL_CHECK above.

Operations

See examples and

Future

Must

  • update / improve documentation
  • more testing

Should

  • investigate if it works as PI or P controller too.
    • PI as derived or base class?
  • add examples to test more
  • improve unit test
  • investigate if compute() can determine the interval
    • interval = millis() - lastTime;
    • need to know start/stop moment to get the math right.

Could

  • move all code to .cpp

Wont

Support

If you appreciate my libraries, you can support the development and maintenance. Improve the quality of the libraries by providing issues and Pull Requests, or donate through PayPal or GitHub sponsors.

Thank you,

About

PID library for Arduino

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

No packages published

Contributors 2

  •  
  •  

Languages