-
Notifications
You must be signed in to change notification settings - Fork 0
/
drone_final.ino
493 lines (413 loc) · 12.9 KB
/
drone_final.ino
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#include<Wire.h>
const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup() {
// put your setup code here, to run once:
initMPU6050();
//Serial.begin(115200);
Serial1.begin(115200);
calibAccelGyro();
initDT();
//accelNoiseTest();
initYPR();
initMotorSpeed();
}
void loop() {
// put your main code here, to run repeatedly:
readAccelGyro();
calcDT();
calcAccelYPR();
calcGyroYPR();
calcFilteredYPR();
//calcYPRtoStdPID();
calcYPRtoDualPID();
calcMotorSpeed();
checkMspPacket();
updateMotorSpeed();
//static int cnt;
//cnt++;
//if(cnt%2==0)
//SendDataToProcessing();
}
void initMPU6050() {
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
}
void readAccelGyro() {
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
Tmp=Wire.read()<<8|Wire.read();
GyX=Wire.read()<<8|Wire.read();
GyY=Wire.read()<<8|Wire.read();
GyZ=Wire.read()<<8|Wire.read();
}
float dt;
float accel_angle_x, accel_angle_y, accel_angle_z;
//float gyro_angle_x, gyro_angle_y, gyro_angle_z;
float filtered_angle_x, filtered_angle_y, filtered_angle_z;
extern float roll_output, pitch_output, yaw_output;
extern float motorA_speed, motorB_speed, motorC_speed, motorD_speed;
//void SendDataToProcessing(){
//Serial.print(F("DEL:"));
//Serial.print(dt,DEC);
//Serial.print(F("#RPY:"));
//Serial.print(filtered_angle_y, 2);
//Serial.print(F(","));
//Serial.print(filtered_angle_x, 2);
//Serial.print(F(","));
//Serial.print(filtered_angle_z, 2);
//Serial.print(F("#PID:"));
//Serial.print(roll_output, 2);
//Serial.print(F(","));
//Serial.print(pitch_output, 2);
//Serial.print(F(","));
//Serial.print(yaw_output, 2);
//Serial.print(F("#A:"));
//Serial.print(motorA_speed, 2);
//Serial.print(F("#B:"));
//Serial.print(motorB_speed, 2);
//Serial.print(F("#C:"));
//Serial.print(motorC_speed, 2);
//Serial.print(F("#D:"));
//Serial.print(motorD_speed, 2);
//delay(500);
//}
float baseAcX, baseAcY, baseAcZ;
float baseGyX, baseGyY, baseGyZ;
void calibAccelGyro() {
float sumAcX = 0, sumAcY = 0, sumAcZ = 0;
float sumGyX = 0, sumGyY = 0, sumGyZ = 0;
readAccelGyro();
for(int i=0;i<10;i++) {
readAccelGyro();
sumAcX += AcX, sumAcY += AcY, sumAcZ += AcZ;
sumGyX += GyX, sumGyY += GyY, sumGyZ += GyZ;
delay(100);
}
baseAcX = sumAcX / 10;
baseAcY = sumAcY / 10;
baseAcZ = sumAcZ / 10;
baseGyX = sumGyX / 10;
baseGyY = sumGyY / 10;
baseGyZ = sumGyZ / 10;
}
unsigned long t_now;
unsigned long t_prev;
void initDT() {
t_prev = micros();
}
void calcDT() {
t_now = micros();
dt = (t_now - t_prev)/1000000.0;
t_prev = t_now;
}
void calcAccelYPR() {
float accel_x, accel_y, accel_z;
float accel_xz, accel_yz;
const float RADIANS_TO_DEGREES = 180/3.14159;
accel_x = AcX - baseAcX;
accel_y = AcY - baseAcY;
accel_z = AcZ + (16384 - baseAcZ);
accel_yz = sqrt(pow(accel_y,2) + pow(accel_z,2));
accel_angle_y = atan(-accel_x/accel_yz)*RADIANS_TO_DEGREES;
accel_xz = sqrt(pow(accel_x,2) + pow(accel_z,2));
accel_angle_x = atan( accel_y/accel_xz)*RADIANS_TO_DEGREES;
accel_angle_z = 0;
}
float gyro_x, gyro_y, gyro_z;
void calcGyroYPR() {
const float GYROXYZ_TO_DEGREES_PER_SEC = 131;
gyro_x =(GyX - baseGyX)/GYROXYZ_TO_DEGREES_PER_SEC;
gyro_y =(GyY - baseGyY)/GYROXYZ_TO_DEGREES_PER_SEC;
gyro_z =(GyZ - baseGyZ)/GYROXYZ_TO_DEGREES_PER_SEC;
//gyro_angle_x += gyro_x * dt;
//gyro_angle_y += gyro_y * dt;
//gyro_angle_z += gyro_z * dt;
}
void calcFilteredYPR() {
const float ALPHA = 0.96;
float tmp_angle_x, tmp_angle_y, tmp_angle_z;
tmp_angle_x = filtered_angle_x + gyro_x * dt;
tmp_angle_y = filtered_angle_y + gyro_y * dt;
tmp_angle_z = filtered_angle_z + gyro_z * dt;
filtered_angle_x = ALPHA * tmp_angle_x + (1.0-ALPHA) * accel_angle_x;
filtered_angle_y = ALPHA * tmp_angle_y + (1.0-ALPHA) * accel_angle_y;
filtered_angle_z = tmp_angle_z;
}
//void accelNoiseTest() {
// analogWrite( 6, 40);
// analogWrite(10, 40);
// analogWrite( 9, 40);
// analogWrite( 5, 40);
//}
//void stdPID( float& setpoint,
//float& input,
//float& prev_input,
//float& kp,
//float& ki,
//float& kd,
//float& iterm,
//float& output){
// float error;
// float dInput;
// float pterm, dterm;
// error = setpoint - input;
// dInput = input - prev_input;
// prev_input = input;
// pterm = kp*error;
// iterm += ki*error*dt;
// dterm = -kd*dInput/dt;
// output = pterm + iterm + dterm;
//}
//float roll_target_angle = 0.0;
//float roll_prev_angle = 0.0;
//float roll_kp = 1;
//float roll_ki = 0;
//float roll_kd = 0;
//float roll_iterm;
//float roll_output;
//float pitch_target_angle = 0.0;
//float pitch_prev_angle = 0.0;
//float pitch_kp = 1;
//float pitch_ki = 0;
//float pitch_kd = 0;
//float pitch_iterm;
//float pitch_output;
//float yaw_target_angle = 0.0;
//float yaw_prev_angle = 0.0;
//float yaw_kp = 1;
//float yaw_ki = 0;
//float yaw_kd = 0;
//float yaw_iterm;
//float yaw_output;
float base_roll_target_angle;
float base_pitch_target_angle;
float base_yaw_target_angle;
extern float roll_target_angle;
extern float pitch_target_angle;
extern float yaw_target_angle;
void initYPR() {
for(int i=0;i<10;i++){
readAccelGyro();
calcDT();
calcAccelYPR();
calcGyroYPR();
calcFilteredYPR();
base_roll_target_angle += filtered_angle_y;
base_pitch_target_angle += filtered_angle_x;
base_yaw_target_angle += filtered_angle_z;
delay(100);
}
base_roll_target_angle /= 10;
base_pitch_target_angle /= 10;
base_yaw_target_angle /= 10;
roll_target_angle = base_roll_target_angle;
pitch_target_angle = base_pitch_target_angle;
yaw_target_angle = base_yaw_target_angle;
}
//void calcYPRtoStdPID() {
//stdPID( roll_target_angle,
//filtered_angle_y,
//roll_prev_angle,
//roll_kp,
//roll_ki,
//roll_kd,
//roll_iterm,
//roll_output);
// stdPID( pitch_target_angle,
//filtered_angle_x,
//pitch_prev_angle,
//pitch_kp,
//pitch_ki,
//pitch_kd,
//pitch_iterm,
//pitch_output);
//stdPID( yaw_target_angle,
//filtered_angle_z,
//yaw_prev_angle,
//yaw_kp,
//yaw_ki,
//yaw_kd,
//yaw_iterm,
//yaw_output);
//}
float throttle = 0;
float motorA_speed, motorB_speed, motorC_speed, motorD_speed;
void calcMotorSpeed() {
motorA_speed = (throttle == 0) ? 0:
throttle + yaw_output + roll_output + pitch_output;
motorB_speed = (throttle == 0) ? 0:
throttle - yaw_output - roll_output + pitch_output;
motorC_speed = (throttle == 0) ? 0:
throttle + yaw_output - roll_output - pitch_output;
motorD_speed = (throttle == 0) ? 0:
throttle - yaw_output + roll_output - pitch_output;
if(motorA_speed < 0) motorA_speed = 0;
if(motorA_speed > 255) motorA_speed = 255;
if(motorB_speed < 0) motorB_speed = 0;
if(motorB_speed > 255) motorB_speed = 255;
if(motorC_speed < 0) motorC_speed = 0;
if(motorC_speed > 255) motorC_speed = 255;
if(motorD_speed < 0) motorD_speed = 0;
if(motorD_speed >255) motorD_speed = 255;
}
enum {
HEAD1, HEAD2, HEAD3, DATASIZE, CMD,
ROLL, PITCH, YAW, THROTTLE,
AUX, CRC, PACKETSIZE,
};
uint8_t mspPacket[PACKETSIZE];
void checkMspPacket(){
static uint32_t cnt;
if(Serial1.available() > 0){
while(Serial1.available() > 0){
uint8_t mspData = Serial1.read();
if(mspData == '$') cnt = HEAD1;
else cnt++;
mspPacket[cnt] = mspData;
if(cnt == CRC) {
if(mspPacket[CMD] == 150) {
throttle = mspPacket[THROTTLE];
roll_target_angle = base_roll_target_angle;
pitch_target_angle = base_pitch_target_angle;
yaw_target_angle = base_yaw_target_angle;
roll_target_angle += mspPacket[ROLL]-125;
pitch_target_angle += -(mspPacket[PITCH]-125);
yaw_target_angle += -(mspPacket[YAW]-125);
//1.smart throttle
float throttle_user;
float throttle_prev;
float throttle_now;
#define THROTTLE_CHANGE
throttle_user = throttle;
if(throttle_user<=throttle_prev-THROTTLE_CHANGE){
throttle_now = throttle_prev-THROTTLE_CHANGE;
}else{
throttle_now=throttle_user;
}
throttle = throttle_now;
throttle_prev = throttle_now;
}
}
}
}
}
#define THROTTLE_MAX 255
#define THROTTLE_MIN 0
int motorA_pin = 6;
int motorB_pin = 10;
int motorC_pin = 9;
int motorD_pin = 5;
void initMotorSpeed(){
analogWrite(motorA_pin, THROTTLE_MIN);
analogWrite(motorB_pin, THROTTLE_MIN);
analogWrite(motorC_pin, THROTTLE_MIN);
analogWrite(motorD_pin, THROTTLE_MIN);
}
void updateMotorSpeed(){
analogWrite(motorA_pin, motorA_speed);
analogWrite(motorB_pin, motorB_speed);
analogWrite(motorC_pin, motorC_speed);
analogWrite(motorD_pin, motorD_speed);
}
void dualPID(
float target_angle,
float angle_in,
float rate_in,
float stabilize_kp,
float stabilize_ki,
float rate_kp,
float rate_ki,
float& stabilize_iterm,
float& rate_iterm,
float& output
){
float angle_error;
float desired_rate;
float rate_error;
float stabilize_pterm, rate_pterm;
angle_error = target_angle - angle_in;
stabilize_pterm = stabilize_kp * angle_error;
stabilize_iterm += stabilize_ki * angle_error*dt;
desired_rate = stabilize_pterm;
rate_error = desired_rate - rate_in;
rate_pterm = rate_kp * rate_error;
rate_iterm += rate_ki * rate_error*dt;
output = rate_pterm + rate_iterm + stabilize_iterm;
}
float roll_target_angle = 0.0;
float roll_angle_in;
float roll_rate_in;
float roll_stabilize_kp = 1;
float roll_stabilize_ki = 0;
float roll_rate_kp = 1;
float roll_rate_ki = 0;
float roll_stabilize_iterm;
float roll_rate_iterm;
float roll_output;
float pitch_target_angle = 0.0;
float pitch_angle_in;
float pitch_rate_in;
float pitch_stabilize_kp = 1;
float pitch_stabilize_ki = 0;
float pitch_rate_kp = 1;
float pitch_rate_ki = 0;
float pitch_stabilize_iterm;
float pitch_rate_iterm;
float pitch_output;
float yaw_target_angle = 0.0;
float yaw_angle_in;
float yaw_rate_in;
float yaw_stabilize_kp = 1;
float yaw_stabilize_ki = 0;
float yaw_rate_kp = 1;
float yaw_rate_ki = 0;
float yaw_stabilize_iterm;
float yaw_rate_iterm;
float yaw_output;
void calcYPRtoDualPID(){
roll_angle_in = filtered_angle_y;
roll_rate_in = gyro_y;
dualPID( roll_target_angle,
roll_angle_in,
roll_rate_in,
roll_stabilize_kp,
roll_stabilize_ki,
roll_rate_kp,
roll_rate_ki,
roll_stabilize_iterm,
roll_rate_iterm,
roll_output);
pitch_angle_in = filtered_angle_x;
pitch_rate_in = gyro_x;
dualPID( pitch_target_angle,
pitch_angle_in,
pitch_rate_in,
pitch_stabilize_kp,
pitch_stabilize_ki,
pitch_rate_kp,
pitch_rate_ki,
pitch_stabilize_iterm,
pitch_rate_iterm,
pitch_output);
yaw_angle_in = filtered_angle_z;
yaw_rate_in = gyro_z;
dualPID( yaw_target_angle,
yaw_angle_in,
yaw_rate_in,
yaw_stabilize_kp,
yaw_stabilize_ki,
yaw_rate_kp,
yaw_rate_ki,
yaw_stabilize_iterm,
yaw_rate_iterm,
yaw_output);
}