-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpid.cpp
221 lines (161 loc) · 7.77 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
The PID Controller makes adjustments to the motor speeds in order to adjust orientation in the desired angle/direction
*/
#include "pid.h"
#include "config.h"
/* PID Gains for the Rate Controller */
float KpR = 0.8;
float KiR = 0.065;
float KdR = 7.5;
float KpRZ = 0.1;
float KiRZ = 0;
float KdRZ = 0;
/* PID Gains for the Stabilization Controller */
float Kp = 0; // 0.12;
float Ki = 0;
float Kd = 0; // 0.53 or 1.25
float KpZ = 0;
float KiZ = 0;
float KdZ = 0;
float outputX, outputY, outputZ;
axis_int16_t desiredAngle;
axis_float_t currentAngle, lastAngle;
axis_float_t error, deltaError, errorSum;
axis_float_t errorRate, deltaRate, rateIntegral, currentRate, previousRate, rateOutput;
int_pwmOut motorSpeed;
void initPids(){
//time since last calculation
/* Call the computePID function to calculate the motor speeds for the quadcopter */
computePids();
resetPids();
/* Store the previous loop's measurements in order to calculate derivative for PID controller(s) */
lastAngle.x = currentAngle.x;
lastAngle.y = currentAngle.y;
lastAngle.z = currentAngle.z;
previousRate.x = currentRate.x;
previousRate.y = currentRate.y;
previousRate.z = currentRate.z;
/* On-the-fly PID tuning configuration —> Use the remote's aux channels for adjusting gains while the device is running */
// KpR = (0.0 + chAuxPot1()/5 + chAuxPot2()/2);
// Ki = (0 + (2*chAuxPot1()) + (4*chAuxPot2())) * SAMPLETIME_S;
// Kd = (0.0 + (chAuxPot1()/5) + (chAuxPot2()/2)) / SAMPLETIME_S;
}
void computePids(){
/* —————————————————————————————————— The Rate PID Controller —————————————————————————————————— */
currentRate.x = imu_rates().x; // read rotational rate from the IMU
currentRate.y = imu_rates().y;
currentRate.z = imu_rates().z;
errorRate.x = (-1 * chRoll()) - currentRate.x; // compute present error (proportional)
errorRate.y = (-1 * chPitch()) - currentRate.y;
errorRate.z = chYaw() - currentRate.z;
rateIntegral.x += KiR * errorRate.x; // compute the total accumulation of error (integral)
rateIntegral.y += KiR * errorRate.y;
rateIntegral.z += KiRZ * errorRate.z;
deltaRate.x = currentRate.x - previousRate.x; // compute the change in error (derivative)
deltaRate.y = currentRate.y - previousRate.y;
deltaRate.z = currentRate.z - previousRate.z;
rateOutput.x = KpR * errorRate.x + rateIntegral.x - KdR * deltaRate.x;
rateOutput.y = KpR * errorRate.y + rateIntegral.y - KdR * deltaRate.y;
rateOutput.z = KpRZ * errorRate.z + rateIntegral.z - KdRZ * deltaRate.z;
motorSpeed.one = (chThrottle() - rateOutput.y - rateOutput.z);
motorSpeed.two = (chThrottle() - rateOutput.x + rateOutput.z);
motorSpeed.three = (chThrottle() + rateOutput.y - rateOutput.z);
motorSpeed.four = (chThrottle() + rateOutput.x + rateOutput.z);
/* —————————————————————————————————— The Angle PID Controller ——————————————————————————————————
#ifdef HORIZON
currentAngle.x = imu_angles().x; //read angle from IMU and set it to the current angle
currentAngle.y = imu_angles().y;
currentAngle.z = imu_angles().z;
//currentAngle.z = 0;
#endif
// compute all the working error vars
// read rotational rate data (°/s) from remote and set it to the desired angle
error.x = chRoll() - currentAngle.x; //present error (instantaneous error)
error.y = chPitch() - currentAngle.y;
error.z = chYaw() - currentAngle.z;
errorSum.x += Ki * error.x; //integral of error (total accumulation of error)
errorSum.y += Ki * error.y;
errorSum.z += KiZ * error.z;
deltaError.x = currentAngle.x - lastAngle.x; //derivative of error (change in error)
deltaError.y = currentAngle.y - lastAngle.y;
deltaError.z = currentAngle.z - lastAngle.z;
/* —————————————————————————————————— Clamp the range of integral values ——————————————————————————————————
if(errorSum.x > MAX_INTEGRAL){
errorSum.x = MAX_INTEGRAL;
}else if (errorSum.x < (MAX_INTEGRAL * -1)){
errorSum.x = MAX_INTEGRAL * -1;
}
if(errorSum.y > MAX_INTEGRAL){
errorSum.y = MAX_INTEGRAL;
}else if (errorSum.y < (MAX_INTEGRAL * -1)){
errorSum.y = MAX_INTEGRAL * -1;
}
if(errorSum.z > MAX_INTEGRAL){
errorSum.z = MAX_INTEGRAL;
}else if (errorSum.z < (MAX_INTEGRAL * -1)){
errorSum.z = MAX_INTEGRAL * -1;
}
/* —————————————————————————————————— Calculate the motor speed adjustments for each axis of rotation based on PID calculations ——————————————————————————————————
outputX = (Kp * error.x + errorSum.x - Kd * deltaError.x);
outputY = (Kp * error.y + errorSum.y - Kd * deltaError.y);
outputZ = (KpZ * error.z + errorSum.z - KdZ * deltaError.z);
/* —————————————————————————————————— Write outputs to corresponding motors at the calculated speed ——————————————————————————————————
motorSpeed.one = (chThrottle() - outputX + outputY - outputZ);
motorSpeed.two = (chThrottle() + outputX + outputY + outputZ);
motorSpeed.three = (chThrottle() + outputX - outputY - outputZ);
motorSpeed.four = (chThrottle() - outputX - outputY + outputZ);
/* —————————————————————————————————— clamp the min and max output from the pid controller (to match the needed 0-255 for pwm) —————————————————————————————————— */
if(motorSpeed.one > ESC_MAX){
motorSpeed.one = ESC_MAX;
}else if (motorSpeed.one < ESC_MIN){
motorSpeed.one = ESC_MIN;
}else{ }
if(motorSpeed.two > ESC_MAX){
motorSpeed.two = ESC_MAX;
}else if (motorSpeed.two < ESC_MIN){
motorSpeed.two = ESC_MIN;
}else{ }
if(motorSpeed.three > ESC_MAX){
motorSpeed.three = ESC_MAX;
}else if (motorSpeed.three < ESC_MIN){
motorSpeed.three = ESC_MIN;
}else{ }
if(motorSpeed.four > ESC_MAX){
motorSpeed.four = ESC_MAX;
}else if (motorSpeed.four < ESC_MIN){
motorSpeed.four = ESC_MIN;
}else{ }
#ifdef PRINT_SERIALDATA
if(chAux2() == 0){
Serial.print("RX-X:");
Serial.print(chRoll());
Serial.print(", RX-Y:");
Serial.print(chPitch());
Serial.print(", Rate-X:");
Serial.print(currentRate.x);
Serial.print(", Rate-Y:");
Serial.println(currentRate.y);
}
#endif
}
void resetPids(){
if(chThrottle() < 10){
errorSum.x = 0;
errorSum.y = 0;
errorSum.z = 0;
rateIntegral.x = 0;
rateIntegral.y = 0;
rateIntegral.z = 0;
}else if(armingState() != lastArmingState()){
//reset the integral term when the quadcopter is armed
errorSum.x = 0;
errorSum.y = 0;
errorSum.z = 0;
rateIntegral.x = 0;
rateIntegral.y = 0;
rateIntegral.z = 0;
}
}
int_pwmOut motorPwmOut(){
return motorSpeed;
}