-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpid.c
More file actions
46 lines (40 loc) · 1.09 KB
/
pid.c
File metadata and controls
46 lines (40 loc) · 1.09 KB
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
/*
* pid.c
*
* Created: 24.03.2017 13:52:07
* Author: Jorgen Jackwitz
*/
#include "pid.h"
#define TIMECONSTANT 1000000
#define GAINCONSTANT 1000
int32_t pid(Pid_t *PID, uint16_t currentValue, uint16_t setpoint){
int output = 0;
PID->totError = setpoint - currentValue;
// Proportional gain
int propGain = (PID->Kp)*(PID->totError);
// Integration gain
PID->intError = PID->intError + (PID->timeStep)*(PID->totError);
int intGain = PID->intError*(PID->Ki);
// Derivation gain
PID->derError = (PID->totError - PID->lastError)/(PID->timeStep);
int derGain = (PID->derError)*(PID->Kd);
PID->lastError = PID->totError;
/*
printf("totError: %d \t", PID->totError);
printf("IntError: %d \t", PID->intError);
printf("DerError: %d \t",PID->derError);
printf("PropGain: %d \t", propGain);
printf("IntGain: %d \t", intGain);
printf("DerGain: %d \t", derGain);
*/
// Pid output
output = propGain + intGain + derGain;
//printf("Out: %d \n", output);
return output;
}
void pid_init(Pid_t *PID, float t, float p, float i, float d){
PID->Kp = p;
PID->Ki = i;
PID->Kd = d;
PID->timeStep = t;
}