forked from AAUAutomationAndControl/PREDICT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcs_multi.c
More file actions
623 lines (433 loc) · 18.3 KB
/
fcs_multi.c
File metadata and controls
623 lines (433 loc) · 18.3 KB
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//
// Copyright 2018 Aalborg University, Denamrk.
// All rights reserved.
// Author: Shibarchi Majumder (sm@es.aau.dk)
// Oktay Baris (okba@dtu.dk)
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// author, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, author, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
// NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are
// those of the authors and should not be interpreted as representing official
// policies, either expressed or implied, of the copyright holder.
//
//
const int NOC_MASTER = 0;
#include <machine/patmos.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "libcorethread/corethread.h"
#define AUTO_TAKEOFF 1
#define ALT_HOLD 2
#define AUTO_THROTTLE 3
#define CLIMB 4
typedef unsigned char BYTE;
_iodev_ptr_t uart1_ptr = (_iodev_ptr_t) 0xF0080000;
_iodev_ptr_t uart2_ptr = (_iodev_ptr_t) 0xF00E0000;
_iodev_ptr_t uart3_ptr = (_iodev_ptr_t) 0xF00F0000;
BYTE out_elev_H, out_elev_L, out_ail_H, out_ail_L, out_rud_H, out_rud_L, out_thr_H, out_thr_L;
int mode = AUTO_TAKEOFF;
float pitch = 0;
float pitch2 = 0;
float roll = 0;
float heading = 0;
const float dt = 0.05;
float longitude, latitude;
// Data Fusion parameters
_UNCACHED volatile float acc_x = 0;
_UNCACHED volatile float acc_y = 0;
_UNCACHED volatile float acc_z = 0;
_UNCACHED volatile float P = 0;
_UNCACHED volatile float Q = 0;
_UNCACHED volatile float ias_sen = 0;
_UNCACHED volatile float p_ias_sen = 0;
_UNCACHED volatile float alt_ft_msl = 0;
_UNCACHED volatile float alt_ft_agl = 0;
_UNCACHED volatile float theta_cal = 0.0;
_UNCACHED volatile float phi_cal = 0.0;
float P0_1_1 = 0;
float P0_2_1 = 0;
float P0_1_2 = 0;
float P0_2_2 = 0;
float P1_1_1 = 0;
float P1_2_1 = 0;
float P1_1_2 = 0;
float P1_2_2 = 0;
const float gyro_noise = 0.001;
const float gyro_bias = 0.03;
const float Sensor_accuracy = 0.03;
float Kalman_gain_0_1 = 0;
float Kalman_gain_0_2 = 0;
float Kalman_gain_1_1 = 0;
float Kalman_gain_1_2 = 0;
float bias_1 = 0;
float bias_2 = 0;
float c99max (float a, float lowest){
if (a< lowest){
a = lowest;
}
return a;
}
//Thread 1
void Estimator1(void *arg) {
acc_x = (acc_x - (ias_sen - p_ias_sen)/dt);
float theta_acc = (float)(atan2(acc_x, sqrt(acc_z*acc_z + acc_y*acc_y))*57.324);
float temp1 = P - bias_1;
theta_cal += dt * temp1;
P0_1_1 += dt * (dt* P0_2_2 - P0_1_2 - P0_2_1 + gyro_noise);
P0_1_2 -= dt * P0_2_2;
P0_2_1 -= dt * P0_2_2;
P0_2_2 += gyro_bias * dt;
float temp3 = c99max(P0_1_1 + Sensor_accuracy, 0.00001);
//calculate Kalman Gain
Kalman_gain_0_1 = c99max(P0_1_1 / temp3, .000001);
Kalman_gain_0_2 = c99max(P0_1_2 / temp3, .000001);
// calculate system state
theta_cal += Kalman_gain_0_1 * (theta_acc - theta_cal);
bias_1 += Kalman_gain_0_2 * (theta_acc - theta_cal);
//Calculate Aposteriori Covarience
P0_1_1 -= Kalman_gain_0_1 * P0_1_1;
P0_1_2 -= Kalman_gain_0_1 * P0_1_2;
P0_2_1 -= Kalman_gain_0_2 * P0_2_1;
P0_2_2 -= Kalman_gain_0_2 * P0_2_2;
}
// Thread 2
void Estimator2(void *arg) {
p_ias_sen = ias_sen;
float phi_acc = (float)(atan2(acc_y,acc_z)*57.324);
float temp2 = Q- bias_2;
phi_cal += dt * temp2;
P1_1_1 += dt * (dt* P1_2_2 - P1_1_2 - P1_2_1 + gyro_noise);
P1_1_2 -= dt * P1_2_2;
P1_2_1 -= dt * P1_2_2;
P1_2_2 += gyro_bias * dt;
float temp4 = c99max(P1_1_1 + Sensor_accuracy, 0.00001);
//calculate Kalman Gain
Kalman_gain_1_1 = c99max(P1_1_1 / temp4, .000001);
Kalman_gain_1_2 = c99max(P1_1_2 / temp4, .000001);
// calculate system state
phi_cal += Kalman_gain_1_1 * (phi_acc - phi_cal);
bias_2 += Kalman_gain_1_2 * (phi_acc - phi_cal);
//Calculate Aposteriori Covarience
P1_1_1 -= Kalman_gain_1_1 * P1_1_1;
P1_1_2 -= Kalman_gain_1_1 * P1_1_2;
P1_2_1 -= Kalman_gain_1_2 * P1_2_1;
P1_2_2 -= Kalman_gain_1_2 * P1_2_2;
}
void Estimator(void *arg){
acc_x = (acc_x - (ias_sen - p_ias_sen)/dt);
p_ias_sen = ias_sen;
float theta_acc = (float)(atan2(acc_x, sqrt(acc_z*acc_z + acc_y*acc_y))*57.324);
float phi_acc = (float)(atan2(acc_y,acc_z)*57.324);
float temp1 = P - bias_1;
theta_cal += dt * temp1;
float temp2 = Q- bias_2;
phi_cal += dt * temp2;
P0_1_1 += dt * (dt* P0_2_2 - P0_1_2 - P0_2_1 + gyro_noise);
P0_1_2 -= dt * P0_2_2;
P0_2_1 -= dt * P0_2_2;
P0_2_2 += gyro_bias * dt;
P1_1_1 += dt * (dt* P1_2_2 - P1_1_2 - P1_2_1 + gyro_noise);
P1_1_2 -= dt * P1_2_2;
P1_2_1 -= dt * P1_2_2;
P1_2_2 += gyro_bias * dt;
float temp3 = c99max(P0_1_1 + Sensor_accuracy, 0.00001);
float temp4 = c99max(P1_1_1 + Sensor_accuracy, 0.00001);
Kalman_gain_0_1 = c99max(P0_1_1 / temp3, .000001);
Kalman_gain_0_2 = c99max(P0_1_2 / temp3, .000001);
Kalman_gain_1_1 = c99max(P1_1_1 / temp4, .000001);
Kalman_gain_1_2 = c99max(P1_1_2 / temp4, .000001);
theta_cal += Kalman_gain_0_1 * (theta_acc - theta_cal);
phi_cal += Kalman_gain_1_1 * (phi_acc - phi_cal);
bias_1 += Kalman_gain_0_2 * (theta_acc - theta_cal);
bias_2 += Kalman_gain_1_2 * (phi_acc - phi_cal);
P0_1_1 -= Kalman_gain_0_1 * P0_1_1;
P0_1_2 -= Kalman_gain_0_1 * P0_1_2;
P0_2_1 -= Kalman_gain_0_2 * P0_2_1;
P0_2_2 -= Kalman_gain_0_2 * P0_2_2;
P1_1_1 -= Kalman_gain_1_1 * P1_1_1;
P1_1_2 -= Kalman_gain_1_1 * P1_1_2;
P1_2_1 -= Kalman_gain_1_2 * P1_2_1;
P1_2_2 -= Kalman_gain_1_2 * P1_2_2;
}
int receiving = 0;
int bit_1 = 0;
int bit_2 = 0;
int bit_3 = 0;
int elev, ail, rudder, throttle, n_wheel;
int all_received = 0;
int total_bytes = 52;
int i = 0;
unsigned char raw_bytes_from_sim[53];//original value was 52
float p_control_accu_err = 0;
float p_control_d_err = 0;
float cruise_control_accu_err = 0;
float cruise_control_d_err = 0;
float pitch_control_prop_gain = 2;
float pitch_control_diff_gain = 2;
float pitch_control_int_gain = 0.001;
float cruise_control_prop_gain = 0.020;
float cruise_control_diff_gain = 0.02;
float cruise_control_int_gain = 0.0001;
void longitudinal_control(int d_pitch) {
rudder = 0;
float err = d_pitch - pitch;
p_control_accu_err += err;
float der = err - p_control_d_err;
p_control_d_err = err;
elev = (int) (err * pitch_control_prop_gain + (p_control_accu_err *pitch_control_int_gain) + der * pitch_control_diff_gain);
//printf("err: %f, derr: %f, ierr: %f, elev: %d \n", err, der, p_control_accu_err, elev );
}
float min_max(float max, float min, float val){
if (val > max){
val = max;
}
else if(val < min){
val = min;
}
return val;
}
void cruise_control(int altitude) {
rudder = 0;
throttle = 60;
float err = altitude - alt_ft_msl;
cruise_control_accu_err += err;
float der = err - cruise_control_d_err;
longitudinal_control(min_max(15,-15,(err * cruise_control_prop_gain )));
}
void delay(int a) {
for (int i = 0; i < a * 10000; i++) {
}
}
void inline write(_iodev_ptr_t uart_base_ptr, unsigned char c) {
while ((*uart_base_ptr & 0x01) == 0);
*(uart_base_ptr+1) = c;
}
char inline read(_iodev_ptr_t uart_base_ptr) {
while ((*uart_base_ptr & 0x02) == 0);
return *(uart_base_ptr+1);
}
int safe_byte(float a) {
int temp = (int) a;
temp = temp + 100;
if (temp > 200) {
temp = 200;
}
else if (temp < 0) {
temp = 0;
}
return temp;
}
float roll_p = 1;
float roll_d = 30;
float roll_i = 0.0001;
float r_control_accu_err = 0.0;
float r_control_d_err = 0.0;
void lateral_control(float d_roll) {
float err = d_roll - roll;
r_control_accu_err += err;
float der = err - r_control_d_err;
r_control_d_err = err;
ail = (int) (err * roll_p + (r_control_accu_err *roll_i) + der * roll_d);
}
void HDG_control (float d_hdg){
float err = d_hdg - heading;
if(err >= 180){
lateral_control(min_max(10,-10,-err*1));
}
else{
lateral_control(min_max(10,-10,err*1));
}
}
float conv_to_float(unsigned char byte1, unsigned char byte2,unsigned char byte3, unsigned char byte4 ){
union u_tag {
unsigned char b[4];
float fval;
} u;
u.b[0] = byte1;
u.b[1] = byte2;
u.b[2] = byte3;
u.b[3] = byte4;
return u.fval;
}
int main() {
printf("Patmos Started!!");
//int id = get_cpuid();
//int cnt = get_cpucnt();
for(;;){
unsigned char serial = read(uart2_ptr);
if (receiving == 1) {
i += 1;
}
if (serial == 255 && receiving == 0) {
bit_1 = 1;
}
else if (serial == 254 && receiving == 0 && bit_1 == 1){
bit_2 = 1;
}
else if (serial == 253 && receiving == 0 && bit_2 == 1) {
bit_3 = 1;
}
else if (serial == 252 && receiving == 0 && bit_3 == 1) {
receiving = 1;
bit_1 = 0;
bit_2 = 0;
bit_3 = 0;
}
else {
bit_1 = 0;
bit_2 = 0;
bit_3 = 0;
}
raw_bytes_from_sim[i] = serial;
if (i == total_bytes) {
receiving = 0;
i = 0;
bit_1 = 0;
bit_2 = 0;
bit_3 = 0;
all_received = 1;
}
if(all_received == 1){
//unsigned char b2[] = { raw_bytes_from_sim[4], raw_bytes_from_sim[3],raw_bytes_from_sim[2],raw_bytes_from_sim[1] };
//memcpy(&pitch, &b2, sizeof(pitch));
//pitch2 = (float)((raw_bytes_from_sim[4] << 24) | (raw_bytes_from_sim[3] << 16) | (raw_bytes_from_sim[2] <<8) | (raw_bytes_from_sim[1])) ;
pitch = conv_to_float(raw_bytes_from_sim[4], raw_bytes_from_sim[3],raw_bytes_from_sim[2],raw_bytes_from_sim[1]);
//printf(" Pitch1 : %b \n", pitch);
//printf(" Pitch2 : %b \n", pitch2);
//unsigned char c[] = { raw_bytes_from_sim[8], raw_bytes_from_sim[7],raw_bytes_from_sim[6],raw_bytes_from_sim[5] };
//memcpy(&roll, &c, sizeof(roll));
//roll = (raw_bytes_from_sim[5] << 24) | (raw_bytes_from_sim[6] << 16) | (raw_bytes_from_sim[7] <<8) | (raw_bytes_from_sim[8]) ;
roll = conv_to_float(raw_bytes_from_sim[8], raw_bytes_from_sim[7],raw_bytes_from_sim[6],raw_bytes_from_sim[5]);
//unsigned char d[] = { raw_bytes_from_sim[12], raw_bytes_from_sim[11],raw_bytes_from_sim[10],raw_bytes_from_sim[9] };
//memcpy(&heading, &d, sizeof(heading));
//heading = (raw_bytes_from_sim[9] << 24) | (raw_bytes_from_sim[10] << 16) | (raw_bytes_from_sim[11] <<8) | (raw_bytes_from_sim[12]) ;
heading = conv_to_float(raw_bytes_from_sim[12], raw_bytes_from_sim[11],raw_bytes_from_sim[10],raw_bytes_from_sim[9]);
//unsigned char e[] = { raw_bytes_from_sim[16], raw_bytes_from_sim[15],raw_bytes_from_sim[14],raw_bytes_from_sim[13] };
//memcpy(&ias_sen, &e, sizeof(ias_sen));
//ias_sen = (raw_bytes_from_sim[13] << 24) | (raw_bytes_from_sim[14] << 16) | (raw_bytes_from_sim[15] <<8) | (raw_bytes_from_sim[16]) ;
ias_sen = conv_to_float(raw_bytes_from_sim[16], raw_bytes_from_sim[15],raw_bytes_from_sim[14],raw_bytes_from_sim[13]);
//unsigned char f[] = { raw_bytes_from_sim[20], raw_bytes_from_sim[19],raw_bytes_from_sim[18],raw_bytes_from_sim[17] };
//memcpy(&alt_ft_msl, &f, sizeof(alt_ft_msl));
//alt_ft_msl = (raw_bytes_from_sim[17] << 24) | (raw_bytes_from_sim[18] << 16) | (raw_bytes_from_sim[19] <<8) | (raw_bytes_from_sim[20]) ;
alt_ft_msl = conv_to_float(raw_bytes_from_sim[20], raw_bytes_from_sim[19],raw_bytes_from_sim[18],raw_bytes_from_sim[17]);
//unsigned char g[] = { raw_bytes_from_sim[24], raw_bytes_from_sim[23],raw_bytes_from_sim[22],raw_bytes_from_sim[21] };
//memcpy(&alt_ft_agl, &g, sizeof(alt_ft_agl));
//alt_ft_agl = (raw_bytes_from_sim[21] << 24) | (raw_bytes_from_sim[22] << 16) | (raw_bytes_from_sim[23] <<8) | (raw_bytes_from_sim[24]) ;
alt_ft_agl = conv_to_float(raw_bytes_from_sim[24], raw_bytes_from_sim[23],raw_bytes_from_sim[22],raw_bytes_from_sim[21]);
//unsigned char h[] = { raw_bytes_from_sim[28], raw_bytes_from_sim[27],raw_bytes_from_sim[26],raw_bytes_from_sim[25] };
//memcpy(&acc_x, &h, sizeof(acc_x));
//acc_x = (raw_bytes_from_sim[25] << 24) | (raw_bytes_from_sim[26] << 16) | (raw_bytes_from_sim[27] <<8) | (raw_bytes_from_sim[28]) ;
acc_x = conv_to_float(raw_bytes_from_sim[28], raw_bytes_from_sim[27],raw_bytes_from_sim[26],raw_bytes_from_sim[25]);
//unsigned char i[] = { raw_bytes_from_sim[32], raw_bytes_from_sim[31],raw_bytes_from_sim[30],raw_bytes_from_sim[29] };
//memcpy(&acc_y, &i, sizeof(acc_y));
//acc_y = (raw_bytes_from_sim[29] << 24) | (raw_bytes_from_sim[30] << 16) | (raw_bytes_from_sim[31] <<8) | (raw_bytes_from_sim[32]) ;
acc_y = conv_to_float(raw_bytes_from_sim[32], raw_bytes_from_sim[31],raw_bytes_from_sim[30],raw_bytes_from_sim[29]);
//unsigned char j[] = { raw_bytes_from_sim[36], raw_bytes_from_sim[35],raw_bytes_from_sim[34],raw_bytes_from_sim[33] };
//memcpy(&acc_z, &j, sizeof(acc_z));
//acc_z = (raw_bytes_from_sim[33] << 24) | (raw_bytes_from_sim[34] << 16) | (raw_bytes_from_sim[35] <<8) | (raw_bytes_from_sim[36]) ;
acc_z = conv_to_float(raw_bytes_from_sim[36], raw_bytes_from_sim[35],raw_bytes_from_sim[34],raw_bytes_from_sim[33]);
//unsigned char k[] = { raw_bytes_from_sim[40], raw_bytes_from_sim[39],raw_bytes_from_sim[38],raw_bytes_from_sim[37] };
//memcpy(&P, &k, sizeof(P));
//P = (raw_bytes_from_sim[40] << 24) | (raw_bytes_from_sim[39] << 16) | (raw_bytes_from_sim[38] <<8) | (raw_bytes_from_sim[37]) ;
P = conv_to_float(raw_bytes_from_sim[40], raw_bytes_from_sim[39],raw_bytes_from_sim[38],raw_bytes_from_sim[37]);
//unsigned char l[] = { raw_bytes_from_sim[44], raw_bytes_from_sim[43],raw_bytes_from_sim[42],raw_bytes_from_sim[41] };
//memcpy(&Q, &l, sizeof(Q));
//Q = (raw_bytes_from_sim[41] << 24) | (raw_bytes_from_sim[42] << 16) | (raw_bytes_from_sim[43] <<8) | (raw_bytes_from_sim[44]) ;
Q = conv_to_float(raw_bytes_from_sim[44], raw_bytes_from_sim[43],raw_bytes_from_sim[42],raw_bytes_from_sim[41]);
//unsigned char m[] = { raw_bytes_from_sim[48], raw_bytes_from_sim[47],raw_bytes_from_sim[46],raw_bytes_from_sim[45] };
//memcpy(&latitude, &m, sizeof(latitude));
//latitude = (raw_bytes_from_sim[45] << 24) | (raw_bytes_from_sim[46] << 16) | (raw_bytes_from_sim[47] <<8) | (raw_bytes_from_sim[48]) ;
latitude = conv_to_float(raw_bytes_from_sim[48], raw_bytes_from_sim[47],raw_bytes_from_sim[46],raw_bytes_from_sim[45]);
//unsigned char n[] = { raw_bytes_from_sim[52], raw_bytes_from_sim[51],raw_bytes_from_sim[50],raw_bytes_from_sim[49] };
//memcpy(&longitude, &n, sizeof(longitude));
//longitude = (raw_bytes_from_sim[49] << 24) | (raw_bytes_from_sim[50] << 16) | (raw_bytes_from_sim[51] <<8) | (raw_bytes_from_sim[52]) ;
longitude = conv_to_float(raw_bytes_from_sim[52], raw_bytes_from_sim[51],raw_bytes_from_sim[50],raw_bytes_from_sim[49]);
// this will be paralellized
//Estimator( acc_x, acc_y, acc_z, P, Q);
int slave_param =1;
corethread_create(1, &Estimator1, (void*)slave_param);
corethread_create(2, &Estimator2, (void*)slave_param);
corethread_join(1,(void*)slave_param);
corethread_join(2,(void*)slave_param);
//corethread_create(1, &Estimator, (void*)slave_param);
//corethread_join(1,(void*)slave_param);
//Estimator();
/*pitch_control_prop_gain = raw_bytes_from_sim[25];
pitch_control_diff_gain = raw_bytes_from_sim[26];
pitch_control_int_gain = raw_bytes_from_sim[27];
pitch_control_prop_gain = pitch_control_prop_gain / 10;
pitch_control_diff_gain = pitch_control_diff_gain / 100;
pitch_control_int_gain = pitch_control_int_gain / 10000;*/
printf("Pitch: %f, Roll: %f, IAS: %f, accx: %f, accy: %f, accz: %f, P: %f Q: %f lat: %f lon: %f thetac: %f phic: %f\n",
pitch, roll, ias_sen, acc_x, acc_y, acc_z, P, Q, latitude, longitude, theta_cal, phi_cal);
pitch = theta_cal;
roll = phi_cal;
//printf("%d %d %d %d \n",raw_bytes_from_sim[36], raw_bytes_from_sim[35], raw_bytes_from_sim[34], raw_bytes_from_sim[33]);
}
all_received = 0;
if (mode == AUTO_TAKEOFF) {
n_wheel = 0;
throttle = 100;
rudder = 0;
elev = 05;
ail = 0;
if (ias_sen > 170) {
mode = CLIMB;
}
}
else if (mode == CLIMB) {
if (alt_ft_msl <= 3000) {
longitudinal_control(15);
lateral_control(0);
}
else {
mode = ALT_HOLD;
}
}
else if (mode == ALT_HOLD) {
HDG_control(150);
cruise_control(4000);
}
elev = safe_byte(elev);
ail = safe_byte(ail);
rudder = safe_byte(rudder);
throttle = safe_byte(throttle);
/*out_elev_L = (elev & 0xFF);
out_elev_H = ((elev >> 8) & 0xFF);
out_ail_L = (ail & 0xFF);
out_ail_H = ((ail >> 8) & 0xFF);
out_rud_L = (rudder & 0xFF);
out_rud_H = ((rudder >> 8) & 0xFF);
out_thr_L = (throttle & 0xFF);
out_thr_H = ((throttle >> 8) & 0xFF);*/
BYTE send_sequence [] = { 255,254,253,252, elev, ail, rudder, throttle};
//printf("Elev: %d,aileron: %d, rudder: %d throttle: %d \n", elev, ail, rudder, throttle );
for (int send_i = 0; send_i < 8; send_i ++){
write(uart2_ptr,send_sequence[send_i]);
}
if(ias_sen < 20){
mode = AUTO_TAKEOFF;
}
}//for
}