Revision a6ca8c2b
ID | a6ca8c2b8a16a4d9889c8c3138a695a057550468 |
Wrote pid control stuff...untested.
control/pid_control.cpp | ||
---|---|---|
1 |
#include "pid_control.h" |
|
2 |
#include "assert.h" |
|
3 |
|
|
4 |
PID_control::PID_control(int p_term, int d_term, int i_term, float goal); |
|
5 |
{ |
|
6 |
P_err = 0; |
|
7 |
I_err = 0; |
|
8 |
D_err = 0; |
|
9 |
|
|
10 |
k_p = p_term; |
|
11 |
k_d = d_term; |
|
12 |
k_i = i_term; |
|
13 |
|
|
14 |
set_config = goal; |
|
15 |
} |
|
16 |
|
|
17 |
PID_control::PID_control(int p_term, int d_term, int i_term) |
|
18 |
{ |
|
19 |
P_err = 0; |
|
20 |
I_err = 0; |
|
21 |
D_err = 0; |
|
22 |
|
|
23 |
k_p = p_term; |
|
24 |
k_d = d_term; |
|
25 |
k_i = i_term; |
|
26 |
} |
|
27 |
|
|
28 |
void PID_control::change_goal(float goal) |
|
29 |
{ |
|
30 |
set_config == goal; |
|
31 |
I_err = 0; |
|
32 |
} |
|
33 |
|
|
34 |
float PID_control::pid(float input) |
|
35 |
{ |
|
36 |
prev_error = error; |
|
37 |
error = set_config - input; |
|
38 |
|
|
39 |
P_err = error; |
|
40 |
I_err += prev_error; |
|
41 |
D_err = error - prev_error; |
|
42 |
|
|
43 |
return k_p*P_err + k_i*I_err + k_d * D_err; |
|
44 |
} |
|
45 |
|
control/pid_control.h | ||
---|---|---|
1 |
#ifndef _PID_CONTROL_H_ |
|
2 |
#define _PID_CONTROL_H_ |
|
3 |
|
|
4 |
class PID_control |
|
5 |
{ |
|
6 |
public: |
|
7 |
PID_control(int p_term, int d_term, int i_term, float goal); |
|
8 |
PID_control(int p_term, int d_term, int i_term); |
|
9 |
|
|
10 |
void change_goal(float goal); |
|
11 |
|
|
12 |
float pid(float input) |
|
13 |
|
|
14 |
private: |
|
15 |
int k_p; |
|
16 |
int k_i; |
|
17 |
int k_d; |
|
18 |
|
|
19 |
float set_config; |
|
20 |
float error; |
|
21 |
float prev_error; |
|
22 |
|
|
23 |
float P_err; |
|
24 |
float I_err; |
|
25 |
float D_err; |
|
26 |
}; |
|
27 |
|
|
28 |
#endif |
Also available in: Unified diff