-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpid.cpp
63 lines (55 loc) · 1.06 KB
/
pid.cpp
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
59
60
61
62
63
/*
* pid.cpp
*
* Created: 20.04.2018 14:13:06
* Author: kamil
*/
#define F_CPU 32000000UL
#include "pid.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <stddef.h>
PID::PID(int16_t _set_point, uint8_t _Kp, uint8_t _Kd, uint8_t _Ki)
{
set_point = _set_point;
process_value = 0;
error = 0;
last_error = 0,
integrated_error = 0;
Kp = _Kp;
Kd = _Kd;
Ki = _Ki;
}
void PID::set_gains(const uint8_t &_Kp, const uint8_t &_Kd, const uint8_t &_Ki)
{
Kp = _Kp;
Kd = _Kd;
Ki = _Ki;
}
void PID::new_set_point(const int16_t &_set_point)
{
set_point = _set_point;
}
int16_t PID::get_correction()
{
int16_t correction;
error = set_point - process_value;
if(error < 25)
{
if(integrated_error < 30000)
integrated_error += error;
}
else
{
integrated_error = 0;
}
correction = Kp * error + Kd * (error - last_error) + Ki * integrated_error;
last_error = error;
return correction;
}
void PID::update_process_value(const int16_t &_process_value)
{
process_value = _process_value;
}