https://github.com/blockos/pid
A simple PID template library.
https://github.com/blockos/pid
Last synced: 12 months ago
JSON representation
A simple PID template library.
- Host: GitHub
- URL: https://github.com/blockos/pid
- Owner: BlockoS
- License: apache-2.0
- Created: 2015-06-13T17:07:24.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-01-05T13:28:37.000Z (about 10 years ago)
- Last Synced: 2023-03-10T19:25:52.998Z (almost 3 years ago)
- Language: C++
- Size: 42 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple PID template #
This is a simple template class for PID computation first intended for the Arduino and fixed-point maths.
It features output clamping, integral anti-windup. Integration mechanism can be selected at compile time.
The currently available integration schemes are : standard, trapezoidal and Simpson.
Only automatic mode is supported for the moment.
# Example #
The following example uses fixed-point PID and outputs the setpoint and process value (speed) in a file.
```cpp
// file: test/cruise.cpp
#include
#include
#include "PID.h"
// Fixed point bias.
#define BIAS 256
typedef PID PID_t;
int main()
{
PID_t control;
// Vehicle mass.
PID_t::value_type mass = 1000;
// Damping factor.
PID_t::value_type damping = 50;
// Target speed.
PID_t::value_type reference = 10;
// Current vehicle speed.
PID_t::value_type speed = 0;
// PID output.
PID_t::value_type output = 0;
control.Setup(720,1, 40,1, 10,1, 100);
control.SetSetpoint(reference * BIAS);
control.SetOutputLimits ( 0*BIAS, 32768*BIAS);
control.SetIntegralLimits( -32768*BIAS, 32768*BIAS);
control.Start(speed);
for(int i=0; i<120; i++)
{
// Compute PID.
output = control.Update(speed);
// Compute speed.
speed += (output - damping*speed) * control.GetSamplingTime() / mass / 1000;
// Output speed and setpoint.
std::cout << i << '\t' << (speed / (float)BIAS) << '\t';
std::cout << (control.GetSetpoint() / (float)BIAS) << std::endl;
}
return 0;
}
```
The generated file is then used to produce the following plot with Gnuplot.

# License #
All files are licensed under the Apache License version 2.0 unless otherwise marked. Full text of the Apache License version 2.0 is available in LICENSE.txt