-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpid.cpp
136 lines (105 loc) · 3.07 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
PID Controller
author : vincent JAUNET
mail : [email protected]
date : 10-01-2013
Description:
the PID class is a collection of routine necessary
to perform a PID control based on setpoints (remote control),
inputs (measured attitude).
Can be used for any type of system and features :
- Derivative on measurement
- Windsup of integral errors
Copyright (c) <2014> <Vincent Jaunet>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "pid.h"
PID yprSTAB[3];
PID yprRATE[3];
//default constructo
PID::PID()
{
//PID constants
m_Kd = 0;
m_Ki = 0;
m_Kp = 0;
//PID variables
m_err = 0;
m_sum_err = 0;
m_ddt_err = 0;
m_lastInput= 0;
m_outmax = 200;
m_outmin = -200;
}
PID::PID(float kp_,float ki_,float kd_)
{
//PID constants
m_Kd = kp_;
m_Ki = ki_;
m_Kp = kd_;
//PID variables
m_err = 0;
m_sum_err = 0;
m_ddt_err = 0;
m_lastInput= 0;
m_outmax = 400;
m_outmin = -400;
}
float PID::update_pid_std(float setpoint, float input, float dt)
{
//Computes error
m_err = setpoint-input;
//Integrating errors
m_sum_err += m_err * m_Ki * dt;
//calculating error derivative
//Input derivative is used to avoid derivative kick
m_ddt_err = -m_Kd / dt * (input - m_lastInput);
//Calculation of the output
//winds up boundaries
m_output = m_Kp*m_err + m_sum_err + m_ddt_err;
if (m_output > m_outmax) {
//winds up boundaries
m_sum_err = 0.0;
m_output = m_outmax;
}else if (m_output < m_outmin) {
//winds up boundaries
m_sum_err = 0.0;
m_output = m_outmin;
}
m_lastInput= input;
//printf("kp %f ki %f kd %f\n", m_Kp, m_Ki, m_Kd);
//printf("setpt %7.2f input %7.2f output %f\n", setpoint, input, m_output);
//printf("err %7.2f ddt_err %7.2f sum_err %7.2f\n", m_err, m_ddt_err, m_sum_err);
return m_output;
}
void PID::reset()
{
m_sum_err = 0;
m_ddt_err = 0;
m_lastInput = 0;
}
void PID::set_Kpid(float Kp,float Ki, float Kd)
{
m_Kp = Kp;
m_Ki = Ki;
m_Kd = Kd;
}
void PID::set_windup_bounds(float Min,float Max)
{
m_outmax = Max;
m_outmin = Min;
}