-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1330 lines (1063 loc) · 32 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 <math.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include "main.h"
#include "device.h"
#include "uartdebug.h"
#include "mpu6500.h"
#include "hp206c.h"
#include "esp8266.h"
#include "qmc5883l.h"
#include "dsp.h"
#include "crsf.h"
// Max length for info packet
// sent back to operator
#define INFOLEN 512
// device numbers
#define MPU_DEV 0
#define HP_DEV 1
#define QMC_DEV 2
#define CRSF_DEV 3
#define DEV_COUNT 4
// timer settings
#define PRESCALER 48
#define OCSFREQ 48000000
#define TIMPERIOD 0xffff
#define TICKSPERSEC (OCSFREQ / PRESCALER)
// PWM settings
#define PWM_MAXCOUNT 2500
// DSP settings
#define PID_FREQ 1000
#define CALIB_FREQ 25
#define HP_FREQ 25
#define CRSF_FREQ 100
#define USER_FLASH 0x0801f800
#define USER_SETSLOTS (0x80 / sizeof(struct settings))
// Timer events
#define TEV_PID 0
#define TEV_CHECK 1
#define TEV_CALIB 2
#define TEV_HP 3
#define TEV_COUNT 4
// Debug connection port
#define SERVPORT 8880
#define WIFI_TIMEOUT 3
#define ELRS_TIMEOUT 3
#define checktimev(ev) ((ev)->ms > TICKSPERSEC / (ev)->freq)
#define resettimev(ev) (ev)->ms = 0;
#define updatetimev(ev, s) (ev)->ms += (s);
struct settings {
float mx0, my0, mz0;
float mxsc, mysc, mzsc;
float magdecl;
float gx0, gy0, gz0;
float roll0, pitch0, yaw0;
float tcoef, ttcoef, atcoef;
int speedpid;
int yawspeedpid;
float p, i, d;
float sp, si, sd;
float yp, yi, yd;
float ysp, ysi, ysd;
float zsp, zsi, zsd;
};
struct timev {
int ms;
int freq;
int (*cb)(int);
};
void systemclock_config();
static void gpio_init();
static void i2c_init();
static void spi1_init();
static void dma_init();
static void tim1_init();
static void tim2_init();
static void adc1_init(void);
static void usart1_init();
static void usart2_init();
static void usart3_init();
static void esc_init();
static void hp_init();
static void mpu_init();
static void qmc_init();
static void espdev_init();
static void crsfdev_init();
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
I2C_HandleTypeDef hi2c1;
SPI_HandleTypeDef hspi1;
TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim2;
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
UART_HandleTypeDef huart3;
DMA_HandleTypeDef hdma_usart2_rx;
// IC drivers
struct cdevice dev[DEV_COUNT];
struct esp_device espdev;
struct crsf_device crsfdev;
// DSP contexts
struct dsp_lpf tlpf;
struct dsp_lpf altlpf;
float temp;
struct dsp_compl pitchcompl;
struct dsp_compl rollcompl;
struct dsp_pidval pitchpv;
struct dsp_pidval rollpv;
struct dsp_pidval pitchspv;
struct dsp_pidval rollspv;
struct dsp_pidval yawpv;
struct dsp_pidval yawspv;
struct dsp_pidval tpv;
// control values
float thrust = 0.0;
float rolltarget = 0.0, pitchtarget = 0.0, yawtarget = 0.0;
float en = 0.0;
int magcalibmode = 0;
int elrs = 0;
// pressure and altitude initial values
float alt0;
// settings
struct settings st;
// timer events
struct timev evs[TEV_COUNT];
int loops = 0;
int loopscount = 0;
int wifitimeout = WIFI_TIMEOUT;
int elrstimeout = ELRS_TIMEOUT;
uint32_t getadcv(ADC_HandleTypeDef *hadc)
{
uint32_t v;
HAL_ADC_Start(hadc);
HAL_ADC_PollForConversion(hadc, 1);
v = HAL_ADC_GetValue(hadc);
HAL_ADC_Stop(hadc);
return v;
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
esp_interrupt(&espdev, huart);
if (dev[CRSF_DEV].status == DEVSTATUS_INIT)
dev[CRSF_DEV].interrupt(dev[CRSF_DEV].priv, huart);
}
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
esp_error(&espdev, huart);
}
int inittimev(struct timev *ev, int freq, int (*cb)(int))
{
ev->ms = 0;
ev->freq = freq;
ev->cb = cb;
return 0;
}
int averageposition(float *ax, float *ay, float *az, float *gx,
float *gy, float *gz)
{
struct mpu_data md;
float axt, ayt, azt, gxt, gyt, gzt;
int i;
axt = ayt = azt = gxt = gyt = gzt = 0;
for (i = 0; i < 250; ++i) {
dev[MPU_DEV].read(dev[MPU_DEV].priv, &md,
sizeof(struct mpu_data));
gxt += md.gfx;
gyt += md.gfy;
gzt += md.gfz;
axt += md.afx;
ayt += md.afy;
azt += md.afz;
HAL_Delay(10);
}
*gx = gxt / 250.0;
*gy = gyt / 250.0;
*gz = gzt / 250.0;
*ax = axt / 250.0;
*ay = ayt / 250.0;
*az = azt / 250.0;
return 0;
}
float qmc_heading(float r, float p, float x, float y, float z)
{
x = st.mxsc * (x + st.mx0);
y = st.mysc * (y + st.my0);
z = st.mzsc * (z + st.mz0);
x = x * cosf(p) + y * sinf(r) * sinf(p)
+ z * cosf(r) * sinf(p);
y = y * cosf(r) - z * sinf(r);
return circf(atan2f(y, x) + st.magdecl);
}
int setthrust(float ltd, float rtd, float rbd, float lbd)
{
if (isnan(ltd) || isnan(rtd) || isnan(rbd) || isnan(lbd))
ltd = rtd = rbd = lbd = 0.0;
TIM1->CCR1 = (uint16_t) ((trimuf(ltd) * 0.05 + 0.049)
* (float) PWM_MAXCOUNT);
TIM1->CCR2 = (uint16_t) ((trimuf(rtd) * 0.05 + 0.049)
* (float) PWM_MAXCOUNT);
TIM1->CCR3 = (uint16_t) ((trimuf(rbd) * 0.05 + 0.049)
* (float) PWM_MAXCOUNT);
TIM1->CCR4 = (uint16_t) ((trimuf(lbd) * 0.05 + 0.049)
* (float) PWM_MAXCOUNT);
return 0;
}
int writesettings(int slot)
{
FLASH_EraseInitTypeDef ferase;
uint32_t serror;
uint32_t sz;
uint32_t *pt;
uint32_t addr;
int j;
__disable_irq();
HAL_FLASH_Unlock();
ferase.TypeErase = FLASH_TYPEERASE_PAGES;
ferase.PageAddress = USER_FLASH;
ferase.NbPages = 1;
HAL_FLASHEx_Erase(&ferase, &serror);
sz = sizeof(struct settings);
pt = (uint32_t *) &st;
addr = USER_FLASH + sizeof(struct settings) * slot;
for (j = 0; j < sz / 4; ++j) {
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr, pt[j]);
addr += 4;
}
HAL_FLASH_Lock();
__enable_irq();
return 0;
}
int readsettings(int slot)
{
memcpy(&st, (void *) (USER_FLASH + slot),
sizeof(struct settings));
return 0;
}
int initstabilize(float alt)
{
float ax, ay, az;
alt0 = alt;
averageposition(&ax, &ay, &az, &(st.gx0), &(st.gy0), &(st.gz0));
dsp_initcompl(&pitchcompl, st.tcoef, PID_FREQ);
dsp_initcompl(&rollcompl, st.tcoef, PID_FREQ);
dsp_initpidval(&pitchpv, st.p, st.i, st.d, 0.0);
dsp_initpidval(&rollpv, st.p, st.i, st.d, 0.0);
dsp_initpidval(&pitchspv, st.sp, st.si, st.sd, 0.0);
dsp_initpidval(&rollspv, st.sp, st.si, st.sd, 0.0);
dsp_initpidval(&yawspv, st.ysp, st.ysi, st.ysd, 0.0);
dsp_initpidval(&yawpv, st.yp, st.yi, st.yd, 0.0);
dsp_initpidval(&tpv, st.zsp, st.zsi, st.zsd, 0.0);
dsp_initlpf(&altlpf, st.atcoef, HP_FREQ);
dsp_initlpf(&tlpf, st.ttcoef, PID_FREQ);
return 0;
}
int stabilize(int ms)
{
struct mpu_data md;
struct qmc_data hd;
float roll, pitch, yaw;
float rollcor, pitchcor, yawcor, thrustcor;
float gy, gx, gz;
float dt;
dt = ms / (float) TICKSPERSEC;
dt = (dt < 0.000001) ? 0.000001 : dt;
dev[MPU_DEV].read(dev[MPU_DEV].priv, &md,
sizeof(struct mpu_data));
dsp_updatelpf(&tlpf, md.afz);
gy = deg2rad((md.gfy - st.gy0));
gx = deg2rad((md.gfx - st.gx0));
gz = deg2rad((md.gfz - st.gz0));
roll = dsp_updatecompl(&rollcompl, gy * dt,
atan2f(-md.afx,
sqrt(md.afy * md.afy + md.afz * md.afz))) - st.roll0;
pitch = dsp_updatecompl(&pitchcompl, gx * dt,
atan2f(md.afy,
sqrt(md.afx * md.afx + md.afz * md.afz))) - st.pitch0;
dev[QMC_DEV].read(dev[QMC_DEV].priv, &hd,
sizeof(struct qmc_data));
yaw = circf(qmc_heading(pitch, roll, hd.fx, hd.fy, hd.fz)
- st.yaw0);
if (st.speedpid) {
rollcor = dsp_pid(&rollspv, rolltarget, gy, dt);
pitchcor = dsp_pid(&pitchspv, pitchtarget, gx, dt);
}
else {
rollcor = dsp_pid(&rollpv, rolltarget, roll, dt);
pitchcor = dsp_pid(&pitchpv, pitchtarget, pitch, dt);
rollcor = dsp_pid(&rollspv, rollcor, gy, dt);
pitchcor = dsp_pid(&pitchspv, pitchcor, gx, dt);
}
if (st.yawspeedpid)
yawcor = dsp_pid(&yawspv, yawtarget, gz, dt);
else {
yawcor = dsp_circpid(&yawpv, yawtarget, yaw, dt);
yawcor = dsp_pid(&yawspv, yawcor, gz, dt);
}
thrustcor = dsp_pid(&tpv, thrust + 1.0, dsp_getlpf(&tlpf), dt);
setthrust(en * (thrustcor + 0.5 * rollcor
+ 0.5 * pitchcor - 0.5 * yawcor),
en * (thrustcor - 0.5 * rollcor
+ 0.5 * pitchcor + 0.5 * yawcor),
en * (thrustcor - 0.5 * rollcor
- 0.5 * pitchcor - 0.5 * yawcor),
en * (thrustcor + 0.5 * rollcor
- 0.5 * pitchcor + 0.5 * yawcor));
++loops;
return 0;
}
int checkconnection(int ms)
{
if (wifitimeout != 0)
--wifitimeout;
if (elrstimeout != 0)
--elrstimeout;
if (wifitimeout <= 0 && elrs == 0) {
char s[INFOLEN];
setthrust(0.0, 0.0, 0.0, 0.0);
en = 0.0;
snprintf(s, INFOLEN, "wi-fi timed out!\n\r");
esp_send(&espdev, s);
}
if (elrstimeout <= 0 && elrs == 1) {
setthrust(0.0, 0.0, 0.0, 0.0);
en = 0.0;
}
// since it runs every 1 second update loop counter here too
loopscount = loops;
loops = 0;
return 0;
}
int magcalib(int ms)
{
struct qmc_data hd;
char s[INFOLEN];
if (!magcalibmode)
return 0;
dev[QMC_DEV].read(dev[QMC_DEV].priv, &hd,
sizeof(struct qmc_data));
sprintf(s, "%f %f %f\r\n",
(double) (st.mxsc * (hd.fx + st.mx0)),
(double) (st.mysc * (hd.fy + st.my0)),
(double) (st.mzsc * (hd.fz + st.mz0)));
esp_send(&espdev, s);
return 0;
}
int hpupdate(int ms)
{
struct hp_data hd;
if (dev[HP_DEV].status != DEVSTATUS_INIT)
return 0;
dev[HP_DEV].read(dev[HP_DEV].priv, &hd,
sizeof(struct hp_data));
dsp_updatelpf(&altlpf, hd.altf);
temp = hd.tempf;
return 0;
}
int crsfget(int ms)
{
return 0;
}
int parsecommand(char **toks, int maxtoks, char *data)
{
int i;
for (i = 0; i < maxtoks; ++i)
toks[i] = "";
i = 0;
toks[i++] = strtok((char *) data, " ");
while (i < maxtoks && (toks[i++] = strtok(NULL, " ")) != NULL);
return (i - 1);
}
int sprintpos(char *s, struct mpu_data *md, struct qmc_data *hd)
{
s[0] = '\0';
snprintf(s + strlen(s), INFOLEN - strlen(s),
"%-7sx = %0.3f; y = %0.3f; z = %0.3f\n\r", "accel: ",
(double) md->afx, (double) md->afy, (double) md->afz);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"%-7sx = %0.3f; y = %0.3f; z = %0.3f\n\r", "gyro: ",
(double) md->gfx, (double) md->gfy, (double) md->gfz);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"roll: %0.3f; pitch: %0.3f; yaw: %0.3f\n\r",
(double) (dsp_getcompl(&rollcompl) - st.roll0),
(double) (dsp_getcompl(&pitchcompl) - st.pitch0),
(double) circf(qmc_heading(
-(dsp_getcompl(&pitchcompl) - st.pitch0),
-(dsp_getcompl(&rollcompl) - st.roll0),
hd->fx, hd->fy, hd->fz) - st.yaw0));
snprintf(s + strlen(s), INFOLEN - strlen(s),
"z acceleration: %f\r\n",
(double) dsp_getlpf(&tlpf));
snprintf(s + strlen(s), INFOLEN - strlen(s),
"battery: %0.3f\n\r",
getadcv(&hadc1) / (double) 0xfff * (double) 9.9276);
return 0;
}
int sprintqmc(char *s, struct qmc_data *hd)
{
s[0] = '\0';
snprintf(s + strlen(s), INFOLEN - strlen(s),
"x = %0.3f; y = %0.3f; z = %0.3f\n\r",
(double) hd->fx, (double) hd->fy, (double) hd->fz);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"heading: %f\r\n", (double) qmc_heading(
(dsp_getcompl(&pitchcompl) - st.pitch0),
(dsp_getcompl(&rollcompl) - st.roll0),
hd->fx, hd->fy, hd->fz));
return 0;
}
int sprintvalues(char *s)
{
s[0] = '\0';
snprintf(s + strlen(s), INFOLEN - strlen(s),
"t: %.3f; r: %.3f; p: %.3f; y: %.3f\r\n",
(double) thrust, (double) rolltarget,
(double) pitchtarget, (double) yawtarget);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"mag off: %.3f; %.3f; %.3f\r\n",
(double) st.mx0, (double) st.my0, (double) st.mz0);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"mag scale: %.3f; %.3f; %.3f\r\n",
(double) st.mxsc, (double) st.mysc, (double) st.mzsc);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"mag decl: %.5f\r\n",
(double) st.magdecl);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"gyro cor: %.3f; %.3f; %.3f\r\n",
(double) st.gx0, (double) st.gy0, (double) st.gz0);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"roll cor: %.3f; pitch cor: %.3f; yaw cor: %.3f\r\n",
(double) st.roll0, (double) st.pitch0,
(double) st.yaw0);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"motors state: %.3f\r\n", (double) en);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"tc: %.6f\r\n", (double) st.tcoef);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"accel tc: %.6f\r\n", (double) st.ttcoef);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"altitude tc: %.6f\r\n", (double) st.atcoef);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"loops count: %d\r\n", loopscount);
return 0;
}
int sprintpid(char *s)
{
s[0] = '\0';
snprintf(s + strlen(s), INFOLEN - strlen(s),
"pos PID: %.3f,%.3f,%.3f\r\n",
(double) st.p, (double) st.i, (double) st.d);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"speed PID: %.3f,%.3f,%.3f\r\n",
(double) st.sp, (double) st.si, (double) st.sd);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"yaw PID: %.3f,%.3f,%.3f\r\n",
(double) st.yp, (double) st.yi, (double) st.yd);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"yaw speed PID: %.3f,%.3f,%.3f\r\n",
(double) st.ysp, (double) st.ysi, (double) st.ysd);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"thrust PID: %.3f,%.3f,%.3f\r\n",
(double) st.zsp, (double) st.zsi, (double) st.zsd);
snprintf(s + strlen(s), INFOLEN - strlen(s),
"%s mode\r\n", st.speedpid ? "single" : "dual");
snprintf(s + strlen(s), INFOLEN - strlen(s),
"%s yaw mode\r\n",
st.yawspeedpid ? "single" : "dual");
return 0;
}
int controlcmd(char *cmd)
{
char s[INFOLEN];
char *toks[12];
if (cmd[0] == '\0')
return 0;
parsecommand(toks, 12, cmd);
if (strcmp(toks[0], "beep") == 0)
wifitimeout = WIFI_TIMEOUT;
else if (strcmp(toks[0], "info") == 0) {
if (strcmp(toks[1], "mpu") == 0) {
struct mpu_data md;
struct qmc_data hd;
dev[MPU_DEV].read(dev[MPU_DEV].priv, &md,
sizeof(struct mpu_data));
dev[QMC_DEV].read(dev[QMC_DEV].priv, &hd,
sizeof(struct qmc_data));
sprintpos(s, &md, &hd);
esp_send(&espdev, s);
}
else if (strcmp(toks[1], "qmc") == 0) {
struct qmc_data hd;
dev[QMC_DEV].read(dev[QMC_DEV].priv, &hd,
sizeof(struct qmc_data));
sprintqmc(s, &hd);
esp_send(&espdev, s);
}
else if (strcmp(toks[1], "hp") == 0) {
float alt;
alt = dsp_getlpf(&altlpf);
snprintf(s, INFOLEN, "temp: %f; alt: %f\r\n",
(double) temp, (double) alt);
esp_send(&espdev, s);
}
else if (strcmp(toks[1], "values") == 0) {
sprintvalues(s);
esp_send(&espdev, s);
}
else if (strcmp(toks[1], "pid") == 0) {
sprintpid(s);
esp_send(&espdev, s);
}
}
else if (strcmp(toks[0], "r") == 0)
en = 0.0;
else if (strcmp(toks[0], "e") == 0)
en = 1.0;
else if (strcmp(toks[0], "c") == 0)
initstabilize(atof(toks[1]));
else if (strcmp(toks[0], "t") == 0) {
float v;
v = atof(toks[2]);
if (strcmp(toks[1], "c") == 0) thrust = v;
else if (strcmp(toks[1], "r") == 0) rolltarget = v;
else if (strcmp(toks[1], "p") == 0) pitchtarget = v;
else if (strcmp(toks[1], "y") == 0) yawtarget = v;
else
goto unknown;
}
else if (strcmp(toks[0], "calib") == 0) {
if (strcmp(toks[1], "mag") == 0) {
if (strcmp(toks[2], "on") == 0)
magcalibmode = 1;
else if (strcmp(toks[2], "off") == 0)
magcalibmode = 0;
else
goto unknown;
}
else
goto unknown;
}
else if (strcmp(toks[0], "flash") == 0) {
if (strcmp(toks[1], "write") == 0)
writesettings(atoi(toks[2]));
else if (strcmp(toks[1], "read") == 0)
readsettings(atoi(toks[2]));
}
else if (strcmp(toks[0], "pid") == 0) {
float v;
v = atof(toks[3]);
if (strcmp(toks[1], "tilt") == 0) {
if (strcmp(toks[2], "mode") == 0) {
if (strcmp(toks[3], "double") == 0)
st.speedpid = 0;
else if (strcmp(toks[3], "single") == 0)
st.speedpid = 1;
else
goto unknown;
}
else if (strcmp(toks[2], "p") == 0)
st.p = v;
else if (strcmp(toks[2], "i") == 0)
st.i = v;
else if (strcmp(toks[2], "d") == 0)
st.d = v;
else
goto unknown;
dsp_setpid(&pitchpv, st.p, st.i, st.d);
dsp_setpid(&rollpv, st.p, st.i, st.d);
}
else if (strcmp(toks[1], "stilt") == 0) {
if (strcmp(toks[2], "p") == 0)
st.sp = v;
else if (strcmp(toks[2], "i") == 0)
st.si = v;
else if (strcmp(toks[2], "d") == 0)
st.sd = v;
else
goto unknown;
dsp_setpid(&pitchspv, st.sp, st.si, st.sd);
dsp_setpid(&rollspv, st.sp, st.si, st.sd);
}
else if (strcmp(toks[1], "yaw") == 0) {
if (strcmp(toks[2], "mode") == 0) {
if (strcmp(toks[3], "double") == 0)
st.yawspeedpid = 0;
else if (strcmp(toks[3], "single") == 0)
st.yawspeedpid = 1;
else
goto unknown;
}
else if (strcmp(toks[2], "p") == 0)
st.yp = v;
else if (strcmp(toks[2], "i") == 0)
st.yi = v;
else if (strcmp(toks[2], "d") == 0)
st.yd = v;
else
goto unknown;
dsp_setpid(&yawpv, st.yp, st.yi, st.yd);
}
else if (strcmp(toks[1], "syaw") == 0) {
if (strcmp(toks[2], "p") == 0)
st.ysp = v;
else if (strcmp(toks[2], "i") == 0)
st.ysi = v;
else if (strcmp(toks[2], "d") == 0)
st.ysd = v;
else
goto unknown;
dsp_setpid(&yawspv, st.ysp, st.ysi, st.ysd);
}
else if (strcmp(toks[1], "climb") == 0) {
if (strcmp(toks[2], "p") == 0)
st.zsp = v;
else if (strcmp(toks[2], "i") == 0)
st.zsi = v;
else if (strcmp(toks[2], "d") == 0)
st.zsd = v;
else
goto unknown;
dsp_setpid(&tpv, st.zsp, st.zsi, st.zsd);
}
else
goto unknown;
}
else if (strcmp(toks[0], "compl") == 0) {
st.tcoef = atof(toks[1]);
dsp_initcompl(&pitchcompl, st.tcoef, PID_FREQ);
dsp_initcompl(&rollcompl, st.tcoef, PID_FREQ);
}
else if (strcmp(toks[0], "lpf") == 0) {
if (strcmp(toks[1], "climb") == 0) {
st.ttcoef = atof(toks[2]);
dsp_initlpf(&tlpf, st.ttcoef, PID_FREQ);
}
else if (strcmp(toks[1], "altitude") == 0) {
st.atcoef = atof(toks[2]);
dsp_initlpf(&altlpf, st.atcoef, HP_FREQ);
}
else
goto unknown;
}
else if (strcmp(toks[0], "adj") == 0) {
float v;
v = atof(toks[2]);
if (strcmp(toks[1], "roll") == 0) st.roll0 = v;
else if (strcmp(toks[1], "pitch") == 0) st.pitch0 = v;
else if (strcmp(toks[1], "yaw") == 0) st.yaw0 = v;
else if (strcmp(toks[1], "mag") == 0) {
if (strcmp(toks[2], "x0") == 0)
st.mx0 = atof(toks[3]);
else if (strcmp(toks[2], "y0") == 0)
st.my0 = atof(toks[3]);
else if (strcmp(toks[2], "z0") == 0)
st.mz0 = atof(toks[3]);
else if (strcmp(toks[2], "xscale") == 0)
st.mxsc = atof(toks[3]);
else if (strcmp(toks[2], "yscale") == 0)
st.mysc = atof(toks[3]);
else if (strcmp(toks[2], "zscale") == 0)
st.mzsc = atof(toks[3]);
else if (strcmp(toks[2], "decl") == 0)
st.magdecl = atof(toks[3]);
else
goto unknown;
}
else
goto unknown;
}
else
goto unknown;
return 0;
unknown:
snprintf(s, INFOLEN, "Unknown command: %s\r\n", cmd);
esp_send(&espdev, s);
return (-1);
}
int crsfcmd(const struct crsf_data *cd)
{
elrs = (cd->chf[7] > 0.5) ? 1 : 0;
elrstimeout = ELRS_TIMEOUT;
if (elrs) {
en = 1.0;
pitchtarget = -cd->chf[0] * (M_PI / 6.0);
rolltarget = -cd->chf[1] * (M_PI / 6.0);
thrust = (cd->chf[2] + 0.75) / 3.5;
yawtarget = cd->chf[9] * M_PI;
en = (cd->chf[5] > 0.5) ? 1 : 0;
if (en < 0.5)
setthrust(0.0, 0.0, 0.0, 0.0);
}
return 0;
}
int main(void)
{
int i;
HAL_Init();
systemclock_config();
HAL_Delay(1000);
for (i = 0; i < DEV_COUNT; ++i)
dev[i].status = DEVSTATUS_NOINIT;
gpio_init();
tim1_init();
tim2_init();
dma_init();
i2c_init();
spi1_init();
adc1_init();
usart1_init();
usart2_init();
usart3_init();
esc_init();
mpu_init();
qmc_init();
espdev_init();
crsfdev_init();
HAL_Delay(1000);
hp_init();
readsettings(0);
initstabilize(0.0);
inittimev(evs + TEV_PID, PID_FREQ, stabilize);
inittimev(evs + TEV_CHECK, 1, checkconnection);
inittimev(evs + TEV_CALIB, CALIB_FREQ, magcalib);
inittimev(evs + TEV_HP, HP_FREQ, hpupdate);
wifitimeout = WIFI_TIMEOUT;
while (1) {
char cmd[ESP_CMDSIZE];
struct crsf_data cd;
int c, i;
__HAL_TIM_SET_COUNTER(&htim2, 0);
if (esp_poll(&espdev, cmd) >= 0)
controlcmd(cmd);
if (dev[CRSF_DEV].read(dev[CRSF_DEV].priv, &cd,
sizeof(struct crsf_data)) >= 0) {
crsfcmd(&cd);
}
for (i = 0; i < TEV_COUNT; ++i) {
if (checktimev(evs + i)) {
evs[i].cb(evs[i].ms);
resettimev(evs + i);
}
}
c = __HAL_TIM_GET_COUNTER(&htim2);
for (i = 0; i < TEV_COUNT; ++i)
updatetimev(evs + i, c);
}
return 0;
}
void systemclock_config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL3;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
error_handler();
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
error_handler();
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2
|RCC_PERIPHCLK_I2C1|RCC_PERIPHCLK_TIM1
|RCC_PERIPHCLK_ADC12;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
PeriphClkInit.Adc12ClockSelection = RCC_ADC12PLLCLK_DIV256;
PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
PeriphClkInit.Tim1ClockSelection = RCC_TIM1CLK_HCLK;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
error_handler();
}
static void gpio_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();