mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.5 PID_RT
This commit is contained in:
parent
ddd5c8e42a
commit
1241b83a82
@ -1,3 +1,18 @@
|
|||||||
|
platforms:
|
||||||
|
rpipico:
|
||||||
|
board: rp2040:rp2040:rpipico
|
||||||
|
package: rp2040:rp2040
|
||||||
|
gcc:
|
||||||
|
features:
|
||||||
|
defines:
|
||||||
|
- ARDUINO_ARCH_RP2040
|
||||||
|
warnings:
|
||||||
|
flags:
|
||||||
|
|
||||||
|
packages:
|
||||||
|
rp2040:rp2040:
|
||||||
|
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
|
||||||
|
|
||||||
compile:
|
compile:
|
||||||
# Choosing to run compilation tests on 2 different Arduino platforms
|
# Choosing to run compilation tests on 2 different Arduino platforms
|
||||||
platforms:
|
platforms:
|
||||||
@ -9,3 +24,4 @@ compile:
|
|||||||
- esp32
|
- esp32
|
||||||
# - esp8266
|
# - esp8266
|
||||||
# - mega2560
|
# - mega2560
|
||||||
|
- rpipico
|
||||||
|
36
libraries/PID_RT/CHANGELOG.md
Normal file
36
libraries/PID_RT/CHANGELOG.md
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
# Change Log PID_RT
|
||||||
|
|
||||||
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||||
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||||
|
|
||||||
|
|
||||||
|
## [0.1.5] - 2022-10-23
|
||||||
|
- add RP2040 to build-CI
|
||||||
|
- add changelog.md
|
||||||
|
- minor edits readme.md
|
||||||
|
|
||||||
|
|
||||||
|
## [0.1.4] - 2021-12-23
|
||||||
|
- update library.json, license,
|
||||||
|
- minor edits
|
||||||
|
|
||||||
|
## [0.1.3] - 2021-11-12
|
||||||
|
- update build-CI, update readme
|
||||||
|
- renamed variables for readability
|
||||||
|
- added history
|
||||||
|
- fixed bug in setK
|
||||||
|
|
||||||
|
## [0.1.2] - 2021-05-28
|
||||||
|
- minor edits
|
||||||
|
|
||||||
|
## [0.1.1] - 2021-05-27
|
||||||
|
- add json check
|
||||||
|
- add lint check
|
||||||
|
|
||||||
|
## [0.1.0] - 2020-12-15
|
||||||
|
- update readme, fix unit test,
|
||||||
|
- fix library.json
|
||||||
|
- add 2nd constructor
|
||||||
|
|
@ -1,21 +1,9 @@
|
|||||||
//
|
//
|
||||||
// FILE: PID_RT.h
|
// FILE: PID_RT.cpp
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.1.4
|
// VERSION: 0.1.5
|
||||||
// PURPOSE: PID library for Arduino
|
// PURPOSE: PID library for Arduino
|
||||||
// URL: https://github.com/RobTillaart/PID
|
// URL: https://github.com/RobTillaart/PID
|
||||||
//
|
|
||||||
// HISTORY
|
|
||||||
// 0.1.0 2020-12-15 update readme, fix unit test,
|
|
||||||
// fix library.json
|
|
||||||
// add 2nd constructor
|
|
||||||
// 0.1.1 2021-05-27 add json check, add lint check
|
|
||||||
// 0.1.2 2021-05-28 minor edits
|
|
||||||
// 0.1.3 2021-11-12 update build-CI, update readme
|
|
||||||
// renamed variables for readability
|
|
||||||
// added history
|
|
||||||
// fixed bug in setK
|
|
||||||
// 0.1.4 2021-12-23 update library.json, license, minor edits
|
|
||||||
|
|
||||||
|
|
||||||
#include "PID_RT.h"
|
#include "PID_RT.h"
|
||||||
@ -55,13 +43,13 @@ void PID_RT::reset()
|
|||||||
__Kd = 0.0;
|
__Kd = 0.0;
|
||||||
_reverse = false;
|
_reverse = false;
|
||||||
_running = false;
|
_running = false;
|
||||||
_POI = true; // Proportional On Input - Error
|
_POI = true; // Proportional On Input - Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PID_RT::setK(float Kp, float Ki, float Kd)
|
bool PID_RT::setK(float Kp, float Ki, float Kd)
|
||||||
{
|
{
|
||||||
// prevent short-cut evaluation.
|
// prevent short-cut evaluation.
|
||||||
bool b = setKp(Kp);
|
bool b = setKp(Kp);
|
||||||
b = b && setKi(Ki);
|
b = b && setKi(Ki);
|
||||||
b = b && setKd(Kd);
|
b = b && setKd(Kd);
|
||||||
@ -83,7 +71,7 @@ bool PID_RT::setKi(float Ki)
|
|||||||
{
|
{
|
||||||
if (Ki < 0) return false;
|
if (Ki < 0) return false;
|
||||||
_Ki = Ki;
|
_Ki = Ki;
|
||||||
__Ki = _Ki * _interval * 0.001; // milliseconds.
|
__Ki = _Ki * _interval * 0.001; // milliseconds.
|
||||||
if (_reverse) __Ki = - __Ki;
|
if (_reverse) __Ki = - __Ki;
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
@ -115,7 +103,7 @@ bool PID_RT::compute(float input)
|
|||||||
_error = _setPoint - _input;
|
_error = _setPoint - _input;
|
||||||
float dI = _lastInput - _input;
|
float dI = _lastInput - _input;
|
||||||
|
|
||||||
// P - proportional on input or proportional on error
|
// P - proportional on input or proportional on error
|
||||||
if (_POI == false)
|
if (_POI == false)
|
||||||
{
|
{
|
||||||
_output = __Kp * _error;
|
_output = __Kp * _error;
|
||||||
@ -126,15 +114,15 @@ bool PID_RT::compute(float input)
|
|||||||
_errorSum += __Kp * dI;
|
_errorSum += __Kp * dI;
|
||||||
}
|
}
|
||||||
|
|
||||||
// I
|
// I
|
||||||
_errorSum += __Ki * _error;
|
_errorSum += __Ki * _error;
|
||||||
if (_errorSum > _rangeMax) _errorSum = _rangeMax;
|
if (_errorSum > _rangeMax) _errorSum = _rangeMax;
|
||||||
else if (_errorSum < _rangeMin) _errorSum = _rangeMin;
|
else if (_errorSum < _rangeMin) _errorSum = _rangeMin;
|
||||||
|
|
||||||
// D
|
// D
|
||||||
_output += _errorSum + __Kd * dI;
|
_output += _errorSum + __Kd * dI;
|
||||||
|
|
||||||
// limit output to range
|
// limit output to range
|
||||||
if (_output > _rangeMax) _output = _rangeMax;
|
if (_output > _rangeMax) _output = _rangeMax;
|
||||||
else if (_output < _rangeMin) _output = _rangeMin;
|
else if (_output < _rangeMin) _output = _rangeMin;
|
||||||
|
|
||||||
@ -148,7 +136,7 @@ bool PID_RT::setInterval(uint32_t interval)
|
|||||||
if (interval != _interval)
|
if (interval != _interval)
|
||||||
{
|
{
|
||||||
_interval = interval;
|
_interval = interval;
|
||||||
// recalculate __Ki and __Kd.
|
// recalculate __Ki and __Kd.
|
||||||
setKi(_Ki);
|
setKi(_Ki);
|
||||||
setKd(_Kd);
|
setKd(_Kd);
|
||||||
return true;
|
return true;
|
||||||
@ -157,5 +145,5 @@ bool PID_RT::setInterval(uint32_t interval)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// -- END OF FILE --
|
// -- END OF FILE --
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
//
|
//
|
||||||
// FILE: PID_RT.h
|
// FILE: PID_RT.h
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.1.4
|
// VERSION: 0.1.5
|
||||||
// PURPOSE: PID library for Arduino
|
// PURPOSE: PID library for Arduino
|
||||||
// URL: https://github.com/RobTillaart/PID_RT
|
// URL: https://github.com/RobTillaart/PID_RT
|
||||||
|
|
||||||
@ -10,7 +10,7 @@
|
|||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
|
||||||
#define PID_LIB_VERSION (F("0.1.4"))
|
#define PID_LIB_VERSION (F("0.1.5"))
|
||||||
|
|
||||||
|
|
||||||
class PID_RT
|
class PID_RT
|
||||||
@ -19,58 +19,58 @@ public:
|
|||||||
PID_RT();
|
PID_RT();
|
||||||
PID_RT(float sp, float Kp, float Ki, float Kd);
|
PID_RT(float sp, float Kp, float Ki, float Kd);
|
||||||
|
|
||||||
void reset();
|
void reset();
|
||||||
|
|
||||||
|
|
||||||
void setPoint(float sp) { _setPoint = sp; };
|
void setPoint(float sp) { _setPoint = sp; };
|
||||||
float getSetPoint() { return _setPoint; };
|
float getSetPoint() { return _setPoint; };
|
||||||
|
|
||||||
|
|
||||||
bool compute(float input);
|
bool compute(float input);
|
||||||
float getOutput() { return _output; };
|
float getOutput() { return _output; };
|
||||||
|
|
||||||
|
|
||||||
// set PID controller on / off
|
// set PID controller on / off
|
||||||
void start() { _running = true; };
|
void start() { _running = true; };
|
||||||
void stop() { _running = false; };
|
void stop() { _running = false; };
|
||||||
bool isRunning() { return _running; };
|
bool isRunning() { return _running; };
|
||||||
|
|
||||||
|
|
||||||
// reverse the behaviour (not implemented yet)
|
// reverse the behaviour (not implemented yet)
|
||||||
void setReverse(bool reverse) { _reverse = reverse; };
|
void setReverse(bool reverse) { _reverse = reverse; };
|
||||||
bool getReverse() { return _reverse; };
|
bool getReverse() { return _reverse; };
|
||||||
|
|
||||||
|
|
||||||
// how often should one do the math
|
// how often should one do the math
|
||||||
bool setInterval(uint32_t interval);
|
bool setInterval(uint32_t interval);
|
||||||
uint32_t getInterval() { return _interval; };
|
uint32_t getInterval() { return _interval; };
|
||||||
|
|
||||||
|
|
||||||
// tune the output range, default 0..100
|
// tune the output range, default 0..100
|
||||||
void setOutputRange(float rangeMin, float rangeMax) { _rangeMin = rangeMin; _rangeMax = rangeMax; };
|
void setOutputRange(float rangeMin, float rangeMax) { _rangeMin = rangeMin; _rangeMax = rangeMax; };
|
||||||
float getOutputMin() { return _rangeMin; };
|
float getOutputMin() { return _rangeMin; };
|
||||||
float getOutputMax() { return _rangeMax; };
|
float getOutputMax() { return _rangeMax; };
|
||||||
|
|
||||||
|
|
||||||
// set the initial K values,
|
// set the initial K values,
|
||||||
// runtime updates are allowed - at your own risk
|
// runtime updates are allowed - at your own risk
|
||||||
bool setK(float Kp, float Ki, float Kd);
|
bool setK(float Kp, float Ki, float Kd);
|
||||||
bool setKp(float Kp);
|
bool setKp(float Kp);
|
||||||
bool setKi(float Ki);
|
bool setKi(float Ki);
|
||||||
bool setKd(float Kd);
|
bool setKd(float Kd);
|
||||||
float getKp() { return _Kp; };
|
float getKp() { return _Kp; };
|
||||||
float getKi() { return _Ki; };
|
float getKi() { return _Ki; };
|
||||||
float getKd() { return _Kd; };
|
float getKd() { return _Kd; };
|
||||||
|
|
||||||
|
|
||||||
// set Proportional on Input or on Error
|
// set Proportional on Input or on Error
|
||||||
void setPropOnInput() { _POI = true; }; // default
|
void setPropOnInput() { _POI = true; }; // default
|
||||||
void setPropOnError() { _POI = false; };
|
void setPropOnError() { _POI = false; };
|
||||||
bool isPropOnInput() { return _POI == true; };
|
bool isPropOnInput() { return _POI == true; };
|
||||||
bool isPropOnError() { return _POI == false; };
|
bool isPropOnError() { return _POI == false; };
|
||||||
|
|
||||||
|
|
||||||
// debugging
|
// debugging
|
||||||
float getInput() { return _input; };
|
float getInput() { return _input; };
|
||||||
float getLastError() { return _error; };
|
float getLastError() { return _error; };
|
||||||
uint32_t getLastTime() { return _lastTime; };
|
uint32_t getLastTime() { return _lastTime; };
|
||||||
@ -99,9 +99,9 @@ private:
|
|||||||
|
|
||||||
bool _reverse = false;
|
bool _reverse = false;
|
||||||
bool _running = false;
|
bool _running = false;
|
||||||
bool _POI = true; // Proportional On Input - Error
|
bool _POI = true; // Proportional On Input - Error
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// -- END OF FILE --
|
// -- END OF FILE --
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ Overwrites the values set in the constructor.
|
|||||||
- **bool isRunning()** return the enable/disable flag.
|
- **bool isRunning()** return the enable/disable flag.
|
||||||
|
|
||||||
|
|
||||||
### Additional parameters
|
### Behaviour parameters
|
||||||
|
|
||||||
- **bool setInterval(uint32_t interval)** set the interval between two **compute()** calls.
|
- **bool setInterval(uint32_t interval)** set the interval between two **compute()** calls.
|
||||||
Returns **true** if changed, otherwise **false**.
|
Returns **true** if changed, otherwise **false**.
|
||||||
@ -54,12 +54,20 @@ Returns **true** if changed, otherwise **false**.
|
|||||||
- **float getOutputMax()** read back setting rangeMax.
|
- **float getOutputMax()** read back setting rangeMax.
|
||||||
- **void setReverse(bool reverse)** reverse behaviour, seldom needed.
|
- **void setReverse(bool reverse)** reverse behaviour, seldom needed.
|
||||||
- **bool getReverse()** read back the setting.
|
- **bool getReverse()** read back the setting.
|
||||||
|
|
||||||
|
|
||||||
|
### K-parameters
|
||||||
|
|
||||||
- **bool setKp(float Kp)** runtime updates are allowed - at your own risk.
|
- **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 setKi(float Ki)** runtime updates are allowed - at your own risk.
|
||||||
- **bool setKd(float Kd)** 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 getKp()** read back setting.
|
||||||
- **float getKi()** read back setting.
|
- **float getKi()** read back setting.
|
||||||
- **float getKd()** read back setting.
|
- **float getKd()** read back setting.
|
||||||
|
|
||||||
|
|
||||||
|
### Proportional
|
||||||
|
|
||||||
- **void setPropOnInput()** this is default behaviour.
|
- **void setPropOnInput()** this is default behaviour.
|
||||||
- **void setPropOnError()** alternative.
|
- **void setPropOnError()** alternative.
|
||||||
- **bool isPropOnInput()** read back setting.
|
- **bool isPropOnInput()** read back setting.
|
||||||
@ -83,8 +91,20 @@ See examples.
|
|||||||
|
|
||||||
## Future
|
## Future
|
||||||
|
|
||||||
- update documentation
|
#### must
|
||||||
|
|
||||||
|
- update / improve documentation
|
||||||
|
- more testing
|
||||||
|
|
||||||
|
#### should
|
||||||
|
|
||||||
- add examples to test more
|
- add examples to test more
|
||||||
- improve unit test
|
- improve unit test
|
||||||
-
|
- move all code to .cpp
|
||||||
|
|
||||||
|
#### could
|
||||||
|
|
||||||
|
- add reference to PID book / website?
|
||||||
|
- investigate if it works as PI or P controller too.
|
||||||
|
- PI as derived or base class?
|
||||||
|
|
||||||
|
@ -2,11 +2,10 @@
|
|||||||
// FILE: PID_basic.ino
|
// FILE: PID_basic.ino
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// PURPOSE: demo
|
// PURPOSE: demo
|
||||||
// DATE: 2020-12-15
|
|
||||||
//
|
//
|
||||||
// connect an LED to the PWM pin
|
// connect an LED to the PWM pin
|
||||||
// connect a potentiometer to A0
|
// connect a potentiometer to A0
|
||||||
// play :)
|
// play :)
|
||||||
|
|
||||||
|
|
||||||
#include "PID_RT.h"
|
#include "PID_RT.h"
|
||||||
@ -15,7 +14,7 @@ PID_RT PID;
|
|||||||
|
|
||||||
const int PWM_PIN = 3; // UNO PWM pin
|
const int PWM_PIN = 3; // UNO PWM pin
|
||||||
|
|
||||||
int op = 0;;
|
int op = 0;
|
||||||
float input = 0;
|
float input = 0;
|
||||||
|
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/RobTillaart/PID_RT"
|
"url": "https://github.com/RobTillaart/PID_RT"
|
||||||
},
|
},
|
||||||
"version": "0.1.4",
|
"version": "0.1.5",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"frameworks": "arduino",
|
"frameworks": "arduino",
|
||||||
"platforms": "*",
|
"platforms": "*",
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name=PID_RT
|
name=PID_RT
|
||||||
version=0.1.4
|
version=0.1.5
|
||||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||||
sentence=Arduino PID library
|
sentence=Arduino PID library
|
||||||
|
Loading…
Reference in New Issue
Block a user