6 #include <avr/interrupt.h>
11 * I2C Register Map (8 Bit)
12 * 0x00 Register select
13 * 0x01 Motor 1 PWM MSB
14 * 0x02 Motor 1 PWM LSB
15 * 0x03 Motor 2 PWM MSB
16 * 0x04 Motor 2 PWM LSB
17 * 0x05 Motor 3 PWM MSB
18 * 0x06 Motor 3 PWM LSB
19 * 0x07 Motor 4 PWM MSB
20 * 0x08 Motor 4 PWM LSB
31 * 0x20 Motor 1 speed wish MSB
32 * 0x21 Motor 1 speed wish LSB
33 * 0x22 Motor 2 speed wish MSB
34 * 0x23 Motor 2 speed wish LSB
35 * 0x24 Motor 3 speed wish MSB
36 * 0x25 Motor 3 speed wish LSB
37 * 0x26 Motor 4 speed wish MSB
38 * 0x27 Motor 4 speed wish LSB
39 * 0x28 Left speed wish (m/s) MSB
40 * 0x29 Left speed wish (m/s)
41 * 0x2A Left speed wish (m/s)
42 * 0x2B Left speed wish (m/s) LSB
43 * 0x2C Right speed wish (m/s) MSB
44 * 0x2D Right speed wish (m/s)
45 * 0x2E Right speed wish (m/s)
46 * 0x2F Right speed wish (m/s) LSB
47 * 0x30 Motor 1 speed MSB
48 * 0x31 Motor 1 speed LSB
49 * 0x32 Motor 2 speed MSB
50 * 0x33 Motor 2 speed LSB
51 * 0x34 Motor 3 speed MSB
52 * 0x35 Motor 3 speed LSB
53 * 0x36 Motor 4 speed MSB
54 * 0x37 Motor 4 speed LSB
55 * 0x38 Speed (m/s) MSB
58 * 0x3B Speed (m/s) LSB
59 * 0x3C Angle (rad/s) MSB
62 * 0x3F Angle (rad/s) LSB
63 * 0x40 Position x (m) MSB
66 * 0x43 Position x (m) LSB
67 * 0x44 Position y (m) MSB
70 * 0x47 Position y (m) LSB
71 * 0x48 Position angle MSB
74 * 0x4B Position angle LSB
76 * 0x50 speed wish (m/s) MSB
77 * 0x51 speed wish (m/s)
78 * 0x52 speed wish (m/s)
79 * 0x53 speed wish (m/s) LSB
80 * 0x54 angle wish (rad/s) MSB
81 * 0x55 angle wish (rad/s)
82 * 0x56 angle wish (rad/s)
83 * 0x57 angle wish (rad/s) LSB
104 // wheel diameter=12cm, encoder=48cpr, gear ratio=1:47
105 // STEP_PER_M = 48*47/(d*pi)
106 // Left real diameter: 0.12808, Right real diameter: 0.121
107 #define STEP_PER_M 5770.8
108 #define STEP_PER_M_LEFT (STEP_PER_M)
109 #define STEP_PER_M_RIGHT (STEP_PER_M)
110 #define WHEEL_DIST 0.39912 // Measured: 0.252
111 #define PWM_BREAK INT16_MIN
113 #define TWI_ACK TWCR = (1<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
114 #define TWI_RESET TWCR &= ~((1 << TWSTO) | (1 << TWEN)); TWI_ACK
115 #define TWI_NAK TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
116 #define ENABLE_PWM_MOTOR1 TCCR1A |= (1 << COM1A1)
117 #define ENABLE_PWM_MOTOR2 TCCR1A |= (1 << COM1B1)
118 #define ENABLE_PWM_MOTOR3 TCCR2 |= (1 << COM21);
119 #define ENABLE_PWM_MOTOR4 TCCR0 |= (1 << COM01);
120 #define DISABLE_PWM_MOTOR1 TCCR1A &= ~(1 << COM1A1)
121 #define DISABLE_PWM_MOTOR2 TCCR1A &= ~(1 << COM1B1)
122 #define DISABLE_PWM_MOTOR3 TCCR2 &= ~(1 << COM21);
123 #define DISABLE_PWM_MOTOR4 TCCR0 &= ~(1 << COM01);
136 static volatile struct {
140 } cmd_vel = {0, 0, 0};
142 static volatile uint8_t ireg=0;
143 static volatile uint8_t bootloader=0;
144 static volatile int16_t motor1=0; // -255..+255
145 static volatile int16_t motor2=0;
146 static volatile int16_t motor3=0;
147 static volatile int16_t motor4=0;
148 static volatile int16_t pos1=0; // step
149 static volatile int16_t pos2=0;
150 static volatile int16_t pos3=0;
151 static volatile int16_t pos4=0;
152 static volatile enum mode motor1_mode=MOTOR_MANUAL;
153 static volatile enum mode motor2_mode=MOTOR_MANUAL;
154 static volatile enum mode motor3_mode=MOTOR_MANUAL;
155 static volatile enum mode motor4_mode=MOTOR_MANUAL;
156 static volatile uint8_t motor1_switch=1;
157 static volatile uint8_t motor2_switch=1;
158 static volatile uint8_t motor3_switch=0;
159 static volatile uint8_t motor4_switch=0;
160 static volatile int16_t speed1_wish=0; // step/s
161 static volatile int16_t speed2_wish=0;
162 static volatile int16_t speed3_wish=0;
163 static volatile int16_t speed4_wish=0;
164 static volatile int16_t speed1_wish_old=0;
165 static volatile int16_t speed2_wish_old=0;
166 static volatile int16_t speed3_wish_old=0;
167 static volatile int16_t speed4_wish_old=0;
168 static volatile uint8_t run_update=0;
169 static volatile int16_t speed1=0; // step/s
170 static volatile int16_t speed2=0;
171 static volatile int16_t speed3=0;
172 static volatile int16_t speed4=0;
173 static volatile ufloat_t pos_x={0.0};
174 static volatile ufloat_t pos_y={0.0};
175 static volatile ufloat_t angle={0.0};
176 static volatile float cur_speed_lin=0;
177 static volatile float cur_speed_rot=0;
178 static volatile uint8_t count_test=0;
179 static volatile uint8_t front_handicap=0;
180 static volatile uint8_t aft_handicap=0;
181 static volatile uint8_t error_state=0;
185 static uint8_t tmp=0;
186 static int16_t tmp16=0;
187 static ufloat_t tmp_speed;
188 static ufloat_t tmp_angle;
192 case 0x60: // start write
198 case 0x00: // register select
200 ireg--; // because we do ireg++ below
203 case 0x01: // Motor 1 MSB
207 case 0x02: // Motor 1 LSB
208 motor1 = tmp<<8 | TWDR;
209 motor1_mode = MOTOR_MANUAL;
212 case 0x03: // Motor 2 MSB
216 case 0x04: // Motor 2 LSB
217 motor2 = tmp<<8 | TWDR;
218 motor2_mode = MOTOR_MANUAL;
221 case 0x05: // Motor 3 MSB
225 case 0x06: // Motor 3 LSB
226 motor3 = tmp<<8 | TWDR;
227 motor3_mode = MOTOR_MANUAL;
230 case 0x07: // Motor 4 MSB
234 case 0x08: // Motor 4 LSB
235 motor4 = tmp<<8 | TWDR;
236 motor4_mode = MOTOR_MANUAL;
239 case 0x20: // Motor 1 speed wish MSB
243 case 0x21: // Motor 1 speed wish LSB
244 speed1_wish = tmp<<8 | TWDR;
245 motor1_mode = MOTOR_PID;
248 case 0x22: // Motor 2 speed wish MSB
252 case 0x23: // Motor 2 speed wish LSB
253 speed2_wish = tmp<<8 | TWDR;
254 motor2_mode = MOTOR_PID;
257 case 0x24: // Motor 3 speed wish MSB
261 case 0x25: // Motor 3 speed wish LSB
262 speed3_wish = tmp<<8 | TWDR;
263 motor3_mode = MOTOR_PID;
266 case 0x26: // Motor 4 speed wish MSB
270 case 0x27: // Motor 4 speed wish LSB
271 speed4_wish = tmp<<8 | TWDR;
272 motor4_mode = MOTOR_PID;
275 case 0x28: // Left speed wish MSB
279 case 0x29: // Left speed wish
280 tmp_speed.i = tmp_speed.i << 8 | TWDR;
283 case 0x2A: // Left speed wish
284 tmp_speed.i = tmp_speed.i << 8 | TWDR;
287 case 0x2B: // Left speed wish LSB
288 tmp_speed.i = tmp_speed.i << 8 | TWDR;
289 speed1_wish = tmp_speed.f*STEP_PER_M_LEFT;
290 speed2_wish = tmp_speed.f*STEP_PER_M_LEFT;
291 motor1_mode = MOTOR_PID;
292 motor2_mode = MOTOR_PID;
295 case 0x2C: // Right speed wish MSB
299 case 0x2D: // Right speed wish
300 tmp_speed.i = tmp_speed.i << 8 | TWDR;
303 case 0x2E: // Right speed wish
304 tmp_speed.i = tmp_speed.i << 8 | TWDR;
307 case 0x2F: // Right speed wish LSB
308 tmp_speed.i = tmp_speed.i << 8 | TWDR;
309 speed1_wish = tmp_speed.f*STEP_PER_M_RIGHT;
310 speed2_wish = tmp_speed.f*STEP_PER_M_RIGHT;
311 motor1_mode = MOTOR_PID;
312 motor2_mode = MOTOR_PID;
315 case 0x50: // speed wish MSB
319 case 0x51: // speed wish
320 tmp_speed.i = tmp_speed.i << 8 | TWDR;
323 case 0x52: // speed wish
324 tmp_speed.i = tmp_speed.i << 8 | TWDR;
327 case 0x53: // speed wish LSB
328 tmp_speed.i = tmp_speed.i << 8 | TWDR;
329 cmd_vel.speed = tmp_speed.f;
332 case 0x54: // angle wish MSB
336 case 0x55: // angle wish
337 tmp_angle.i = tmp_angle.i << 8 | TWDR;
340 case 0x56: // angle wish
341 tmp_angle.i = tmp_angle.i << 8 | TWDR;
344 case 0x57: // angle wish LSB
345 tmp_angle.i = tmp_angle.i << 8 | TWDR;
346 cmd_vel.angle = tmp_angle.f;
350 case 0x90: // Motor 1 switch
351 motor1_switch = TWDR;
354 case 0x91: // Motor 2 switch
355 motor2_switch = TWDR;
358 case 0x92: // Motor 3 switch
359 motor3_switch = TWDR;
362 case 0x93: // Motor 4 switch
363 motor4_switch = TWDR;
366 case 0x94: // Front Handicap
367 front_handicap = TWDR;
371 case 0x95: // Aft Handicap
376 case 0xff: // bootloader
383 case 0xA8: // start read
386 case 0x02: // Motor 1 PWM
390 case 0x03: // Dummy to allow continous read
394 case 0x04: // Motor 2 PWM
398 case 0x05: // Dummy to allow continous read
402 case 0x06: // Motor 3 PWM
406 case 0x07: // Dummy to allow continous read
410 case 0x08: // Motor 4 PWM
414 case 0x09: // Dummy to allow continous read
418 case 0x10: // Hall 1 MSB
423 case 0x11: // Hall 1 LSB
427 case 0x12: // Hall 2 MSB
432 case 0x13: // Hall 2 LSB
436 case 0x14: // Hall 3 MSB
441 case 0x15: // Hall 3 LSB
445 case 0x16: // Hall 4 MSB
450 case 0x17: // Hall 4 LSB
454 case 0x20: // Motor 1 speed wish MSB
455 TWDR = speed1_wish>>8;
458 case 0x21: // Motor 1 speed wish LSB
462 case 0x22: // Motor 2 speed wish MSB
463 TWDR = speed2_wish>>8;
466 case 0x23: // Motor 2 speed wish LSB
470 case 0x24: // Motor 3 speed wish MSB
471 TWDR = speed3_wish>>8;
474 case 0x25: // Motor 3 speed wish LSB
478 case 0x26: // Motor 4 speed wish MSB
479 TWDR = speed4_wish>>8;
482 case 0x27: // Motor 4 speed wish LSB
486 case 0x30: // Motor 1 speed MSB
490 case 0x31: // Motor 1 speed LSB
494 case 0x32: // Motor 2 speed MSB
498 case 0x33: // Motor 2 speed LSB
502 case 0x34: // Motor 3 speed MSB
506 case 0x35: // Motor 3 speed LSB
510 case 0x36: // Motor 4 speed MSB
514 case 0x37: // Motor 4 speed LSB
518 case 0x38: // speed MSB
519 tmp_speed.f = cur_speed_lin;
520 TWDR = tmp_speed.i>>24;
524 TWDR = tmp_speed.i>>16;
528 TWDR = tmp_speed.i>>8;
531 case 0x3B: // speed LSB
535 case 0x3C: // angle MSB
536 tmp_angle.f = cur_speed_rot;
537 TWDR = tmp_angle.i>>24;
541 TWDR = tmp_angle.i>>16;
545 TWDR = tmp_angle.i>>8;
548 case 0x3F: // angle LSB
552 case 0x40: // Position x MSB
556 case 0x41: // Position x
560 case 0x42: // Position x
564 case 0x43: // Position x LSB
568 case 0x44: // Position y MSB
572 case 0x45: // Position y
576 case 0x46: // Position y
580 case 0x47: // Position y LSB
584 case 0x48: // Position angle MSB
588 case 0x49: // Position angle
592 case 0x4A: // Position angle
596 case 0x4B: // Position angle LSB
600 case 0xA0: // Reset reason
601 TWDR = MCUCSR & 0x0f;
605 case 0xA1: // Error status
609 case 0xA2: // count test
624 static void update_hall1(void) {
625 unsigned char status = (PINA >> 0) & 0x3;
626 static unsigned char oldstatus=0;
627 unsigned char diff, new;
633 new ^= 0x1; // convert gray to binary
634 diff = oldstatus - new; // difference last - new
635 if (diff & 0x1) { // bit 0 = value (1)
636 oldstatus = new; // store new as next last
637 if (motor1_switch) pos1 += (diff & 2) - 1; // bit 1 = direction (+/-)
638 else pos1 -= (diff & 2) - 1;
643 static void update_hall2(void) {
644 unsigned char status = (PINA >> 4) & 0x3;
645 static unsigned char oldstatus=0;
646 unsigned char diff, new;
652 new ^= 0x1; // convert gray to binary
653 diff = oldstatus - new; // difference last - new
654 if (diff & 0x1) { // bit 0 = value (1)
655 oldstatus = new; // store new as next last
656 if (motor2_switch) pos2 -= (diff & 2) - 1; // bit 1 = direction (+/-)
657 else pos2 += (diff & 2) - 1;
662 static void update_hall3(void) {
663 unsigned char status = (PINA >> 2) & 0x3;
664 static unsigned char oldstatus=0;
665 unsigned char diff, new;
671 new ^= 0x1; // convert gray to binary
672 diff = oldstatus - new; // difference last - new
673 if (diff & 0x1) { // bit 0 = value (1)
674 oldstatus = new; // store new as next last
675 if (motor3_switch) pos3 -= (diff & 2) - 1; // bit 1 = direction (+/-)
676 else pos3 += (diff & 2) - 1;
681 static void update_hall4(void) {
682 unsigned char status = (PINA >> 6) & 0x3;
683 static unsigned char oldstatus=0;
684 unsigned char diff, new;
690 new ^= 0x1; // convert gray to binary
691 diff = oldstatus - new; // difference last - new
692 if (diff & 0x1) { // bit 0 = value (1)
693 oldstatus = new; // store new as next last
694 if (motor4_switch) pos4 += (diff & 2) - 1; // bit 1 = direction (+/-)
695 else pos4 -= (diff & 2) - 1;
700 static void update_motor(void) {
701 static int16_t m1_old=SHRT_MIN;
702 static int16_t m2_old=SHRT_MIN;
703 static int16_t m3_old=SHRT_MIN;
704 static int16_t m4_old=SHRT_MIN;
706 error_state &= 0xf0; // clear lower bits
707 error_state |= ~((PIND & 0x40)>>3 | (PINB & 0x07)) & 0xf;
709 if (m1_old != motor1) { // update only when changed
712 PORTC &= ~(1 << 3) & ~(1 << 2);
714 } else if (motor1 == PWM_BREAK) {
715 PORTC |= (1 << 3) | (1 << 2);
717 } else if ((!motor1_switch && motor1 > 0) || (motor1_switch && motor1 < 0)) {
724 } else { // motor1 < 0
737 if (m2_old != motor2) { // update only when changed
740 PORTC &= ~(1 << 5) & ~(1 << 4);
742 } else if (motor2 == PWM_BREAK) {
743 PORTC |= (1 << 5) | (1 << 4);
745 } else if ((!motor2_switch && motor2 > 0) || (motor2_switch && motor2 < 0)) {
752 } else { // motor2 < 0
765 if (m3_old != motor3) { // update only when changed
768 PORTC &= ~(1 << 7) & ~(1 << 6);
770 } else if (motor3 == PWM_BREAK) {
771 PORTC |= (1 << 7) | (1 << 6);
773 } else if ((!motor3_switch && motor3 > 0) || (motor3_switch && motor3 < 0)) {
780 } else { // motor3 < 0
793 if (m4_old != motor4) { // update only when changed
796 PORTD &= ~(1 << 3) & ~(1 << 2);
798 } else if (motor4 == PWM_BREAK) {
799 PORTD |= (1 << 3) | (1 << 2);
801 } else if ((!motor4_switch && motor4 > 0) || (motor4_switch && motor4 < 0)) {
808 } else { // motor4 < 0
823 static void update_pos(void) {
824 static int16_t pos1_last=0;
825 static int16_t pos2_last=0;
826 static int16_t pos3_last=0;
827 static int16_t pos4_last=0;
828 int16_t pos1_diff; // steps
832 float diff_left_m, diff_right_m, angle_diff, translation;
833 float pos_x_new, pos_y_new, angle_new;
834 float tmp_speed_lin, tmp_speed_rot;
835 int16_t cur_pos1, cur_pos2, cur_pos3, cur_pos4;
836 int16_t new_speed1, new_speed2, new_speed3, new_speed4;
846 pos1_diff = cur_pos1 - pos1_last;
847 pos2_diff = cur_pos2 - pos2_last;
848 pos3_diff = cur_pos3 - pos3_last;
849 pos4_diff = cur_pos4 - pos4_last;
851 new_speed1 = pos1_diff/PID_T;
852 new_speed2 = pos2_diff/PID_T;
853 new_speed3 = pos3_diff/PID_T;
854 new_speed4 = pos4_diff/PID_T;
856 diff_left_m = (pos1_diff + pos2_diff)/(2*STEP_PER_M_LEFT);
857 diff_right_m = (pos3_diff + pos4_diff)/(2*STEP_PER_M_RIGHT);
858 angle_diff = (diff_right_m - diff_left_m) / WHEEL_DIST;
860 angle_new = angle.f + angle_diff;
861 if (angle_new > 2*M_PI) angle_new-=2*M_PI;
862 else if (angle_new < -2*M_PI) angle_new+=2*M_PI;
864 translation = (diff_left_m + diff_right_m)/2.0;
865 pos_x_new = pos_x.f + cos(angle_new)*translation;
866 pos_y_new = pos_y.f + sin(angle_new)*translation;
868 tmp_speed_lin = translation/PID_T;
869 tmp_speed_rot = angle_diff/PID_T;
880 cur_speed_lin = tmp_speed_lin;
881 cur_speed_rot = tmp_speed_rot;
884 pos1_last = cur_pos1;
885 pos2_last = cur_pos2;
886 pos3_last = cur_pos3;
887 pos4_last = cur_pos4;
891 static void update_pid(void) {
892 static int16_t eold1=0;
893 static int16_t eold2=0;
894 static int16_t eold3=0;
895 static int16_t eold4=0;
896 static int32_t esum1=0;
897 static int32_t esum2=0;
898 static int32_t esum3=0;
899 static int32_t esum4=0;
901 // protect motors from damage if stalling
902 if (labs(esum1) > 140000 && speed1 == 0) {
904 motor1_mode = MOTOR_MANUAL;
905 error_state |= (1<<4);
908 if (labs(esum2) > 140000 && speed2 == 0) {
910 motor2_mode = MOTOR_MANUAL;
911 error_state |= (1<<5);
914 if (labs(esum3) > 140000 && speed3 == 0) {
916 motor3_mode = MOTOR_MANUAL;
917 error_state |= (1<<6);
920 if (labs(esum4) > 140000 && speed4 == 0) {
922 motor4_mode = MOTOR_MANUAL;
923 error_state |= (1<<7);
927 if (motor1_mode == MOTOR_PID) {
928 if (speed1_wish != speed1_wish_old) {
929 if (abs(speed1_wish - speed1_wish_old) > 500) esum1 = 0;
930 speed1_wish_old = speed1_wish;
933 if (speed1_wish == 0) {
936 error_state &= ~(1<<4);
938 int16_t e = speed1_wish - speed1;
940 motor1 = KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
943 if (motor1 > 0 && speed1_wish < 0) motor1=PWM_BREAK;
944 else if (motor1 < 0 && speed1_wish > 0) motor1=PWM_BREAK;
945 else if (motor1 > 255) motor1 = 255;
946 else if (motor1 < -255) motor1 = -255;
949 if (motor2_mode == MOTOR_PID) {
950 if (speed2_wish != speed2_wish_old) {
951 if (abs(speed2_wish - speed2_wish_old) > 500) esum2 = 0;
952 speed2_wish_old = speed2_wish;
955 if (speed2_wish == 0) {
958 error_state &= ~(1<<5);
960 int16_t e = speed2_wish - speed2;
962 motor2 = KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
965 if (motor2 > 0 && speed2_wish < 0) motor2=PWM_BREAK;
966 else if (motor2 < 0 && speed2_wish > 0) motor2=PWM_BREAK;
967 else if (motor2 > 255) motor2 = 255;
968 else if (motor2 < -255) motor2 = -255;
971 if (motor3_mode == MOTOR_PID) {
972 if (speed3_wish != speed3_wish_old) {
973 if (abs(speed3_wish - speed3_wish_old) > 500) esum3 = 0;
974 speed3_wish_old = speed3_wish;
977 if (speed3_wish == 0) {
980 error_state &= ~(1<<6);
982 int16_t e = speed3_wish - speed3;
984 motor3 = KP*e + KI*PID_T*esum3 + KD/PID_T*(e - eold3);
987 if (motor3 > 0 && speed3_wish < 0) motor3=PWM_BREAK;
988 else if (motor3 < 0 && speed3_wish > 0) motor3=PWM_BREAK;
989 else if (motor3 > 255) motor3 = 255;
990 else if (motor3 < -255) motor3 = -255;
993 if (motor4_mode == MOTOR_PID) {
994 if (speed4_wish != speed4_wish_old) {
995 if (abs(speed4_wish - speed4_wish_old) > 500) esum4 = 0;
996 speed4_wish_old = speed4_wish;
999 if (speed4_wish == 0) {
1002 error_state &= ~(1<<7);
1004 int16_t e = speed4_wish - speed4;
1006 motor4 = KP*e + KI*PID_T*esum4 + KD/PID_T*(e - eold4);
1009 if (motor4 > 0 && speed4_wish < 0) motor4=PWM_BREAK;
1010 else if (motor4 < 0 && speed4_wish > 0) motor4=PWM_BREAK;
1011 else if (motor4 > 255) motor4 = 255;
1012 else if (motor4 < -255) motor4 = -255;
1018 ISR(TIMER1_OVF_vect) {
1031 DDRC = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
1032 DDRD = (1 << 7) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
1033 // Pullup Diag/Enable
1034 PORTB = (1 << 0) | (1 << 1) | (1 << 2);
1039 uart_setup_stdout();
1046 // Also used for PWM frequency TIMER1_FREQ (F_CPU/256)
1047 // Timer 1: Fast PWM non-inverting mode, Top=255 => 19.531kHz
1049 //TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10);
1050 // Avoid narrow spike on extreme pwm value 0 by not setting COM1*1
1051 TCCR1A = (1 << WGM10);
1052 TCCR1B = (1 << WGM12) | (1 << CS10);
1057 // Timer 2: Fast PWM non-inverting mode, Top=255
1059 //TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << COM21) | (1 << CS20);
1060 // Avoid narrow spike on extreme pwm value 0 by not setting COM21
1061 TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << CS20);
1065 // Timer 0: Fast PWM non-inverting mode, Top=255
1067 //TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << COM01) | (1 << CS00);
1068 // Avoid narrow spike on extreme pwm value 0 by not setting COM01
1069 TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << CS00);
1072 printf("\r\nStart\r\n");
1074 set_sleep_mode(SLEEP_MODE_IDLE);
1075 // Enable Timer 1 Overflow Interrupt
1076 TIMSK = (1 << TOIE1);
1081 case 0xff: // Magic reg that starts the bootloader
1082 if (bootloader == 0xa5) {
1085 void (*start)(void) = (void*)0x1800;
1092 if (cmd_vel.bUpdate) {
1093 float speed_wish_right, speed_wish_left;
1097 speed = cmd_vel.speed;
1098 angle = cmd_vel.angle;
1099 cmd_vel.bUpdate = 0;
1102 speed_wish_right = (angle*WHEEL_DIST)/2 + speed;
1103 speed_wish_left = speed*2-speed_wish_right;
1105 speed_wish_left*=STEP_PER_M_LEFT;
1106 speed_wish_right*=STEP_PER_M_RIGHT;
1108 if (aft_handicap > 0) {
1109 speed1_wish = speed_wish_left * (100-aft_handicap)/100.0;
1110 speed3_wish = speed_wish_right * (100-aft_handicap)/100.0;
1112 speed1_wish = speed_wish_left;
1113 speed3_wish = speed_wish_right;
1115 if (front_handicap > 0) {
1116 speed2_wish = speed_wish_left * (100-front_handicap)/100.0;
1117 speed4_wish = speed_wish_right * (100-front_handicap)/100.0;
1119 speed2_wish = speed_wish_left;
1120 speed4_wish = speed_wish_right;
1122 motor1_mode = MOTOR_PID;
1123 motor2_mode = MOTOR_PID;
1124 motor3_mode = MOTOR_PID;
1125 motor4_mode = MOTOR_PID;
1128 if (run_update >= 195) { // TIMER1_FREQ/195 = ~100Hz