-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEverestTask.cpp
More file actions
1676 lines (1405 loc) · 54.6 KB
/
EverestTask.cpp
File metadata and controls
1676 lines (1405 loc) · 54.6 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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Altitude estimation using multiple sensors
#include "everestTaskHPP.hpp"
#include <stdio.h>
#include <ctime>
#include <string.h>
#include <direct.h>
#include <cstdio>
#include <cerrno>
#include <vector>
#include <fstream>
#include <iostream>
#include <sstream>
#include "input_data.cpp"
#include "gpsData.cpp"
// #define LOGON
// #define LOGMETRICS
#define TIMERON
#define printf(...) ;
#ifdef LOGMETRICS
static FILE* everestGains = NULL;
#endif
FILE* haloFile;
FILE* everestFile;
int counterEverest = 0;
IMUData avgIMU1Align = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
IMUData avgIMU2Align = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int isAligned = 0;
int openFiles() {
// Define the directory path
std::string directoryPath = "testSuite/results";
// Create the directory if it doesn't exist
if (_mkdir("testSuite") == -1) {
if (errno != EEXIST) {
std::cerr << "Error creating directory: testSuite" << std::endl;
return 1;
}
}
if (_mkdir(directoryPath.c_str()) == -1) {
if (errno != EEXIST) {
std::cerr << "Error creating directory: " << directoryPath << std::endl;
return 1;
}
}
// File names
std::vector<std::string> fileNames = {"gains.txt",
"predictedValues.txt",
"log.txt",
"nearestScenarios.txt",
"nearestScenariosFormatted.txt",
"sigmaPoints.txt",
"sigmaPoints1.txt",
"sigmaPoints2.txt",
"sigmaPoints3.txt",
"sigmaPoints4.txt",
"sigmaPoints5.txt",
"sigmaPoints6.txt",
"predictnalt.txt",
"kalmangains.txt",
"everestgains.txt"};
// Deleting files
for (size_t i = 0; i < fileNames.size(); ++i) {
std::string filePath = directoryPath + "/" + fileNames[i];
if (std::remove(filePath.c_str()) == 0) {
} else {
std::string msg = "Error deleting " + fileNames[i];
std::perror(msg.c_str());
}
}
// Creating and writing to files
std::vector<std::pair<std::string, std::string>> filesToCreate = {
std::make_pair("predictedValues.txt",
"s1_alt,s1_velo,s1_acc,s2_alt,s2_velo,s2_acc,s3_alt,s3_"
"velo,s3_acc,s4_alt,s4_velo,s4_acc,s5_alt,s5_velo,s5_acc,"
"s6_alt,s6_velo,s6_acc\n"),
std::make_pair(
"gains.txt",
"sigmaPoint1->gain_v1[0],[1],[2],s1->gain_vector2[0],[1],[2]...\n"),
std::make_pair("log.txt", ""),
std::make_pair("sigmaPoints.txt", "alt,velo,acc\n"),
std::make_pair("sigmaPoints1.txt", "alt,velo,acc\n"),
std::make_pair("sigmaPoints2.txt", "alt,velo,acc\n"),
std::make_pair("sigmaPoints3.txt", "alt,velo,acc\n"),
std::make_pair("sigmaPoints4.txt", "alt,velo,acc\n"),
std::make_pair("sigmaPoints5.txt", "alt,velo,acc\n"),
std::make_pair("sigmaPoints6.txt", "alt,velo,acc\n"),
std::make_pair("nearestScenarios.txt",
"lowestDistance,secondLowestDistance,firstScenario,"
"SecondScenario\n"),
std::make_pair("nearestScenariosFormatted.txt",
"Header_formatted_scenarios\n"),
std::make_pair(
"predictnalt.txt",
"time,predicted_alt,predicted_vel,predicted_acc,prediction\n"),
std::make_pair("kalmangains.txt", "time,alt,velo,acc,gps_alt\n"),
std::make_pair("everestgains.txt",
"time,gain_IMU,gain_Baro1,gain_Baro2,gainedEstimate\n")};
for (size_t i = 0; i < filesToCreate.size(); ++i) {
std::string filePath = directoryPath + "/" + filesToCreate[i].first;
FILE* file = fopen(filePath.c_str(), "a+");
if (file) {
fputs(filesToCreate[i].second.c_str(), file);
fclose(file);
} else {
fprintf(stderr, "Error opening %s...exiting\n",
filesToCreate[i].first.c_str());
exit(1);
}
}
// Deleting HALO.txt
std::string filePath = directoryPath + "/HALO.txt";
if (std::remove(filePath.c_str()) == 0) {
} else {
std::perror("Error deleting HALO.txt");
}
// deleting infusion.txt
filePath = directoryPath + "/infusion.txt";
if (std::remove(filePath.c_str()) == 0) {
} else {
std::perror("Error deleting infusion.txt");
}
haloFile = fopen((directoryPath + "/HALO.txt").c_str(),
"a+"); // Open the file for writing
if (!haloFile) {
fprintf(stderr, "Error opening HALO.txt...exiting\n");
exit(1);
}
fprintf(haloFile,
"Time,Everest_Alt,Everest_Velo,Everest_Accel,Halo_Alt,Halo_Velo,Halo_"
"Accel\n");
// Open infusion.txt
everestFile = fopen((directoryPath + "/infusion.txt").c_str(),
"a+"); // Open the file for writing
if (!everestFile) {
fprintf(stderr, "Error opening infusion.txt...exiting\n");
exit(1);
}
fprintf(everestFile,
"timestamp,roll,pitch,yaw,accelerationError,accelerometerIgnored,"
"accelerationRecoveryTrigger,magneticError,magnetometerIgnored,"
"magneticRecoveryTrigger,initialising,angularRateRecovery,"
"accelerationRecovery,magneticRecovery,earth.axis.z\n");
// delete confidence.txt
filePath = directoryPath + "/confidence.txt";
if (std::remove(filePath.c_str()) == 0) {
} else {
std::perror("Error deleting confidence.txt");
}
// creating confidence.txt
FILE* file = fopen((directoryPath + "/confidence.txt").c_str(),
"a+"); // Open the file for writing
if (!file) {
fprintf(stderr, "Error opening confidence.txt...exiting\n");
exit(1);
}
return 0;
}
/**
* To run: g++ Infusion.cpp EverestTask.cpp -o Everest
* ./Everest
*/
using namespace std;
// SETTINGS (mostly for debugging, keep default for run)
enum debug_level {
RAW = 0, // raw data
Secondary = 1, // all operations before dynamite
Dynamite = 2, // everything during dynamite
Third = 3, // after dynamite
ALL = 4, // all
NONE = 5, // none
HAL0 = 6, // HALO
Calibration = 7 // Calibration
};
bool isTared = false;
debug_level debug = Calibration;
bool firstSampleAfterCalibration = true;
bool useSTD = false;
// INTERNAL VARIABLES
double theTime = CALIBRATION_TIME * RATE_BARO;
float sum = 0;
float pressureSum = 0;
static float previousTimestamp = 0;
bool haloInitialized = false;
std::vector<float> sumZeroOffsetAccel;
std::vector<float> sumZeroOffsetAccel2;
std::vector<float> sumZeroOffsetGyro;
std::vector<float> sumZeroOffsetGyro2;
// Instantiate Everest
madAhrs* ahrs;
Infusion* infusion;
EverestTask everest = EverestTask::getEverest();
float oldTime = everest.timeEverest;
HALO halo;
kinematics* Kinematics = everest.getKinematics(); // tare to ground
madAhrsFlags flags;
madAhrsInternalStates internalStates;
EverestData everestData;
/**
* @brief Calls finalWrapper with data and alignment
* IMPORTANT: PASS 0s FOR NOT UPDATED MEASUREMENTS (BAROS)
*/
float EverestTask::TaskWrapper(EverestData everestData,
MadAxesAlignment alignment,
MadAxesAlignment alignment2) {
return this->finalWrapper(
everestData.accelX1, everestData.accelY1, everestData.accelZ1,
everestData.gyroX1, everestData.gyroY1, everestData.gyroZ1,
everestData.magX1, everestData.magY1, everestData.magZ1,
everestData.accelX2, everestData.accelY2, everestData.accelZ2,
everestData.gyroX2, everestData.gyroY2, everestData.gyroZ2,
everestData.magX2, everestData.magY2, everestData.magZ2,
everestData.pressure1, everestData.pressure2, everestData.timeIMU1,
everestData.timeIMU2, everestData.timeBaro1, everestData.timeBaro2,
alignment, alignment2);
}
/**
* @brief Only done once. Sets pointers for Madgwick
* Internal
*/
void EverestTask::MadgwickSetup() {
// Attaches Madgwick to Everest
infusion = everest.ExternalInitialize();
ahrs = infusion->getMadAhrs();
// Define calibration (replace with actual calibration data if available)
const madMatrix gyroscopeMisalignment = {1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f};
const madVector gyroscopeSensitivity = {1.0f, 1.0f, 1.0f};
const madVector gyroscopeOffset = {0.0f, 0.0f, 0.0f};
const madMatrix accelerometerMisalignment = {1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f};
const madVector accelerometerSensitivity = {1.0f, 1.0f, 1.0f};
const madVector accelerometerOffset = {0.0f, 0.0f, 0.0f};
const madMatrix softIronMatrix = {1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f};
internalStates = infusion->madAhrsGetInternalStates(ahrs);
flags = infusion->madAhrsGetFlags(ahrs);
const madVector hardIronOffset = {0.0f, 0.0f, 0.0f};
// Initialise algorithms
madOffset offset = infusion->getOffset();
infusion->madOffsetInitialise(&offset, SAMPLE_RATE);
infusion->madAhrsInitialise(ahrs);
// Set AHRS algorithm settings
madAhrsSettings settings = {
EarthConventionEnu,
0.5f,
2000.0f, /* replace this with actual gyroscope range in degrees/s */
10.0f,
10.0f,
5 * SAMPLE_RATE, /* 5 seconds */
};
infusion->madAhrsSetSettings(ahrs, &settings);
}
/**
* @brief Wrapper for Madgwick, does offset calc and passes
* data to Madgwick
*
* Internal
* @param data IMUData struct
*
*/
void EverestTask::MadgwickWrapper(IMUData data) {
const float timestamp = data.time;
madVector gyroscope = {data.gyroX, data.gyroY,
data.gyroZ}; // data in degrees/s
madVector accelerometer = {data.accelX, data.accelY,
data.accelZ}; // data in g
madVector mag = {data.magX, data.magY, data.magZ};
// Update gyroscope offset correction algorithm
madOffset offset = infusion->getOffset();
gyroscope = infusion->madOffsetUpdate(&offset, gyroscope);
// Calculate delta time (in seconds)
float deltaTime = (float)(timestamp - previousTimestamp);
previousTimestamp = timestamp;
this->state.deltaTimeIMU = deltaTime;
if (debug == Secondary || debug == ALL) {
printf(
"Averaged: (%.6f, %.6f, %.6f) deg/s, Accel: (%.6f, %.6f, %.6f)g Time: "
"%f\n",
data.gyroX, data.gyroY, data.gyroZ, data.accelX, data.accelY,
data.accelZ, deltaTime);
printf("Mag: (%.6f, %.6f, %.6f) uT\n", mag.axis.x, mag.axis.y, mag.axis.z);
}
// Update gyroscope AHRS algorithm
infusion->madAhrsUpdate(ahrs, gyroscope, accelerometer, mag, deltaTime);
madEuler euler = infusion->getEuler(ahrs);
madVector earth = infusion->madAhrsGetEarthAcceleration(ahrs);
internalStates = infusion->madAhrsGetInternalStates(infusion->getMadAhrs());
flags = infusion->madAhrsGetFlags(infusion->getMadAhrs());
#if defined(LOGON) || defined(LOGMETRICS)
// write to file
fprintf(everestFile, "%f,", timestamp);
fprintf(everestFile, "%f,%f,%f,", euler.angle.roll, euler.angle.pitch,
euler.angle.yaw);
fprintf(everestFile, "%f,%d,%.0f,%.0f,%d,%.0f,%d,%d,%d,%d,%f",
internalStates.accelerationError, internalStates.accelerometerIgnored,
internalStates.accelerationRecoveryTrigger,
internalStates.magneticError, internalStates.magnetometerIgnored,
internalStates.magneticRecoveryTrigger, flags.initialising,
flags.angularRateRecovery, flags.accelerationRecovery,
flags.magneticRecovery, earth.axis.z);
fprintf(everestFile, "\n");
#endif
everest.state.earthAcceleration = earth.axis.z;
if (debug == Secondary || debug == ALL) {
printf("%f,%d,%.0f,%.0f,%d,%.0f,%d,%d,%d,%d\n",
internalStates.accelerationError,
internalStates.accelerometerIgnored,
internalStates.accelerationRecoveryTrigger,
internalStates.magneticError, internalStates.magnetometerIgnored,
internalStates.magneticRecoveryTrigger, flags.initialising,
flags.angularRateRecovery, flags.accelerationRecovery,
flags.magneticRecovery);
}
}
/**
* @brief Averages IMUs and feeds them to Madgwick wrapper
* Should be called every time IMU data is updated
*
* Internal
*/
void EverestTask::IMU_Update(const IMUData& imu1, const IMUData& imu2) {
int numberOfSamples = 2;
// Update IMU1
this->internalIMU_1.time = imu1.time;
this->internalIMU_1.gyroX = imu1.gyroX;
this->internalIMU_1.gyroZ = imu1.gyroZ;
this->internalIMU_1.gyroY = imu1.gyroY;
this->internalIMU_1.accelX = imu1.accelX;
this->internalIMU_1.accelY = imu1.accelY;
this->internalIMU_1.accelZ = imu1.accelZ;
this->internalIMU_1.magX = imu1.magX;
this->internalIMU_1.magY = imu1.magY;
this->internalIMU_1.magZ = imu1.magZ;
// Update IMU2
this->internalIMU_2.time = imu2.time;
this->internalIMU_2.gyroX = imu2.gyroX;
this->internalIMU_2.gyroY = imu2.gyroY;
this->internalIMU_2.gyroZ = imu2.gyroZ;
this->internalIMU_2.accelX = imu2.accelX;
this->internalIMU_2.accelY = imu2.accelY;
this->internalIMU_2.accelZ = imu2.accelZ;
this->internalIMU_2.magX = imu2.magX;
this->internalIMU_2.magY = imu2.magY;
this->internalIMU_2.magZ = imu2.magZ;
if (isinf(internalIMU_1.accelX)) {
numberOfSamples -= 1;
this->internalIMU_1.gyroX = 0;
this->internalIMU_1.gyroY = 0;
this->internalIMU_1.gyroZ = 0;
this->internalIMU_1.accelX = 0;
this->internalIMU_1.accelY = 0;
this->internalIMU_1.accelZ = 0;
this->internalIMU_1.magX = 0;
this->internalIMU_1.magY = 0;
this->internalIMU_1.magZ = 0;
} else {
// Apply calibration
if (debug == Calibration || debug == ALL) {
printf("uncalibrated: %f, %f, %f ", this->internalIMU_1.accelX,
this->internalIMU_1.accelY, this->internalIMU_1.accelZ);
}
this->internalIMU_1.accelX =
this->internalIMU_1.accelX - this->zeroOffsetAccel[0];
this->internalIMU_1.accelY =
this->internalIMU_1.accelY - this->zeroOffsetAccel[1];
this->internalIMU_1.accelZ =
this->internalIMU_1.accelZ - this->zeroOffsetAccel[2];
if (debug == Calibration || debug == ALL) {
printf("-> offset (%f, %f, %f) = calibrated accel (%f, %f, %f)\n",
this->zeroOffsetAccel[0], this->zeroOffsetAccel[1],
this->zeroOffsetAccel[2], this->internalIMU_1.accelX,
this->internalIMU_1.accelY, this->internalIMU_1.accelZ);
}
if (debug == Calibration || debug == ALL) {
printf("uncalibrated: %f, %f, %f ", this->internalIMU_1.gyroX,
this->internalIMU_1.gyroY, this->internalIMU_1.gyroZ);
}
this->internalIMU_1.gyroX =
this->internalIMU_1.gyroX - this->zeroOffsetGyro[0];
this->internalIMU_1.gyroY =
this->internalIMU_1.gyroY - this->zeroOffsetGyro[1];
this->internalIMU_1.gyroZ =
this->internalIMU_1.gyroZ - this->zeroOffsetGyro[2];
if (debug == Calibration || debug == ALL) {
printf("-> offset (%f, %f, %f) = calibrated gyro (%f, %f, %f)\n",
this->zeroOffsetGyro[0], this->zeroOffsetGyro[1],
this->zeroOffsetGyro[2], this->internalIMU_1.gyroX,
this->internalIMU_1.gyroY, this->internalIMU_1.gyroZ);
}
}
if (isinf(internalIMU_2.accelX)) {
numberOfSamples -= 1;
this->internalIMU_2.gyroX = 0;
this->internalIMU_2.gyroY = 0;
this->internalIMU_2.gyroZ = 0;
this->internalIMU_2.accelX = 0;
this->internalIMU_2.accelY = 0;
this->internalIMU_2.accelZ = 0;
this->internalIMU_2.magX = 0;
this->internalIMU_2.magY = 0;
this->internalIMU_2.magZ = 0;
} else {
// Apply calibration
if (debug == Calibration || debug == ALL) {
printf("uncalibrated: %f, %f, %f ", this->internalIMU_2.accelX,
this->internalIMU_2.accelY, this->internalIMU_2.accelZ);
}
this->internalIMU_2.accelX =
this->internalIMU_2.accelX - this->zeroOffsetAccel2[0];
this->internalIMU_2.accelY =
this->internalIMU_2.accelY - this->zeroOffsetAccel2[1];
this->internalIMU_2.accelZ =
this->internalIMU_2.accelZ - this->zeroOffsetAccel2[2];
if (debug == Calibration || debug == ALL) {
printf("-> offset (%f, %f, %f) = calibrated accel2 (%f, %f, %f)\n",
this->zeroOffsetAccel2[0], this->zeroOffsetAccel2[1],
this->zeroOffsetAccel2[2], this->internalIMU_2.accelX,
this->internalIMU_2.accelY, this->internalIMU_2.accelZ);
}
if (debug == Calibration || debug == ALL) {
printf("uncalibrated: %f, %f, %f ", this->internalIMU_2.gyroX,
this->internalIMU_2.gyroY, this->internalIMU_2.gyroZ);
}
this->internalIMU_2.gyroX =
this->internalIMU_2.gyroX - this->zeroOffsetGyro2[0];
this->internalIMU_2.gyroY =
this->internalIMU_2.gyroY - this->zeroOffsetGyro2[1];
this->internalIMU_2.gyroZ =
this->internalIMU_2.gyroZ - this->zeroOffsetGyro2[2];
if (debug == Calibration || debug == ALL) {
printf("-> offset (%f, %f, %f) = calibrated gyro2 (%f, %f, %f)\n",
this->zeroOffsetGyro2[0], this->zeroOffsetGyro2[1],
this->zeroOffsetGyro2[2], this->internalIMU_2.gyroX,
this->internalIMU_2.gyroY, this->internalIMU_2.gyroZ);
}
}
// Calculate average of IMU parameters
#define averageIMU this->state.avgIMU
averageIMU.gyroX =
(this->internalIMU_1.gyroX + this->internalIMU_2.gyroX) / numberOfSamples;
averageIMU.gyroY =
(this->internalIMU_1.gyroY + this->internalIMU_2.gyroY) / numberOfSamples;
averageIMU.gyroZ =
(this->internalIMU_1.gyroZ + this->internalIMU_2.gyroZ) / numberOfSamples;
averageIMU.accelX =
(this->internalIMU_1.accelX + this->internalIMU_2.accelX) /
numberOfSamples;
averageIMU.accelY =
(this->internalIMU_1.accelY + this->internalIMU_2.accelY) /
numberOfSamples;
averageIMU.accelZ =
(this->internalIMU_1.accelZ + this->internalIMU_2.accelZ) /
numberOfSamples;
averageIMU.magX =
(this->internalIMU_1.magX + this->internalIMU_2.magX) / numberOfSamples;
averageIMU.magY =
(this->internalIMU_1.magY + this->internalIMU_2.magY) / numberOfSamples;
averageIMU.magZ =
(this->internalIMU_1.magX + this->internalIMU_2.magZ) / numberOfSamples;
averageIMU.time =
(this->internalIMU_1.time + this->internalIMU_2.time) / numberOfSamples;
#undef averageIMU
if (numberOfSamples == 0) {
this->state.avgIMU = {imu1.time, 0, 0, 0, 0, 0, 0, 0, 0, 0};
}
// feed to Madgwick
this->MadgwickWrapper(state.avgIMU);
}
/**
* @brief updates baro and delta time
* Should be called every time baro data is updated
* Internal
*/
void EverestTask::Baro_Update(const BarosData& Baro1, const BarosData& Baro2) {
// Update Baros
this->baro1.time = Baro1.time;
this->baro1.pressure = Baro1.pressure;
this->baro1.deltaTime = Baro1.time - this->baro1.previousTime;
this->baro1.previousTime = Baro1.time;
this->baro2.time = Baro2.time;
this->baro2.pressure = Baro2.pressure;
this->baro2.deltaTime = Baro2.time - this->baro2.previousTime;
this->baro2.previousTime = Baro2.time;
if (debug == RAW || debug == ALL) {
printf("Baro1: %f Pa, Baro2: %f Pa\n", baro1.pressure, baro2.pressure);
}
}
/**
* @brief Calls IMU and Baro update functions and calculates altitude
* calls Dynamite and updates altitude list
*
* @return calculated altitude
*
* External (only function that should be called after instantiation of
* Everest to pass sensor data to Everest for altitude calculation)
*/
float EverestTask::ExternalUpdate(IMUData imu1, IMUData imu2, BarosData baro1,
BarosData baro2) {
if (!isTared) {
everest.tare(imu1, imu2, baro1, baro2);
return 0;
}
everest.IMU_Update(imu1, imu2);
if (debug == Third || debug == ALL) {
printf("After IMU Update IMU Altitude: %f\n",
everest.state.avgIMU.altitude);
}
everest.Baro_Update(baro1, baro2);
float finalAlt = everest.dynamite();
if (debug == Dynamite || debug == ALL) {
printf("After Dynamite: %f\n", finalAlt);
}
// Update altitude list
this->AltitudeList.secondLastAltitude = this->AltitudeList.lastAltitude;
this->AltitudeList.lastAltitude = finalAlt;
return finalAlt;
}
/**
* @brief (Currently does not work, use final wrapper) Wraps External Update
* with alignment, returns External Update with aligned data
*/
float EverestTask::AlignedExternalUpdate(IMUData imu1, IMUData imu2,
BarosData baro1, BarosData baro2,
MadAxesAlignment alignment) {
// align
madVector alignedIMU1 =
infusion->AxesSwitch({imu1.accelX, imu1.accelY, imu1.accelZ}, alignment);
madVector alignedIMUGyro1 =
infusion->AxesSwitch({imu1.gyroX, imu1.gyroY, imu1.gyroZ}, alignment);
madVector alignedIMU2 =
infusion->AxesSwitch({imu2.accelX, imu2.accelY, imu2.accelZ}, alignment);
madVector alignedIMUGyro2 =
infusion->AxesSwitch({imu2.gyroX, imu2.gyroY, imu2.gyroZ}, alignment);
if (debug == Secondary || debug == ALL) {
printf("Unaligned IMU1:(%.6f, %.6f, %.6f)g,(%.6f, %.6f, %.6f)deg/s\n",
imu1.accelX, imu1.accelY, imu1.accelZ, imu1.gyroX, imu1.gyroY,
imu1.gyroZ);
printf("Unaligned IMU2:(%.6f, %.6f, %.6f)g,(%.6f, %.6f, %.6f)deg/s\n",
imu2.accelX, imu2.accelY, imu2.accelZ, imu2.gyroX, imu2.gyroY,
imu2.gyroZ);
printf("Alignment: %d\n", alignment);
}
// put aligned data into IMUData struct
imu1.accelX = alignedIMU1.axis.x;
imu1.accelY = alignedIMU1.axis.y;
imu1.accelZ = alignedIMU1.axis.z;
imu1.gyroX = alignedIMUGyro1.axis.x;
imu1.gyroY = alignedIMUGyro1.axis.y;
imu1.gyroZ = alignedIMUGyro1.axis.z;
// IMU 2
imu2.accelX = alignedIMU2.axis.x;
imu2.accelY = alignedIMU2.axis.y;
imu2.accelZ = alignedIMU2.axis.z;
imu2.gyroX = alignedIMUGyro2.axis.x;
imu2.gyroY = alignedIMUGyro2.axis.y;
imu2.gyroZ = alignedIMUGyro2.axis.z;
if (debug == Secondary || debug == ALL) {
printf("Aligned IMU1:(%.6f, %.6f, %.6f)g,(%.6f, %.6f, %.6f)deg/s\n",
imu1.accelX, imu1.accelY, imu1.accelZ, imu1.gyroX, imu1.gyroY,
imu1.gyroZ);
printf("Aligned IMU2:(%.6f, %.6f, %.6f)g,(%.6f, %.6f, %.6f)deg/s\n",
imu2.accelX, imu2.accelY, imu2.accelZ, imu2.gyroX, imu2.gyroY,
imu2.gyroZ);
}
return ExternalUpdate(imu1, imu2, baro1, baro2);
}
/**
* Calculates altitude using IMU sensor data and kinematic equations.
*
* @param avgIMU with the average sensor data from the IMU
*
* @category Internal | ASYNCHRONOUS
*
* @return calculated altitude
*/
float EverestTask::deriveForAltitudeIMU(IMUData avgIMU) {
float accelerationZ = everest.state.earthAcceleration * -9.81;
float initialVelocity = this->getKinematics()->initialVelo;
float initialAltitude = this->Kinematics.initialAlt;
double deltaTime = this->state.deltaTimeIMU;
// Derive altitude from IMU
float finalVelocity = initialVelocity + accelerationZ * deltaTime;
float altitude =
initialAltitude + (initialVelocity + finalVelocity) * deltaTime / 2.0;
if (debug == Secondary || debug == ALL) {
printf("\nKinematics\n");
printf("IMU Initial Altitude: %f\n", initialAltitude);
printf("IMU Velocity: %f\n", initialVelocity);
printf("IMU Acceleration: %f\n", accelerationZ);
printf("IMU Delta Time: %f\n", deltaTime);
printf("Derived Altitude: %f\n", altitude);
}
return altitude;
}
/**
* Calculates the altitude based on the given pressure using the barometric
* formula
*
* @param pressure pressure from baros in Pa
*
* @return altitude in meters
*
* @category Internal
*/
float convertToAltitude(float pressure) {
float seaLevelPressure = 1013.25; // sea level pressure in hPa
pressure = pressure / 100.0; // convert to hPa
float altitude = 44330.0 * (1.0 - pow(pressure / seaLevelPressure,
1 / 5.2558)); // barometric formula
// If pressure is less than 100, altitude is 0
if (pressure < 100) {
altitude = 0;
}
if (debug == Dynamite || debug == ALL) {
printf("\nConversion \n");
printf("Pressure: %.f hPa, Altitude: %.f m\n", pressure, altitude);
}
return altitude;
}
/**
* @brief Multi-system trust algorithm. Assumes measurements are updated
* @returns normalised altitude
*
* @category Internal | Asynchronous
*/
float EverestTask::dynamite() {
float IMUAltitude = deriveForAltitudeIMU(everest.state.avgIMU);
this->state.avgIMU.altitude = IMUAltitude;
float BaroAltitude1 = convertToAltitude(this->baro1.pressure);
this->baro1.altitude = BaroAltitude1;
float BaroAltitude2 = convertToAltitude(everest.baro2.pressure);
this->baro2.altitude = BaroAltitude2;
float GPSAltitude = everest.everestData.altitudeGPS;
if (debug == Dynamite || debug == ALL) {
printf("\nDynamite\n");
printf("Baro1 Altitude: %f\n", BaroAltitude1);
printf("Baro2 Altitude: %f\n", BaroAltitude2);
// printf("Baro3 Altitude: %f\n", BaroAltitude3);
// printf("Real Baro Altitude: %f\n", RealBaroAltitude);
printf("IMU Altitude: %f\n", IMUAltitude);
printf("GPSAltitude: %f\n", GPSAltitude);
}
// if pressure is zero, set gain to zero
if (everest.baro1.pressure == 0) {
everest.state.gain_Baro1 = 0;
} else if (everest.state.gain_Baro1 == 0) {
// if not zero, set gain to previous gain
everest.state.gain_Baro1 = everest.state.prev_gain_Baro1;
}
// if not zero, set gain to zero
if (everest.baro2.pressure == 0) {
everest.state.gain_Baro2 = 0;
} else if (everest.state.gain_Baro2 == 0) {
// if not zero, set gain to previous gain
everest.state.gain_Baro2 = everest.state.prev_gain_Baro2;
}
// distribute measurements based on gain
float distributed_IMU_Altitude = IMUAltitude * everest.state.gain_IMU;
float distributed_Baro_Altitude1 = (BaroAltitude1 * everest.state.gain_Baro1);
float distributed_Baro_Altitude2 = (BaroAltitude2 * everest.state.gain_Baro2);
// summation of distributed measurements
float distributed_Sum = distributed_IMU_Altitude +
distributed_Baro_Altitude1 +
distributed_Baro_Altitude2;
if (debug == Dynamite || debug == ALL) {
printf("Distributed Sum: %f\n\n", distributed_Sum);
}
// summation of gains
float sumGain = everest.state.gain_IMU + everest.state.gain_Baro1 +
everest.state.gain_Baro2;
if (debug == Dynamite || debug == ALL) {
printf("Sum Gain: %f\n\n", sumGain);
}
// normalised altitude
float normalised_Altitude = (distributed_Sum) / sumGain;
if (debug == Dynamite || debug == ALL) {
printf("Normalised Altitude: %f\n\n", normalised_Altitude);
}
// overrides normalised altitude with GPS altitude if available, Don't!!
/*if (everest.availableMeasurements[4] == 1) {
std::cout << "GPS Altitude: " << everest.everestData.altitudeGPS
<< std::endl;
std::cout << "Normalised Altitude before: " << normalised_Altitude
<< std::endl;
normalised_Altitude = everest.everestData.altitudeGPS;
std::cout << "Normalised Altitude after: " << normalised_Altitude
<< std::endl;
}*/
// Update Kinematics
Kinematics.finalAltitude = normalised_Altitude;
if (debug == Dynamite || debug == ALL) {
printf("Final Altitude: %f\n\n", Kinematics.finalAltitude);
}
// update velocity
Kinematics.initialVelo = (Kinematics.finalAltitude - Kinematics.initialAlt) /
(this->state.deltaTimeIMU);
if (debug == Dynamite || debug == ALL) {
printf("Initial Velocity: %f\n", Kinematics.initialVelo);
}
// update altitude
Kinematics.initialAlt = Kinematics.finalAltitude;
recalculateGain(normalised_Altitude);
/*
if (everest.availableMeasurements[4] == 1) {
updateGainsWithGPS();
}*/
// Save the gains that are not zero as previous gains
// so once we have recovery phase these old gains are used
if (everest.state.gain_IMU != 0) {
everest.state.prev_gain_IMU = everest.state.gain_IMU;
}
if (everest.state.gain_Baro1 != 0) {
everest.state.prev_gain_Baro1 = everest.state.gain_Baro1;
}
if (everest.state.gain_Baro2 != 0) {
everest.state.prev_gain_Baro2 = everest.state.gain_Baro2;
}
if (debug == Dynamite || debug == ALL) {
printf("Previous Gains\n");
printf("Prev Gain IMU: %f\n", everest.state.prev_gain_IMU);
printf("Prev Gain Baro1: %f\n", everest.state.prev_gain_Baro1);
printf("Prev Gain Baro2: %f\n", everest.state.prev_gain_Baro2);
// printf("Prev Gain Baro3: %f\n", everest.state.prev_gain_Baro3);
// printf("Prev Gain Real Baro: %f\n\n", everest.state.prev_gain_Real_Baro);
}
return normalised_Altitude;
}
/**
* @brief Updates the gains with GPS data
*
*/
void EverestTask::updateGainsWithGPS() {
float gpsAltitude = everest.everestData.altitudeGPS;
float imuGPSDiff = fabsf(everest.state.avgIMU.altitude - gpsAltitude);
float baro1GPSDiff = fabsf(everest.baro1.altitude - gpsAltitude);
float baro2GPSDiff = fabsf(everest.baro2.altitude - gpsAltitude);
float totalDiff = imuGPSDiff + baro1GPSDiff + baro2GPSDiff;
float gain_IMU = everest.state.gain_IMU * (1 - imuGPSDiff / totalDiff);
float gain_Baro1 = everest.state.gain_Baro1 * (1 - baro1GPSDiff / totalDiff);
float gain_Baro2 = everest.state.gain_Baro2 * (1 - baro2GPSDiff / totalDiff);
if (debug == Dynamite || debug == ALL) {
printf("\nUpdate Gains with GPS\n");
printf("IMU GPS Diff: %f\n", imuGPSDiff);
printf("Baro1 GPS Diff: %f\n", baro1GPSDiff);
printf("Baro2 GPS Diff: %f\n", baro2GPSDiff);
printf("Total Diff: %f\n", totalDiff);
printf("New Gain IMU: %f\n", gain_IMU);
printf("New Gain Baro1: %f\n", gain_Baro1);
printf("New Gain Baro2: %f\n", gain_Baro2);
}
everest.state.gain_IMU = gain_IMU;
everest.state.gain_Baro1 = gain_Baro1;
everest.state.gain_Baro2 = gain_Baro2;
}
/**
* @brief calculation: new gain = 1 / abs(estimate - measurement)
*
*/
void EverestTask::recalculateGain(float estimate) {
float gainedEstimate = deriveChangeInVelocityToGetAltitude(
estimate); // pre-integrated for altitude
// epsilon prevents gains from approaching 0 or infinity.
float epsilon = 100;
float gain_IMU = 1 / (fabsf(gainedEstimate - this->state.avgIMU.altitude) +
epsilon); // change to previous trusts
float gain_Baro1 =
1 / (fabsf(gainedEstimate - this->baro1.altitude) + epsilon);
float gain_Baro2 =
1 / (fabsf(gainedEstimate - this->baro2.altitude) + epsilon);
if (debug == Third || debug == ALL) {
printf("\nRecalculate Gain - Before normalization\n");
printf("Gain IMU: %f\n", gain_IMU);
printf("Gain Baro1: %f\n", gain_Baro1);
printf("Gain Baro2: %f\n", gain_Baro2);
printf("Gained Estimate: %f\n", gainedEstimate);
printf("Altitude: %f\n", estimate);
printf("Baro1: %f\n", this->baro1.altitude);
printf("Baro2: %f\n", this->baro2.altitude);
printf("GPS Altitude: %f\n", this->everestData.altitudeGPS);
}
// normalise
this->state.gain_IMU = gain_IMU / (gain_IMU + gain_Baro1 + gain_Baro2);
this->state.gain_Baro1 = gain_Baro1 / (gain_IMU + gain_Baro1 + gain_Baro2);
this->state.gain_Baro2 = gain_Baro2 / (gain_IMU + gain_Baro1 + gain_Baro2);
if (debug == Dynamite || debug == ALL) {
printf("\nRecalculate Gain\n");
printf("New Gain IMU: %f\n", this->state.gain_IMU);
printf("New Gain Baro1: %f\n", this->state.gain_Baro1);
printf("New Gain Baro2: %f\n", this->state.gain_Baro2);
// printf("New Gain Baro3: %f\n", this->state.gain_Baro3);
// printf("New Gain Real Baro: %f\n\n", this->state.gain_Real_Baro);
}
#ifdef LOGMETRICS
if (everestGains == NULL)
everestGains = fopen("testSuite/results/everestgains.txt", "a+");
fprintf(everestGains, "%.6f,%.6f,%.6f,%.6f,%.6f\n", everestData.timeIMU1,
this->state.gain_IMU, this->state.gain_Baro1, this->state.gain_Baro2,
gainedEstimate);
fflush(everestGains);
#endif
}
/**
* @brief Converts the STDs to coefficients
*/
void EverestTask::calculateSTDCoefficients() {
// calculate standard deviation coefficients
float std_IMU = this->state.gain_IMU;
float std_Baro1 = this->state.gain_Baro1;
float std_Baro2 = this->state.gain_Baro2;
float sumSTD1 = pow(everest.state.gain_IMU, 2) +
pow(everest.state.gain_Baro1, 2) +
pow(everest.state.gain_Baro2, 2);
// normalise
this->state.std_IMU = pow(std_IMU, 2) / sumSTD1;
this->state.std_Baro1 = pow(std_Baro1, 2) / sumSTD1;
this->state.std_Baro2 = pow(std_Baro2, 2) / sumSTD1;
if (debug == Dynamite || debug == ALL) {
printf("\nStandard Deviation Coefficients\n");
printf("STD IMU: %f\n", this->state.std_IMU);
printf("STD Baro1: %f\n", this->state.std_Baro1);
printf("STD Baro2: %f\n", this->state.std_Baro2);
}
}
/**
* @brief Calculates the derivative of the altitude
*
* @param estimate the estimated altitude
*
* @return velocity
*
* Internal
*/
float EverestTask::deriveChangeInVelocityToGetAltitude(float estimate) {
double deltaTimeAverage = (this->baro1.deltaTime + this->baro2.deltaTime +
this->state.deltaTimeIMU) /
3.0;
float velocityZ = (this->AltitudeList.secondLastAltitude -
4 * this->AltitudeList.lastAltitude + 3 * estimate) /
(2.0 * deltaTimeAverage);
float newAltitude =
this->AltitudeList.lastAltitude + velocityZ * deltaTimeAverage;
if (debug == Dynamite || debug == ALL) {