-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPID.cpp
176 lines (157 loc) · 4.5 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "Pid.h"
float minusError(float target, float current)
{
return target - current;
}
float circularError(float target, float current)
{
float error = target - current;
if (error > M_PI) {
error -= 2*M_PI;
return error;
}
if (error < -M_PI) {
error += 2*M_PI;
return error;
}
}
void PID::initialize(float dt, float pidConfig[7])
{
this->dt = dt;
for (int i=0;i<3;i++) {
pid[i] = pidConfig[i];
}
errorIMax = pidConfig[3];
errorIMin = pidConfig[4];
outMax = pidConfig[5];
outMin = pidConfig[6];
}
float PID::update()
{
float error = getError(target, current);
errorI += error*dt;
if (errorI > errorIMax) {
errorI = errorIMax;
}
if (errorI < errorIMin) {
errorI = errorIMin;
}
float result = pid[0] * error + pid[1] * errorI + pid[2] * errorD;
if (next) {
next->target = result;
return next->update();
}
return result;
}
/*
float PID::update(float error, float dt)
{
float errorD = (error - prevError)/dt;
return update(error, errorD, dt);
}
*/
void PID::reset()
{
errorI = 0;
if (next) {
next->reset();
}
}
void PIDSystem::initialize(float dt, int yprtSanity[4], float PIDSystemConfig[5][7])
{
int i;
this->dt = dt;
for (i=0;i<4;i++) {
sanity[i] = yprtSanity[i];
}
for (i=0;i<3;i++) {
attitudePID[i].initialize(dt, PIDSystemConfig[i]);
}
for (i=3;i<5;i++) {
altitudePID[i].initialize(dt, PIDSystemConfig[i]);
}
// Yaw requires circularError
attitudePID[0].getError = circularError;
}
void PIDSystem::setAttitudeTarget(int index, float target)
{
// Setting target first than enabled PID status to get good result in multithreading
attitudePID[index].target = target;
enabled[index] = &attitudePID[index];
}
void PIDSystem::setAttitudeTargets(float targets[3])
{
for (int i=0;i<3;i++) {
attitudePID[i].target = targets[i];
enabled[i] = &attitudePID[i];
}
}
void PIDSystem::setAltitudeTarget(float target, uint8_t type)
{
// Some subtle sequence
altitudeTarget[type] = target;
altitudeType = type;
altPIDType = PID_ATTITUDE;
}
// Updating yprt[4], so ESC controller must read it right after the update.
void PIDSystem::update(statusContainer& status, float dt)
{
float rates[3];
rates[0] = -status.gyroscope[2]; // -gz
rates[1] = -status.gyroscope[1]; // -gy
rates[2] = status.gyroscope[0]; // +gx
int i;
for (i=0;i<3;i++) {
switch(PIDTypes[i]) {
case PID_RATE:
yprt[i] = ratePID[i].update(rateTargets[i] - rates[i], 0, dt);
break;
case PID_ATTITUDE:
if (i==0) {
// Yaw round up
float error = attitudeTargets[0] - status.attitude[0];
}
yprt[i] = attitudePID[i].update(attitudeTargets[i] - status.attitude[i], rates[i], dt);
break;
default:
//Wrong type, do nothing
warn("Wrong PID type!");
break;
}
}
// first PID type then altitude type
switch(altPIDType) {
case PID_RATE:
switch(altitudeType) {
case ALTITUDE_BARO:
yprt[3] = VzPID[0].update(VzTarget - status.baroFilterVelocityZ, status.accAbsolute[2], dt);
break;
case ALTITUDE_SONAR:
yprt[3] = VzPID[1].update(VzTarget - status.sonarFilterVelocityZ, status.accAbsolute[2], dt);
break;
default:
//Wrong type, do nothing
warn("Wrong PID type!");
break;
}
break;
case PID_ATTITUDE:
switch(altitudeType) {
case ALTITUDE_BARO:
yprt[3] = altitudePID[0].update(altitudeTarget[0] - status.baroFilterAltitude, status.baroFilterVelocityZ, dt);
break;
case ALTITUDE_SONAR:
yprt[3] = altitudePID[1].update(altitudeTarget[1] - status.sonarFilterAltitude, status.sonarFilterVelocityZ, dt);
break;
default:
//Wrong type, do nothing
warn("Wrong PID type!");
break;
}
break;
default:
//Wrong type, do nothing
warn("Wrong PID type!");
break;
}
}