-
Notifications
You must be signed in to change notification settings - Fork 0
/
arm_car.c.bak5
executable file
·1433 lines (1213 loc) · 38.5 KB
/
arm_car.c.bak5
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
/*
* STM32F4 Wifi flight controller
* Copyright (C) 2012-2014 Adam Williams <broadcast at earthling dot net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
// Useful commands to install it:
// make arm
// uart_programmer car.bin
#include "arm_car.h"
#include "arm_math.h"
#include "cc1101.h"
#include "imu.h"
#include "linux.h"
#include "uart.h"
#include "misc.h"
#include "stm32f4xx.h"
#include "stm32f4xx_adc.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_tim.h"
#include <math.h>
#define SYNC_CODE 0xe5
#define DEBUG_PIN GPIO_Pin_4
#define DEBUG_GPIO GPIOB
#define WAVEFORM_SIZE (sizeof(sin_table) / sizeof(uint16_t))
#define MAX_PERIOD 65535
#define STEERING_PERIOD 4096
// Amount to change phase
#define FAST_STEERING 12
#define SLOW_STEERING 4
// minimum period stepper mode can go without stalling
#define MIN_STALL_PERIOD 2048
// period to switch to FREEWHEEL mode
#define MIN_STEPPER_PERIOD 1024
// minimum period for getting back EMF in freewheel mode
#define MIN_EMF_PERIOD 256
//#define MIN_EMF_PERIOD 1024
#define MOTOR_STEP 10
// convert EMF period to stepper period
#define EMF_TO_STEPPER (360 / 3 / MOTOR_STEP)
// number of PWM periods between phase changes
#define PWM_DOWNSAMPLE 128
#define PHASE_PRESCALE 128
#define PWM_PERIOD 1024
// -5 is the highest the timers can do without malfunctioning
#define MAX_PWM (PWM_PERIOD - 5)
// motors
#define LEFT_MOTOR 1
#define RIGHT_MOTOR 0
// motor modes
#define STEPPER 0
// float 1 phase with fixed commutation
#define FREEWHEEL 1
// freewheel with back EMF
#define FREEWHEEL_EMF 2
// back EMF filtering
// how rapidly the threshold voltage should adapt
#define BACK_EMF_WEIGHT 0.1f
// how rapidly the crossing time should adapt
#define PERIOD_WEIGHT 0.25f
#define I_DOWNSAMPLE (NAV_HZ / 100)
#define LEAD_COMPENSATION 1
#define THROTTLE_MIN 0x100
#define THROTTLE_MAX 0xff00
// analog range
#define STEERING_MID 0x8000
// deadband
#define STEERING_MIN 0x800
#define STEERING_MAX 0x7f00
#define MAX_SIN 65535
#define FIX_PHASE(x) \
while(x < 0) x += 360; \
while(x >= 360) x -= 360;
#define RIGHT_IN1_PIN GPIO_Pin_5
#define RIGHT_IN1_GPIO GPIOA
#define RIGHT_IN2_PIN GPIO_Pin_6
#define RIGHT_IN2_GPIO GPIOA
#define RIGHT_IN3_PIN GPIO_Pin_4
#define RIGHT_IN3_GPIO GPIOC
#define RIGHT_EN1_PIN GPIO_Pin_4
#define RIGHT_EN1_GPIO GPIOA
#define RIGHT_EN2_PIN GPIO_Pin_7
#define RIGHT_EN2_GPIO GPIOA
#define RIGHT_EN3_PIN GPIO_Pin_3
#define RIGHT_EN3_GPIO GPIOA
#define LEFT_IN1_PIN GPIO_Pin_1
#define LEFT_IN1_GPIO GPIOB
#define LEFT_IN2_PIN GPIO_Pin_2
#define LEFT_IN2_GPIO GPIOB
#define LEFT_IN3_PIN GPIO_Pin_13
#define LEFT_IN3_GPIO GPIOB
#define LEFT_EN3_PIN GPIO_Pin_5
#define LEFT_EN3_GPIO GPIOC
#define LEFT_EN2_PIN GPIO_Pin_12
#define LEFT_EN2_GPIO GPIOB
#define LEFT_EN1_PIN GPIO_Pin_0
#define LEFT_EN1_GPIO GPIOB
car_t car;
const uint16_t sin_table[] =
{
0x7fff, 0x80c8, 0x8191, 0x825a, 0x8323, 0x83ec, 0x84b5, 0x857e, 0x8647, 0x8710, 0x87d8, 0x88a1, 0x896a, 0x8a32, 0x8afa, 0x8bc3,
0x8c8b, 0x8d53, 0x8e1b, 0x8ee2, 0x8faa, 0x9072, 0x9139, 0x9200, 0x92c7, 0x938e, 0x9454, 0x951b, 0x95e1, 0x96a7, 0x976d, 0x9832,
0x98f8, 0x99bd, 0x9a82, 0x9b46, 0x9c0a, 0x9cce, 0x9d92, 0x9e56, 0x9f19, 0x9fdc, 0xa09e, 0xa161, 0xa223, 0xa2e4, 0xa3a5, 0xa466,
0xa527, 0xa5e7, 0xa6a7, 0xa766, 0xa826, 0xa8e4, 0xa9a3, 0xaa61, 0xab1e, 0xabdb, 0xac98, 0xad54, 0xae10, 0xaecb, 0xaf86, 0xb041,
0xb0fb, 0xb1b4, 0xb26d, 0xb326, 0xb3de, 0xb495, 0xb54c, 0xb603, 0xb6b9, 0xb76e, 0xb823, 0xb8d8, 0xb98c, 0xba3f, 0xbaf2, 0xbba4,
0xbc55, 0xbd07, 0xbdb7, 0xbe67, 0xbf16, 0xbfc5, 0xc073, 0xc120, 0xc1cd, 0xc279, 0xc324, 0xc3cf, 0xc47a, 0xc523, 0xc5cc, 0xc674,
0xc71c, 0xc7c2, 0xc869, 0xc90e, 0xc9b3, 0xca57, 0xcafa, 0xcb9d, 0xcc3f, 0xcce0, 0xcd80, 0xce20, 0xcebf, 0xcf5d, 0xcffa, 0xd097,
0xd132, 0xd1ce, 0xd268, 0xd301, 0xd39a, 0xd432, 0xd4c9, 0xd55f, 0xd5f4, 0xd689, 0xd71d, 0xd7af, 0xd842, 0xd8d3, 0xd963, 0xd9f3,
0xda81, 0xdb0f, 0xdb9c, 0xdc28, 0xdcb3, 0xdd3d, 0xddc6, 0xde4f, 0xded6, 0xdf5d, 0xdfe2, 0xe067, 0xe0eb, 0xe16e, 0xe1f0, 0xe271,
0xe2f1, 0xe370, 0xe3ee, 0xe46b, 0xe4e7, 0xe562, 0xe5dd, 0xe656, 0xe6ce, 0xe745, 0xe7bc, 0xe831, 0xe8a5, 0xe918, 0xe98b, 0xe9fc,
0xea6c, 0xeadb, 0xeb4a, 0xebb7, 0xec23, 0xec8e, 0xecf8, 0xed61, 0xedc9, 0xee2f, 0xee95, 0xeefa, 0xef5e, 0xefc0, 0xf022, 0xf082,
0xf0e1, 0xf140, 0xf19d, 0xf1f9, 0xf254, 0xf2ae, 0xf306, 0xf35e, 0xf3b4, 0xf40a, 0xf45e, 0xf4b1, 0xf503, 0xf554, 0xf5a4, 0xf5f3,
0xf640, 0xf68d, 0xf6d8, 0xf722, 0xf76b, 0xf7b3, 0xf7f9, 0xf83f, 0xf883, 0xf8c6, 0xf908, 0xf949, 0xf989, 0xf9c7, 0xfa04, 0xfa41,
0xfa7c, 0xfab5, 0xfaee, 0xfb25, 0xfb5c, 0xfb91, 0xfbc4, 0xfbf7, 0xfc28, 0xfc59, 0xfc88, 0xfcb6, 0xfce2, 0xfd0e, 0xfd38, 0xfd61,
0xfd89, 0xfdb0, 0xfdd5, 0xfdf9, 0xfe1c, 0xfe3e, 0xfe5e, 0xfe7e, 0xfe9c, 0xfeb9, 0xfed4, 0xfeef, 0xff08, 0xff20, 0xff37, 0xff4c,
0xff61, 0xff74, 0xff86, 0xff96, 0xffa6, 0xffb4, 0xffc1, 0xffcd, 0xffd7, 0xffe0, 0xffe8, 0xffef, 0xfff5, 0xfff9, 0xfffc, 0xfffe,
0xffff, 0xfffe, 0xfffc, 0xfff9, 0xfff5, 0xffef, 0xffe8, 0xffe0, 0xffd7, 0xffcd, 0xffc1, 0xffb4, 0xffa6, 0xff96, 0xff86, 0xff74,
0xff61, 0xff4c, 0xff37, 0xff20, 0xff08, 0xfeef, 0xfed4, 0xfeb9, 0xfe9c, 0xfe7e, 0xfe5e, 0xfe3e, 0xfe1c, 0xfdf9, 0xfdd5, 0xfdb0,
0xfd89, 0xfd61, 0xfd38, 0xfd0e, 0xfce2, 0xfcb6, 0xfc88, 0xfc59, 0xfc28, 0xfbf7, 0xfbc4, 0xfb91, 0xfb5c, 0xfb25, 0xfaee, 0xfab5,
0xfa7c, 0xfa41, 0xfa04, 0xf9c7, 0xf989, 0xf949, 0xf908, 0xf8c6, 0xf883, 0xf83f, 0xf7f9, 0xf7b3, 0xf76b, 0xf722, 0xf6d8, 0xf68d,
0xf640, 0xf5f3, 0xf5a4, 0xf554, 0xf503, 0xf4b1, 0xf45e, 0xf40a, 0xf3b4, 0xf35e, 0xf306, 0xf2ae, 0xf254, 0xf1f9, 0xf19d, 0xf140,
0xf0e1, 0xf082, 0xf022, 0xefc0, 0xef5e, 0xeefa, 0xee95, 0xee2f, 0xedc9, 0xed61, 0xecf8, 0xec8e, 0xec23, 0xebb7, 0xeb4a, 0xeadb,
0xea6c, 0xe9fc, 0xe98b, 0xe918, 0xe8a5, 0xe831, 0xe7bc, 0xe745, 0xe6ce, 0xe656, 0xe5dd, 0xe562, 0xe4e7, 0xe46b, 0xe3ee, 0xe370,
0xe2f1, 0xe271, 0xe1f0, 0xe16e, 0xe0eb, 0xe067, 0xdfe2, 0xdf5d, 0xded6, 0xde4f, 0xddc6, 0xdd3d, 0xdcb3, 0xdc28, 0xdb9c, 0xdb0f,
0xda81, 0xd9f3, 0xd963, 0xd8d3, 0xd842, 0xd7af, 0xd71d, 0xd689, 0xd5f4, 0xd55f, 0xd4c9, 0xd432, 0xd39a, 0xd301, 0xd268, 0xd1ce,
0xd132, 0xd097, 0xcffa, 0xcf5d, 0xcebf, 0xce20, 0xcd80, 0xcce0, 0xcc3f, 0xcb9d, 0xcafa, 0xca57, 0xc9b3, 0xc90e, 0xc869, 0xc7c2,
0xc71c, 0xc674, 0xc5cc, 0xc523, 0xc47a, 0xc3cf, 0xc324, 0xc279, 0xc1cd, 0xc120, 0xc073, 0xbfc5, 0xbf16, 0xbe67, 0xbdb7, 0xbd07,
0xbc55, 0xbba4, 0xbaf2, 0xba3f, 0xb98c, 0xb8d8, 0xb823, 0xb76e, 0xb6b9, 0xb603, 0xb54c, 0xb495, 0xb3de, 0xb326, 0xb26d, 0xb1b4,
0xb0fb, 0xb041, 0xaf86, 0xaecb, 0xae10, 0xad54, 0xac98, 0xabdb, 0xab1e, 0xaa61, 0xa9a3, 0xa8e4, 0xa826, 0xa766, 0xa6a7, 0xa5e7,
0xa527, 0xa466, 0xa3a5, 0xa2e4, 0xa223, 0xa161, 0xa09e, 0x9fdc, 0x9f19, 0x9e56, 0x9d92, 0x9cce, 0x9c0a, 0x9b46, 0x9a82, 0x99bd,
0x98f8, 0x9832, 0x976d, 0x96a7, 0x95e1, 0x951b, 0x9454, 0x938e, 0x92c7, 0x9200, 0x9139, 0x9072, 0x8faa, 0x8ee2, 0x8e1b, 0x8d53,
0x8c8b, 0x8bc3, 0x8afa, 0x8a32, 0x896a, 0x88a1, 0x87d8, 0x8710, 0x8647, 0x857e, 0x84b5, 0x83ec, 0x8323, 0x825a, 0x8191, 0x80c8,
0x7fff, 0x7f36, 0x7e6d, 0x7da4, 0x7cdb, 0x7c12, 0x7b49, 0x7a80, 0x79b7, 0x78ee, 0x7826, 0x775d, 0x7694, 0x75cc, 0x7504, 0x743b,
0x7373, 0x72ab, 0x71e3, 0x711c, 0x7054, 0x6f8c, 0x6ec5, 0x6dfe, 0x6d37, 0x6c70, 0x6baa, 0x6ae3, 0x6a1d, 0x6957, 0x6891, 0x67cc,
0x6706, 0x6641, 0x657c, 0x64b8, 0x63f4, 0x6330, 0x626c, 0x61a8, 0x60e5, 0x6022, 0x5f60, 0x5e9d, 0x5ddb, 0x5d1a, 0x5c59, 0x5b98,
0x5ad7, 0x5a17, 0x5957, 0x5898, 0x57d8, 0x571a, 0x565b, 0x559d, 0x54e0, 0x5423, 0x5366, 0x52aa, 0x51ee, 0x5133, 0x5078, 0x4fbd,
0x4f03, 0x4e4a, 0x4d91, 0x4cd8, 0x4c20, 0x4b69, 0x4ab2, 0x49fb, 0x4945, 0x4890, 0x47db, 0x4726, 0x4672, 0x45bf, 0x450c, 0x445a,
0x43a9, 0x42f7, 0x4247, 0x4197, 0x40e8, 0x4039, 0x3f8b, 0x3ede, 0x3e31, 0x3d85, 0x3cda, 0x3c2f, 0x3b84, 0x3adb, 0x3a32, 0x398a,
0x38e2, 0x383c, 0x3795, 0x36f0, 0x364b, 0x35a7, 0x3504, 0x3461, 0x33bf, 0x331e, 0x327e, 0x31de, 0x313f, 0x30a1, 0x3004, 0x2f67,
0x2ecc, 0x2e30, 0x2d96, 0x2cfd, 0x2c64, 0x2bcc, 0x2b35, 0x2a9f, 0x2a0a, 0x2975, 0x28e1, 0x284f, 0x27bc, 0x272b, 0x269b, 0x260b,
0x257d, 0x24ef, 0x2462, 0x23d6, 0x234b, 0x22c1, 0x2238, 0x21af, 0x2128, 0x20a1, 0x201c, 0x1f97, 0x1f13, 0x1e90, 0x1e0e, 0x1d8d,
0x1d0d, 0x1c8e, 0x1c10, 0x1b93, 0x1b17, 0x1a9c, 0x1a21, 0x19a8, 0x1930, 0x18b9, 0x1842, 0x17cd, 0x1759, 0x16e6, 0x1673, 0x1602,
0x1592, 0x1523, 0x14b4, 0x1447, 0x13db, 0x1370, 0x1306, 0x129d, 0x1235, 0x11cf, 0x1169, 0x1104, 0x10a0, 0x103e, 0x0fdc, 0x0f7c,
0x0f1d, 0x0ebe, 0x0e61, 0x0e05, 0x0daa, 0x0d50, 0x0cf8, 0x0ca0, 0x0c4a, 0x0bf4, 0x0ba0, 0x0b4d, 0x0afb, 0x0aaa, 0x0a5a, 0x0a0b,
0x09be, 0x0971, 0x0926, 0x08dc, 0x0893, 0x084b, 0x0805, 0x07bf, 0x077b, 0x0738, 0x06f6, 0x06b5, 0x0675, 0x0637, 0x05fa, 0x05bd,
0x0582, 0x0549, 0x0510, 0x04d9, 0x04a2, 0x046d, 0x043a, 0x0407, 0x03d6, 0x03a5, 0x0376, 0x0348, 0x031c, 0x02f0, 0x02c6, 0x029d,
0x0275, 0x024e, 0x0229, 0x0205, 0x01e2, 0x01c0, 0x01a0, 0x0180, 0x0162, 0x0145, 0x012a, 0x010f, 0x00f6, 0x00de, 0x00c7, 0x00b2,
0x009d, 0x008a, 0x0078, 0x0068, 0x0058, 0x004a, 0x003d, 0x0031, 0x0027, 0x001e, 0x0016, 0x000f, 0x0009, 0x0005, 0x0002, 0x0000,
0x0000, 0x0000, 0x0002, 0x0005, 0x0009, 0x000f, 0x0016, 0x001e, 0x0027, 0x0031, 0x003d, 0x004a, 0x0058, 0x0068, 0x0078, 0x008a,
0x009d, 0x00b2, 0x00c7, 0x00de, 0x00f6, 0x010f, 0x012a, 0x0145, 0x0162, 0x0180, 0x01a0, 0x01c0, 0x01e2, 0x0205, 0x0229, 0x024e,
0x0275, 0x029d, 0x02c6, 0x02f0, 0x031c, 0x0348, 0x0376, 0x03a5, 0x03d6, 0x0407, 0x043a, 0x046d, 0x04a2, 0x04d9, 0x0510, 0x0549,
0x0582, 0x05bd, 0x05fa, 0x0637, 0x0675, 0x06b5, 0x06f6, 0x0738, 0x077b, 0x07bf, 0x0805, 0x084b, 0x0893, 0x08dc, 0x0926, 0x0971,
0x09be, 0x0a0b, 0x0a5a, 0x0aaa, 0x0afb, 0x0b4d, 0x0ba0, 0x0bf4, 0x0c4a, 0x0ca0, 0x0cf8, 0x0d50, 0x0daa, 0x0e05, 0x0e61, 0x0ebe,
0x0f1d, 0x0f7c, 0x0fdc, 0x103e, 0x10a0, 0x1104, 0x1169, 0x11cf, 0x1235, 0x129d, 0x1306, 0x1370, 0x13db, 0x1447, 0x14b4, 0x1523,
0x1592, 0x1602, 0x1673, 0x16e6, 0x1759, 0x17cd, 0x1842, 0x18b9, 0x1930, 0x19a8, 0x1a21, 0x1a9c, 0x1b17, 0x1b93, 0x1c10, 0x1c8e,
0x1d0d, 0x1d8d, 0x1e0e, 0x1e90, 0x1f13, 0x1f97, 0x201c, 0x20a1, 0x2128, 0x21af, 0x2238, 0x22c1, 0x234b, 0x23d6, 0x2462, 0x24ef,
0x257d, 0x260b, 0x269b, 0x272b, 0x27bc, 0x284f, 0x28e1, 0x2975, 0x2a0a, 0x2a9f, 0x2b35, 0x2bcc, 0x2c64, 0x2cfd, 0x2d96, 0x2e30,
0x2ecc, 0x2f67, 0x3004, 0x30a1, 0x313f, 0x31de, 0x327e, 0x331e, 0x33bf, 0x3461, 0x3504, 0x35a7, 0x364b, 0x36f0, 0x3795, 0x383c,
0x38e2, 0x398a, 0x3a32, 0x3adb, 0x3b84, 0x3c2f, 0x3cda, 0x3d85, 0x3e31, 0x3ede, 0x3f8b, 0x4039, 0x40e8, 0x4197, 0x4247, 0x42f7,
0x43a9, 0x445a, 0x450c, 0x45bf, 0x4672, 0x4726, 0x47db, 0x4890, 0x4945, 0x49fb, 0x4ab2, 0x4b69, 0x4c20, 0x4cd8, 0x4d91, 0x4e4a,
0x4f03, 0x4fbd, 0x5078, 0x5133, 0x51ee, 0x52aa, 0x5366, 0x5423, 0x54e0, 0x559d, 0x565b, 0x571a, 0x57d8, 0x5898, 0x5957, 0x5a17,
0x5ad7, 0x5b98, 0x5c59, 0x5d1a, 0x5ddb, 0x5e9d, 0x5f60, 0x6022, 0x60e5, 0x61a8, 0x626c, 0x6330, 0x63f4, 0x64b8, 0x657c, 0x6641,
0x6706, 0x67cc, 0x6891, 0x6957, 0x6a1d, 0x6ae3, 0x6baa, 0x6c70, 0x6d37, 0x6dfe, 0x6ec5, 0x6f8c, 0x7054, 0x711c, 0x71e3, 0x72ab,
0x7373, 0x743b, 0x7504, 0x75cc, 0x7694, 0x775d, 0x7826, 0x78ee, 0x79b7, 0x7a80, 0x7b49, 0x7c12, 0x7cdb, 0x7da4, 0x7e6d, 0x7f36
};
int calculate_waveform(int x, int motor)
{
int result = (int)sin_table[x] * car.motor[motor].power / MAX_SIN;
CLAMP(result, 0, MAX_PWM);
return result;
}
// calculate motor PWM
void write_motor(int number)
{
motor_t *motor = &car.motor[number];
FIX_PHASE(motor->phase);
if(motor->mode != STEPPER)
{
motor->pwm[0] = motor->power;
motor->pwm[1] = motor->power;
motor->pwm[2] = motor->power;
}
else
{
int index1 = (int)((motor->phase * WAVEFORM_SIZE / 360)) % WAVEFORM_SIZE;
int index2 = (index1 + 120 * WAVEFORM_SIZE / 360) % WAVEFORM_SIZE;
int index3 = (index1 + 240 * WAVEFORM_SIZE / 360) % WAVEFORM_SIZE;
/*
* TRACE2
* print_number(calculate_waveform(index1));
* print_number(calculate_waveform(index2));
* print_number(calculate_waveform(index3));
*/
motor->pwm[0] = calculate_waveform(index1, number);
motor->pwm[1] = calculate_waveform(index2, number);
motor->pwm[2] = calculate_waveform(index3, number);
}
//car.pwm[0][0] = 0;
//car.pwm[0][1] = 0;
//car.pwm[0][2] = 0;
}
void write_motors()
{
int i = 0;
for(i = 0; i < MOTORS; i++)
{
write_motor(i);
}
}
float init_pid(pid_t *pid,
float p_gain,
float i_gain,
float d_gain,
float i_limit,
float o_limit)
{
pid->p_gain = p_gain;
pid->i_gain = i_gain;
pid->d_gain = d_gain;
pid->i_limit = i_limit;
pid->o_limit = o_limit;
}
float do_pid(pid_t *pid, float p_error, float d_error)
{
float p_result = p_error * pid->p_gain;
float d_result = d_error * pid->d_gain;
pid->error_accum += p_error;
pid->counter++;
if(pid->counter >= I_DOWNSAMPLE)
{
// average of all errors
pid->error_accum /= pid->counter;
// I factor
pid->accum += pid->error_accum * pid->i_gain;
CLAMP(pid->accum, -pid->i_limit, pid->i_limit);
pid->counter = 0;
pid->error_accum = 0;
}
float result = p_result + d_result + pid->accum;
CLAMP(result, -pid->o_limit, pid->o_limit);
return result;
}
void handle_motor(int motor_number)
{
motor_t *motor = &car.motor[motor_number];
if(!imu.need_gyro_center)
{
if(car.throttle > THROTTLE_MIN)
{
if(motor->throttle_reverse != car.throttle_reverse)
{
motor->period = MAX_PERIOD;
motor->mode = STEPPER;
}
if(motor->throttle != car.throttle)
{
motor->phase = 0;
motor->mode = STEPPER;
}
if(motor->mode == STEPPER)
{
int sign = motor->throttle_reverse ? -1 : 1;
motor->phase += MOTOR_STEP * sign * motor->direction;
motor->power = car.stepper_power;
write_motor(motor_number);
if(motor->need_period_change)
{
motor->need_period_change = 0;
//TRACE2
//print_number(motor->period);
}
// Go to FREEWHEEL mode to get up to back EMF speed
if(motor->period <= MIN_STEPPER_PERIOD &&
motor->phase >= 150 &&
motor->phase < 180)
{
//TRACE2
//print_number(motor->phase);
// scale current delay to commutation range
motor->period *= EMF_TO_STEPPER;
motor->disabled[0] = 0;
motor->disabled[1] = 0;
motor->disabled[2] = 1;
motor->voltage[0] = 1;
motor->voltage[1] = 0;
motor->voltage[2] = 0;
motor->mode = FREEWHEEL;
motor->power = car.freewheel_power;
write_motor(motor_number);
// Since you can stall in FREEWHEEL mode, just make stepper mode go all the way
// motor->mode = FREEWHEEL_EMF;
// motor->power = MAX_PWM;
// write_motor(motor_number);
// initialize the back EMF filters
// motor->crossing_time = motor->period / 2;
// motor->threshold_emf = -1;
// motor->commutations = 0;
}
motor->phase_timer->CCR1 = motor->period;
}
else
if(motor->mode == FREEWHEEL)
{
motor->commutate = 1;
motor->commutations++;
if(motor->need_period_change)
{
motor->need_period_change = 0;
motor->phase_timer->CCR1 = motor->period;
}
// if(motor->period <= MIN_EMF_PERIOD * EMF_TO_STEPPER)
// {
//TRACE2
// motor->mode = FREEWHEEL_EMF;
// motor->power = car.operating_power;
// write_motor(motor_number);
//
// initialize the back EMF filters
// motor->crossing_time = motor->period / 2;
// motor->threshold_emf = -1;
// motor->commutations = 0;
// }
}
else
// FREEWHEEL_EMF
{
// stalled
if(motor->min_emf > 100 &&
motor->commutations > 1024)
{
TRACE2
motor_t *motor2 = &car.motor[LEFT_MOTOR];
motor2->mode = STEPPER;
motor2->power = car.operating_power;
motor2->period = MIN_STALL_PERIOD;
motor2->phase_timer->CCR1 = motor->period;
motor2 = &car.motor[RIGHT_MOTOR];
motor2->mode = STEPPER;
motor2->power = car.operating_power;
motor2->period = MIN_STALL_PERIOD;
motor2->phase_timer->CCR1 = motor->period;
write_motors();
}
else
{
motor->commutate = 1;
motor->commutations++;
}
if(motor_number == LEFT_MOTOR)
{
car.debug_counter++;
if(car.debug_counter > 100)
{
car.debug_counter = 0;
TRACE2
print_number(motor->period);
}
}
//print_float(motor->crossing_time);
}
}
else
// car.throttle <= THROTTLE_MIN
{
// reset
motor->power = 0;
motor->mode = STEPPER;
motor->period = MAX_PERIOD;
if(car.steering < STEERING_MID - STEERING_MAX)
{
motor->power = car.stepper_power;
motor->period = STEERING_PERIOD;
motor->phase += FAST_STEERING * motor->direction * -motor->turn_sign;
}
else
if(car.steering < STEERING_MID - STEERING_MIN)
{
motor->power = car.stepper_power;
motor->period = STEERING_PERIOD;
motor->phase += SLOW_STEERING * motor->direction * -motor->turn_sign;
}
else
if(car.steering > STEERING_MID + STEERING_MAX)
{
motor->power = car.stepper_power;
motor->period = STEERING_PERIOD;
motor->phase += FAST_STEERING * motor->direction * motor->turn_sign;
}
else
if(car.steering > STEERING_MID + STEERING_MIN)
{
motor->power = car.stepper_power;
motor->period = STEERING_PERIOD;
motor->phase += SLOW_STEERING * motor->direction * motor->turn_sign;
}
motor->phase_timer->CCR1 = motor->period;
write_motor(motor_number);
}
motor->throttle_reverse = car.throttle_reverse;
motor->throttle = car.throttle;
}
}
// phase timer wrapped
void TIM1_CC_IRQHandler()
{
if(TIM1->SR & TIM_FLAG_CC1)
{
TIM1->SR = ~TIM_FLAG_CC1;
TIM1->CNT = 0;
TOGGLE_PIN(DEBUG_GPIO, DEBUG_PIN);
handle_motor(LEFT_MOTOR);
}
}
void TIM3_IRQHandler()
{
if(TIM3->SR & TIM_FLAG_CC1)
{
TIM3->SR = ~TIM_FLAG_CC1;
TIM3->CNT = 0;
handle_motor(RIGHT_MOTOR);
}
}
void handle_drive()
{
imu.got_ahrs = 0;
DISABLE_INTERRUPTS
write_motors();
ENABLE_INTERRUPTS
}
void update_period(int motor_number)
{
motor_t *motor = &car.motor[motor_number];
motor->delay_counter++;
if(motor->delay_counter >= PWM_DOWNSAMPLE)
{
motor->delay_counter = 0;
// have to change the period here, so the update rate is constant
if(motor->mode == STEPPER)
{
if(motor->period > MIN_STEPPER_PERIOD)
{
motor->need_period_change = 1;
motor->period -= motor->period / 32;
}
}
else
if(motor->mode == FREEWHEEL)
{
if(motor->period > MIN_EMF_PERIOD * EMF_TO_STEPPER)
{
motor->need_period_change = 1;
motor->period -= motor->period / 128;
}
}
}
}
void enable_phase(int motor_number,
int phase,
int in_pin,
GPIO_TypeDef *in_gpio,
int en_pin,
GPIO_TypeDef *en_gpio)
{
motor_t *motor = &car.motor[motor_number];
if(motor->mode != STEPPER)
{
if(!motor->disabled[phase] && motor->pwm[phase] > 0)
{
if(motor->voltage[phase])
{
SET_PIN(in_gpio, in_pin);
}
else
{
CLEAR_PIN(in_gpio, in_pin);
}
SET_PIN(en_gpio, en_pin);
}
else
{
CLEAR_PIN(en_gpio, en_pin);
}
}
else
{
if(motor->pwm[phase] > 0)
{
SET_PIN(in_gpio, in_pin);
}
SET_PIN(en_gpio, en_pin);
}
}
void disable_phase(int motor_number,
int phase,
int in_pin,
GPIO_TypeDef *in_gpio,
int en_pin,
GPIO_TypeDef *en_gpio)
{
motor_t *motor = &car.motor[motor_number];
if(motor->mode != STEPPER)
{
if(motor->pwm[phase] < MAX_PWM)
{
CLEAR_PIN(en_gpio, en_pin);
}
}
else
{
if(motor->pwm[phase] < MAX_PWM)
{
CLEAR_PIN(in_gpio, in_pin);
}
}
}
void handle_commutation(int motor_number)
{
motor_t *motor = &car.motor[motor_number];
if(motor->mode != STEPPER && motor->commutate)
{
int reverse = motor->throttle_reverse ? -1 : 1;
motor->commutate = 0;
if(motor->direction * reverse > 0)
{
int temp = motor->disabled[0];
motor->disabled[0] = motor->disabled[1];
motor->disabled[1] = motor->disabled[2];
motor->disabled[2] = temp;
temp = motor->voltage[0];
motor->voltage[0] = motor->voltage[1];
motor->voltage[1] = motor->voltage[2];
motor->voltage[2] = temp;
}
else
{
int temp = motor->disabled[2];
motor->disabled[2] = motor->disabled[1];
motor->disabled[1] = motor->disabled[0];
motor->disabled[0] = temp;
temp = motor->voltage[2];
motor->voltage[2] = motor->voltage[1];
motor->voltage[1] = motor->voltage[0];
motor->voltage[0] = temp;
}
// set ADC to probe floating phase
if(motor_number == RIGHT_MOTOR)
{
if(motor->disabled[0])
{
ADC_RegularChannelConfig(motor->adc, ADC_Channel_11, 1, ADC_SampleTime_480Cycles);
}
else
if(motor->disabled[1])
{
ADC_RegularChannelConfig(motor->adc, ADC_Channel_12, 1, ADC_SampleTime_480Cycles);
}
else
if(motor->disabled[2])
{
ADC_RegularChannelConfig(motor->adc, ADC_Channel_10, 1, ADC_SampleTime_480Cycles);
}
}
else
/// motor_number == LEFT_MOTOR
{
if(motor->disabled[0])
{
ADC_RegularChannelConfig(motor->adc, ADC_Channel_0, 1, ADC_SampleTime_480Cycles);
}
else
if(motor->disabled[1])
{
ADC_RegularChannelConfig(motor->adc, ADC_Channel_13, 1, ADC_SampleTime_480Cycles);
}
else
if(motor->disabled[2])
{
ADC_RegularChannelConfig(motor->adc, ADC_Channel_1, 1, ADC_SampleTime_480Cycles);
}
}
ADC_SoftwareStartConv(motor->adc);
// blend in new back EMF range
if(motor->threshold_emf < 0)
{
motor->threshold_emf = (motor->min_emf + motor->max_emf) / 2;
}
else
{
motor->threshold_emf = (1.0f - BACK_EMF_WEIGHT) * motor->threshold_emf +
BACK_EMF_WEIGHT * (motor->min_emf + motor->max_emf) / 2;
}
if(motor->disabled[2])
{
// TRACE2
// print_text("motor=");
// print_number(motor_number);
// print_number(motor->min_emf);
// print_number(motor->max_emf);
// print_text("threshold_emf=");
// print_number(car.threshold_emf[0]);
// print_text("crossing_time2=");
// print_number(motor->crossing_time2);
// print_text("period=");
// print_number(car.period);
// print_text("total_conversions=");
// print_number(motor->total_conversions);
}
motor->total_conversions = 0;
motor->min_emf = 0xffff;
motor->max_emf = 0x0;
motor->emf_direction = -1;
motor->crossing_time2 = -1;
// print_lf();
}
}
void handle_backemf(int motor_number)
{
motor_t *motor = &car.motor[motor_number];
if(motor->mode != STEPPER)
{
if((motor->adc->SR & ADC_FLAG_EOC) != 0)
{
int voltage = motor->adc->DR;
motor->adc->SR = ~ADC_FLAG_EOC;
ADC_SoftwareStartConv(motor->adc);
if(motor->phase_timer->CNT > motor->period / 8 &&
motor->total_conversions > 3)
{
// print_number(voltage);
if(voltage < motor->min_emf) motor->min_emf = voltage;
if(voltage > motor->max_emf) motor->max_emf = voltage;
if(motor->emf_direction < 0)
{
if(voltage < motor->threshold_emf)
motor->emf_direction = 1;
else
motor->emf_direction = 0;
}
else
if(motor->crossing_time2 < 0)
{
// get the time of the crossing
if((motor->emf_direction == 1 &&
voltage > motor->threshold_emf) ||
(motor->emf_direction == 0 &&
voltage < motor->threshold_emf))
{
motor->crossing_time2 = motor->phase_timer->CNT;
if(motor->commutations > 8 &&
motor->mode == FREEWHEEL_EMF)
{
//TRACE2
//print_number(motor->crossing_time2);
//print_number(motor->crossing_time);
// Lowpass filtered crossing time seems required
motor->crossing_time =
(1.0f - PERIOD_WEIGHT) * motor->crossing_time +
PERIOD_WEIGHT * motor->crossing_time2;
motor->period = motor->crossing_time * 2;
// derate to increase torque
// this decreases the back EMF, so there's a limit to the derating
if(motor->period < MIN_EMF_PERIOD *
100 /
120 *
EMF_TO_STEPPER)
{
motor->period = motor->period *
120 /
100;
}
motor->phase_timer->CCR1 = motor->period;
// Didn't work to set it directly off the crossing time
// motor->phase_timer->CCR1 =
// motor->period =
// motor->crossing_time2 * 2;
}
}
}
}
motor->total_conversions++;
}
}
}
void TIM2_IRQHandler()
{
if(TIM2->SR & TIM_FLAG_Update)
{
TIM2->SR = ~TIM_FLAG_Update;
TIM2->CCR1 = car.motor[LEFT_MOTOR].pwm[0];
TIM2->CCR2 = car.motor[LEFT_MOTOR].pwm[1];
TIM2->CCR3 = car.motor[LEFT_MOTOR].pwm[2];
update_period(LEFT_MOTOR);
handle_commutation(LEFT_MOTOR);
enable_phase(LEFT_MOTOR,
0,
LEFT_IN1_PIN,
LEFT_IN1_GPIO,
LEFT_EN1_PIN,
LEFT_EN1_GPIO);
enable_phase(LEFT_MOTOR,
1,
LEFT_IN2_PIN,
LEFT_IN2_GPIO,
LEFT_EN2_PIN,
LEFT_EN2_GPIO);
enable_phase(LEFT_MOTOR,
2,
LEFT_IN3_PIN,
LEFT_IN3_GPIO,
LEFT_EN3_PIN,
LEFT_EN3_GPIO);
handle_backemf(LEFT_MOTOR);
}
if(TIM2->SR & TIM_FLAG_CC1)
{
TIM2->SR = ~TIM_FLAG_CC1;
disable_phase(LEFT_MOTOR,
0,
LEFT_IN1_PIN,
LEFT_IN1_GPIO,
LEFT_EN1_PIN,
LEFT_EN1_GPIO);
}
if(TIM2->SR & TIM_FLAG_CC2)
{
TIM2->SR = ~TIM_FLAG_CC2;
disable_phase(LEFT_MOTOR,
1,
LEFT_IN2_PIN,
LEFT_IN2_GPIO,
LEFT_EN2_PIN,
LEFT_EN2_GPIO);
}
if(TIM2->SR & TIM_FLAG_CC3)
{
TIM2->SR = ~TIM_FLAG_CC3;
disable_phase(LEFT_MOTOR,
2,
LEFT_IN3_PIN,
LEFT_IN3_GPIO,
LEFT_EN3_PIN,
LEFT_EN3_GPIO);
}
}
void TIM5_IRQHandler()
{
if(TIM5->SR & TIM_FLAG_Update)
{
TIM5->SR = ~TIM_FLAG_Update;
TIM5->CCR1 = car.motor[RIGHT_MOTOR].pwm[0];
TIM5->CCR2 = car.motor[RIGHT_MOTOR].pwm[1];
TIM5->CCR3 = car.motor[RIGHT_MOTOR].pwm[2];
update_period(RIGHT_MOTOR);
handle_commutation(RIGHT_MOTOR);
enable_phase(RIGHT_MOTOR,
0,
RIGHT_IN1_PIN,
RIGHT_IN1_GPIO,
RIGHT_EN1_PIN,
RIGHT_EN1_GPIO);
enable_phase(RIGHT_MOTOR,
1,
RIGHT_IN2_PIN,
RIGHT_IN2_GPIO,
RIGHT_EN2_PIN,
RIGHT_EN2_GPIO);
enable_phase(RIGHT_MOTOR,
2,
RIGHT_IN3_PIN,
RIGHT_IN3_GPIO,
RIGHT_EN3_PIN,
RIGHT_EN3_GPIO);
handle_backemf(RIGHT_MOTOR);
}
if(TIM5->SR & TIM_FLAG_CC1)
{
TIM5->SR = ~TIM_FLAG_CC1;
disable_phase(RIGHT_MOTOR,
0,
RIGHT_IN1_PIN,
RIGHT_IN1_GPIO,
RIGHT_EN1_PIN,
RIGHT_EN1_GPIO);
}
if(TIM5->SR & TIM_FLAG_CC2)
{
TIM5->SR = ~TIM_FLAG_CC2;
disable_phase(RIGHT_MOTOR,
1,
RIGHT_IN2_PIN,
RIGHT_IN2_GPIO,
RIGHT_EN2_PIN,
RIGHT_EN2_GPIO);
}
if(TIM5->SR & TIM_FLAG_CC3)
{
TIM5->SR = ~TIM_FLAG_CC3;
disable_phase(RIGHT_MOTOR,
2,
RIGHT_IN3_PIN,
RIGHT_IN3_GPIO,
RIGHT_EN3_PIN,
RIGHT_EN3_GPIO);
}
}
void USART6_IRQHandler(void)
{
unsigned char c = USART6->DR;
uart.input = c;
uart.got_input = 1;
}
uint16_t get_chksum(uint8_t *buffer, uint8_t size)
{
uint8_t i;
uint16_t result = 0;
uint16_t result2;
size /= 2;
for(i = 0; i < size; i++)
{
uint16_t prev_result = result;
// Not sure if word aligned
uint16_t value = (buffer[0]) | (buffer[1] << 8);
result += value;
// Carry bit
if(result < prev_result) result++;
buffer += 2;
}
result2 = (result & 0xff) << 8;
result2 |= (result & 0xff00) >> 8;
return result2;
}
void handle_radio()
{
if(radio.packet[0] != SYNC_CODE) return;
uint16_t chksum = get_chksum(radio.packet, PACKET_SIZE - 2);
if((chksum & 0xff) == radio.packet[PACKET_SIZE - 2] &&
((chksum >> 8) & 0xff) == radio.packet[PACKET_SIZE - 1])
{
// packet good
if(!imu.need_gyro_center)
{
car.led_counter++;
if(car.led_counter >= LED_DELAY2)
{
TOGGLE_PIN(LED_GPIO, LED_PIN);
car.led_counter = 0;
}
}
car.throttle_reverse = radio.packet[1] & 0x1;
car.throttle = radio.packet[2] | (radio.packet[3] << 8);
car.steering = radio.packet[4] | (radio.packet[5] << 8);
if(car.throttle >= THROTTLE_MAX)
{
if(!imu.have_gyro_center && !imu.need_gyro_center)