6 #include <avr/interrupt.h>
9 #include <avr/eeprom.h>
14 * I2C Register Map (8 Bit)
15 * 0x00 Register select
16 * 0x01 Motor 1 PWM MSB
17 * 0x02 Motor 1 PWM LSB
18 * 0x03 Motor 2 PWM MSB
19 * 0x04 Motor 2 PWM LSB
20 * 0x05 Motor 3 PWM MSB
21 * 0x06 Motor 3 PWM LSB
22 * 0x07 Motor 4 PWM MSB
23 * 0x08 Motor 4 PWM LSB
34 * 0x20 Motor 1 speed wish MSB
35 * 0x21 Motor 1 speed wish LSB
36 * 0x22 Motor 2 speed wish MSB
37 * 0x23 Motor 2 speed wish LSB
38 * 0x24 Motor 3 speed wish MSB
39 * 0x25 Motor 3 speed wish LSB
40 * 0x26 Motor 4 speed wish MSB
41 * 0x27 Motor 4 speed wish LSB
42 * 0x28 Left speed wish (m/s) MSB
43 * 0x29 Left speed wish (m/s)
44 * 0x2A Left speed wish (m/s)
45 * 0x2B Left speed wish (m/s) LSB
46 * 0x2C Right speed wish (m/s) MSB
47 * 0x2D Right speed wish (m/s)
48 * 0x2E Right speed wish (m/s)
49 * 0x2F Right speed wish (m/s) LSB
50 * 0x30 Motor 1 speed MSB
51 * 0x31 Motor 1 speed LSB
52 * 0x32 Motor 2 speed MSB
53 * 0x33 Motor 2 speed LSB
54 * 0x34 Motor 3 speed MSB
55 * 0x35 Motor 3 speed LSB
56 * 0x36 Motor 4 speed MSB
57 * 0x37 Motor 4 speed LSB
58 * 0x38 Speed (m/s) MSB
61 * 0x3B Speed (m/s) LSB
62 * 0x3C Angle (rad/s) MSB
65 * 0x3F Angle (rad/s) LSB
66 * 0x40 Position x (m) MSB
69 * 0x43 Position x (m) LSB
70 * 0x44 Position y (m) MSB
73 * 0x47 Position y (m) LSB
74 * 0x48 Position angle MSB
77 * 0x4B Position angle LSB
79 * 0x50 speed wish (m/s) MSB
80 * 0x51 speed wish (m/s)
81 * 0x52 speed wish (m/s)
82 * 0x53 speed wish (m/s) LSB
83 * 0x54 angle wish (rad/s) MSB
84 * 0x55 angle wish (rad/s)
85 * 0x56 angle wish (rad/s)
86 * 0x57 angle wish (rad/s) LSB
107 // wheel diameter=12cm, encoder=48cpr, gear ratio=1:47
108 // STEP_PER_M = 48*47/(d*pi)
109 // Left real diameter: 0.12808, Right real diameter: 0.121
110 #define STEP_PER_M 5573.0
111 #define STEP_PER_M_LEFT (STEP_PER_M)
112 #define STEP_PER_M_RIGHT (STEP_PER_M)
113 #define WHEEL_DIST 0.39912 // Measured: 0.252
114 #define PWM_BREAK INT16_MIN
115 #define STALL_LIMIT 140000
117 #define TWI_ACK TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) | (1<<TWIE)
118 #define TWI_NAK TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
119 #define TWI_RESET TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWSTO) | (1<<TWEN) | (1<<TWIE);
120 #define ENABLE_PWM_MOTOR1 TCCR1A |= (1 << COM1A1)
121 #define ENABLE_PWM_MOTOR2 TCCR1A |= (1 << COM1B1)
122 #define ENABLE_PWM_MOTOR3 TCCR2 |= (1 << COM21);
123 #define ENABLE_PWM_MOTOR4 TCCR0 |= (1 << COM01);
124 #define DISABLE_PWM_MOTOR1 TCCR1A &= ~(1 << COM1A1)
125 #define DISABLE_PWM_MOTOR2 TCCR1A &= ~(1 << COM1B1)
126 #define DISABLE_PWM_MOTOR3 TCCR2 &= ~(1 << COM21);
127 #define DISABLE_PWM_MOTOR4 TCCR0 &= ~(1 << COM01);
140 static volatile struct {
144 } cmd_vel = {0, 0, 0};
146 static volatile uint8_t ireg=0;
147 static volatile uint8_t bootloader=0;
148 static volatile int16_t motor1=0; // -255..+255
149 static volatile int16_t motor2=0;
150 static volatile int16_t motor3=0;
151 static volatile int16_t motor4=0;
152 static volatile int16_t pos1=0; // step
153 static volatile int16_t pos2=0;
154 static volatile int16_t pos3=0;
155 static volatile int16_t pos4=0;
156 static volatile enum mode motor1_mode=MOTOR_MANUAL;
157 static volatile enum mode motor2_mode=MOTOR_MANUAL;
158 static volatile enum mode motor3_mode=MOTOR_MANUAL;
159 static volatile enum mode motor4_mode=MOTOR_MANUAL;
160 static volatile uint8_t motor1_switch=1;
161 static volatile uint8_t motor2_switch=1;
162 static volatile uint8_t motor3_switch=0;
163 static volatile uint8_t motor4_switch=0;
164 static volatile int16_t speed1_wish=0; // step/s
165 static volatile int16_t speed2_wish=0;
166 static volatile int16_t speed3_wish=0;
167 static volatile int16_t speed4_wish=0;
168 static volatile int16_t speed1_wish_old=0;
169 static volatile int16_t speed2_wish_old=0;
170 static volatile int16_t speed3_wish_old=0;
171 static volatile int16_t speed4_wish_old=0;
172 static volatile uint8_t run_update=0;
173 static volatile int16_t speed1=0; // step/s
174 static volatile int16_t speed2=0;
175 static volatile int16_t speed3=0;
176 static volatile int16_t speed4=0;
177 static volatile ufloat_t pos_x={0.0};
178 static volatile ufloat_t pos_y={0.0};
179 static volatile ufloat_t angle={0.0};
180 static volatile float cur_speed_lin=0;
181 static volatile float cur_speed_rot=0;
182 static volatile uint8_t count_test=0;
183 static volatile uint8_t front_handicap=0;
184 static volatile uint8_t aft_handicap=0;
185 static volatile uint8_t error_state=0;
189 static uint8_t tmp=0;
190 static int16_t tmp16=0;
191 static ufloat_t tmp_speed;
192 static ufloat_t tmp_angle;
196 case TW_SR_SLA_ACK: // start write
200 case TW_SR_DATA_ACK: // write
202 case 0x00: // register select
204 ireg--; // because we do ireg++ below
207 case 0x01: // Motor 1 MSB
211 case 0x02: // Motor 1 LSB
212 motor1 = tmp<<8 | TWDR;
213 motor1_mode = MOTOR_MANUAL;
216 case 0x03: // Motor 2 MSB
220 case 0x04: // Motor 2 LSB
221 motor2 = tmp<<8 | TWDR;
222 motor2_mode = MOTOR_MANUAL;
225 case 0x05: // Motor 3 MSB
229 case 0x06: // Motor 3 LSB
230 motor3 = tmp<<8 | TWDR;
231 motor3_mode = MOTOR_MANUAL;
234 case 0x07: // Motor 4 MSB
238 case 0x08: // Motor 4 LSB
239 motor4 = tmp<<8 | TWDR;
240 motor4_mode = MOTOR_MANUAL;
243 case 0x20: // Motor 1 speed wish MSB
247 case 0x21: // Motor 1 speed wish LSB
248 speed1_wish = tmp<<8 | TWDR;
249 motor1_mode = MOTOR_PID;
252 case 0x22: // Motor 2 speed wish MSB
256 case 0x23: // Motor 2 speed wish LSB
257 speed2_wish = tmp<<8 | TWDR;
258 motor2_mode = MOTOR_PID;
261 case 0x24: // Motor 3 speed wish MSB
265 case 0x25: // Motor 3 speed wish LSB
266 speed3_wish = tmp<<8 | TWDR;
267 motor3_mode = MOTOR_PID;
270 case 0x26: // Motor 4 speed wish MSB
274 case 0x27: // Motor 4 speed wish LSB
275 speed4_wish = tmp<<8 | TWDR;
276 motor4_mode = MOTOR_PID;
279 case 0x28: // Left speed wish MSB
283 case 0x29: // Left speed wish
284 tmp_speed.i = tmp_speed.i << 8 | TWDR;
287 case 0x2A: // Left speed wish
288 tmp_speed.i = tmp_speed.i << 8 | TWDR;
291 case 0x2B: // Left speed wish LSB
292 tmp_speed.i = tmp_speed.i << 8 | TWDR;
293 speed1_wish = tmp_speed.f*STEP_PER_M_LEFT;
294 speed2_wish = tmp_speed.f*STEP_PER_M_LEFT;
295 motor1_mode = MOTOR_PID;
296 motor2_mode = MOTOR_PID;
299 case 0x2C: // Right speed wish MSB
303 case 0x2D: // Right speed wish
304 tmp_speed.i = tmp_speed.i << 8 | TWDR;
307 case 0x2E: // Right speed wish
308 tmp_speed.i = tmp_speed.i << 8 | TWDR;
311 case 0x2F: // Right speed wish LSB
312 tmp_speed.i = tmp_speed.i << 8 | TWDR;
313 speed1_wish = tmp_speed.f*STEP_PER_M_RIGHT;
314 speed2_wish = tmp_speed.f*STEP_PER_M_RIGHT;
315 motor1_mode = MOTOR_PID;
316 motor2_mode = MOTOR_PID;
319 case 0x50: // speed wish MSB
323 case 0x51: // speed wish
324 tmp_speed.i = tmp_speed.i << 8 | TWDR;
327 case 0x52: // speed wish
328 tmp_speed.i = tmp_speed.i << 8 | TWDR;
331 case 0x53: // speed wish LSB
332 tmp_speed.i = tmp_speed.i << 8 | TWDR;
333 cmd_vel.speed = tmp_speed.f;
336 case 0x54: // angle wish MSB
340 case 0x55: // angle wish
341 tmp_angle.i = tmp_angle.i << 8 | TWDR;
344 case 0x56: // angle wish
345 tmp_angle.i = tmp_angle.i << 8 | TWDR;
348 case 0x57: // angle wish LSB
349 tmp_angle.i = tmp_angle.i << 8 | TWDR;
350 cmd_vel.angle = tmp_angle.f;
354 case 0x90: // Motor 1 switch
355 motor1_switch = TWDR;
358 case 0x91: // Motor 2 switch
359 motor2_switch = TWDR;
362 case 0x92: // Motor 3 switch
363 motor3_switch = TWDR;
366 case 0x93: // Motor 4 switch
367 motor4_switch = TWDR;
370 case 0x94: // Front Handicap
371 front_handicap = TWDR;
375 case 0x95: // Aft Handicap
380 case 0xff: // bootloader
387 case TW_ST_SLA_ACK: // start read
388 case TW_ST_DATA_ACK: // read
390 case 0x02: // Motor 1 PWM
394 case 0x03: // Dummy to allow continous read
398 case 0x04: // Motor 2 PWM
402 case 0x05: // Dummy to allow continous read
406 case 0x06: // Motor 3 PWM
410 case 0x07: // Dummy to allow continous read
414 case 0x08: // Motor 4 PWM
418 case 0x09: // Dummy to allow continous read
422 case 0x10: // Hall 1 MSB
427 case 0x11: // Hall 1 LSB
431 case 0x12: // Hall 2 MSB
436 case 0x13: // Hall 2 LSB
440 case 0x14: // Hall 3 MSB
445 case 0x15: // Hall 3 LSB
449 case 0x16: // Hall 4 MSB
454 case 0x17: // Hall 4 LSB
458 case 0x20: // Motor 1 speed wish MSB
459 TWDR = speed1_wish>>8;
462 case 0x21: // Motor 1 speed wish LSB
466 case 0x22: // Motor 2 speed wish MSB
467 TWDR = speed2_wish>>8;
470 case 0x23: // Motor 2 speed wish LSB
474 case 0x24: // Motor 3 speed wish MSB
475 TWDR = speed3_wish>>8;
478 case 0x25: // Motor 3 speed wish LSB
482 case 0x26: // Motor 4 speed wish MSB
483 TWDR = speed4_wish>>8;
486 case 0x27: // Motor 4 speed wish LSB
490 case 0x30: // Motor 1 speed MSB
494 case 0x31: // Motor 1 speed LSB
498 case 0x32: // Motor 2 speed MSB
502 case 0x33: // Motor 2 speed LSB
506 case 0x34: // Motor 3 speed MSB
510 case 0x35: // Motor 3 speed LSB
514 case 0x36: // Motor 4 speed MSB
518 case 0x37: // Motor 4 speed LSB
522 case 0x38: // speed MSB
523 tmp_speed.f = cur_speed_lin;
524 TWDR = tmp_speed.i>>24;
528 TWDR = tmp_speed.i>>16;
532 TWDR = tmp_speed.i>>8;
535 case 0x3B: // speed LSB
539 case 0x3C: // angle MSB
540 tmp_angle.f = cur_speed_rot;
541 TWDR = tmp_angle.i>>24;
545 TWDR = tmp_angle.i>>16;
549 TWDR = tmp_angle.i>>8;
552 case 0x3F: // angle LSB
556 case 0x40: // Position x MSB
560 case 0x41: // Position x
564 case 0x42: // Position x
568 case 0x43: // Position x LSB
572 case 0x44: // Position y MSB
576 case 0x45: // Position y
580 case 0x46: // Position y
584 case 0x47: // Position y LSB
588 case 0x48: // Position angle MSB
592 case 0x49: // Position angle
596 case 0x4A: // Position angle
600 case 0x4B: // Position angle LSB
604 case 0xA0: // Reset reason
605 TWDR = MCUCSR & 0x0f;
609 case 0xA1: // Error status
613 case 0xA2: // count test
631 static void update_hall1(void) {
632 unsigned char status = (PINA >> 0) & 0x3;
633 static unsigned char oldstatus=0;
634 unsigned char diff, new;
640 new ^= 0x1; // convert gray to binary
641 diff = oldstatus - new; // difference last - new
642 if (diff & 0x1) { // bit 0 = value (1)
643 oldstatus = new; // store new as next last
644 if (motor1_switch) pos1 += (diff & 2) - 1; // bit 1 = direction (+/-)
645 else pos1 -= (diff & 2) - 1;
650 static void update_hall2(void) {
651 unsigned char status = (PINA >> 4) & 0x3;
652 static unsigned char oldstatus=0;
653 unsigned char diff, new;
659 new ^= 0x1; // convert gray to binary
660 diff = oldstatus - new; // difference last - new
661 if (diff & 0x1) { // bit 0 = value (1)
662 oldstatus = new; // store new as next last
663 if (motor2_switch) pos2 -= (diff & 2) - 1; // bit 1 = direction (+/-)
664 else pos2 += (diff & 2) - 1;
669 static void update_hall3(void) {
670 unsigned char status = (PINA >> 2) & 0x3;
671 static unsigned char oldstatus=0;
672 unsigned char diff, new;
678 new ^= 0x1; // convert gray to binary
679 diff = oldstatus - new; // difference last - new
680 if (diff & 0x1) { // bit 0 = value (1)
681 oldstatus = new; // store new as next last
682 if (motor3_switch) pos3 -= (diff & 2) - 1; // bit 1 = direction (+/-)
683 else pos3 += (diff & 2) - 1;
688 static void update_hall4(void) {
689 unsigned char status = (PINA >> 6) & 0x3;
690 static unsigned char oldstatus=0;
691 unsigned char diff, new;
697 new ^= 0x1; // convert gray to binary
698 diff = oldstatus - new; // difference last - new
699 if (diff & 0x1) { // bit 0 = value (1)
700 oldstatus = new; // store new as next last
701 if (motor4_switch) pos4 += (diff & 2) - 1; // bit 1 = direction (+/-)
702 else pos4 -= (diff & 2) - 1;
707 static void update_motor(void) {
708 static int16_t m1_old=SHRT_MIN;
709 static int16_t m2_old=SHRT_MIN;
710 static int16_t m3_old=SHRT_MIN;
711 static int16_t m4_old=SHRT_MIN;
713 error_state &= 0xf0; // clear lower bits
714 error_state |= ~((PIND & 0x40)>>3 | (PINB & 0x07)) & 0xf;
716 if (m1_old != motor1) { // update only when changed
719 PORTC &= ~(1 << 3) & ~(1 << 2);
721 } else if (motor1 == PWM_BREAK) {
722 PORTC |= (1 << 3) | (1 << 2);
724 } else if ((!motor1_switch && motor1 > 0) || (motor1_switch && motor1 < 0)) {
731 } else { // motor1 < 0
744 if (m2_old != motor2) { // update only when changed
747 PORTC &= ~(1 << 5) & ~(1 << 4);
749 } else if (motor2 == PWM_BREAK) {
750 PORTC |= (1 << 5) | (1 << 4);
752 } else if ((!motor2_switch && motor2 > 0) || (motor2_switch && motor2 < 0)) {
759 } else { // motor2 < 0
772 if (m3_old != motor3) { // update only when changed
775 PORTC &= ~(1 << 7) & ~(1 << 6);
777 } else if (motor3 == PWM_BREAK) {
778 PORTC |= (1 << 7) | (1 << 6);
780 } else if ((!motor3_switch && motor3 > 0) || (motor3_switch && motor3 < 0)) {
787 } else { // motor3 < 0
800 if (m4_old != motor4) { // update only when changed
803 PORTD &= ~(1 << 3) & ~(1 << 2);
805 } else if (motor4 == PWM_BREAK) {
806 PORTD |= (1 << 3) | (1 << 2);
808 } else if ((!motor4_switch && motor4 > 0) || (motor4_switch && motor4 < 0)) {
815 } else { // motor4 < 0
830 static void update_pos(void) {
831 static int16_t pos1_last=0;
832 static int16_t pos2_last=0;
833 static int16_t pos3_last=0;
834 static int16_t pos4_last=0;
835 int16_t pos1_diff; // steps
839 float diff_left_m, diff_right_m, angle_diff, translation;
840 float pos_x_new, pos_y_new, angle_new;
841 float tmp_speed_lin, tmp_speed_rot;
842 int16_t cur_pos1, cur_pos2, cur_pos3, cur_pos4;
843 int16_t new_speed1, new_speed2, new_speed3, new_speed4;
853 pos1_diff = cur_pos1 - pos1_last;
854 pos2_diff = cur_pos2 - pos2_last;
855 pos3_diff = cur_pos3 - pos3_last;
856 pos4_diff = cur_pos4 - pos4_last;
858 new_speed1 = pos1_diff/PID_T;
859 new_speed2 = pos2_diff/PID_T;
860 new_speed3 = pos3_diff/PID_T;
861 new_speed4 = pos4_diff/PID_T;
863 diff_left_m = (pos1_diff + pos2_diff)/(2*STEP_PER_M_LEFT);
864 diff_right_m = (pos3_diff + pos4_diff)/(2*STEP_PER_M_RIGHT);
865 angle_diff = (diff_right_m - diff_left_m) / WHEEL_DIST;
867 angle_new = angle.f + angle_diff;
868 if (angle_new > 2*M_PI) angle_new-=2*M_PI;
869 else if (angle_new < -2*M_PI) angle_new+=2*M_PI;
871 translation = (diff_left_m + diff_right_m)/2.0;
872 pos_x_new = pos_x.f + cos(angle_new)*translation;
873 pos_y_new = pos_y.f + sin(angle_new)*translation;
875 tmp_speed_lin = translation/PID_T;
876 tmp_speed_rot = angle_diff/PID_T;
887 cur_speed_lin = tmp_speed_lin;
888 cur_speed_rot = tmp_speed_rot;
891 pos1_last = cur_pos1;
892 pos2_last = cur_pos2;
893 pos3_last = cur_pos3;
894 pos4_last = cur_pos4;
898 static void update_pid(void) {
899 static int16_t eold1=0;
900 static int16_t eold2=0;
901 static int16_t eold3=0;
902 static int16_t eold4=0;
903 static int32_t esum1=0;
904 static int32_t esum2=0;
905 static int32_t esum3=0;
906 static int32_t esum4=0;
908 // protect motors from damage if stalling
909 if (labs(esum1) > STALL_LIMIT && speed1 == 0) {
911 motor1_mode = MOTOR_MANUAL;
912 error_state |= (1<<4);
915 if (labs(esum2) > STALL_LIMIT && speed2 == 0) {
917 motor2_mode = MOTOR_MANUAL;
918 error_state |= (1<<5);
921 if (labs(esum3) > STALL_LIMIT && speed3 == 0) {
923 motor3_mode = MOTOR_MANUAL;
924 error_state |= (1<<6);
927 if (labs(esum4) > STALL_LIMIT && speed4 == 0) {
929 motor4_mode = MOTOR_MANUAL;
930 error_state |= (1<<7);
934 if (motor1_mode == MOTOR_PID) {
935 if (speed1_wish != speed1_wish_old) {
936 if (abs(speed1_wish - speed1_wish_old) > 500) esum1 = 0;
937 speed1_wish_old = speed1_wish;
940 if (speed1_wish == 0) {
943 error_state &= ~(1<<4);
945 int16_t e = speed1_wish - speed1;
947 motor1 = KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
950 if (motor1 > 0 && speed1_wish < 0) motor1=PWM_BREAK;
951 else if (motor1 < 0 && speed1_wish > 0) motor1=PWM_BREAK;
952 else if (motor1 > 255) motor1 = 255;
953 else if (motor1 < -255) motor1 = -255;
956 if (motor2_mode == MOTOR_PID) {
957 if (speed2_wish != speed2_wish_old) {
958 if (abs(speed2_wish - speed2_wish_old) > 500) esum2 = 0;
959 speed2_wish_old = speed2_wish;
962 if (speed2_wish == 0) {
965 error_state &= ~(1<<5);
967 int16_t e = speed2_wish - speed2;
969 motor2 = KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
972 if (motor2 > 0 && speed2_wish < 0) motor2=PWM_BREAK;
973 else if (motor2 < 0 && speed2_wish > 0) motor2=PWM_BREAK;
974 else if (motor2 > 255) motor2 = 255;
975 else if (motor2 < -255) motor2 = -255;
978 if (motor3_mode == MOTOR_PID) {
979 if (speed3_wish != speed3_wish_old) {
980 if (abs(speed3_wish - speed3_wish_old) > 500) esum3 = 0;
981 speed3_wish_old = speed3_wish;
984 if (speed3_wish == 0) {
987 error_state &= ~(1<<6);
989 int16_t e = speed3_wish - speed3;
991 motor3 = KP*e + KI*PID_T*esum3 + KD/PID_T*(e - eold3);
994 if (motor3 > 0 && speed3_wish < 0) motor3=PWM_BREAK;
995 else if (motor3 < 0 && speed3_wish > 0) motor3=PWM_BREAK;
996 else if (motor3 > 255) motor3 = 255;
997 else if (motor3 < -255) motor3 = -255;
1000 if (motor4_mode == MOTOR_PID) {
1001 if (speed4_wish != speed4_wish_old) {
1002 if (abs(speed4_wish - speed4_wish_old) > 500) esum4 = 0;
1003 speed4_wish_old = speed4_wish;
1006 if (speed4_wish == 0) {
1009 error_state &= ~(1<<7);
1011 int16_t e = speed4_wish - speed4;
1013 motor4 = KP*e + KI*PID_T*esum4 + KD/PID_T*(e - eold4);
1016 if (motor4 > 0 && speed4_wish < 0) motor4=PWM_BREAK;
1017 else if (motor4 < 0 && speed4_wish > 0) motor4=PWM_BREAK;
1018 else if (motor4 > 255) motor4 = 255;
1019 else if (motor4 < -255) motor4 = -255;
1025 ISR(TIMER1_OVF_vect) {
1038 DDRC = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
1039 DDRD = (1 << 7) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
1040 // Pullup Diag/Enable
1041 PORTB = (1 << 0) | (1 << 1) | (1 << 2);
1046 uart_setup_stdout();
1053 // Also used for PWM frequency TIMER1_FREQ (F_CPU/256)
1054 // Timer 1: Fast PWM non-inverting mode, Top=255 => 19.531kHz
1056 //TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10);
1057 // Avoid narrow spike on extreme pwm value 0 by not setting COM1*1
1058 TCCR1A = (1 << WGM10);
1059 TCCR1B = (1 << WGM12) | (1 << CS10);
1064 // Timer 2: Fast PWM non-inverting mode, Top=255
1066 //TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << COM21) | (1 << CS20);
1067 // Avoid narrow spike on extreme pwm value 0 by not setting COM21
1068 TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << CS20);
1072 // Timer 0: Fast PWM non-inverting mode, Top=255
1074 //TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << COM01) | (1 << CS00);
1075 // Avoid narrow spike on extreme pwm value 0 by not setting COM01
1076 TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << CS00);
1079 printf("\r\nStart\r\n");
1081 set_sleep_mode(SLEEP_MODE_IDLE);
1082 // Enable Timer 1 Overflow Interrupt
1083 TIMSK = (1 << TOIE1);
1088 case 0xff: // Magic reg that starts the bootloader
1089 if (bootloader == 0xa5) {
1091 // write mark to first area in eeprom
1092 eeprom_write_byte((uint8_t*)0, 123);
1094 // Use watchdog to restart
1095 wdt_enable(WDTO_15MS);
1100 if (cmd_vel.bUpdate) {
1101 float speed_wish_right, speed_wish_left;
1105 speed = cmd_vel.speed;
1106 angle = cmd_vel.angle;
1107 cmd_vel.bUpdate = 0;
1110 speed_wish_right = (angle*WHEEL_DIST)/2 + speed;
1111 speed_wish_left = speed*2-speed_wish_right;
1113 speed_wish_left*=STEP_PER_M_LEFT;
1114 speed_wish_right*=STEP_PER_M_RIGHT;
1116 if (aft_handicap > 0) {
1117 speed1_wish = speed_wish_left * (100-aft_handicap)/100.0;
1118 speed3_wish = speed_wish_right * (100-aft_handicap)/100.0;
1120 speed1_wish = speed_wish_left;
1121 speed3_wish = speed_wish_right;
1123 if (front_handicap > 0) {
1124 speed2_wish = speed_wish_left * (100-front_handicap)/100.0;
1125 speed4_wish = speed_wish_right * (100-front_handicap)/100.0;
1127 speed2_wish = speed_wish_left;
1128 speed4_wish = speed_wish_right;
1130 motor1_mode = MOTOR_PID;
1131 motor2_mode = MOTOR_PID;
1132 motor3_mode = MOTOR_PID;
1133 motor4_mode = MOTOR_PID;
1136 if (run_update >= 195) { // TIMER1_FREQ/195 = ~100Hz