-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpid.c
58 lines (41 loc) · 1.5 KB
/
pid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* pid.c
*
* Created on: May 19, 2014
* Author: Tim Toliver
*
* This is from Tim Wescott's PID without a PhD
*/
#include "pid.h"
float UpdatePID(SPid * pid, float error, float position) {
float pTerm, dTerm, iTerm;
pid->iMax = 65535; // these correspond to the max and min of the integrator state limited to the max output of the drive
pid->iMin = 0;
pTerm = pid->pGain * error; // calculate the proportional term
pid->iState += error; // calculate the integral state with appropriate limiting
if (pid->iState > pid->iMax)
pid->iState = pid->iMax;
else if (pid->iState < pid->iMin)
pid->iState = pid->iMin;
iTerm = pid->iGain * pid->iState; // calculate the integral term
dTerm = pid->dGain * (position - pid->dState); // calculate the derivative term
pid->dState = position;
return pTerm + iTerm - dTerm;
}
/*
signed int UpdatePID(SPid * pid, signed int error, signed int position) {
signed int pTerm, dTerm, iTerm;
pid->iMax = 3250;
pid->iMin = 0;
pTerm = pid->pGain * error; // calculate the proportional term
pid->iState += error; // calculate the integral state with appropriate limiting
if (pid->iState > pid->iMax)
pid->iState = pid->iMax;
else if (pid->iState < pid->iMin)
pid->iState = pid->iMin;
iTerm = pid->iGain * pid->iState; // calculate the integral term
dTerm = pid->dGain * (pid->dState - position);
pid->dState = position;
return pTerm + dTerm + iTerm;
}
*/