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
90 * 0x94 TLE Error status
96 #define TWI_ACK TWCR = (1<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
97 #define TWI_RESET TWCR &= ~((1 << TWSTO) | (1 << TWEN)); TWI_ACK
98 #define TWI_NAK TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
104 #define STEP_PER_M 3376.1 // wheel diameter=12cm, encoder=48cpr, gear ratio=1:34
105 #define WHEEL_DIST 0.252
117 static volatile struct {
121 } cmd_vel = {0, 0, 0};
123 static volatile uint8_t ireg=0;
124 static volatile uint8_t bootloader=0;
125 static volatile int16_t motor1=0; // -255..+255
126 static volatile int16_t motor2=0;
127 static volatile int16_t motor3=0;
128 static volatile int16_t motor4=0;
129 static volatile int16_t pos1=0; // step
130 static volatile int16_t pos2=0;
131 static volatile int16_t pos3=0;
132 static volatile int16_t pos4=0;
133 static volatile enum mode motor1_mode=MOTOR_MANUAL;
134 static volatile enum mode motor2_mode=MOTOR_MANUAL;
135 static volatile enum mode motor3_mode=MOTOR_MANUAL;
136 static volatile enum mode motor4_mode=MOTOR_MANUAL;
137 static volatile uint8_t motor1_switch=0;
138 static volatile uint8_t motor2_switch=0;
139 static volatile uint8_t motor3_switch=0;
140 static volatile uint8_t motor4_switch=0;
141 static volatile int16_t speed1_wish=0; // step/s
142 static volatile int16_t speed2_wish=0;
143 static volatile int16_t speed3_wish=0;
144 static volatile int16_t speed4_wish=0;
145 static volatile uint16_t run_update=0;
146 static volatile int16_t speed1=0; // step/s
147 static volatile int16_t speed2=0;
148 static volatile int16_t speed3=0;
149 static volatile int16_t speed4=0;
150 static volatile ufloat_t pos_x={0.0};
151 static volatile ufloat_t pos_y={0.0};
152 static volatile ufloat_t angle={0.0};
153 static volatile float cur_speed_lin=0;
154 static volatile float cur_speed_rot=0;
158 static uint8_t tmp=0;
159 static int16_t tmp16=0;
160 static ufloat_t tmp_speed;
161 static ufloat_t tmp_angle;
165 case 0x60: // start write
171 case 0x00: // register select
173 ireg--; // because we do ireg++ below
176 case 0x01: // Motor 1 MSB
180 case 0x02: // Motor 1 LSB
181 motor1 = tmp<<8 | TWDR;
182 motor1_mode = MOTOR_MANUAL;
185 case 0x03: // Motor 2 MSB
189 case 0x04: // Motor 2 LSB
190 motor2 = tmp<<8 | TWDR;
191 motor2_mode = MOTOR_MANUAL;
194 case 0x05: // Motor 3 MSB
198 case 0x06: // Motor 3 LSB
199 motor3 = tmp<<8 | TWDR;
200 motor3_mode = MOTOR_MANUAL;
203 case 0x07: // Motor 4 MSB
207 case 0x08: // Motor 4 LSB
208 motor4 = tmp<<8 | TWDR;
209 motor4_mode = MOTOR_MANUAL;
212 case 0x20: // Motor 1 speed wish MSB
216 case 0x21: // Motor 1 speed wish LSB
217 speed1_wish = tmp<<8 | TWDR;
218 motor1_mode = MOTOR_PID;
221 case 0x22: // Motor 2 speed wish MSB
225 case 0x23: // Motor 2 speed wish LSB
226 speed2_wish = tmp<<8 | TWDR;
227 motor2_mode = MOTOR_PID;
230 case 0x24: // Motor 3 speed wish MSB
234 case 0x25: // Motor 3 speed wish LSB
235 speed3_wish = tmp<<8 | TWDR;
236 motor3_mode = MOTOR_PID;
239 case 0x26: // Motor 4 speed wish MSB
243 case 0x27: // Motor 4 speed wish LSB
244 speed4_wish = tmp<<8 | TWDR;
245 motor4_mode = MOTOR_PID;
248 case 0x28: // Left speed wish MSB
252 case 0x29: // Left speed wish
253 tmp_speed.i = tmp_speed.i << 8 | TWDR;
256 case 0x2A: // Left speed wish
257 tmp_speed.i = tmp_speed.i << 8 | TWDR;
260 case 0x2B: // Left speed wish LSB
261 tmp_speed.i = tmp_speed.i << 8 | TWDR;
262 speed1_wish = tmp_speed.f*STEP_PER_M;
263 speed2_wish = tmp_speed.f*STEP_PER_M;
264 motor1_mode = MOTOR_PID;
265 motor2_mode = MOTOR_PID;
268 case 0x2C: // Right speed wish MSB
272 case 0x2D: // Right speed wish
273 tmp_speed.i = tmp_speed.i << 8 | TWDR;
276 case 0x2E: // Right speed wish
277 tmp_speed.i = tmp_speed.i << 8 | TWDR;
280 case 0x2F: // Right speed wish LSB
281 tmp_speed.i = tmp_speed.i << 8 | TWDR;
282 speed1_wish = tmp_speed.f*STEP_PER_M;
283 speed2_wish = tmp_speed.f*STEP_PER_M;
284 motor1_mode = MOTOR_PID;
285 motor2_mode = MOTOR_PID;
288 case 0x50: // speed wish MSB
292 case 0x51: // speed wish
293 tmp_speed.i = tmp_speed.i << 8 | TWDR;
296 case 0x52: // speed wish
297 tmp_speed.i = tmp_speed.i << 8 | TWDR;
300 case 0x53: // speed wish LSB
301 tmp_speed.i = tmp_speed.i << 8 | TWDR;
302 cmd_vel.speed = tmp_speed.f;
305 case 0x54: // angle wish MSB
309 case 0x55: // angle wish
310 tmp_angle.i = tmp_angle.i << 8 | TWDR;
313 case 0x56: // angle wish
314 tmp_angle.i = tmp_angle.i << 8 | TWDR;
317 case 0x57: // angle wish LSB
318 tmp_angle.i = tmp_angle.i << 8 | TWDR;
319 cmd_vel.angle = tmp_angle.f;
323 case 0x90: // Motor 1 switch
324 motor1_switch = TWDR;
327 case 0x91: // Motor 2 switch
328 motor2_switch = TWDR;
331 case 0x92: // Motor 3 switch
332 motor3_switch = TWDR;
335 case 0x93: // Motor 4 switch
336 motor4_switch = TWDR;
339 case 0xff: // bootloader
346 case 0xA8: // start read
349 case 0x02: // Motor 1 PWM
353 case 0x04: // Motor 2 PWM
357 case 0x06: // Motor 3 PWM
361 case 0x08: // Motor 4 PWM
365 case 0x10: // Hall 1 MSB
370 case 0x11: // Hall 1 LSB
374 case 0x12: // Hall 2 MSB
379 case 0x13: // Hall 2 LSB
383 case 0x14: // Hall 3 MSB
388 case 0x15: // Hall 3 LSB
392 case 0x16: // Hall 4 MSB
397 case 0x17: // Hall 4 LSB
401 case 0x20: // Motor 1 speed wish MSB
402 TWDR = speed1_wish>>8;
405 case 0x21: // Motor 1 speed wish LSB
409 case 0x22: // Motor 2 speed wish MSB
410 TWDR = speed2_wish>>8;
413 case 0x23: // Motor 2 speed wish LSB
417 case 0x24: // Motor 3 speed wish MSB
418 TWDR = speed3_wish>>8;
421 case 0x25: // Motor 3 speed wish LSB
425 case 0x26: // Motor 4 speed wish MSB
426 TWDR = speed4_wish>>8;
429 case 0x27: // Motor 4 speed wish LSB
433 case 0x30: // Motor 1 speed MSB
437 case 0x31: // Motor 1 speed LSB
441 case 0x32: // Motor 2 speed MSB
445 case 0x33: // Motor 2 speed LSB
449 case 0x34: // Motor 3 speed MSB
453 case 0x35: // Motor 3 speed LSB
457 case 0x36: // Motor 4 speed MSB
461 case 0x37: // Motor 4 speed LSB
465 case 0x38: // speed MSB
466 tmp_speed.f = cur_speed_lin;
467 TWDR = tmp_speed.i>>24;
471 TWDR = tmp_speed.i>>16;
475 TWDR = tmp_speed.i>>8;
478 case 0x3B: // speed LSB
482 case 0x3C: // angle MSB
483 tmp_angle.f = cur_speed_rot;
484 TWDR = tmp_angle.i>>24;
488 TWDR = tmp_angle.i>>16;
492 TWDR = tmp_angle.i>>8;
495 case 0x3F: // angle LSB
499 case 0x40: // Position x MSB
503 case 0x41: // Position x
507 case 0x42: // Position x
511 case 0x43: // Position x LSB
515 case 0x44: // Position y MSB
519 case 0x45: // Position y
523 case 0x46: // Position y
527 case 0x47: // Position y LSB
531 case 0x48: // Position angle MSB
535 case 0x49: // Position angle
539 case 0x4A: // Position angle
543 case 0x4B: // Position angle LSB
547 case 0x94: // TLE Error status
548 TWDR = ~((PIND & 0x40)>>3 | (PINB & 0x07)) & 0xf;
563 static void update_hall1(void) {
564 unsigned char status = (PINA >> 0) & 0x3;
565 static unsigned char oldstatus=0;
566 unsigned char diff, new;
572 new ^= 0x1; // convert gray to binary
573 diff = oldstatus - new; // difference last - new
574 if (diff & 0x1) { // bit 0 = value (1)
575 oldstatus = new; // store new as next last
576 if (motor1_switch) pos1 += (diff & 2) - 1; // bit 1 = direction (+/-)
577 else pos1 -= (diff & 2) - 1;
582 static void update_hall2(void) {
583 unsigned char status = (PINA >> 4) & 0x3;
584 static unsigned char oldstatus=0;
585 unsigned char diff, new;
591 new ^= 0x1; // convert gray to binary
592 diff = oldstatus - new; // difference last - new
593 if (diff & 0x1) { // bit 0 = value (1)
594 oldstatus = new; // store new as next last
595 if (motor2_switch) pos2 -= (diff & 2) - 1; // bit 1 = direction (+/-)
596 else pos2 += (diff & 2) - 1;
601 static void update_hall3(void) {
602 unsigned char status = (PINA >> 2) & 0x3;
603 static unsigned char oldstatus=0;
604 unsigned char diff, new;
610 new ^= 0x1; // convert gray to binary
611 diff = oldstatus - new; // difference last - new
612 if (diff & 0x1) { // bit 0 = value (1)
613 oldstatus = new; // store new as next last
614 if (motor3_switch) pos3 -= (diff & 2) - 1; // bit 1 = direction (+/-)
615 else pos3 += (diff & 2) - 1;
620 static void update_hall4(void) {
621 unsigned char status = (PINA >> 6) & 0x3;
622 static unsigned char oldstatus=0;
623 unsigned char diff, new;
629 new ^= 0x1; // convert gray to binary
630 diff = oldstatus - new; // difference last - new
631 if (diff & 0x1) { // bit 0 = value (1)
632 oldstatus = new; // store new as next last
633 if (motor4_switch) pos4 += (diff & 2) - 1; // bit 1 = direction (+/-)
634 else pos4 -= (diff & 2) - 1;
639 static void update_motor(void) {
640 static int16_t m1_old=SHRT_MIN;
641 static int16_t m2_old=SHRT_MIN;
642 static int16_t m3_old=SHRT_MIN;
643 static int16_t m4_old=SHRT_MIN;
645 if (m1_old != motor1) { // update only when changed
648 PORTC |= (1 << 3) | (1 << 2);
649 } else if ((!motor1_switch && motor1 > 0) || (motor1_switch && motor1 < 0)) {
651 PORTC &= ~(1 << 3) & ~(1 << 2);
652 } else { // motor1 < 0
662 if (m2_old != motor2) { // update only when changed
665 PORTC |= (1 << 5) | (1 << 4);
666 } else if ((!motor2_switch && motor2 > 0) || (motor2_switch && motor2 < 0)) {
668 PORTC &= ~(1 << 5) & ~(1 << 4);
669 } else { // motor2 < 0
679 if (m3_old != motor3) { // update only when changed
682 PORTC |= (1 << 7) | (1 << 6);
683 } else if ((!motor3_switch && motor3 > 0) || (motor3_switch && motor3 < 0)) {
685 PORTC &= ~(1 << 7) & ~(1 << 6);
686 } else { // motor3 < 0
696 if (m4_old != motor4) { // update only when changed
699 PORTD |= (1 << 3) | (1 << 2);
700 } else if ((!motor4_switch && motor4 > 0) || (motor4_switch && motor4 < 0)) {
702 PORTD &= ~(1 << 3) & ~(1 << 2);
703 } else { // motor4 < 0
715 static void update_pos(void) {
716 static int16_t pos1_last=0;
717 static int16_t pos2_last=0;
718 static int16_t pos3_last=0;
719 static int16_t pos4_last=0;
720 int16_t pos1_diff; // steps
724 float diff_left_m, diff_right_m, angle_diff, translation;
725 float pos_x_diff, pos_y_diff, angle_new;
726 int16_t speed_l, speed_r;
727 float tmp_speed_lin, tmp_speed_rot;
730 pos1_diff = pos1 - pos1_last;
731 pos2_diff = pos2 - pos2_last;
732 pos3_diff = pos3 - pos3_last;
733 pos4_diff = pos4 - pos4_last;
734 speed1 = pos1_diff/PID_T;
735 speed2 = pos2_diff/PID_T;
736 speed3 = pos3_diff/PID_T;
737 speed4 = pos4_diff/PID_T;
740 diff_left_m = (pos1_diff + pos2_diff)/(2*STEP_PER_M);
741 diff_right_m = (pos3_diff + pos4_diff)/(2*STEP_PER_M);
742 angle_diff = (diff_right_m - diff_left_m) / WHEEL_DIST;
744 angle_new = angle.f + angle_diff;
745 if (angle_new > 2*M_PI) angle_new-=2*M_PI;
746 else if (angle_new < 2*M_PI) angle_new+=2*M_PI;
748 translation = (diff_left_m + diff_right_m)/2.0;
749 pos_x_diff = cos(angle_new)*translation;
750 pos_y_diff = sin(angle_new)*translation;
754 pos_x.f += pos_x_diff;
755 pos_y.f += pos_y_diff;
758 speed_l = (speed1+speed2)/2;
759 speed_r = (speed3+speed4)/2;
760 tmp_speed_lin = (speed_l + speed_r)/(2.0*STEP_PER_M);
761 tmp_speed_rot = (speed_r - speed_l)/(M_PI*WHEEL_DIST*STEP_PER_M);
764 cur_speed_lin = tmp_speed_lin;
765 cur_speed_rot = tmp_speed_rot;
775 static void update_pid(void) {
776 static int16_t eold1=0;
777 static int16_t eold2=0;
778 static int16_t eold3=0;
779 static int16_t eold4=0;
780 static int32_t esum1=0;
781 static int32_t esum2=0;
782 static int32_t esum3=0;
783 static int32_t esum4=0;
785 if (motor1_mode == MOTOR_PID) {
786 if (speed1_wish == 0) {
791 int16_t e = speed1_wish - speed1;
793 motor1 += KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
796 if (motor1 > 255) motor1 = 255;
797 else if (motor1 < -255) motor1 = -255;
800 if (motor2_mode == MOTOR_PID) {
801 if (speed2_wish == 0) {
806 int16_t e = speed2_wish - speed2;
808 motor2 += KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
811 if (motor2 > 255) motor2 = 255;
812 else if (motor2 < -255) motor2 = -255;
815 if (motor3_mode == MOTOR_PID) {
816 if (speed3_wish == 0) {
821 int16_t e = speed3_wish - speed3;
823 motor3 += KP*e + KI*PID_T*esum3 + KD/PID_T*(e - eold3);
826 if (motor3 > 255) motor3 = 255;
827 else if (motor3 < -255) motor3 = -255;
830 if (motor4_mode == MOTOR_PID) {
831 if (speed4_wish == 0) {
836 int16_t e = speed4_wish - speed4;
838 motor4 += KP*e + KI*PID_T*esum4 + KD/PID_T*(e - eold4);
841 if (motor4 > 255) motor4 = 255;
842 else if (motor4 < -255) motor4 = -255;
848 ISR(TIMER1_OVF_vect) {
861 DDRC = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
862 DDRD = (1 << 7) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
873 // Timer 1: Fast PWM inverting mode, Top=256 => 31.25kHz
875 TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << COM1A0) | (1 << COM1B0) | (1 << WGM10);
876 TCCR1B = (1 << WGM12) | (1 << CS10);
881 // Timer 2: Fast PWM inverting mode, Top=256
883 TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << COM21) | (1 << COM20) | (1 << CS20);
887 // Timer 0: Fast PWM inverting mode, Top=256
889 TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << COM01) | (1 << COM00) | (1 << CS00);
892 printf("\r\nStart\r\n");
894 set_sleep_mode(SLEEP_MODE_IDLE);
895 // Enable Timer 1 Overflow Interrupt
896 TIMSK = (1 << TOIE1);
901 case 0xff: // Magic reg that starts the bootloader
902 if (bootloader == 0xa5) {
905 void (*start)(void) = (void*)0x1800;
912 if (cmd_vel.bUpdate) {
913 float speed_wish_right = cmd_vel.angle*M_PI*WHEEL_DIST/2 + cmd_vel.speed;
914 float speed_wish_left = cmd_vel.speed*2-speed_wish_right;
917 speed_wish_left*=STEP_PER_M;
918 speed_wish_right*=STEP_PER_M;
920 speed1_wish = speed_wish_left;
921 speed2_wish = speed_wish_left;
922 speed3_wish = speed_wish_right;
923 speed4_wish = speed_wish_right;
924 motor1_mode = MOTOR_PID;
925 motor2_mode = MOTOR_PID;
926 motor3_mode = MOTOR_PID;
927 motor4_mode = MOTOR_PID;
930 if (run_update >= 312) { // ~100Hz