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
64 * 0x40 Position x (m) MSB
67 * 0x43 Position x (m) LSB
68 * 0x44 Position y (m) MSB
71 * 0x47 Position y (m) LSB
72 * 0x48 Position angle MSB
75 * 0x4B Position angle LSB
77 * 0x50 speed wish (m/s) MSB
78 * 0x51 speed wish (m/s)
79 * 0x52 speed wish (m/s)
80 * 0x53 speed wish (m/s) LSB
81 * 0x54 angle wish (rad/s) MSB
82 * 0x55 angle wish (rad/s)
83 * 0x56 angle wish (rad/s)
84 * 0x57 angle wish (rad/s) LSB
94 * 0xA1 TLE Error status
101 #define TWI_ACK TWCR = (1<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
102 #define TWI_RESET TWCR &= ~((1 << TWSTO) | (1 << TWEN)); TWI_ACK
103 #define TWI_NAK TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
109 #define STEP_PER_M 3376.1 // wheel diameter=12cm, encoder=48cpr, gear ratio=1:34
110 #define WHEEL_DIST 0.252
122 static volatile struct {
126 } cmd_vel = {0, 0, 0};
128 static volatile uint8_t ireg=0;
129 static volatile uint8_t bootloader=0;
130 static volatile int16_t motor1=0; // -255..+255
131 static volatile int16_t motor2=0;
132 static volatile int16_t motor3=0;
133 static volatile int16_t motor4=0;
134 static volatile int16_t pos1=0; // step
135 static volatile int16_t pos2=0;
136 static volatile int16_t pos3=0;
137 static volatile int16_t pos4=0;
138 static volatile enum mode motor1_mode=MOTOR_MANUAL;
139 static volatile enum mode motor2_mode=MOTOR_MANUAL;
140 static volatile enum mode motor3_mode=MOTOR_MANUAL;
141 static volatile enum mode motor4_mode=MOTOR_MANUAL;
142 static volatile uint8_t motor1_switch=0;
143 static volatile uint8_t motor2_switch=0;
144 static volatile uint8_t motor3_switch=0;
145 static volatile uint8_t motor4_switch=0;
146 static volatile int16_t speed1_wish=0; // step/s
147 static volatile int16_t speed2_wish=0;
148 static volatile int16_t speed3_wish=0;
149 static volatile int16_t speed4_wish=0;
150 static volatile uint8_t run_update=0;
151 static volatile int16_t speed1=0; // step/s
152 static volatile int16_t speed2=0;
153 static volatile int16_t speed3=0;
154 static volatile int16_t speed4=0;
155 static volatile ufloat_t pos_x={0.0};
156 static volatile ufloat_t pos_y={0.0};
157 static volatile ufloat_t angle={0.0};
158 static volatile float cur_speed_lin=0;
159 static volatile float cur_speed_rot=0;
160 static volatile uint8_t count_test=0;
161 static volatile uint8_t front_handicap=0;
162 static volatile uint8_t aft_handicap=0;
166 static uint8_t tmp=0;
167 static int16_t tmp16=0;
168 static ufloat_t tmp_speed;
169 static ufloat_t tmp_angle;
173 case 0x60: // start write
179 case 0x00: // register select
181 ireg--; // because we do ireg++ below
184 case 0x01: // Motor 1 MSB
188 case 0x02: // Motor 1 LSB
189 motor1 = tmp<<8 | TWDR;
190 motor1_mode = MOTOR_MANUAL;
193 case 0x03: // Motor 2 MSB
197 case 0x04: // Motor 2 LSB
198 motor2 = tmp<<8 | TWDR;
199 motor2_mode = MOTOR_MANUAL;
202 case 0x05: // Motor 3 MSB
206 case 0x06: // Motor 3 LSB
207 motor3 = tmp<<8 | TWDR;
208 motor3_mode = MOTOR_MANUAL;
211 case 0x07: // Motor 4 MSB
215 case 0x08: // Motor 4 LSB
216 motor4 = tmp<<8 | TWDR;
217 motor4_mode = MOTOR_MANUAL;
220 case 0x20: // Motor 1 speed wish MSB
224 case 0x21: // Motor 1 speed wish LSB
225 speed1_wish = tmp<<8 | TWDR;
226 motor1_mode = MOTOR_PID;
229 case 0x22: // Motor 2 speed wish MSB
233 case 0x23: // Motor 2 speed wish LSB
234 speed2_wish = tmp<<8 | TWDR;
235 motor2_mode = MOTOR_PID;
238 case 0x24: // Motor 3 speed wish MSB
242 case 0x25: // Motor 3 speed wish LSB
243 speed3_wish = tmp<<8 | TWDR;
244 motor3_mode = MOTOR_PID;
247 case 0x26: // Motor 4 speed wish MSB
251 case 0x27: // Motor 4 speed wish LSB
252 speed4_wish = tmp<<8 | TWDR;
253 motor4_mode = MOTOR_PID;
256 case 0x28: // Left speed wish MSB
260 case 0x29: // Left speed wish
261 tmp_speed.i = tmp_speed.i << 8 | TWDR;
264 case 0x2A: // Left speed wish
265 tmp_speed.i = tmp_speed.i << 8 | TWDR;
268 case 0x2B: // Left speed wish LSB
269 tmp_speed.i = tmp_speed.i << 8 | TWDR;
270 speed1_wish = tmp_speed.f*STEP_PER_M;
271 speed2_wish = tmp_speed.f*STEP_PER_M;
272 motor1_mode = MOTOR_PID;
273 motor2_mode = MOTOR_PID;
276 case 0x2C: // Right speed wish MSB
280 case 0x2D: // Right speed wish
281 tmp_speed.i = tmp_speed.i << 8 | TWDR;
284 case 0x2E: // Right speed wish
285 tmp_speed.i = tmp_speed.i << 8 | TWDR;
288 case 0x2F: // Right speed wish LSB
289 tmp_speed.i = tmp_speed.i << 8 | TWDR;
290 speed1_wish = tmp_speed.f*STEP_PER_M;
291 speed2_wish = tmp_speed.f*STEP_PER_M;
292 motor1_mode = MOTOR_PID;
293 motor2_mode = MOTOR_PID;
296 case 0x50: // speed wish MSB
300 case 0x51: // speed wish
301 tmp_speed.i = tmp_speed.i << 8 | TWDR;
304 case 0x52: // speed wish
305 tmp_speed.i = tmp_speed.i << 8 | TWDR;
308 case 0x53: // speed wish LSB
309 tmp_speed.i = tmp_speed.i << 8 | TWDR;
310 cmd_vel.speed = tmp_speed.f;
313 case 0x54: // angle wish MSB
317 case 0x55: // angle wish
318 tmp_angle.i = tmp_angle.i << 8 | TWDR;
321 case 0x56: // angle wish
322 tmp_angle.i = tmp_angle.i << 8 | TWDR;
325 case 0x57: // angle wish LSB
326 tmp_angle.i = tmp_angle.i << 8 | TWDR;
327 cmd_vel.angle = tmp_angle.f;
331 case 0x90: // Motor 1 switch
332 motor1_switch = TWDR;
335 case 0x91: // Motor 2 switch
336 motor2_switch = TWDR;
339 case 0x92: // Motor 3 switch
340 motor3_switch = TWDR;
343 case 0x93: // Motor 4 switch
344 motor4_switch = TWDR;
347 case 0x94: // Front Handicap
348 front_handicap = TWDR;
351 case 0x95: // Aft Handicap
355 case 0xff: // bootloader
362 case 0xA8: // start read
365 case 0x02: // Motor 1 PWM
369 case 0x04: // Motor 2 PWM
373 case 0x06: // Motor 3 PWM
377 case 0x08: // Motor 4 PWM
381 case 0x10: // Hall 1 MSB
386 case 0x11: // Hall 1 LSB
390 case 0x12: // Hall 2 MSB
395 case 0x13: // Hall 2 LSB
399 case 0x14: // Hall 3 MSB
404 case 0x15: // Hall 3 LSB
408 case 0x16: // Hall 4 MSB
413 case 0x17: // Hall 4 LSB
417 case 0x20: // Motor 1 speed wish MSB
418 TWDR = speed1_wish>>8;
421 case 0x21: // Motor 1 speed wish LSB
425 case 0x22: // Motor 2 speed wish MSB
426 TWDR = speed2_wish>>8;
429 case 0x23: // Motor 2 speed wish LSB
433 case 0x24: // Motor 3 speed wish MSB
434 TWDR = speed3_wish>>8;
437 case 0x25: // Motor 3 speed wish LSB
441 case 0x26: // Motor 4 speed wish MSB
442 TWDR = speed4_wish>>8;
445 case 0x27: // Motor 4 speed wish LSB
449 case 0x30: // Motor 1 speed MSB
453 case 0x31: // Motor 1 speed LSB
457 case 0x32: // Motor 2 speed MSB
461 case 0x33: // Motor 2 speed LSB
465 case 0x34: // Motor 3 speed MSB
469 case 0x35: // Motor 3 speed LSB
473 case 0x36: // Motor 4 speed MSB
477 case 0x37: // Motor 4 speed LSB
481 case 0x38: // speed MSB
482 tmp_speed.f = cur_speed_lin;
483 TWDR = tmp_speed.i>>24;
487 TWDR = tmp_speed.i>>16;
491 TWDR = tmp_speed.i>>8;
494 case 0x3B: // speed LSB
498 case 0x3C: // angle MSB
499 tmp_angle.f = cur_speed_rot;
500 TWDR = tmp_angle.i>>24;
504 TWDR = tmp_angle.i>>16;
508 TWDR = tmp_angle.i>>8;
511 case 0x3F: // angle LSB
515 case 0x40: // Position x MSB
519 case 0x41: // Position x
523 case 0x42: // Position x
527 case 0x43: // Position x LSB
531 case 0x44: // Position y MSB
535 case 0x45: // Position y
539 case 0x46: // Position y
543 case 0x47: // Position y LSB
547 case 0x48: // Position angle MSB
551 case 0x49: // Position angle
555 case 0x4A: // Position angle
559 case 0x4B: // Position angle LSB
563 case 0xA0: // Reset reason
564 TWDR = MCUCSR & 0x0f;
568 case 0xA1: // TLE Error status
569 TWDR = ~((PIND & 0x40)>>3 | (PINB & 0x07)) & 0xf;
572 case 0xA2: // count test
587 static void update_hall1(void) {
588 unsigned char status = (PINA >> 0) & 0x3;
589 static unsigned char oldstatus=0;
590 unsigned char diff, new;
596 new ^= 0x1; // convert gray to binary
597 diff = oldstatus - new; // difference last - new
598 if (diff & 0x1) { // bit 0 = value (1)
599 oldstatus = new; // store new as next last
600 if (motor1_switch) pos1 += (diff & 2) - 1; // bit 1 = direction (+/-)
601 else pos1 -= (diff & 2) - 1;
606 static void update_hall2(void) {
607 unsigned char status = (PINA >> 4) & 0x3;
608 static unsigned char oldstatus=0;
609 unsigned char diff, new;
615 new ^= 0x1; // convert gray to binary
616 diff = oldstatus - new; // difference last - new
617 if (diff & 0x1) { // bit 0 = value (1)
618 oldstatus = new; // store new as next last
619 if (motor2_switch) pos2 -= (diff & 2) - 1; // bit 1 = direction (+/-)
620 else pos2 += (diff & 2) - 1;
625 static void update_hall3(void) {
626 unsigned char status = (PINA >> 2) & 0x3;
627 static unsigned char oldstatus=0;
628 unsigned char diff, new;
634 new ^= 0x1; // convert gray to binary
635 diff = oldstatus - new; // difference last - new
636 if (diff & 0x1) { // bit 0 = value (1)
637 oldstatus = new; // store new as next last
638 if (motor3_switch) pos3 -= (diff & 2) - 1; // bit 1 = direction (+/-)
639 else pos3 += (diff & 2) - 1;
644 static void update_hall4(void) {
645 unsigned char status = (PINA >> 6) & 0x3;
646 static unsigned char oldstatus=0;
647 unsigned char diff, new;
653 new ^= 0x1; // convert gray to binary
654 diff = oldstatus - new; // difference last - new
655 if (diff & 0x1) { // bit 0 = value (1)
656 oldstatus = new; // store new as next last
657 if (motor4_switch) pos4 += (diff & 2) - 1; // bit 1 = direction (+/-)
658 else pos4 -= (diff & 2) - 1;
663 static void update_motor(void) {
664 static int16_t m1_old=SHRT_MIN;
665 static int16_t m2_old=SHRT_MIN;
666 static int16_t m3_old=SHRT_MIN;
667 static int16_t m4_old=SHRT_MIN;
669 if (m1_old != motor1) { // update only when changed
672 PORTC |= (1 << 3) | (1 << 2);
673 } else if ((!motor1_switch && motor1 > 0) || (motor1_switch && motor1 < 0)) {
675 PORTC &= ~(1 << 3) & ~(1 << 2);
676 } else { // motor1 < 0
686 if (m2_old != motor2) { // update only when changed
689 PORTC |= (1 << 5) | (1 << 4);
690 } else if ((!motor2_switch && motor2 > 0) || (motor2_switch && motor2 < 0)) {
692 PORTC &= ~(1 << 5) & ~(1 << 4);
693 } else { // motor2 < 0
703 if (m3_old != motor3) { // update only when changed
706 PORTC |= (1 << 7) | (1 << 6);
707 } else if ((!motor3_switch && motor3 > 0) || (motor3_switch && motor3 < 0)) {
709 PORTC &= ~(1 << 7) & ~(1 << 6);
710 } else { // motor3 < 0
720 if (m4_old != motor4) { // update only when changed
723 PORTD |= (1 << 3) | (1 << 2);
724 } else if ((!motor4_switch && motor4 > 0) || (motor4_switch && motor4 < 0)) {
726 PORTD &= ~(1 << 3) & ~(1 << 2);
727 } else { // motor4 < 0
739 static void update_pos(void) {
740 static int16_t pos1_last=0;
741 static int16_t pos2_last=0;
742 static int16_t pos3_last=0;
743 static int16_t pos4_last=0;
744 int16_t pos1_diff; // steps
748 float diff_left_m, diff_right_m, angle_diff, translation;
749 float pos_x_new, pos_y_new, angle_new;
750 int16_t speed_l, speed_r;
751 float tmp_speed_lin, tmp_speed_rot;
752 int16_t cur_pos1, cur_pos2, cur_pos3, cur_pos4;
753 int16_t new_speed1, new_speed2, new_speed3, new_speed4;
763 pos1_diff = cur_pos1 - pos1_last;
764 pos2_diff = cur_pos2 - pos2_last;
765 pos3_diff = cur_pos3 - pos3_last;
766 pos4_diff = cur_pos4 - pos4_last;
768 new_speed1 = pos1_diff/PID_T;
769 new_speed2 = pos2_diff/PID_T;
770 new_speed3 = pos3_diff/PID_T;
771 new_speed4 = pos4_diff/PID_T;
773 diff_left_m = (pos1_diff + pos2_diff)/(2*STEP_PER_M);
774 diff_right_m = (pos3_diff + pos4_diff)/(2*STEP_PER_M);
775 angle_diff = (diff_right_m - diff_left_m) / WHEEL_DIST;
777 angle_new = angle.f + angle_diff;
778 if (angle_new > 2*M_PI) angle_new-=2*M_PI;
779 else if (angle_new < 2*M_PI) angle_new+=2*M_PI;
781 translation = (diff_left_m + diff_right_m)/2.0;
782 pos_x_new = pos_x.f + cos(angle_new)*translation;
783 pos_y_new = pos_y.f + sin(angle_new)*translation;
785 speed_l = (new_speed1+new_speed2)/2;
786 speed_r = (new_speed3+new_speed4)/2;
787 tmp_speed_lin = (speed_l + speed_r)/(2.0*STEP_PER_M);
788 tmp_speed_rot = (speed_r - speed_l)/(M_PI*WHEEL_DIST*STEP_PER_M);
799 cur_speed_lin = tmp_speed_lin;
800 cur_speed_rot = tmp_speed_rot;
803 pos1_last = cur_pos1;
804 pos2_last = cur_pos2;
805 pos3_last = cur_pos3;
806 pos4_last = cur_pos4;
810 static void update_pid(void) {
811 static int16_t eold1=0;
812 static int16_t eold2=0;
813 static int16_t eold3=0;
814 static int16_t eold4=0;
815 static int32_t esum1=0;
816 static int32_t esum2=0;
817 static int32_t esum3=0;
818 static int32_t esum4=0;
820 if (motor1_mode == MOTOR_PID) {
821 if (speed1_wish == 0) {
826 int16_t e = speed1_wish - speed1;
828 motor1 += KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
831 if (motor1 > 255) motor1 = 255;
832 else if (motor1 < -255) motor1 = -255;
835 if (motor2_mode == MOTOR_PID) {
836 if (speed2_wish == 0) {
841 int16_t e = speed2_wish - speed2;
843 motor2 += KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
846 if (motor2 > 255) motor2 = 255;
847 else if (motor2 < -255) motor2 = -255;
850 if (motor3_mode == MOTOR_PID) {
851 if (speed3_wish == 0) {
856 int16_t e = speed3_wish - speed3;
858 motor3 += KP*e + KI*PID_T*esum3 + KD/PID_T*(e - eold3);
861 if (motor3 > 255) motor3 = 255;
862 else if (motor3 < -255) motor3 = -255;
865 if (motor4_mode == MOTOR_PID) {
866 if (speed4_wish == 0) {
871 int16_t e = speed4_wish - speed4;
873 motor4 += KP*e + KI*PID_T*esum4 + KD/PID_T*(e - eold4);
876 if (motor4 > 255) motor4 = 255;
877 else if (motor4 < -255) motor4 = -255;
883 ISR(TIMER1_OVF_vect) {
896 DDRC = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
897 DDRD = (1 << 7) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
899 PORTB = (1 << 0) | (1 << 1) | (1 << 2);
911 // Timer 1: Fast PWM inverting mode, Top=256 => 15.625kHz
913 TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << COM1A0) | (1 << COM1B0) | (1 << WGM10);
914 TCCR1B = (1 << WGM12) | (1 << CS10);
919 // Timer 2: Fast PWM inverting mode, Top=256
921 TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << COM21) | (1 << COM20) | (1 << CS20);
925 // Timer 0: Fast PWM inverting mode, Top=256
927 TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << COM01) | (1 << COM00) | (1 << CS00);
930 printf("\r\nStart\r\n");
932 set_sleep_mode(SLEEP_MODE_IDLE);
933 // Enable Timer 1 Overflow Interrupt
934 TIMSK = (1 << TOIE1);
939 case 0xff: // Magic reg that starts the bootloader
940 if (bootloader == 0xa5) {
943 void (*start)(void) = (void*)0x1800;
950 if (cmd_vel.bUpdate) {
951 float speed_wish_right, speed_wish_left;
955 speed = cmd_vel.speed;
956 angle = cmd_vel.angle;
960 speed_wish_right = angle*M_PI*WHEEL_DIST/2 + speed;
961 speed_wish_left = speed*2-speed_wish_right;
963 speed_wish_left*=STEP_PER_M;
964 speed_wish_right*=STEP_PER_M;
966 speed1_wish = speed_wish_left * (100-aft_handicap)/100.0;
967 speed2_wish = speed_wish_left * (100-front_handicap)/100.0;
968 speed3_wish = speed_wish_right * (100-front_handicap)/100.0;
969 speed4_wish = speed_wish_right * (100-aft_handicap)/100.0;
970 motor1_mode = MOTOR_PID;
971 motor2_mode = MOTOR_PID;
972 motor3_mode = MOTOR_PID;
973 motor4_mode = MOTOR_PID;
976 if (run_update >= 156) { // ~100Hz