Skip to content

Commit f658189

Browse files
author
David R Forrest
committed
Add example of back-caclulation in userspace using outputSum
1 parent 6ea5568 commit f658189

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/********************************************************
2+
* PID Basic Example
3+
* Reading analog input 0 to control analog PWM output 3
4+
********************************************************/
5+
6+
#include <PID_v1.h>
7+
8+
#define PIN_INPUT 0
9+
#define PIN_OUTPUT 3
10+
11+
//Define Variables we'll be connecting to
12+
double Setpoint, Input, Output;
13+
14+
//Specify the links and initial tuning parameters
15+
double Kp=2, Ki=5, Kd=1;
16+
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
17+
18+
void setup()
19+
{
20+
//initialize the variables we're linked to
21+
Input = analogRead(PIN_INPUT);
22+
Setpoint = 100;
23+
24+
//turn the PID on
25+
myPID.SetMode(AUTOMATIC);
26+
}
27+
28+
void loop()
29+
{
30+
Input = analogRead(PIN_INPUT);
31+
if(myPID.Compute()){
32+
// check if wound-up beyond output limits:
33+
if(myPID.outputSum + Kp * (Setpoint - Input) > 255){
34+
// wound up too high, backcalculate to feasible:
35+
myPID.outputSum = 255-Kp * (Setpoint - Input);
36+
}
37+
}
38+
analogWrite(PIN_OUTPUT, Output);
39+
}
40+
41+

0 commit comments

Comments
 (0)