forked from AAUAutomationAndControl/PREDICT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcs_multi_onchip.c
544 lines (385 loc) · 12.9 KB
/
fcs_multi_onchip.c
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
//
// Copyright 2018 Aalborg University, Denamrk.
// All rights reserved.
// Author: Shibarchi Majumder ([email protected])
// Oktay Baris ([email protected])
//
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
//shared Scrachpad memory ptr
#define SSPM_BASE ((volatile float _SPM *) 0xE8020000)
// Data SPM
#define SPM_COMP_BASE ((volatile float _SPM *) 0x00000000)
// a timer for measuring the execution time
volatile int _SPM *timer_ptr = (volatile int _SPM *) (PATMOS_IO_TIMER+4);
//for measurements to locate them in the external memory
_UNCACHED volatile int start_measurement;
_UNCACHED volatile int stop_measurement;
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;
//flags for synchronisation
_UNCACHED volatile int flag_est1 = 0;
_UNCACHED volatile int flag_est2 = 0;
// Sensory Data
_UNCACHED volatile float pitch = 0;
_UNCACHED volatile float roll = 0;
_UNCACHED volatile float heading = 0; // not used
_UNCACHED volatile float ias_sen = 0;
_UNCACHED volatile float alt_ft_msl = 0;
_UNCACHED volatile float alt_ft_agl = 0; // not used
_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 latitude = 0; //not used
_UNCACHED volatile float longitude = 0; // not used
//Estimator produced data for Intercore Communication
_UNCACHED volatile float theta_cal = 0.0; // estimator 1
_UNCACHED volatile float phi_cal = 0.0; // estimator 2
// Constants
const float gyro_noise = 0.001;
const float gyro_bias = 0.03;
const float Sensor_accuracy = 0.03;
const float dt = 0.05;
float c99max (float a, float lowest){
if (a< lowest){
a = lowest;
}
return a;
}
//Thread 1
void Estimator1(void *arg) {
//Declare
volatile _SPM float *p_ias_sen = SPM_COMP_BASE;
volatile _SPM float *P0_1_1 = SPM_COMP_BASE+1;
volatile _SPM float *P0_2_1 = SPM_COMP_BASE+2;
volatile _SPM float *P0_1_2 = SPM_COMP_BASE+3;
volatile _SPM float *P0_2_2 = SPM_COMP_BASE+4;
volatile _SPM float *bias_1 = SPM_COMP_BASE+5;
volatile _SPM float *Kalman_gain_0_1 = SPM_COMP_BASE+6;
volatile _SPM float *Kalman_gain_0_2 = SPM_COMP_BASE+7;
//Init
*p_ias_sen = 0;
*P0_1_1 = 0;
*P0_2_1 = 0;
*P0_1_2 = 0;
*P0_2_2 = 0;
*bias_1 = 0;
*Kalman_gain_0_1 = 0;
*Kalman_gain_0_2 = 0;
float theta_acc;
float temp1;
float temp3;
for(;;){
while( !( flag_est1 == 1 ) ){
;
}
//start_measurement = *timer_ptr;
acc_x = (acc_x - (ias_sen - *p_ias_sen)/dt);
*p_ias_sen = ias_sen;
theta_acc = (float)(atan2(acc_x, sqrt(acc_z*acc_z + acc_y*acc_y))*57.324);
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;
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);
flag_est1 = 0;
//stop_measurement = *timer_ptr;
}
}
// Thread 2
void Estimator2(void *arg) {
//declare
volatile _SPM float *P1_1_1 = SPM_COMP_BASE;
volatile _SPM float *P1_2_1 = SPM_COMP_BASE+1;
volatile _SPM float *P1_1_2 = SPM_COMP_BASE+2;
volatile _SPM float *P1_2_2 = SPM_COMP_BASE+3;
volatile _SPM float *bias_2 = SPM_COMP_BASE+4;
volatile _SPM float *Kalman_gain_1_1 = SPM_COMP_BASE+5;
volatile _SPM float *Kalman_gain_1_2 = SPM_COMP_BASE+6;
//init
*P1_1_1 = 0;
*P1_2_1 = 0;
*P1_1_2 = 0;
*P1_2_2 = 0;
*bias_2 = 0;
*Kalman_gain_1_1 = 0;
*Kalman_gain_1_2 = 0;
float phi_acc;
float temp2;
float temp4;
for(;;){
while( !( flag_est2 == 1 ) ){
;
}
//start_measurement = *timer_ptr;
phi_acc = (float)(atan2(acc_y,acc_z)*57.324);
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;
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);
flag_est2 = 0;
// stop_measurement = *timer_ptr;
}
}
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];
// controller variables
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) {
//#pragma loopbound min 0 max 99
while ((*uart_base_ptr & 0x01) == 0);
*(uart_base_ptr+1) = c;
}
char inline read(_iodev_ptr_t uart_base_ptr) {
//#pragma loopbound min 0 max 99
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) __attribute__((noinline));
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) __attribute__((noinline));
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 slave_param =1;
corethread_create(1, &Estimator1, (void*)slave_param);
corethread_create(2, &Estimator2, (void*)slave_param);
// set a flag for priting out measurements
int print_flag = 0;
// control loop
for(;;){
//start_measurement = *timer_ptr;
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){
pitch = conv_to_float(raw_bytes_from_sim[4], raw_bytes_from_sim[3],raw_bytes_from_sim[2],raw_bytes_from_sim[1]);
roll = conv_to_float(raw_bytes_from_sim[8], raw_bytes_from_sim[7],raw_bytes_from_sim[6],raw_bytes_from_sim[5]);
heading = conv_to_float(raw_bytes_from_sim[12], raw_bytes_from_sim[11],raw_bytes_from_sim[10],raw_bytes_from_sim[9]);
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]);
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]);
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]);
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]);
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]);
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]);
P = conv_to_float(raw_bytes_from_sim[40], raw_bytes_from_sim[39],raw_bytes_from_sim[38],raw_bytes_from_sim[37]);
Q = conv_to_float(raw_bytes_from_sim[44], raw_bytes_from_sim[43],raw_bytes_from_sim[42],raw_bytes_from_sim[41]);
latitude = conv_to_float(raw_bytes_from_sim[48], raw_bytes_from_sim[47],raw_bytes_from_sim[46],raw_bytes_from_sim[45]);
longitude = conv_to_float(raw_bytes_from_sim[52], raw_bytes_from_sim[51],raw_bytes_from_sim[50],raw_bytes_from_sim[49]);
//start_measurement = *timer_ptr;
flag_est1 = 1;
flag_est2 = 1;
while( !( flag_est1 == 0 ) ){
;
}
while( !( flag_est2 == 0 ) ){
;
}
//stop_measurement = *timer_ptr;
//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;
print_flag =1;
//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);
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;
}
if(print_flag == 1){
print_flag =0;
printf("Control Loop Execution Time : %d \n", stop_measurement-start_measurement );
}
}//for
}//loop