2021-01-29 06:31:58 -05:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: PID_RT.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-11-22 05:48:02 -05:00
|
|
|
// VERSION: 0.1.5
|
2021-01-29 06:31:58 -05:00
|
|
|
// PURPOSE: PID library for Arduino
|
|
|
|
// URL: https://github.com/RobTillaart/PID_RT
|
|
|
|
|
2021-05-28 07:41:04 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#include "Arduino.h"
|
|
|
|
|
2021-05-28 07:41:04 -04:00
|
|
|
|
2022-11-22 05:48:02 -05:00
|
|
|
#define PID_LIB_VERSION (F("0.1.5"))
|
2021-11-12 07:52:09 -05:00
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
class PID_RT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PID_RT();
|
|
|
|
PID_RT(float sp, float Kp, float Ki, float Kd);
|
|
|
|
|
2022-11-22 05:48:02 -05:00
|
|
|
void reset();
|
2021-11-12 07:52:09 -05:00
|
|
|
|
|
|
|
|
2022-11-22 05:48:02 -05:00
|
|
|
void setPoint(float sp) { _setPoint = sp; };
|
|
|
|
float getSetPoint() { return _setPoint; };
|
2021-11-12 07:52:09 -05:00
|
|
|
|
|
|
|
|
2022-11-22 05:48:02 -05:00
|
|
|
bool compute(float input);
|
|
|
|
float getOutput() { return _output; };
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-11-12 07:52:09 -05:00
|
|
|
|
2022-11-22 05:48:02 -05:00
|
|
|
// set PID controller on / off
|
|
|
|
void start() { _running = true; };
|
|
|
|
void stop() { _running = false; };
|
|
|
|
bool isRunning() { return _running; };
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-11-12 07:52:09 -05:00
|
|
|
|
2022-11-22 05:48:02 -05:00
|
|
|
// reverse the behaviour (not implemented yet)
|
|
|
|
void setReverse(bool reverse) { _reverse = reverse; };
|
|
|
|
bool getReverse() { return _reverse; };
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-11-12 07:52:09 -05:00
|
|
|
|
2022-11-22 05:48:02 -05:00
|
|
|
// how often should one do the math
|
2021-01-29 06:31:58 -05:00
|
|
|
bool setInterval(uint32_t interval);
|
|
|
|
uint32_t getInterval() { return _interval; };
|
|
|
|
|
2021-11-12 07:52:09 -05:00
|
|
|
|
2022-11-22 05:48:02 -05:00
|
|
|
// tune the output range, default 0..100
|
|
|
|
void setOutputRange(float rangeMin, float rangeMax) { _rangeMin = rangeMin; _rangeMax = rangeMax; };
|
|
|
|
float getOutputMin() { return _rangeMin; };
|
|
|
|
float getOutputMax() { return _rangeMax; };
|
2021-11-12 07:52:09 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-11-22 05:48:02 -05:00
|
|
|
// set the initial K values,
|
|
|
|
// runtime updates are allowed - at your own risk
|
|
|
|
bool setK(float Kp, float Ki, float Kd);
|
|
|
|
bool setKp(float Kp);
|
|
|
|
bool setKi(float Ki);
|
|
|
|
bool setKd(float Kd);
|
|
|
|
float getKp() { return _Kp; };
|
|
|
|
float getKi() { return _Ki; };
|
|
|
|
float getKd() { return _Kd; };
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-11-12 07:52:09 -05:00
|
|
|
|
2022-11-22 05:48:02 -05:00
|
|
|
// set Proportional on Input or on Error
|
2021-01-29 06:31:58 -05:00
|
|
|
void setPropOnInput() { _POI = true; }; // default
|
|
|
|
void setPropOnError() { _POI = false; };
|
|
|
|
bool isPropOnInput() { return _POI == true; };
|
|
|
|
bool isPropOnError() { return _POI == false; };
|
|
|
|
|
2021-11-12 07:52:09 -05:00
|
|
|
|
2022-11-22 05:48:02 -05:00
|
|
|
// debugging
|
2021-01-29 06:31:58 -05:00
|
|
|
float getInput() { return _input; };
|
|
|
|
float getLastError() { return _error; };
|
|
|
|
uint32_t getLastTime() { return _lastTime; };
|
|
|
|
|
2021-11-12 07:52:09 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
private:
|
|
|
|
uint32_t _lastTime = 0;
|
|
|
|
uint32_t _interval = 250;
|
|
|
|
|
|
|
|
float _errorSum = 0.0;
|
|
|
|
|
|
|
|
float _setPoint = 0.0;
|
|
|
|
float _input = 0.0;
|
|
|
|
float _lastInput = 0.0;
|
|
|
|
float _error = 0.0;
|
|
|
|
float _output = 0.0;
|
2021-11-12 07:52:09 -05:00
|
|
|
float _rangeMin = 0.0;
|
|
|
|
float _rangeMax = 100.0;
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
float _Kp = 0.0;
|
|
|
|
float _Ki = 0.0;
|
|
|
|
float _Kd = 0.0;
|
|
|
|
float __Kp = 0.0;
|
|
|
|
float __Ki = 0.0;
|
|
|
|
float __Kd = 0.0;
|
|
|
|
|
|
|
|
bool _reverse = false;
|
|
|
|
bool _running = false;
|
2022-11-22 05:48:02 -05:00
|
|
|
bool _POI = true; // Proportional On Input - Error
|
2021-01-29 06:31:58 -05:00
|
|
|
};
|
|
|
|
|
2021-05-28 07:41:04 -04:00
|
|
|
|
2022-11-22 05:48:02 -05:00
|
|
|
// -- END OF FILE --
|
2021-12-23 09:44:14 -05:00
|
|
|
|