-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
2263 lines (1832 loc) · 74.6 KB
/
main.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
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
#include "thal.h"
#include "mavlink.h"
#include <math.h>
// *** Config stuff
#define FIRMWARE_VERSION 1 // Firmware version
#define MESSAGE_LOOP_HZ 15 // Max frequency of messages in Hz (keep this number low, like around 15)
#define RX_PANIC 2 // Number of seconds after missing RX before craft considered "disconnected"
#define THROT_SENS 1.1
// *** LED stuff
unsigned char flashPLED, flashVLED, flashRLED;
float gim_ptemp;
// *** ILink stuff
ilink_identify_t ilink_identify;
ilink_thalstat_t ilink_thalstat;
ilink_thalctrl_t ilink_thalctrl;
ilink_imu_t ilink_rawimu;
ilink_imu_t ilink_scaledimu;
ilink_altitude_t ilink_altitude;
ilink_attitude_t ilink_attitude;
ilink_attitude_t ilink_attitude_rx;
ilink_thalparam_t ilink_thalparam_tx;
ilink_thalparam_t ilink_thalparam_rx;
ilink_thalpareq_t ilink_thalpareq;
ilink_iochan_t ilink_inputs0;
ilink_iochan_t ilink_outputs0;
ilink_position_t ilink_position;
void LinkInit(void);
// *** Timers and counters
unsigned int sysMS;
unsigned long long sysUS;
unsigned short RxWatchdog;
unsigned short UltraWatchdog;
// *** Functions
void SensorZero(void);
void CalibrateGyro(void);
void CalibrateGyroTemp(unsigned int seconds);
void CalibrateMagneto(void);
// *** Configs
#define FAST_RATE 400
#define SLOW_RATE 75
/*
#define INPUT_RATE 50
#define ALT_RATE 25*/
#define ZEROTHROTMAX 1*FAST_RATE
#define SLOW_DIVIDER FAST_RATE/SLOW_RATE
/*#define INPUT_DIVIDER AHRS_RATE/INPUT_RATE
#define ALT_DIVIDER AHRS_RATE/ALT_RATE*/
unsigned short slowSoftscale;//, inputSoftscale, baroSoftscale;
// TODO: check ESC response at THROTTLEOFFSET, consider raising THROTTLEOFFSET to 1000
#define THROTTLEOFFSET 900 // Corresponds to zero output PWM. Nominally 1000=1ms, but 800 works better
#define IDLETHROTTLE 175 // Minimum PWM output to ESC when in-flight to avoid motors turning off
#define MAXTHROTTLE 1200 // Maximum PWM output to ESC (PWM = THROTTLEOFFSET + MAXTHROTTLE)
#define MAXTHROTTLEPERCENT 0.9 // Maximum percentage throttle should be (reserves some extra output for stabilisation at high throttle).
#define OFFSTICK 50
#define MIDSTICK 512 // Corresponds to input when stick is in middle (approx value).
#define MAXSTICK 850 // Corresponds to input when stick is at the top
#define MAXTHRESH (MAXSTICK+MIDSTICK)/2 - 50
#define MINTHRESH (MIDSTICK+OFFSTICK)/2 + 50
//#define CONSTRAIN 600 //Constraint on maximum motor demands
// *** Parameters
typedef struct paramStorage_struct {
char name[16];
float value;
} paramStorage_t;
struct paramStorage_struct paramStorage[] = {
{"DRIFT_AKp", 0.2f},
{"DRIFT_MKp", 0.02f},
#define DRIFT_AccelKp paramStorage[0].value
#define DRIFT_MagKp paramStorage[1].value
{"SPR_ULTRA", 0.95f},
#define SPR_ULTRA paramStorage[2].value
{"YAW_SEN", 0.00002f},
{"PITCH_SEN", 0.0022f},
{"ROLL_SEN", 0.0022f},
#define YAW_SENS paramStorage[3].value
#define PITCH_SENS paramStorage[4].value
#define ROLL_SENS paramStorage[5].value
{"YAW_DZN", 0.001f},
#define YAW_DEADZONE paramStorage[6].value
{"PITCH_Kp", 400.0f},
{"PITCH_Ki", 2.0f},
{"PITCH_Kd", 100.0f},
{"PITCH_Kdd", 1500.0f},
{"PITCH_Bst", 0.0f},
{"PITCH_De", 0.999f},
#define PITCH_Kp paramStorage[7].value
#define PITCH_Ki paramStorage[8].value
#define PITCH_Kd paramStorage[9].value
#define PITCH_Kdd paramStorage[10].value
#define PITCH_Boost paramStorage[11].value
#define PITCH_De paramStorage[12].value
{"ROLL_Kp", 400.0f},
{"ROLL_Ki", 2.0f},
{"ROLL_Kd", 100.0f},
{"ROLL_Kdd", 1500.0f},
{"ROLL_Bst", 0.00f},
{"ROLL_De", 0.999f},
#define ROLL_Kp paramStorage[13].value
#define ROLL_Ki paramStorage[14].value
#define ROLL_Kd paramStorage[15].value
#define ROLL_Kdd paramStorage[16].value
#define ROLL_Boost paramStorage[17].value
#define ROLL_De paramStorage[18].value
{"YAW_Kp", 1000.0f},
{"YAW_Kd", 250.0f},
{"YAW_Bst", 0.00f},
#define YAW_Kp paramStorage[19].value
#define YAW_Kd paramStorage[20].value
#define YAW_Boost paramStorage[21].value
// Mode
{"MODE_SIMP", 0.0f}, // Simplicity Mode (0 for off 1 for on)
{"MODE_ULTRA", 1.0f}, // Ultrasound Mode
{"MODE_GIM", 1.0f}, // Gimbal Lock
{"MODE_SWTCH", 5.0f}, // AUX1 SWITCH: 1 to toggle simplicity, 2 to toggle Ultra, 3 to toggle Gimbal, 4 to toggle GPS
#define MODE_SIMP paramStorage[22].value
#define MODE_ULTRA paramStorage[23].value
#define MODE_GIM paramStorage[24].value
#define MODE_SWTCH paramStorage[25].value
//Limits
{"LIM_ANGLE", 0.4f}, // Roll and Pitch Angle Limit in Radians
{"LIM_ALT", 1000.0f}, // Altitude Limit in metres when in Ultrasound Mode
#define LIM_ANGLE paramStorage[26].value
#define LIM_ALT paramStorage[27].value
// Magneto Correction
{"CAL_MAGN1", 0.001756f},
{"CAL_MAGN2", 0.00008370f},
{"CAL_MAGN3", 0.00005155f},
{"CAL_MAGN5", 0.001964f},
{"CAL_MAGN6", 0.00002218f},
{"CAL_MAGN9", 0.001768f},
{"CAL_MAGM1", 0.0f},
{"CAL_MAGM2", 0.0f},
{"CAL_MAGM3", 0.0f},
#define MAGCOR_N1 paramStorage[28].value
#define MAGCOR_N2 paramStorage[29].value
#define MAGCOR_N3 paramStorage[30].value
#define MAGCOR_N5 paramStorage[31].value
#define MAGCOR_N6 paramStorage[32].value
#define MAGCOR_N9 paramStorage[33].value
#define MAGCOR_M1 paramStorage[34].value
#define MAGCOR_M2 paramStorage[35].value
#define MAGCOR_M3 paramStorage[36].value
// Ultrasound
//{"ULTRA_Kp", 0.05f},
//{"ULTRA_Kd", 5.0f},
//{"ULTRA_Ki", 0.00001f},
{"ULTRA_Kp", 0.03f},
{"ULTRA_Kd", 3.0f},
{"ULTRA_Ki", 0.00001f},
{"ULTRA_De", 0.9999f},
{"ULTRA_TKOFF", 250.0f},
{"ULTRA_LND", 70.0f},
#define ULTRA_Kp paramStorage[37].value
#define ULTRA_Kd paramStorage[38].value
#define ULTRA_Ki paramStorage[39].value
#define ULTRA_De paramStorage[40].value
#define ULTRA_TKOFF paramStorage[41].value
#define ULTRA_LND paramStorage[42].value
{"CAL_GYROX", 0.0f},
{"CAL_GYROY", 0.0f},
{"CAL_GYROZ", 0.0f},
#define CAL_GYROX paramStorage[43].value
#define CAL_GYROY paramStorage[44].value
#define CAL_GYROZ paramStorage[45].value
{"DETUNE", 0.2f},
#define DETUNE paramStorage[46].value
{"LIM_RATE", 100.0f},
#define LIM_RATE paramStorage[47].value
{"LIM_ULTRA", 0.35f},
#define LIM_ULTRA paramStorage[48].value
{"ULTRA_DRMP", 3.0f},
{"ULTRA_DTCT", 10.0f},
#define ULTRA_DRMP paramStorage[49].value
#define ULTRA_DTCT paramStorage[50].value
{"LIM_THROT", 0.3f},
#define LIM_THROT paramStorage[51].value
{"ULTRA_OVDEC", 0.05f},
#define ULTRA_OVDEC paramStorage[52].value
{"ULTRA_DEAD", 100},
#define ULTRA_DEAD paramStorage[53].value
{"ULTRA_OVTH", 10},
#define ULTRA_OVTH paramStorage[54].value
{"MODE_GPS", 0.0f},
#define MODE_GPS paramStorage[55].value
{"GPS_Kp", 0.05f},
{"GPS_Kde", 1.0f},
{"GPS_Ki", 0.0f},
{"GPS_Kd", 0.1f},
#define GPS_Kp paramStorage[56].value
#define GPS_Kde paramStorage[57].value
#define GPS_Ki paramStorage[58].value
#define GPS_Kd paramStorage[59].value
{"GPA_ALTKp", 10.0f},
{"GPA_ALTKi", 0.1f},
{"GPA_ALTKde", 0.999f},
{"GPA_ALTKd", 0.0f},
#define GPS_ALTKp paramStorage[60].value
#define GPS_ALTKi paramStorage[61].value
#define GPS_ALTKde paramStorage[62].value
#define GPS_ALTKd paramStorage[63].value
{"GPS_ALTHo", 2.5f},
#define GPS_ALTHo paramStorage[64].value
{"CAL_AUTO", 1.0f},
#define CAL_AUTO paramStorage[65].value
//Gimbal in the Pitch Axis
{"GIM_PMID", 1500.0f}, //Mid Point
{"GIM_PSCAL", -1650.0f}, //Scaling
{"GIM_PLIMH", 2300.0f}, //Limit High
{"GIM_PLIML", 700.0f}, //Limit Low
{"GIM_PPLUS", 100.0f}, //Limit Low
#define GIM_PMID paramStorage[66].value
#define GIM_PSCAL paramStorage[67].value
#define GIM_PLIMH paramStorage[68].value
#define GIM_PLIML paramStorage[69].value
#define GIM_PPLUS paramStorage[70].value
//Gimbal in the Roll Axis
{"GIM_RMID", 1500.0f}, //Mid Point
{"GIM_RSCAL", 1650.0f}, //Scaling
{"GIM_RLIMH", 2300.0f}, //Limit High
{"GIM_RLIML", 700.0f}, //Limit Low
#define GIM_RMID paramStorage[71].value
#define GIM_RSCAL paramStorage[72].value
#define GIM_RLIMH paramStorage[73].value
#define GIM_RLIML paramStorage[74].value
{"SPR_OUT", 0.3f},
#define SPR_OUT paramStorage[75].value
{"MODE_AUTO", 0.0f},
#define MODE_AUTO paramStorage[76].value
/*{"CURVE_X1", 30.0f},
{"CURVE_Y1", 45.0f},
{"CURVE_X2", 55.0f},
{"CURVE_Y2", 70.0f},
{"CURVE_Yn", 70.0f},
#define THROTTLE_X1 paramStorage[8].value
#define THROTTLE_Y1 paramStorage[9].value
#define THROTTLE_X2 paramStorage[10].value
#define THROTTLE_Y2 paramStorage[11].value
#define THROTTLE_Yn paramStorage[12].value */
/*{"ADJ_UP", 1.0f}, // Ultrasound minimmum throttle
{"ADJ_DOWN", 30.0f}, // Ultrasound minimimumum throttle
#define ADJ_UP paramStorage[73].value
#define ADJ_DOWN paramStorage[74].value */
};
unsigned int paramSendCount;
unsigned int paramCount;
unsigned char paramSendSingle;
void EEPROMLoadAll(void);
void EEPROMSaveAll(void);
#define EEPROM_MAX_PARAMS 100 // this should be greater than or equal to the above number of parameters
#define EEPROM_OFFSET 0 // EEPROM Offset used for moving the EEPROM values around storage (wear levelling I guess)
#define EEPROM_VERSION 18 // version of variables in EEPROM, change this value to invalidate EEPROM contents and restore defaults
// *** quaternion storage
float q1, q2, q3, q4;
float thetaAngle, phiAngle, psiAngle;
float MM1, MM2, MM3, MM4, MM5, MM6, MM7, MM8, MM9;
float lat_diff;
float lon_diff;
float lat_diff_i;
float lon_diff_i;
float lat_diff_d;
float lon_diff_d;
float lon_diff_old;
float lat_diff_old;
float alt_diff_old;
float alt_diff;
float alt_diff_i;
float alt_diff_d;
float alt_throttle;
float gpsThrottle;
typedef struct{
float demandav;
float demand;
float demandtemp;
float demandtempold;
float derivativetemp;
float secondderivativetemp;
float derivativetempold;
float demandOld;
float valueOld;
float derivative;
float integral;
float bias;
} directionStruct;
directionStruct pitch;
directionStruct roll;
directionStruct yaw;
typedef struct {
float value;
float valueOld;
float error;
float demand;
float demandav;
float derivative;
float integral;
float demandincr;
float demandold;
} altStruct;
altStruct alt;
float thetaAngle, phiAngle, psiAngle, psiAngleinit;
// *** Sensor Storage
#define GAV_LEN 6
#define AAV_LEN 80
#define MAV_LEN 6
typedef struct{
volatile signed short raw;
volatile float av;
volatile float value;
volatile float offset;
volatile float error;
volatile float verterror;
volatile signed int total;
signed short history[GAV_LEN];
} sensorStructGyro;
typedef struct{
sensorStructGyro X;
sensorStructGyro Y;
sensorStructGyro Z;
unsigned int count;
} threeAxisSensorStructGyro;
typedef struct{
volatile signed short raw;
volatile float av;
volatile float value;
volatile signed int total;
signed short history[AAV_LEN];
} sensorStructAccel;
typedef struct{
sensorStructAccel X;
sensorStructAccel Y;
sensorStructAccel Z;
unsigned int count;
} threeAxisSensorStructAccel;
typedef struct{
volatile signed short raw;
volatile float av;
volatile float value;
volatile signed int total;
signed short history[AAV_LEN];
} sensorStructMag;
typedef struct{
sensorStructMag X;
sensorStructMag Y;
sensorStructMag Z;
unsigned int count;
} threeAxisSensorStructMag;
threeAxisSensorStructGyro Gyro;
threeAxisSensorStructAccel Accel;
threeAxisSensorStructMag Mag;
void ReadGyroSensors(void);
void ReadAccelSensors(void);
void ReadMagSensors(void);
// *** Input stuff
unsigned short rcInput[7];
unsigned int rxLoss;
unsigned int rxFirst;
signed short yawtrim;
signed short throttletrim;
float throttle;
float nonLinearThrottle(float input);
unsigned int auxState, flapState, flapEdge;
float pitchcorrectionav, rollcorrectionav, yawcorrectionav;
// *** Ultrasound
float ultra;
float ultraav = 0;
unsigned int ultraLoss;
float ultrathrottle;
float ultraTkOffThrottle;
unsigned int ultraTkOffInput;
unsigned int airborne;
unsigned int landing;
unsigned int throttleHoldOff;
// *** Output stuff
float motorN, motorE, motorS, motorW;
float motorNav, motorEav, motorSav, motorWav;
float tempN;
float tempE;
float tempS;
float tempW;
float pitch_dd;
float roll_dd;
float throttleold;
float CFDC_mag_x_zero_local;
float CFDC_mag_y_zero_local;
float CFDC_mag_z_zero_local;
float CFDC_mag_x_zero_local_derotate;
float CFDC_mag_y_zero_local_derotate;
float CFDC_mag_z_zero_local_derotate;
float CFDC_mag_x_zero_local_unelipse;
float CFDC_mag_y_zero_local_unelipse;
float CFDC_mag_z_zero_local_unelipse;
float CFDC_mag_x_zero_local_unelipse_decentre;
float CFDC_mag_y_zero_local_unelipse_decentre;
float CFDC_mag_z_zero_local_unelipse_decentre;
float CFDC_mag_x_zero;
float CFDC_mag_y_zero;
float CFDC_mag_z_zero;
float CFDC_mag_x_error;
float CFDC_mag_y_error;
float CFDC_mag_z_error;
float CFDC_qdiff1;
float CFDC_qdiff2;
float CFDC_qdiff3;
float CFDC_qdiff4;
float CFDC_iq1_zero;
float CFDC_iq2_zero;
float CFDC_iq3_zero;
float CFDC_iq4_zero;
float CFDC_count;
unsigned int yaw_lock;
// *** ARM thingy
unsigned int armed, calib, zeroThrotCounter;
void Arm(void);
void Disarm(void);
// *** Button stuff
unsigned int PRGBlankTimer; // Blanking time for button pushes
unsigned int PRGTimer; // Timer for button pushes, continuously increments as the button is held
unsigned int PRGPushTime; // Contains the time that a button was pushed for, populated after button is released
// ****************************************************************************
// *** Initialiseation
// ****************************************************************************
// *** Initialiser function: sets everything up.
void setup() {
// *** LED setup
LEDInit(PLED | VLED);
LEDOff(PLED | VLED);
flashPLED = 0;
flashVLED = 0;
flashRLED = 0;
armed = 0;
calib = 0;
zeroThrotCounter = 0;
// *** Timers and couters6
rxLoss = 50;
ultraLoss = ULTRA_OVTH + 1;
sysMS = 0;
sysUS = 0;
SysTickInit(); // SysTick enable (default 1ms)
// *** Parameters
paramCount = sizeof(paramStorage)/20;
EEPROMLoadAll();
paramSendCount = paramCount;
paramSendSingle = 0;
// *** Establish ILink
ilink_thalstat.sensorStatus = 1; // set ilink status to boot
ilink_thalstat.flightMode = (0x1 << 0); // attitude control
ilink_identify.deviceID = WHO_AM_I;
ilink_identify.firmVersion = FIRMWARE_VERSION;
ILinkInit(SLAVE);
// *** Initialise input
throttle = 0;
rxFirst = 0;
auxState = 0;
flapEdge = 0;
RXInit();
// *** Initialise Ultrasound
UltraInit();
//UltraFast();
alt.demandincr = 0;
// *** Battery sensor
ADCInit(CHN7);
ADCTrigger(CHN7);
Delay(1);
ilink_thalstat.battVoltage = (ADCGet() * 6325) >> 10; // Because the factor is 6325/1024, we can do this in integer maths by right-shifting 10 bits instead of dividing by 1024.
// *** Calibrate Sensors
Delay(500);
SensorInit();
SensorZero();
// Current Field Distortion Compensation initialisation
CFDC_count = 0;
// *** Ultrasound
UltraInit();
UltraFast();
// *** quaternion AHRS init
q1 = 1;
q2 = 0;
q3 = 0;
q4 = 0;
// *** Rotation matrix Initialisation
MM1 = 1;
MM2 = 0;
MM3 = 0;
MM4 = 0;
MM5 = 1;
MM6 = 0;
MM7 = 0;
MM8 = 0;
MM9 = 1;
pitch_dd = 0;
roll_dd = 0;
pitch.bias = 0;
roll.bias = 0;
/*thetaAngle = 0;
phiAngle = 0;
psiAngle = 0;
pitchcorrectionav = 0;
rollcorrectionav = 0;
yawcorrectionav = 0;*/
// *** Timer for AHRS
// Set high confidence in accelerometer/magneto to rotate AHRS to initial heading
float tempAccelKp = DRIFT_AccelKp;
float tempMagKp = DRIFT_MagKp;
DRIFT_MagKp *= 100;
DRIFT_AccelKp *= 100;
//float tempQAMK = QAMK;
//QAMK = 1;
Timer0Init(59);
Timer0Match0(1200000/FAST_RATE, INTERRUPT | RESET);
Delay(1000);
//QAMK = tempQAMK;
DRIFT_MagKp = tempMagKp;
DRIFT_AccelKp = tempAccelKp;
slowSoftscale = 0;
//inputSoftscale = 3;
//baroSoftscale = 7;
// *** Initialise PWM outputs
motorN = 0;
motorE = 0;
motorS = 0;
motorW = 0;
motorNav = 0;
motorEav = 0;
motorSav = 0;
motorWav = 0;
throttleold = 0;
// *** Initialise timers and loops
//PWMInit(PWM_NESW);
PWMInit(PWM_X | PWM_Y);
gim_ptemp = 0;
RITInitms(1000/MESSAGE_LOOP_HZ);
flashPLED = 0;
LEDOff(PLED);
lat_diff = 0;
lon_diff = 0;
lat_diff_i = 0;
lon_diff_i = 0;
lat_diff_d = 0;
lon_diff_d = 0;
yaw_lock = 0;
}
void Arm(void) {
if(CAL_AUTO > 0) {
CalibrateGyroTemp(1);
}
PWMInit(PWM_NESW);
PWMSetNESW(THROTTLEOFFSET, THROTTLEOFFSET, THROTTLEOFFSET, THROTTLEOFFSET);
if(armed == 0) {
Delay(500);
PWMSetNESW(THROTTLEOFFSET + IDLETHROTTLE, THROTTLEOFFSET + IDLETHROTTLE, THROTTLEOFFSET + IDLETHROTTLE, THROTTLEOFFSET + IDLETHROTTLE);
Delay(100);
PWMSetNESW(THROTTLEOFFSET, THROTTLEOFFSET, THROTTLEOFFSET, THROTTLEOFFSET);
Delay(300);
PWMSetNESW(THROTTLEOFFSET + IDLETHROTTLE, THROTTLEOFFSET + IDLETHROTTLE, THROTTLEOFFSET + IDLETHROTTLE, THROTTLEOFFSET + IDLETHROTTLE);
Delay(100);
PWMSetNESW(THROTTLEOFFSET, THROTTLEOFFSET, THROTTLEOFFSET, THROTTLEOFFSET);
}
armed = 1;
psiAngleinit = psiAngle; // simplicity reference
ilink_thalstat.sensorStatus &= ~(0x7); // mask status
ilink_thalstat.sensorStatus |= 4; // active/armed
}
void Disarm(void) {
if(armed) {
PWMSetNESW(THROTTLEOFFSET, THROTTLEOFFSET, THROTTLEOFFSET, THROTTLEOFFSET);
Delay(100);
PWMSetN(THROTTLEOFFSET + IDLETHROTTLE);
Delay(100);
PWMSetN(THROTTLEOFFSET);
Delay(33);
PWMSetE(THROTTLEOFFSET + IDLETHROTTLE);
Delay(100);
PWMSetE(THROTTLEOFFSET);
Delay(33);
PWMSetS(THROTTLEOFFSET + IDLETHROTTLE);
Delay(100);
PWMSetS(THROTTLEOFFSET);
Delay(33);
PWMSetW(THROTTLEOFFSET + IDLETHROTTLE);
Delay(100);
PWMSetW(THROTTLEOFFSET);
Delay(100);
}
PWMSetNESW(0, 0, 0, 0);
armed = 0;
ilink_thalstat.sensorStatus &= ~(0x7); // mask status
ilink_thalstat.sensorStatus |= 3; // standby
}
void CalibrateMagneto(void) {
unsigned int i;
unsigned int good;
float Xav, Yav, Zav;
float distance;
signed short Xmax, Xmin, Ymax, Ymin, Zmax, Zmin;
unsigned int started;
if(armed == 0) {
ilink_thalstat.sensorStatus = 2; // calibrating
started = 0;
Xav = 0;
Yav = 0;
Zav = 0;
flashVLED = 1;
good = 0;
ReadMagSensors();
Xmax = Mag.X.raw;
Xmin = Xmax;
Ymax = Mag.Y.raw;
Ymin = Ymax;
Zmax = Mag.Z.raw;
Zmin = Zmax;
// Wait for Gyro to be steady or 20 seconds
for(i=0; i<5000; i++) {
ReadGyroSensors();
// calculate distance of data from running average
distance = (Xav - (float)Gyro.X.raw)*(Xav - (float)Gyro.X.raw);
distance += (Yav - (float)Gyro.Y.raw)*(Yav - (float)Gyro.Y.raw);
distance += (Zav - (float)Gyro.Z.raw)*(Zav - (float)Gyro.Z.raw);
if(started == 0) {
// before starting, wait for gyro to move around
if(distance > 2000) {
// high-movement, increment good counter and add average value.
good++;
if(good >= 10) {
started = 1; // if enough movement readings, escape loop
good = 0;
flashVLED = 2;
}
}
else {
good = 0;
}
}
else {
ReadMagSensors();
if(Mag.X.raw > Xmax) Xmax = Mag.X.raw;
else if(Mag.X.raw < Xmin) Xmin = Mag.X.raw;
if(Mag.Y.raw > Ymax) Ymax = Mag.Y.raw;
else if(Mag.Y.raw < Ymin) Ymin = Mag.Y.raw;
if(Mag.Z.raw > Zmax) Zmax = Mag.Z.raw;
else if(Mag.Z.raw < Zmin) Zmin = Mag.Z.raw;
if(distance < 2000) {
// high-movement, increment good counter and add average value.
good++;
if(good >= 200) break; // if enough movement readings, escape loop
}
else {
good = 0;
}
}
Xav *= 0.95f;
Xav += 0.05f * (float)Gyro.X.raw;
Yav *= 0.95f;
Yav += 0.05f * (float)Gyro.Y.raw;
Zav *= 0.95f;
Zav += 0.05f * (float)Gyro.Z.raw;
Delay(14);
}
MAGCOR_M1 = (Xmax + Xmin)/2;
MAGCOR_M2 = (Ymax + Ymin)/2;
MAGCOR_M3 = (Zmax + Zmin)/2;
EEPROMSaveAll();
flashPLED = 0;
LEDOff(PLED);
ilink_thalstat.sensorStatus &= ~(0x7); // mask status
ilink_thalstat.sensorStatus |= 3; // standby
}
}
// *** Calibrates all the sensors
void SensorZero(void) {
unsigned int i;
signed short data[4];
if(!GetGyro(data) || !GetMagneto(data) || !GetAccel(data) || GetBaro() == 0) {
LEDInit(PLED | VLED);
LEDOn(PLED);
LEDOff(VLED);
flashPLED = 2;
flashVLED = 2;
while(1);
}
// *** Zero totals
Gyro.X.total = 0;
Gyro.Y.total = 0;
Gyro.Z.total = 0;
Accel.X.total = 0;
Accel.Y.total = 0;
Accel.Z.total = 0;
Mag.X.total = 0;
Mag.Y.total = 0;
Mag.Z.total = 0;
Gyro.X.error = 0;
Gyro.Y.error = 0;
Gyro.Z.error = 0;
Gyro.X.verterror = 0;
Gyro.Y.verterror = 0;
Gyro.Z.verterror = 0;
for(i=0; (i<GAV_LEN); i++) {
Gyro.X.history[i] = 0;
Gyro.Y.history[i] = 0;
Gyro.Z.history[i] = 0;
}
for(i=0; (i<AAV_LEN); i++) {
Accel.X.history[i] = 0;
Accel.Y.history[i] = 0;
Accel.Z.history[i] = 0;
}
for(i=0; (i<MAV_LEN); i++) {
Mag.X.history[i] = 0;
Mag.Y.history[i] = 0;
Mag.Z.history[i] = 0;
}
Gyro.X.offset = 0;
Gyro.Y.offset = 0;
Gyro.Z.offset = 0;
// pre-seed averages
for(i=0; (i<GAV_LEN); i++) {
ReadGyroSensors();
}
for(i=0; (i<AAV_LEN); i++) {
ReadAccelSensors();
}
for(i=0; (i<MAV_LEN); i++) {
ReadMagSensors();
}
ilink_thalstat.sensorStatus |= (0xf << 3);
}
void CalibrateGyro(void) {
CalibrateGyroTemp(6);
CAL_GYROX = Gyro.X.offset;
CAL_GYROY = Gyro.Y.offset;
CAL_GYROZ = Gyro.Z.offset;
EEPROMSaveAll();
}
void CalibrateGyroTemp(unsigned int seconds) {
unsigned int i;
// *** Calibrate Gyro
unsigned int good;
float Xav, Yav, Zav;
signed int Xtotal, Ytotal, Ztotal;
float distance;
if(armed == 0) {
ReadGyroSensors();
Xtotal = 0;
Ytotal = 0;
Ztotal = 0;
Xav = Gyro.X.raw;
Yav = Gyro.Y.raw;
Zav = Gyro.Z.raw;
flashPLED = 1;
good = 0;
// Wait for Gyro to be steady or 20 seconds
for(i=0; i<5000; i++) {
ReadGyroSensors();
// calculate distance of data from running average
distance = (Xav - (float)Gyro.X.raw)*(Xav - (float)Gyro.X.raw);
distance += (Yav - (float)Gyro.Y.raw)*(Yav - (float)Gyro.Y.raw);
distance += (Zav - (float)Gyro.Z.raw)*(Zav - (float)Gyro.Z.raw);
if(distance < 2000) {
// low-movement, increment good counter and add average value.
good++;
Xtotal += Gyro.X.raw;
Ytotal += Gyro.Y.raw;
Ztotal += Gyro.Z.raw;
if(good >= 333) break; // if enough good readings, escape loop
}
else {
// high movement, zero good counter, and average values.
good = 0;
Xtotal = 0;
Ytotal = 0;
Ztotal = 0;
}
Xav *= 0.95f;
Xav += 0.05f * (float)Gyro.X.raw;
Yav *= 0.95f;
Yav += 0.05f * (float)Gyro.Y.raw;
Zav *= 0.95f;
Zav += 0.05f * (float)Gyro.Z.raw;
Delay(3);
}
ilink_thalstat.sensorStatus &= ~(0x7); // mask status
ilink_thalstat.sensorStatus |= 2; // calibrating
flashPLED=2;
LEDOn(PLED);
// at this point should have at least 200 good Gyro readings, take some more
for(i=0; i<seconds*333; i++) {
ReadGyroSensors();
Xtotal += Gyro.X.raw;
Ytotal += Gyro.Y.raw;
Ztotal += Gyro.Z.raw;
Delay(3);
}
Gyro.X.offset = (float)Xtotal/(float)((seconds+1) * 333);
Gyro.Y.offset = (float)Ytotal/(float)((seconds+1) * 333);
Gyro.Z.offset = (float)Ztotal/(float)((seconds+1) * 333);
/*CAL_GYROX = Gyro.X.offset;
CAL_GYROY = Gyro.Y.offset;
CAL_GYROZ = Gyro.Z.offset;
EEPROMSaveAll();*/
flashPLED = 0;
LEDOff(PLED);
ilink_thalstat.sensorStatus &= ~(0x7); // mask status
ilink_thalstat.sensorStatus |= 3; // standby
}
}
// ****************************************************************************
// *** Main loop and timer loops
// ****************************************************************************
// *** Main loop, nothing much happens in here.
void loop() {
//if(idleCount < IDLE_MAX) idleCount++; // this is the counter for CPU idle time
// *** Deal with button push
if(PRGBlankTimer == 0) {
if(PRGTimer > 3000) {
RXBind();
PRGPushTime = 0;
PRGTimer = 0;
PRGBlankTimer = 200;
}
}
__WFI();
}
// *** SysTick timer: deals with general timing
void SysTickInterrupt(void) {
sysMS += 1;
sysUS += 1000;