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
93 * 0xA1 TLE Error status
100 #define TWI_ACK TWCR = (1<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
101 #define TWI_RESET TWCR &= ~((1 << TWSTO) | (1 << TWEN)); TWI_ACK
102 #define TWI_NAK TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
108 // wheel diameter=12cm, encoder=48cpr, gear ratio=1:34, real wheel diameter: 0.12454m
109 #define STEP_PER_M_AVG 4171.4
110 #define STEP_PER_M_LEFT (STEP_PER_M_AVG)
111 #define STEP_PER_M_RIGHT (STEP_PER_M_AVG)
112 #define WHEEL_DIST 0.36923 // Real: 0.252
124 static volatile struct {
128 } cmd_vel = {0, 0, 0};
130 static volatile uint8_t ireg=0;
131 static volatile uint8_t bootloader=0;
132 static volatile int16_t motor1=0; // -255..+255
133 static volatile int16_t motor2=0;
134 static volatile int16_t motor3=0;
135 static volatile int16_t motor4=0;
136 static volatile int16_t pos1=0; // step
137 static volatile int16_t pos2=0;
138 static volatile int16_t pos3=0;
139 static volatile int16_t pos4=0;
140 static volatile enum mode motor1_mode=MOTOR_MANUAL;
141 static volatile enum mode motor2_mode=MOTOR_MANUAL;
142 static volatile enum mode motor3_mode=MOTOR_MANUAL;
143 static volatile enum mode motor4_mode=MOTOR_MANUAL;
144 static volatile uint8_t motor1_switch=0;
145 static volatile uint8_t motor2_switch=0;
146 static volatile uint8_t motor3_switch=0;
147 static volatile uint8_t motor4_switch=0;
148 static volatile int16_t speed1_wish=0; // step/s
149 static volatile int16_t speed2_wish=0;
150 static volatile int16_t speed3_wish=0;
151 static volatile int16_t speed4_wish=0;
152 static volatile uint8_t run_update=0;
153 static volatile int16_t speed1=0; // step/s
154 static volatile int16_t speed2=0;
155 static volatile int16_t speed3=0;
156 static volatile int16_t speed4=0;
157 static volatile ufloat_t pos_x={0.0};
158 static volatile ufloat_t pos_y={0.0};
159 static volatile ufloat_t angle={0.0};
160 static volatile float cur_speed_lin=0;
161 static volatile float cur_speed_rot=0;
162 static volatile uint8_t count_test=0;
163 static volatile uint8_t front_handicap=0;
164 static volatile uint8_t aft_handicap=0;
165 static volatile uint8_t error_state=0;
169 static uint8_t tmp=0;
170 static int16_t tmp16=0;
171 static ufloat_t tmp_speed;
172 static ufloat_t tmp_angle;
176 case 0x60: // start write
182 case 0x00: // register select
184 ireg--; // because we do ireg++ below
187 case 0x01: // Motor 1 MSB
191 case 0x02: // Motor 1 LSB
192 motor1 = tmp<<8 | TWDR;
193 motor1_mode = MOTOR_MANUAL;
196 case 0x03: // Motor 2 MSB
200 case 0x04: // Motor 2 LSB
201 motor2 = tmp<<8 | TWDR;
202 motor2_mode = MOTOR_MANUAL;
205 case 0x05: // Motor 3 MSB
209 case 0x06: // Motor 3 LSB
210 motor3 = tmp<<8 | TWDR;
211 motor3_mode = MOTOR_MANUAL;
214 case 0x07: // Motor 4 MSB
218 case 0x08: // Motor 4 LSB
219 motor4 = tmp<<8 | TWDR;
220 motor4_mode = MOTOR_MANUAL;
223 case 0x20: // Motor 1 speed wish MSB
227 case 0x21: // Motor 1 speed wish LSB
228 speed1_wish = tmp<<8 | TWDR;
229 motor1_mode = MOTOR_PID;
232 case 0x22: // Motor 2 speed wish MSB
236 case 0x23: // Motor 2 speed wish LSB
237 speed2_wish = tmp<<8 | TWDR;
238 motor2_mode = MOTOR_PID;
241 case 0x24: // Motor 3 speed wish MSB
245 case 0x25: // Motor 3 speed wish LSB
246 speed3_wish = tmp<<8 | TWDR;
247 motor3_mode = MOTOR_PID;
250 case 0x26: // Motor 4 speed wish MSB
254 case 0x27: // Motor 4 speed wish LSB
255 speed4_wish = tmp<<8 | TWDR;
256 motor4_mode = MOTOR_PID;
259 case 0x28: // Left speed wish MSB
263 case 0x29: // Left speed wish
264 tmp_speed.i = tmp_speed.i << 8 | TWDR;
267 case 0x2A: // Left speed wish
268 tmp_speed.i = tmp_speed.i << 8 | TWDR;
271 case 0x2B: // Left speed wish LSB
272 tmp_speed.i = tmp_speed.i << 8 | TWDR;
273 speed1_wish = tmp_speed.f*STEP_PER_M_LEFT;
274 speed2_wish = tmp_speed.f*STEP_PER_M_LEFT;
275 motor1_mode = MOTOR_PID;
276 motor2_mode = MOTOR_PID;
279 case 0x2C: // Right speed wish MSB
283 case 0x2D: // Right speed wish
284 tmp_speed.i = tmp_speed.i << 8 | TWDR;
287 case 0x2E: // Right speed wish
288 tmp_speed.i = tmp_speed.i << 8 | TWDR;
291 case 0x2F: // Right speed wish LSB
292 tmp_speed.i = tmp_speed.i << 8 | TWDR;
293 speed1_wish = tmp_speed.f*STEP_PER_M_RIGHT;
294 speed2_wish = tmp_speed.f*STEP_PER_M_RIGHT;
295 motor1_mode = MOTOR_PID;
296 motor2_mode = MOTOR_PID;
299 case 0x50: // speed wish MSB
303 case 0x51: // speed wish
304 tmp_speed.i = tmp_speed.i << 8 | TWDR;
307 case 0x52: // speed wish
308 tmp_speed.i = tmp_speed.i << 8 | TWDR;
311 case 0x53: // speed wish LSB
312 tmp_speed.i = tmp_speed.i << 8 | TWDR;
313 cmd_vel.speed = tmp_speed.f;
316 case 0x54: // angle wish MSB
320 case 0x55: // angle wish
321 tmp_angle.i = tmp_angle.i << 8 | TWDR;
324 case 0x56: // angle wish
325 tmp_angle.i = tmp_angle.i << 8 | TWDR;
328 case 0x57: // angle wish LSB
329 tmp_angle.i = tmp_angle.i << 8 | TWDR;
330 cmd_vel.angle = tmp_angle.f;
334 case 0x90: // Motor 1 switch
335 motor1_switch = TWDR;
338 case 0x91: // Motor 2 switch
339 motor2_switch = TWDR;
342 case 0x92: // Motor 3 switch
343 motor3_switch = TWDR;
346 case 0x93: // Motor 4 switch
347 motor4_switch = TWDR;
350 case 0x94: // Front Handicap
351 front_handicap = TWDR;
355 case 0x95: // Aft Handicap
360 case 0xff: // bootloader
367 case 0xA8: // start read
370 case 0x02: // Motor 1 PWM
374 case 0x04: // Motor 2 PWM
378 case 0x06: // Motor 3 PWM
382 case 0x08: // Motor 4 PWM
386 case 0x10: // Hall 1 MSB
391 case 0x11: // Hall 1 LSB
395 case 0x12: // Hall 2 MSB
400 case 0x13: // Hall 2 LSB
404 case 0x14: // Hall 3 MSB
409 case 0x15: // Hall 3 LSB
413 case 0x16: // Hall 4 MSB
418 case 0x17: // Hall 4 LSB
422 case 0x20: // Motor 1 speed wish MSB
423 TWDR = speed1_wish>>8;
426 case 0x21: // Motor 1 speed wish LSB
430 case 0x22: // Motor 2 speed wish MSB
431 TWDR = speed2_wish>>8;
434 case 0x23: // Motor 2 speed wish LSB
438 case 0x24: // Motor 3 speed wish MSB
439 TWDR = speed3_wish>>8;
442 case 0x25: // Motor 3 speed wish LSB
446 case 0x26: // Motor 4 speed wish MSB
447 TWDR = speed4_wish>>8;
450 case 0x27: // Motor 4 speed wish LSB
454 case 0x30: // Motor 1 speed MSB
458 case 0x31: // Motor 1 speed LSB
462 case 0x32: // Motor 2 speed MSB
466 case 0x33: // Motor 2 speed LSB
470 case 0x34: // Motor 3 speed MSB
474 case 0x35: // Motor 3 speed LSB
478 case 0x36: // Motor 4 speed MSB
482 case 0x37: // Motor 4 speed LSB
486 case 0x38: // speed MSB
487 tmp_speed.f = cur_speed_lin;
488 TWDR = tmp_speed.i>>24;
492 TWDR = tmp_speed.i>>16;
496 TWDR = tmp_speed.i>>8;
499 case 0x3B: // speed LSB
503 case 0x3C: // angle MSB
504 tmp_angle.f = cur_speed_rot;
505 TWDR = tmp_angle.i>>24;
509 TWDR = tmp_angle.i>>16;
513 TWDR = tmp_angle.i>>8;
516 case 0x3F: // angle LSB
520 case 0x40: // Position x MSB
524 case 0x41: // Position x
528 case 0x42: // Position x
532 case 0x43: // Position x LSB
536 case 0x44: // Position y MSB
540 case 0x45: // Position y
544 case 0x46: // Position y
548 case 0x47: // Position y LSB
552 case 0x48: // Position angle MSB
556 case 0x49: // Position angle
560 case 0x4A: // Position angle
564 case 0x4B: // Position angle LSB
568 case 0xA0: // Reset reason
569 TWDR = MCUCSR & 0x0f;
573 case 0xA1: // TLE Error status
577 case 0xA2: // count test
592 static void update_hall1(void) {
593 unsigned char status = (PINA >> 0) & 0x3;
594 static unsigned char oldstatus=0;
595 unsigned char diff, new;
601 new ^= 0x1; // convert gray to binary
602 diff = oldstatus - new; // difference last - new
603 if (diff & 0x1) { // bit 0 = value (1)
604 oldstatus = new; // store new as next last
605 if (motor1_switch) pos1 += (diff & 2) - 1; // bit 1 = direction (+/-)
606 else pos1 -= (diff & 2) - 1;
611 static void update_hall2(void) {
612 unsigned char status = (PINA >> 4) & 0x3;
613 static unsigned char oldstatus=0;
614 unsigned char diff, new;
620 new ^= 0x1; // convert gray to binary
621 diff = oldstatus - new; // difference last - new
622 if (diff & 0x1) { // bit 0 = value (1)
623 oldstatus = new; // store new as next last
624 if (motor2_switch) pos2 -= (diff & 2) - 1; // bit 1 = direction (+/-)
625 else pos2 += (diff & 2) - 1;
630 static void update_hall3(void) {
631 unsigned char status = (PINA >> 2) & 0x3;
632 static unsigned char oldstatus=0;
633 unsigned char diff, new;
639 new ^= 0x1; // convert gray to binary
640 diff = oldstatus - new; // difference last - new
641 if (diff & 0x1) { // bit 0 = value (1)
642 oldstatus = new; // store new as next last
643 if (motor3_switch) pos3 -= (diff & 2) - 1; // bit 1 = direction (+/-)
644 else pos3 += (diff & 2) - 1;
649 static void update_hall4(void) {
650 unsigned char status = (PINA >> 6) & 0x3;
651 static unsigned char oldstatus=0;
652 unsigned char diff, new;
658 new ^= 0x1; // convert gray to binary
659 diff = oldstatus - new; // difference last - new
660 if (diff & 0x1) { // bit 0 = value (1)
661 oldstatus = new; // store new as next last
662 if (motor4_switch) pos4 += (diff & 2) - 1; // bit 1 = direction (+/-)
663 else pos4 -= (diff & 2) - 1;
668 static void update_motor(void) {
669 static int16_t m1_old=SHRT_MIN;
670 static int16_t m2_old=SHRT_MIN;
671 static int16_t m3_old=SHRT_MIN;
672 static int16_t m4_old=SHRT_MIN;
674 error_state = ~((PIND & 0x40)>>3 | (PINB & 0x07)) & 0xf;
676 if (motor1_mode == MOTOR_PID && bit_is_set(error_state, 0)) {
677 // if error and running: stop
678 if (m1_old != 0) motor1 = 0;
679 // if we start motor in error state: start with full power
680 else if (motor1 > 0) motor1 = 255;
681 else if (motor1 < 0) motor1 = -255;
683 if (motor2_mode == MOTOR_PID && bit_is_set(error_state, 1)) {
684 // if error and running: stop
685 if (m2_old != 0) motor2 = 0;
686 // if we start motor in error state: start with full power
687 else if (motor2 > 0) motor2 = 255;
688 else if (motor2 < 0) motor2 = -255;
690 if (motor3_mode == MOTOR_PID && bit_is_set(error_state, 2)) {
691 // if error and running: stop
692 if (m3_old != 0) motor3 = 0;
693 // if we start motor in error state: start with full power
694 else if (motor3 > 0) motor3 = 255;
695 else if (motor3 < 0) motor3 = -255;
697 if (motor4_mode == MOTOR_PID && bit_is_set(error_state, 3)) {
698 // if error and running: stop
699 if (m4_old != 0) motor4 = 0;
700 // if we start motor in error state: start with full power
701 else if (motor4 > 0) motor4 = 255;
702 else if (motor4 < 0) motor4 = -255;
705 if (m1_old != motor1) { // update only when changed
708 PORTC |= (1 << 3) | (1 << 2);
709 } else if ((!motor1_switch && motor1 > 0) || (motor1_switch && motor1 < 0)) {
711 PORTC &= ~(1 << 3) & ~(1 << 2);
712 } else { // motor1 < 0
722 if (m2_old != motor2) { // update only when changed
725 PORTC |= (1 << 5) | (1 << 4);
726 } else if ((!motor2_switch && motor2 > 0) || (motor2_switch && motor2 < 0)) {
728 PORTC &= ~(1 << 5) & ~(1 << 4);
729 } else { // motor2 < 0
739 if (m3_old != motor3) { // update only when changed
742 PORTC |= (1 << 7) | (1 << 6);
743 } else if ((!motor3_switch && motor3 > 0) || (motor3_switch && motor3 < 0)) {
745 PORTC &= ~(1 << 7) & ~(1 << 6);
746 } else { // motor3 < 0
756 if (m4_old != motor4) { // update only when changed
759 PORTD |= (1 << 3) | (1 << 2);
760 } else if ((!motor4_switch && motor4 > 0) || (motor4_switch && motor4 < 0)) {
762 PORTD &= ~(1 << 3) & ~(1 << 2);
763 } else { // motor4 < 0
775 static void update_pos(void) {
776 static int16_t pos1_last=0;
777 static int16_t pos2_last=0;
778 static int16_t pos3_last=0;
779 static int16_t pos4_last=0;
780 int16_t pos1_diff; // steps
784 float diff_left_m, diff_right_m, angle_diff, translation;
785 float pos_x_new, pos_y_new, angle_new;
786 int16_t speed_l, speed_r;
787 float tmp_speed_lin, tmp_speed_rot;
788 int16_t cur_pos1, cur_pos2, cur_pos3, cur_pos4;
789 int16_t new_speed1, new_speed2, new_speed3, new_speed4;
799 pos1_diff = cur_pos1 - pos1_last;
800 pos2_diff = cur_pos2 - pos2_last;
801 pos3_diff = cur_pos3 - pos3_last;
802 pos4_diff = cur_pos4 - pos4_last;
804 new_speed1 = pos1_diff/PID_T;
805 new_speed2 = pos2_diff/PID_T;
806 new_speed3 = pos3_diff/PID_T;
807 new_speed4 = pos4_diff/PID_T;
809 diff_left_m = (pos1_diff + pos2_diff)/(2*STEP_PER_M_LEFT);
810 diff_right_m = (pos3_diff + pos4_diff)/(2*STEP_PER_M_RIGHT);
811 angle_diff = (diff_right_m - diff_left_m) / WHEEL_DIST;
813 angle_new = angle.f + angle_diff;
814 if (angle_new > 2*M_PI) angle_new-=2*M_PI;
815 else if (angle_new < -2*M_PI) angle_new+=2*M_PI;
817 translation = (diff_left_m + diff_right_m)/2.0;
818 pos_x_new = pos_x.f + cos(angle_new)*translation;
819 pos_y_new = pos_y.f + sin(angle_new)*translation;
821 speed_l = (new_speed1+new_speed2)/2;
822 speed_r = (new_speed3+new_speed4)/2;
823 tmp_speed_lin = (speed_l + speed_r)/(2.0*STEP_PER_M_AVG);
824 tmp_speed_rot = (speed_r - speed_l)/(M_PI*WHEEL_DIST*STEP_PER_M_AVG);
835 cur_speed_lin = tmp_speed_lin;
836 cur_speed_rot = tmp_speed_rot;
839 pos1_last = cur_pos1;
840 pos2_last = cur_pos2;
841 pos3_last = cur_pos3;
842 pos4_last = cur_pos4;
846 static void update_pid(void) {
847 static int16_t eold1=0;
848 static int16_t eold2=0;
849 static int16_t eold3=0;
850 static int16_t eold4=0;
851 static int32_t esum1=0;
852 static int32_t esum2=0;
853 static int32_t esum3=0;
854 static int32_t esum4=0;
856 if (motor1_mode == MOTOR_PID) {
857 if (speed1_wish == 0) {
862 int16_t e = speed1_wish - speed1;
864 motor1 = KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
867 if (motor1 > 255) motor1 = 255;
868 else if (motor1 < -255) motor1 = -255;
871 if (motor2_mode == MOTOR_PID) {
872 if (speed2_wish == 0) {
877 int16_t e = speed2_wish - speed2;
879 motor2 = KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
882 if (motor2 > 255) motor2 = 255;
883 else if (motor2 < -255) motor2 = -255;
886 if (motor3_mode == MOTOR_PID) {
887 if (speed3_wish == 0) {
892 int16_t e = speed3_wish - speed3;
894 motor3 = KP*e + KI*PID_T*esum3 + KD/PID_T*(e - eold3);
897 if (motor3 > 255) motor3 = 255;
898 else if (motor3 < -255) motor3 = -255;
901 if (motor4_mode == MOTOR_PID) {
902 if (speed4_wish == 0) {
907 int16_t e = speed4_wish - speed4;
909 motor4 = KP*e + KI*PID_T*esum4 + KD/PID_T*(e - eold4);
912 if (motor4 > 255) motor4 = 255;
913 else if (motor4 < -255) motor4 = -255;
919 ISR(TIMER1_OVF_vect) {
932 DDRC = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
933 DDRD = (1 << 7) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
935 PORTB = (1 << 0) | (1 << 1) | (1 << 2);
947 // Timer 1: Fast PWM inverting mode, Top=256 => 15.625kHz
949 TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << COM1A0) | (1 << COM1B0) | (1 << WGM10);
950 TCCR1B = (1 << WGM12) | (1 << CS10);
955 // Timer 2: Fast PWM inverting mode, Top=256
957 TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << COM21) | (1 << COM20) | (1 << CS20);
961 // Timer 0: Fast PWM inverting mode, Top=256
963 TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << COM01) | (1 << COM00) | (1 << CS00);
966 printf("\r\nStart\r\n");
968 set_sleep_mode(SLEEP_MODE_IDLE);
969 // Enable Timer 1 Overflow Interrupt
970 TIMSK = (1 << TOIE1);
975 case 0xff: // Magic reg that starts the bootloader
976 if (bootloader == 0xa5) {
979 void (*start)(void) = (void*)0x1800;
986 if (cmd_vel.bUpdate) {
987 float speed_wish_right, speed_wish_left;
991 speed = cmd_vel.speed;
992 angle = cmd_vel.angle;
996 speed_wish_right = angle*M_PI*WHEEL_DIST/2 + speed;
997 speed_wish_left = speed*2-speed_wish_right;
999 speed_wish_left*=STEP_PER_M_LEFT;
1000 speed_wish_right*=STEP_PER_M_RIGHT;
1002 if (aft_handicap > 0) {
1003 speed1_wish = speed_wish_left * (100-aft_handicap)/100.0;
1004 speed4_wish = speed_wish_right * (100-aft_handicap)/100.0;
1006 speed1_wish = speed_wish_left;
1007 speed4_wish = speed_wish_right;
1009 if (front_handicap > 0) {
1010 speed2_wish = speed_wish_left * (100-front_handicap)/100.0;
1011 speed3_wish = speed_wish_right * (100-front_handicap)/100.0;
1013 speed2_wish = speed_wish_left;
1014 speed3_wish = speed_wish_right;
1016 motor1_mode = MOTOR_PID;
1017 motor2_mode = MOTOR_PID;
1018 motor3_mode = MOTOR_PID;
1019 motor4_mode = MOTOR_PID;
1022 if (run_update >= 156) { // ~100Hz