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 uint8_t ireg=0;
118 static volatile uint8_t bootloader=0;
119 static volatile int16_t motor1=0; // -255..+255
120 static volatile int16_t motor2=0;
121 static volatile int16_t motor3=0;
122 static volatile int16_t motor4=0;
123 static volatile int16_t pos1=0; // step
124 static volatile int16_t pos2=0;
125 static volatile int16_t pos3=0;
126 static volatile int16_t pos4=0;
127 static volatile enum mode motor1_mode=MOTOR_MANUAL;
128 static volatile enum mode motor2_mode=MOTOR_MANUAL;
129 static volatile enum mode motor3_mode=MOTOR_MANUAL;
130 static volatile enum mode motor4_mode=MOTOR_MANUAL;
131 static volatile uint8_t motor1_switch=0;
132 static volatile uint8_t motor2_switch=0;
133 static volatile uint8_t motor3_switch=0;
134 static volatile uint8_t motor4_switch=0;
135 static volatile int16_t speed1_wish=0; // step/s
136 static volatile int16_t speed2_wish=0;
137 static volatile int16_t speed3_wish=0;
138 static volatile int16_t speed4_wish=0;
139 static volatile uint8_t run_update=0;
140 static volatile int16_t speed1=0; // step/s
141 static volatile int16_t speed2=0;
142 static volatile int16_t speed3=0;
143 static volatile int16_t speed4=0;
144 static volatile ufloat_t pos_x={0.0};
145 static volatile ufloat_t pos_y={0.0};
146 static volatile ufloat_t angle={0.0};
150 static uint8_t tmp=0;
151 static int16_t tmp16=0;
152 static ufloat_t tmp_speed;
153 static ufloat_t tmp_angle;
157 case 0x60: // start write
163 case 0x00: // register select
165 ireg--; // because we do ireg++ below
168 case 0x01: // Motor 1 MSB
172 case 0x02: // Motor 1 LSB
173 motor1 = tmp<<8 | TWDR;
174 motor1_mode = MOTOR_MANUAL;
177 case 0x03: // Motor 2 MSB
181 case 0x04: // Motor 2 LSB
182 motor2 = tmp<<8 | TWDR;
183 motor2_mode = MOTOR_MANUAL;
186 case 0x05: // Motor 3 MSB
190 case 0x06: // Motor 3 LSB
191 motor3 = tmp<<8 | TWDR;
192 motor3_mode = MOTOR_MANUAL;
195 case 0x07: // Motor 4 MSB
199 case 0x08: // Motor 4 LSB
200 motor4 = tmp<<8 | TWDR;
201 motor4_mode = MOTOR_MANUAL;
204 case 0x20: // Motor 1 speed wish MSB
208 case 0x21: // Motor 1 speed wish LSB
209 speed1_wish = tmp<<8 | TWDR;
210 motor1_mode = MOTOR_PID;
213 case 0x22: // Motor 2 speed wish MSB
217 case 0x23: // Motor 2 speed wish LSB
218 speed2_wish = tmp<<8 | TWDR;
219 motor2_mode = MOTOR_PID;
222 case 0x24: // Motor 3 speed wish MSB
226 case 0x25: // Motor 3 speed wish LSB
227 speed3_wish = tmp<<8 | TWDR;
228 motor3_mode = MOTOR_PID;
231 case 0x26: // Motor 4 speed wish MSB
235 case 0x27: // Motor 4 speed wish LSB
236 speed4_wish = tmp<<8 | TWDR;
237 motor4_mode = MOTOR_PID;
240 case 0x28: // Left speed wish MSB
244 case 0x29: // Left speed wish
245 tmp_speed.i = tmp_speed.i << 8 | TWDR;
248 case 0x2A: // Left speed wish
249 tmp_speed.i = tmp_speed.i << 8 | TWDR;
252 case 0x2B: // Left speed wish LSB
253 tmp_speed.i = tmp_speed.i << 8 | TWDR;
254 speed1_wish = tmp_speed.f*STEP_PER_M;
255 speed2_wish = tmp_speed.f*STEP_PER_M;
256 motor1_mode = MOTOR_PID;
257 motor2_mode = MOTOR_PID;
260 case 0x2C: // Right speed wish MSB
264 case 0x2D: // Right speed wish
265 tmp_speed.i = tmp_speed.i << 8 | TWDR;
268 case 0x2E: // Right speed wish
269 tmp_speed.i = tmp_speed.i << 8 | TWDR;
272 case 0x2F: // Right speed wish LSB
273 tmp_speed.i = tmp_speed.i << 8 | TWDR;
274 speed1_wish = tmp_speed.f*STEP_PER_M;
275 speed2_wish = tmp_speed.f*STEP_PER_M;
276 motor1_mode = MOTOR_PID;
277 motor2_mode = MOTOR_PID;
280 case 0x50: // speed wish MSB
284 case 0x51: // speed wish
285 tmp_speed.i = tmp_speed.i << 8 | TWDR;
288 case 0x52: // speed wish
289 tmp_speed.i = tmp_speed.i << 8 | TWDR;
292 case 0x53: // speed wish LSB
293 tmp_speed.i = tmp_speed.i << 8 | TWDR;
296 case 0x54: // angle wish MSB
300 case 0x55: // angle wish
301 tmp_angle.i = tmp_angle.i << 8 | TWDR;
304 case 0x56: // angle wish
305 tmp_angle.i = tmp_angle.i << 8 | TWDR;
308 case 0x57: // angle wish LSB
309 tmp_angle.i = tmp_angle.i << 8 | TWDR;
311 float speed_wish_right = tmp_angle.f*M_PI*WHEEL_DIST/2 + tmp_speed.f;
312 float speed_wish_left = tmp_speed.f*2-speed_wish_right;
313 speed1_wish = speed_wish_left*STEP_PER_M;
314 speed2_wish = speed_wish_left*STEP_PER_M;
315 speed3_wish = speed_wish_right*STEP_PER_M;
316 speed4_wish = speed_wish_right*STEP_PER_M;
318 motor1_mode = MOTOR_PID;
319 motor2_mode = MOTOR_PID;
320 motor3_mode = MOTOR_PID;
321 motor4_mode = MOTOR_PID;
324 case 0x90: // Motor 1 switch
325 motor1_switch = TWDR;
328 case 0x91: // Motor 2 switch
329 motor2_switch = TWDR;
332 case 0x92: // Motor 3 switch
333 motor3_switch = TWDR;
336 case 0x93: // Motor 4 switch
337 motor4_switch = TWDR;
340 case 0xff: // bootloader
347 case 0xA8: // start read
350 case 0x02: // Motor 1 PWM
354 case 0x04: // Motor 2 PWM
358 case 0x06: // Motor 3 PWM
362 case 0x08: // Motor 4 PWM
366 case 0x10: // Hall 1 MSB
371 case 0x11: // Hall 1 LSB
375 case 0x12: // Hall 2 MSB
380 case 0x13: // Hall 2 LSB
384 case 0x14: // Hall 3 MSB
389 case 0x15: // Hall 3 LSB
393 case 0x16: // Hall 4 MSB
398 case 0x17: // Hall 4 LSB
402 case 0x20: // Motor 1 speed wish MSB
403 TWDR = speed1_wish>>8;
406 case 0x21: // Motor 1 speed wish LSB
410 case 0x22: // Motor 2 speed wish MSB
411 TWDR = speed2_wish>>8;
414 case 0x23: // Motor 2 speed wish LSB
418 case 0x24: // Motor 3 speed wish MSB
419 TWDR = speed3_wish>>8;
422 case 0x25: // Motor 3 speed wish LSB
426 case 0x26: // Motor 4 speed wish MSB
427 TWDR = speed4_wish>>8;
430 case 0x27: // Motor 4 speed wish LSB
434 case 0x30: // Motor 1 speed MSB
438 case 0x31: // Motor 1 speed LSB
442 case 0x32: // Motor 2 speed MSB
446 case 0x33: // Motor 2 speed LSB
450 case 0x34: // Motor 3 speed MSB
454 case 0x35: // Motor 3 speed LSB
458 case 0x36: // Motor 4 speed MSB
462 case 0x37: // Motor 4 speed LSB
466 case 0x38: // speed MSB
468 int16_t speed_l = (speed3+speed4)/2;
469 int16_t speed_r = (speed1+speed2)/2;
470 tmp_speed.f = (speed_l + speed_r)/(2.0*STEP_PER_M);
471 tmp_angle.f = (speed_r - speed_l)/(M_PI*WHEEL_DIST*STEP_PER_M);
473 TWDR = tmp_speed.i>>24;
477 TWDR = tmp_speed.i>>16;
481 TWDR = tmp_speed.i>>8;
484 case 0x3B: // speed LSB
488 case 0x3C: // angle MSB
489 TWDR = tmp_angle.i>>24;
493 TWDR = tmp_angle.i>>16;
497 TWDR = tmp_angle.i>>8;
500 case 0x3F: // angle LSB
504 case 0x40: // Position x MSB
508 case 0x41: // Position x
512 case 0x42: // Position x
516 case 0x43: // Position x LSB
520 case 0x44: // Position y MSB
524 case 0x45: // Position y
528 case 0x46: // Position y
532 case 0x47: // Position y LSB
536 case 0x48: // Position angle MSB
540 case 0x49: // Position angle
544 case 0x4A: // Position angle
548 case 0x4B: // Position angle LSB
552 case 0x94: // TLE Error status
553 TWDR = (PIND & 0x40)>>2 | (PINB & 0x07);
568 static void update_hall1(void) {
569 unsigned char status = (PINA >> 0) & 0x3;
570 static unsigned char oldstatus=0;
571 unsigned char diff, new;
577 new ^= 0x1; // convert gray to binary
578 diff = oldstatus - new; // difference last - new
579 if (diff & 0x1) { // bit 0 = value (1)
580 oldstatus = new; // store new as next last
581 if (motor1_switch) pos1 += (diff & 2) - 1; // bit 1 = direction (+/-)
582 else pos1 -= (diff & 2) - 1;
587 static void update_hall2(void) {
588 unsigned char status = (PINA >> 4) & 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 (motor2_switch) pos2 -= (diff & 2) - 1; // bit 1 = direction (+/-)
601 else pos2 += (diff & 2) - 1;
606 static void update_hall3(void) {
607 unsigned char status = (PINA >> 2) & 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 (motor3_switch) pos3 -= (diff & 2) - 1; // bit 1 = direction (+/-)
620 else pos3 += (diff & 2) - 1;
625 static void update_hall4(void) {
626 unsigned char status = (PINA >> 6) & 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 (motor4_switch) pos4 += (diff & 2) - 1; // bit 1 = direction (+/-)
639 else pos4 -= (diff & 2) - 1;
644 static void update_motor(void) {
645 static int16_t m1_old=SHRT_MIN;
646 static int16_t m2_old=SHRT_MIN;
647 static int16_t m3_old=SHRT_MIN;
648 static int16_t m4_old=SHRT_MIN;
650 if (m1_old != motor1) { // update only when changed
653 PORTC |= (1 << 3) | (1 << 2);
654 } else if ((!motor1_switch && motor1 > 0) || (motor1_switch && motor1 < 0)) {
656 PORTC &= ~(1 << 3) & ~(1 << 2);
657 } else { // motor1 < 0
667 if (m2_old != motor2) { // update only when changed
670 PORTC |= (1 << 5) | (1 << 4);
671 } else if ((!motor2_switch && motor2 > 0) || (motor2_switch && motor2 < 0)) {
673 PORTC &= ~(1 << 5) & ~(1 << 4);
674 } else { // motor2 < 0
684 if (m3_old != motor3) { // update only when changed
687 PORTC |= (1 << 7) | (1 << 6);
688 } else if ((!motor3_switch && motor3 > 0) || (motor3_switch && motor3 < 0)) {
690 PORTC &= ~(1 << 7) & ~(1 << 6);
691 } else { // motor3 < 0
701 if (m4_old != motor4) { // update only when changed
704 PORTD |= (1 << 3) | (1 << 2);
705 } else if ((!motor4_switch && motor4 > 0) || (motor4_switch && motor4 < 0)) {
707 PORTD &= ~(1 << 3) & ~(1 << 2);
708 } else { // motor4 < 0
720 static void update_pos(void) {
721 static int16_t pos1_last=0;
722 static int16_t pos2_last=0;
723 static int16_t pos3_last=0;
724 static int16_t pos4_last=0;
725 int16_t pos1_diff; // steps
729 float diff_left_m, diff_right_m, angle_diff, translation;
732 pos1_diff = pos1 - pos1_last;
733 pos2_diff = pos2 - pos2_last;
734 pos3_diff = pos3 - pos3_last;
735 pos4_diff = pos4 - pos4_last;
736 speed1 = pos1_diff/PID_T;
737 speed2 = pos2_diff/PID_T;
738 speed3 = pos3_diff/PID_T;
739 speed4 = pos4_diff/PID_T;
742 diff_left_m = (pos1_diff + pos2_diff)/(2*STEP_PER_M);
743 diff_right_m = (pos3_diff + pos4_diff)/(2*STEP_PER_M);
744 angle_diff = (diff_right_m - diff_left_m) / WHEEL_DIST;
748 if (angle.f > 2*M_PI) angle.f-=2*M_PI;
749 else if (angle.f < 2*M_PI) angle.f+=2*M_PI;
753 translation = (diff_left_m + diff_right_m)/2.0;
754 pos_x.f += cos(angle.f)*translation;
755 pos_y.f += sin(angle.f)*translation;
765 static void update_pid(void) {
766 static int16_t eold1=0;
767 static int16_t eold2=0;
768 static int16_t eold3=0;
769 static int16_t eold4=0;
770 static int32_t esum1=0;
771 static int32_t esum2=0;
772 static int32_t esum3=0;
773 static int32_t esum4=0;
775 if (motor1_mode == MOTOR_PID) {
776 if (speed1_wish == 0) {
779 int16_t e = speed1_wish - speed1;
781 motor1 += KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
784 if (motor1 > 255) motor1 = 255;
785 else if (motor1 < -255) motor1 = -255;
788 if (motor2_mode == MOTOR_PID) {
789 if (speed2_wish == 0) {
792 int16_t e = speed2_wish - speed2;
794 motor2 += KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
797 if (motor2 > 255) motor2 = 255;
798 else if (motor2 < -255) motor2 = -255;
801 if (motor3_mode == MOTOR_PID) {
802 if (speed3_wish == 0) {
805 int16_t e = speed3_wish - speed3;
807 motor3 += KP*e + KI*PID_T*esum3 + KD/PID_T*(e - eold3);
810 if (motor3 > 255) motor3 = 255;
811 else if (motor3 < -255) motor3 = -255;
814 if (motor4_mode == MOTOR_PID) {
815 if (speed4_wish == 0) {
818 int16_t e = speed4_wish - speed4;
820 motor4 += KP*e + KI*PID_T*esum4 + KD/PID_T*(e - eold4);
823 if (motor4 > 255) motor4 = 255;
824 else if (motor4 < -255) motor4 = -255;
830 ISR(TIMER1_OVF_vect) {
843 DDRC = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
844 DDRD = (1 << 7) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
855 // Timer 1: Fast PWM inverting mode, Top=256 => 15.625kHz
857 TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << COM1A0) | (1 << COM1B0) | (1 << WGM10);
858 TCCR1B = (1 << WGM12) | (1 << CS10);
863 // Timer 2: Fast PWM inverting mode, Top=256 => 15.625kHz
865 TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << COM21) | (1 << COM20) | (1 << CS20);
869 // Timer 0: Fast PWM inverting mode, Top=256 => 15.625kHz
871 TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << COM01) | (1 << COM00) | (1 << CS00);
874 printf("\r\nStart\r\n");
876 set_sleep_mode(SLEEP_MODE_IDLE);
877 // Enable Timer 1 Overflow Interrupt
878 TIMSK = (1 << TOIE1);
883 case 0xff: // Magic reg that starts the bootloader
884 if (bootloader == 0xa5) {
887 void (*start)(void) = (void*)0x1800;
895 if (run_update >= 156) { // ~100Hz