-
Notifications
You must be signed in to change notification settings - Fork 0
/
arm_truck.c.7v
executable file
·1429 lines (1192 loc) · 33 KB
/
arm_truck.c.7v
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
/*
* 1 handed truck
* 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;../copter/uart_programmer truck.bin
// pass bluetooth to the debug port to configure the device by enabling
// BLUETOOTH_PASSTHROUGH
// useful configuration commands:
// at+version
// AT+VERSION
// at+nametruck
// AT+NAMEtruck
// at+baud8
// AT+BAUD8
// new devices start at 9600 baud. Be sure to set the initial baud rate
// to 9600, then 115200 after configuration.
// Some kind of delay in the terminal program is required to paste text in,
// but the entire command must be sent in under a second.
#include "arm_truck.h"
#include "arm_math.h"
#include "cc1101.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 "stm32f4xx_flash.h"
#include <math.h>
// pass bluetooth to debug port
//#define BLUETOOTH_PASSTHROUGH
#define SYNC_CODE 0xe5
// Address of persistent settings
#define SETTINGS_ADDRESS 0x0800c000
// Magic number for settings
#define SETTINGS_MAGIC 0x10291976
#define DEBUG_PIN GPIO_Pin_4
#define DEBUG_GPIO GPIOB
#define HEADLIGHT_PIN GPIO_Pin_13
#define HEADLIGHT_GPIO GPIOC
// packets per second
#define PACKET_RATE 40
#define THROTTLE_MAX 0x1
// analog range
#define STEERING_MID 0x0
#define BATTERY_OVERSAMPLE 1000
#define GYRO_OVERSAMPLE 20
#define GYRO_CENTER_TOTAL 4096
// TIM10 wraps at this frequency
#define TIMER_HZ 100
// gyro update rate
#define NAV_HZ 1024
// PWM frequency
#define PWM_HZ 50
// timer for LED flashing
#define LED_DELAY (NAV_HZ / 2)
// 50hz
#define PWM_PERIOD 1680000
// 2ms
#define MAX_PWM 168000
// 1ms
#define MIN_PWM 84000
truck_t truck;
void write_pwm();
void imu_led_flash()
{
truck.led_counter++;
if(truck.led_counter >= LED_DELAY)
{
TOGGLE_PIN(LED_GPIO2, LED_PIN2);
CLEAR_PIN(LED_GPIO1, LED_PIN1);
truck.led_counter = 0;
}
}
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 >= truck.pid_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 reset_pid(pid_t *pid)
{
pid->error_accum = 0;
pid->accum = 0;
pid->counter = 0;
}
void handle_controls()
{
DISABLE_INTERRUPTS
ENABLE_INTERRUPTS
}
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(!truck.need_gyro_center)
{
truck.led_counter++;
if(truck.led_counter >= LED_DELAY2)
{
if(truck.have_gyro_center)
{
// flash green
TOGGLE_PIN(LED_GPIO1, LED_PIN1);
CLEAR_PIN(LED_GPIO2, LED_PIN2);
}
else
{
// flash red
TOGGLE_PIN(LED_GPIO2, LED_PIN2);
CLEAR_PIN(LED_GPIO1, LED_PIN1);
}
truck.led_counter = 0;
}
}
//TRACE2
//print_hex2(radio.packet[2]);
DISABLE_INTERRUPTS
truck.throttle_reverse = BIT_IS_CLEAR(radio.packet[2], 0) ? 0 : 1;
truck.throttle = BIT_IS_CLEAR(radio.packet[2], 1) ? THROTTLE_MAX : 0;
truck.steering = STEERING_MID;
// low speed steering
if(BIT_IS_CLEAR(radio.packet[2], 3)) truck.steering = 2;
else
if(BIT_IS_CLEAR(radio.packet[2], 4)) truck.steering = 3;
else
// high speed steering
if(BIT_IS_CLEAR(radio.packet[2], 2)) truck.steering = 1;
else
if(BIT_IS_CLEAR(radio.packet[2], 5)) truck.steering = 4;
ENABLE_INTERRUPTS
if(truck.steering)
{
TRACE
print_number(truck.steering);
}
/*
* TRACE2
* print_text("reverse=");
* print_number(truck.throttle_reverse);
* print_text("throttle=");
* print_number(truck.throttle);
* print_text("steering=");
* print_number(truck.steering);
*/
// begin gyro calibration
if(truck.throttle >= THROTTLE_MAX &&
!truck.have_gyro_center &&
!truck.need_gyro_center)
{
TRACE2
print_text("Begin gyro calibration\n");
truck.need_gyro_center = 1;
truck.gyro_center_count = 0;
truck.gyro_center = 0;
truck.gyro_accum = 0;
truck.gyro_min = 0;
truck.gyro_max = 0;
}
if(truck.have_gyro_center)
{
DISABLE_INTERRUPTS
if(truck.throttle > 0 && truck.throttle2 <= 0)
{
truck.current_heading = 0;
truck.throttle_ramp_counter = 0;
reset_pid(&truck.heading_pid);
}
if(truck.steering != truck.steering2)
{
truck.throttle_ramp_counter = 0;
truck.steering_step_counter = 0;
}
truck.steering2 = truck.steering;
truck.throttle2 = truck.throttle;
truck.throttle_reverse2 = truck.throttle_reverse;
ENABLE_INTERRUPTS
}
//TRACE2
//print_text("mode=");
//print_number(truck.mode);
//print_text("period=");
//print_number(truck.period);
//print_text("power=");
//print_number_nospace(left_motor->power);
//print_text(",");
//print_number(right_motor->power);
}
}
void dump_config()
{
TRACE2
print_text("\nheadlights_on=");
print_number(truck.headlights_on);
print_text("\nmid_steering=");
print_number(truck.mid_steering);
print_text("\nmid_throttle=");
print_number(truck.mid_throttle);
print_text("\nmax_throttle_fwd=");
print_number(truck.max_throttle_fwd);
print_text("\nmax_throttle_rev=");
print_number(truck.max_throttle_rev);
print_text("\nmax_steering=");
print_number(truck.max_steering);
print_text("\nmin_steering=");
print_number(truck.min_steering);
print_text("\nauto_steering=");
print_number(truck.auto_steering);
print_text("\ngyro_center_max=");
print_number(truck.gyro_center_max);
print_text("\nangle_to_gyro=");
print_number(truck.angle_to_gyro);
print_text("\nthrottle_ramp_delay=");
print_number(truck.throttle_ramp_delay);
print_text("\nthrottle_ramp_step=");
print_number(truck.throttle_ramp_step);
print_text("\npid_downsample=");
print_number(truck.pid_downsample);
print_text("\nsteering_step_delay=");
print_number(truck.steering_step_delay);
print_text("\nsteering_step=");
print_float(TO_DEG(truck.steering_step));
print_text("\nsteering_overshoot=");
print_float(TO_DEG(truck.steering_overshoot));
print_text("\nsteering PID=");
print_float(truck.heading_pid.p_gain);
print_float(truck.heading_pid.i_gain);
print_float(truck.heading_pid.d_gain);
print_float(truck.heading_pid.i_limit);
print_float(truck.heading_pid.o_limit);
print_lf();
}
void update_headlights()
{
if(truck.headlights_on)
{
SET_PIN(HEADLIGHT_GPIO, HEADLIGHT_PIN);
}
else
{
CLEAR_PIN(HEADLIGHT_GPIO, HEADLIGHT_PIN);
}
}
int read_config_packet(const unsigned char *buffer)
{
int offset = 0;
truck.headlights_on = buffer[offset++];
truck.mid_steering = buffer[offset++];
truck.mid_throttle = buffer[offset++];
truck.max_throttle_fwd = buffer[offset++];
truck.max_throttle_rev = buffer[offset++];
truck.max_steering = buffer[offset++];
truck.min_steering = buffer[offset++];
truck.auto_steering = buffer[offset++];
truck.gyro_center_max = READ_UINT16(buffer, offset);
truck.angle_to_gyro = READ_UINT16(buffer, offset);
truck.throttle_ramp_delay = READ_UINT16(buffer, offset);
truck.throttle_ramp_step = READ_UINT16(buffer, offset);
truck.pid_downsample = READ_UINT16(buffer, offset);
truck.steering_step_delay = READ_UINT16(buffer, offset);
truck.steering_step = READ_FLOAT32(buffer, offset);
truck.steering_overshoot = READ_FLOAT32(buffer, offset);
float p = READ_FLOAT32(buffer, offset);
float i = READ_FLOAT32(buffer, offset);
float d = READ_FLOAT32(buffer, offset);
float i_limit = READ_FLOAT32(buffer, offset);
float o_limit = READ_FLOAT32(buffer, offset);
init_pid(&truck.heading_pid,
p, // P gain
i, // I gain
d, // D gain
i_limit, // I limit
o_limit); // O limit
update_headlights();
// debug
//truck.max_throttle_fwd = 0;
//truck.max_throttle_rev = 0;
//truck.auto_steering = 1;
return offset;
}
void save_config(unsigned char *buffer, int bytes)
{
const unsigned char buffer2[] =
{
(SETTINGS_MAGIC >> 24) & 0xff,
(SETTINGS_MAGIC >> 16) & 0xff,
(SETTINGS_MAGIC >> 8) & 0xff,
SETTINGS_MAGIC & 0xff,
};
USART_Cmd(USART3, DISABLE);
USART_Cmd(USART1, DISABLE);
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
if (FLASH_EraseSector(FLASH_Sector_3, VoltageRange_3) != FLASH_COMPLETE)
{
}
/* Clear pending flags (if any) */
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
int address = SETTINGS_ADDRESS;
if(FLASH_ProgramWord(address, *(int*)(buffer2)) != FLASH_COMPLETE)
{
}
address += 4;
// Align on 4 bytes
while(bytes % 4) buffer[bytes++] = 0;
int i;
for(i = 0; i < bytes; i += 4)
{
if (FLASH_ProgramWord(address + i, *(int*)(buffer + i)) != FLASH_COMPLETE)
{
}
}
FLASH_Lock();
USART_Cmd(USART3, ENABLE);
USART_Cmd(USART1, ENABLE);
TRACE2
print_text("Saved flash config\n");
}
void load_config()
{
// Load settings from flash
const unsigned char *buffer = (unsigned char*)SETTINGS_ADDRESS;
if((buffer[0] == ((SETTINGS_MAGIC >> 24) & 0xff)) &&
(buffer[1] == ((SETTINGS_MAGIC >> 16) & 0xff)) &&
(buffer[2] == ((SETTINGS_MAGIC >> 8) & 0xff)) &&
(buffer[3] == (SETTINGS_MAGIC & 0xff)))
{
TRACE2
print_text("Loading flash config\n");
read_config_packet(buffer + 4);
}
}
#ifdef BLUETOOTH_PASSTHROUGH
static void bluetooth_passthrough()
{
send_uart_binary(&truck.bluetooth.data, 1);
}
#endif // BLUETOOTH_PASSTHROUGH
void handle_beacon()
{
uint16_t chksum = get_chksum(truck.bluetooth.receive_buf,
truck.bluetooth.receive_size - 2);
if((chksum & 0xff) == truck.bluetooth.receive_buf[truck.bluetooth.receive_size - 2] &&
((chksum >> 8) & 0xff) == truck.bluetooth.receive_buf[truck.bluetooth.receive_size - 1])
{
switch(truck.bluetooth.receive_buf[6])
{
// battery voltage
case 0:
{
int offset = 0;
truck.bluetooth.send_buf[offset++] = 0xff;
truck.bluetooth.send_buf[offset++] = 0x2d;
truck.bluetooth.send_buf[offset++] = 0xd4;
truck.bluetooth.send_buf[offset++] = 0xe5;
// size
WRITE_INT16(truck.bluetooth.send_buf, offset, 26);
// battery response
truck.bluetooth.send_buf[offset++] = 0;
truck.bluetooth.send_buf[offset++] = 0;
WRITE_INT32(truck.bluetooth.send_buf, offset, truck.battery);
WRITE_FLOAT32(truck.bluetooth.send_buf, offset, truck.battery_voltage);
WRITE_INT16(truck.bluetooth.send_buf, offset, (int)truck.gyro_center);
WRITE_INT16(truck.bluetooth.send_buf, offset, truck.gyro_max - truck.gyro_min);
WRITE_FLOAT32(truck.bluetooth.send_buf, offset, truck.current_heading);
chksum = get_chksum(truck.bluetooth.send_buf, offset);
WRITE_INT16(truck.bluetooth.send_buf, offset, chksum);
truck.bluetooth.send_offset = 0;
truck.bluetooth.send_size = offset;
// TRACE2
// print_buffer(truck.bluetooth.send_buf, truck.bluetooth.send_size);
break;
}
// reset gyro
case 1:
truck.have_gyro_center = 0;
truck.need_gyro_center = 0;
truck.gyro_center_count = 0;
truck.gyro_center = 0;
truck.gyro_accum = 0;
truck.gyro_min = 0;
truck.gyro_max = 0;
break;
// config file
case 2:
{
int offset = 8;
//TRACE2
//print_buffer(truck.bluetooth.receive_buf, truck.bluetooth.receive_size);
int bytes = read_config_packet(truck.bluetooth.receive_buf + offset);
int need_save = truck.bluetooth.receive_buf[7];
dump_config();
if(need_save)
{
save_config(truck.bluetooth.receive_buf + offset,
bytes);
CLEAR_PIN(LED_GPIO1, LED_PIN1);
SET_PIN(LED_GPIO2, LED_PIN2);
int i;
int prev_timer_h = truck.timer_high;
int toggle = 0;
int mid_steering_pwm = MIN_PWM +
(MAX_PWM - MIN_PWM) *
truck.mid_steering /
100;
int steering_magnitude = (MAX_PWM - MIN_PWM) / 2 *
truck.min_steering /
100;
truck.writing_settings = 1;
for(i = 0; i < 2; i++)
{
if(toggle == 0)
{
truck.steering_pwm = mid_steering_pwm - steering_magnitude;
write_pwm();
}
else
{
truck.steering_pwm = mid_steering_pwm + steering_magnitude;
write_pwm();
}
toggle ^= 1;
while(1)
{
DISABLE_INTERRUPTS
int time_difference = truck.timer_high - prev_timer_h;
ENABLE_INTERRUPTS
// seems required because of a compiler error
flush_uart();
if(time_difference >= TIMER_HZ / 2) break;
}
DISABLE_INTERRUPTS
prev_timer_h = truck.timer_high;
ENABLE_INTERRUPTS
TOGGLE_PIN(LED_GPIO1, LED_PIN1);
TOGGLE_PIN(LED_GPIO2, LED_PIN2);
}
truck.writing_settings = 0;
CLEAR_PIN(LED_GPIO1, LED_PIN1);
SET_PIN(LED_GPIO2, LED_PIN2);
}
break;
}
}
}
}
void get_code1();
void get_data()
{
truck.bluetooth.receive_buf[truck.bluetooth.receive_offset++] =
truck.bluetooth.data;
if(truck.bluetooth.receive_offset >= truck.bluetooth.receive_size)
{
truck.bluetooth.got_data = 1;
truck.bluetooth.current_function = get_code1;
}
}
void get_size()
{
truck.bluetooth.receive_buf[truck.bluetooth.receive_offset++] =
truck.bluetooth.data;
if(truck.bluetooth.receive_offset >= truck.bluetooth.receive_size)
{
truck.bluetooth.receive_size = truck.bluetooth.receive_buf[4] |
(truck.bluetooth.receive_buf[5] << 8);
if(truck.bluetooth.receive_size < BLUE_BUFSIZE)
{
truck.bluetooth.current_function = get_data;
}
else
{
truck.bluetooth.current_function = get_code1;
}
}
}
void get_code4()
{
if(truck.bluetooth.data == 0xe5)
{
truck.bluetooth.receive_buf[0] = 0xff;
truck.bluetooth.receive_buf[1] = 0x2d;
truck.bluetooth.receive_buf[2] = 0xd4;
truck.bluetooth.receive_buf[3] = 0xe5;
truck.bluetooth.current_function = get_size;
truck.bluetooth.receive_offset = 4;
truck.bluetooth.receive_size = 6;
}
else
truck.bluetooth.current_function = get_code1;
}
void get_code3()
{
if(truck.bluetooth.data == 0xd4)
truck.bluetooth.current_function = get_code4;
else
truck.bluetooth.current_function = get_code1;
}
void get_code2()
{
if(truck.bluetooth.data == 0x2d)
truck.bluetooth.current_function = get_code3;
else
truck.bluetooth.current_function = get_code1;
}
void get_code1()
{
if(truck.bluetooth.data == 0xff)
truck.bluetooth.current_function = get_code2;
}
void USART3_IRQHandler(void)
{
truck.bluetooth.data = USART3->DR;
truck.bluetooth.current_function();
}
// TIM10 wraps at TIMER_HZ
void TIM1_UP_TIM10_IRQHandler()
{
if(TIM10->SR & TIM_FLAG_Update)
{
TIM10->SR = ~TIM_FLAG_Update;
truck.timer_high++;
// Update shutdown timer
if(truck.shutdown_timeout > 0)
{
truck.shutdown_timeout--;
}
}
}
void handle_bluetooth()
{
#ifdef BLUETOOTH_PASSTHROUGH
if(uart_got_input() && (USART3->SR & USART_FLAG_TC) != 0)
{
unsigned char c = uart_get_input();
USART3->DR = c;
}
#else // BLUETOOTH_PASSTHROUGH
if(truck.bluetooth.send_offset < truck.bluetooth.send_size &&
(USART3->SR & USART_FLAG_TC) != 0)
{
USART3->DR = truck.bluetooth.send_buf[truck.bluetooth.send_offset++];
}
if(truck.bluetooth.got_data)
{
truck.bluetooth.got_data = 0;
handle_beacon();
}
#endif // !BLUETOOTH_PASSTHROUGH
}
void init_bluetooth()
{
GPIO_InitTypeDef GPIO_InitStructure;
#ifdef BLUETOOTH_PASSTHROUGH
truck.bluetooth.current_function = bluetooth_passthrough;
TRACE2
print_text("Entering bluetooth passthrough.\n");
#else
truck.bluetooth.current_function = get_code1;
#endif
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
#define UART_RX_PIN 11
#define UART_TX_PIN 10
GPIO_PinAFConfig(GPIOB, UART_RX_PIN, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB, UART_TX_PIN, GPIO_AF_USART3);
// RX enabled
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Pin = 1 << UART_RX_PIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// TX enabled
GPIO_InitStructure.GPIO_Pin = 1 << UART_TX_PIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure;
// once a device is configured
USART_InitStructure.USART_BaudRate = 115200;
// set to configure a new device
// USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* USART configuration */
USART_Init(USART3, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART3, ENABLE);
/* Enable the UART Interrupt */
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
}
void handle_analog()
{
// battery
if((ADC1->SR & ADC_FLAG_EOC))
{
truck.battery_accum += ADC1->DR;
ADC_SoftwareStartConv(ADC1);
truck.battery_count++;
if(truck.battery_count >= BATTERY_OVERSAMPLE)
{
truck.battery = truck.battery_accum / truck.battery_count;
truck.battery_accum = 0;
truck.battery_count = 0;
truck.battery_voltage = truck.battery * 8.67f / 965.0f;
/*
* TRACE2
* print_number(truck.battery);
* print_float(voltage);
*/
}
}
// Gyro
if((ADC2->SR & ADC_FLAG_EOC))
{
truck.gyro_accum += ADC2->DR;
ADC_SoftwareStartConv(ADC2);
truck.gyro_count++;
if(truck.gyro_count >= GYRO_OVERSAMPLE)
{
DISABLE_INTERRUPTS
truck.gyro = truck.gyro_accum / truck.gyro_count;
ENABLE_INTERRUPTS
truck.gyro_accum = 0;
truck.gyro_count = 0;
// TOGGLE_PIN(DEBUG_GPIO, DEBUG_PIN);
// TRACE2
// print_number(gyro);
if(truck.need_gyro_center && !truck.have_gyro_center)
{
imu_led_flash();
truck.gyro_center_accum += truck.gyro;
if(truck.gyro_center_count == 0)
{
truck.gyro_min = truck.gyro;
truck.gyro_max = truck.gyro;
}
else
{
truck.gyro_min = MIN(truck.gyro, truck.gyro_min);
truck.gyro_max = MAX(truck.gyro, truck.gyro_max);
}
truck.gyro_center_count++;
if(ABS(truck.gyro_max - truck.gyro_min) > truck.gyro_center_max)
{
TRACE2
print_text("center too big min=");
print_number(truck.gyro_min);
print_text("max=");
print_number(truck.gyro_max);
print_lf();
truck.gyro_center_count = 0;
truck.gyro_center_accum = 0;
truck.gyro_min = 65535;
truck.gyro_max = -65535;
}
}
if(truck.need_gyro_center &&
!truck.have_gyro_center &&
truck.gyro_center_count >= GYRO_CENTER_TOTAL)
{
truck.gyro_center = (float)truck.gyro_center_accum /
truck.gyro_center_count;
TRACE2
print_number(truck.gyro_center_accum);
print_number(truck.gyro_center_count);
print_text("center=");
print_float(truck.gyro_center);
truck.have_gyro_center = 1;
truck.need_gyro_center = 0;
}
if(truck.have_gyro_center)
{
DISABLE_INTERRUPTS
truck.current_heading += (truck.gyro - truck.gyro_center) /
truck.angle_to_gyro /
NAV_HZ;
truck.current_heading = fix_angle(truck.current_heading);
ENABLE_INTERRUPTS
truck.debug_counter++;
if(truck.debug_counter >= 128)
{
truck.debug_counter = 0;
// TRACE2
// print_float(TO_DEG(truck.current_heading));
}
}
}
}
}
void init_analog()
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Init(ADC2, &ADC_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |
GPIO_Pin_1;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// must call this before ENABLE
ADC_RegularChannelConfig(ADC1,
ADC_Channel_0,
1,
ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC2,
ADC_Channel_1,
1,
ADC_SampleTime_480Cycles);
ADC_Cmd(ADC1, ENABLE);
ADC_Cmd(ADC2, ENABLE);
ADC_SoftwareStartConv(ADC1);
ADC_SoftwareStartConv(ADC2);
}
void TIM2_IRQHandler()
{
if(TIM2->SR & TIM_FLAG_Update)
{
TIM2->SR = ~TIM_FLAG_Update;
SET_PIN(GPIOA, GPIO_Pin_6);
SET_PIN(GPIOA, GPIO_Pin_7);
if(truck.writing_settings) return;
int mid_steering_pwm = MIN_PWM +
(MAX_PWM - MIN_PWM) *
truck.mid_steering /
100;
int mid_throttle_pwm = MIN_PWM +
(MAX_PWM - MIN_PWM) *
truck.mid_throttle /
100;
int throttle_ramp_step = (MAX_PWM - MIN_PWM) *
truck.throttle_ramp_step /
100;
if(truck.have_gyro_center)
{
int throttle_magnitude;
int max_steering_magnitude = (MAX_PWM - MIN_PWM) / 2 *
truck.max_steering /
100;
int min_steering_magnitude = (MAX_PWM - MIN_PWM) / 2 *
truck.min_steering /
100;
int need_feedback = 1;
if(truck.throttle_reverse)