-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpid.cpp
More file actions
41 lines (31 loc) · 810 Bytes
/
pid.cpp
File metadata and controls
41 lines (31 loc) · 810 Bytes
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
#include <Arduino.h>
#include "pid.h"
/**
* @file pid.cpp
*
* Manages initiation and filtering of compass data
* filtering can be done by lowPass filtering or
* midPass/median filtering
*
* @author Nikolai Andre Ek Ovesen (nikolai.ovesen(at)gmail.com)
*/
/**
* Sets tunings for the PID controller by taking in variables from a function call within main file
*/
void pid::setTunings(float p, float i, float d) {
Kp = p;
Ki = i;
Kd = d;
}
/**
* nav() Implements basic PID controller functionality
*/
int pid::nav(float input, float setpoint) {
now = millis();
error = setpoint - input;
timeChange = (now - lastTime);
errorSum = (error * timeChange);
error = (error * Kp) + (errorSum * Ki) + ((error - lastError) * Kd);
output = map(error, -127, 127, -47, 207);
return output;
}