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 test=0;
124 static volatile uint8_t ireg=0;
125 static volatile uint8_t bootloader=0;
126 static volatile int16_t motor1=0; // -255..+255
127 static volatile int16_t motor2=0;
128 static volatile int16_t motor3=0;
129 static volatile int16_t motor4=0;
130 static volatile int16_t pos1=0; // step
131 static volatile int16_t pos2=0;
132 static volatile int16_t pos3=0;
133 static volatile int16_t pos4=0;
134 static volatile enum mode motor1_mode=MOTOR_MANUAL;
135 static volatile enum mode motor2_mode=MOTOR_MANUAL;
136 static volatile enum mode motor3_mode=MOTOR_MANUAL;
137 static volatile enum mode motor4_mode=MOTOR_MANUAL;
138 static volatile uint8_t motor1_switch=0;
139 static volatile uint8_t motor2_switch=0;
140 static volatile uint8_t motor3_switch=0;
141 static volatile uint8_t motor4_switch=0;
142 static volatile int16_t speed1_wish=0; // step/s
143 static volatile int16_t speed2_wish=0;
144 static volatile int16_t speed3_wish=0;
145 static volatile int16_t speed4_wish=0;
146 static volatile uint16_t run_update=0;
147 static volatile int16_t speed1=0; // step/s
148 static volatile int16_t speed2=0;
149 static volatile int16_t speed3=0;
150 static volatile int16_t speed4=0;
151 static volatile ufloat_t pos_x={0.0};
152 static volatile ufloat_t pos_y={0.0};
153 static volatile ufloat_t angle={0.0};
154 static volatile float cur_speed_lin=0;
155 static volatile float cur_speed_rot=0;
159 static uint8_t tmp=0;
160 static int16_t tmp16=0;
161 static ufloat_t tmp_speed;
162 static ufloat_t tmp_angle;
166 case 0x60: // start write
172 case 0x00: // register select
174 ireg--; // because we do ireg++ below
177 case 0x01: // Motor 1 MSB
181 case 0x02: // Motor 1 LSB
182 motor1 = tmp<<8 | TWDR;
183 motor1_mode = MOTOR_MANUAL;
186 case 0x03: // Motor 2 MSB
190 case 0x04: // Motor 2 LSB
191 motor2 = tmp<<8 | TWDR;
192 motor2_mode = MOTOR_MANUAL;
195 case 0x05: // Motor 3 MSB
199 case 0x06: // Motor 3 LSB
200 motor3 = tmp<<8 | TWDR;
201 motor3_mode = MOTOR_MANUAL;
204 case 0x07: // Motor 4 MSB
208 case 0x08: // Motor 4 LSB
209 motor4 = tmp<<8 | TWDR;
210 motor4_mode = MOTOR_MANUAL;
213 case 0x20: // Motor 1 speed wish MSB
217 case 0x21: // Motor 1 speed wish LSB
218 speed1_wish = tmp<<8 | TWDR;
219 motor1_mode = MOTOR_PID;
222 case 0x22: // Motor 2 speed wish MSB
226 case 0x23: // Motor 2 speed wish LSB
227 speed2_wish = tmp<<8 | TWDR;
228 motor2_mode = MOTOR_PID;
231 case 0x24: // Motor 3 speed wish MSB
235 case 0x25: // Motor 3 speed wish LSB
236 speed3_wish = tmp<<8 | TWDR;
237 motor3_mode = MOTOR_PID;
240 case 0x26: // Motor 4 speed wish MSB
244 case 0x27: // Motor 4 speed wish LSB
245 speed4_wish = tmp<<8 | TWDR;
246 motor4_mode = MOTOR_PID;
249 case 0x28: // Left speed wish MSB
253 case 0x29: // Left speed wish
254 tmp_speed.i = tmp_speed.i << 8 | TWDR;
257 case 0x2A: // Left speed wish
258 tmp_speed.i = tmp_speed.i << 8 | TWDR;
261 case 0x2B: // Left speed wish LSB
262 tmp_speed.i = tmp_speed.i << 8 | TWDR;
263 speed1_wish = tmp_speed.f*STEP_PER_M;
264 speed2_wish = tmp_speed.f*STEP_PER_M;
265 motor1_mode = MOTOR_PID;
266 motor2_mode = MOTOR_PID;
269 case 0x2C: // Right speed wish MSB
273 case 0x2D: // Right speed wish
274 tmp_speed.i = tmp_speed.i << 8 | TWDR;
277 case 0x2E: // Right speed wish
278 tmp_speed.i = tmp_speed.i << 8 | TWDR;
281 case 0x2F: // Right speed wish LSB
282 tmp_speed.i = tmp_speed.i << 8 | TWDR;
283 speed1_wish = tmp_speed.f*STEP_PER_M;
284 speed2_wish = tmp_speed.f*STEP_PER_M;
285 motor1_mode = MOTOR_PID;
286 motor2_mode = MOTOR_PID;
289 case 0x50: // speed wish MSB
293 case 0x51: // speed wish
294 tmp_speed.i = tmp_speed.i << 8 | TWDR;
297 case 0x52: // speed wish
298 tmp_speed.i = tmp_speed.i << 8 | TWDR;
301 case 0x53: // speed wish LSB
302 tmp_speed.i = tmp_speed.i << 8 | TWDR;
303 cmd_vel.speed = tmp_speed.f;
306 case 0x54: // angle wish MSB
310 case 0x55: // angle wish
311 tmp_angle.i = tmp_angle.i << 8 | TWDR;
314 case 0x56: // angle wish
315 tmp_angle.i = tmp_angle.i << 8 | TWDR;
318 case 0x57: // angle wish LSB
319 tmp_angle.i = tmp_angle.i << 8 | TWDR;
320 cmd_vel.angle = tmp_angle.f;
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
467 tmp_speed.f = cur_speed_lin;
468 TWDR = tmp_speed.i>>24;
472 TWDR = tmp_speed.i>>16;
476 TWDR = tmp_speed.i>>8;
479 case 0x3B: // speed LSB
483 case 0x3C: // angle MSB
484 tmp_angle.f = cur_speed_rot;
485 TWDR = tmp_angle.i>>24;
489 TWDR = tmp_angle.i>>16;
493 TWDR = tmp_angle.i>>8;
496 case 0x3F: // angle LSB
500 case 0x40: // Position x MSB
504 case 0x41: // Position x
508 case 0x42: // Position x
512 case 0x43: // Position x LSB
516 case 0x44: // Position y MSB
520 case 0x45: // Position y
524 case 0x46: // Position y
528 case 0x47: // Position y LSB
532 case 0x48: // Position angle MSB
536 case 0x49: // Position angle
540 case 0x4A: // Position angle
544 case 0x4B: // Position angle LSB
548 case 0x94: // TLE Error status
549 //TWDR = (PIND & 0x40)>>2 | (PINB & 0x07);
565 static void update_hall1(void) {
566 unsigned char status = (PINA >> 0) & 0x3;
567 static unsigned char oldstatus=0;
568 unsigned char diff, new;
574 new ^= 0x1; // convert gray to binary
575 diff = oldstatus - new; // difference last - new
576 if (diff & 0x1) { // bit 0 = value (1)
577 oldstatus = new; // store new as next last
578 if (motor1_switch) pos1 += (diff & 2) - 1; // bit 1 = direction (+/-)
579 else pos1 -= (diff & 2) - 1;
584 static void update_hall2(void) {
585 unsigned char status = (PINA >> 4) & 0x3;
586 static unsigned char oldstatus=0;
587 unsigned char diff, new;
593 new ^= 0x1; // convert gray to binary
594 diff = oldstatus - new; // difference last - new
595 if (diff & 0x1) { // bit 0 = value (1)
596 oldstatus = new; // store new as next last
597 if (motor2_switch) pos2 -= (diff & 2) - 1; // bit 1 = direction (+/-)
598 else pos2 += (diff & 2) - 1;
603 static void update_hall3(void) {
604 unsigned char status = (PINA >> 2) & 0x3;
605 static unsigned char oldstatus=0;
606 unsigned char diff, new;
612 new ^= 0x1; // convert gray to binary
613 diff = oldstatus - new; // difference last - new
614 if (diff & 0x1) { // bit 0 = value (1)
615 oldstatus = new; // store new as next last
616 if (motor3_switch) pos3 -= (diff & 2) - 1; // bit 1 = direction (+/-)
617 else pos3 += (diff & 2) - 1;
622 static void update_hall4(void) {
623 unsigned char status = (PINA >> 6) & 0x3;
624 static unsigned char oldstatus=0;
625 unsigned char diff, new;
631 new ^= 0x1; // convert gray to binary
632 diff = oldstatus - new; // difference last - new
633 if (diff & 0x1) { // bit 0 = value (1)
634 oldstatus = new; // store new as next last
635 if (motor4_switch) pos4 += (diff & 2) - 1; // bit 1 = direction (+/-)
636 else pos4 -= (diff & 2) - 1;
641 static void update_motor(void) {
642 static int16_t m1_old=SHRT_MIN;
643 static int16_t m2_old=SHRT_MIN;
644 static int16_t m3_old=SHRT_MIN;
645 static int16_t m4_old=SHRT_MIN;
647 if (m1_old != motor1) { // update only when changed
650 PORTC |= (1 << 3) | (1 << 2);
651 } else if ((!motor1_switch && motor1 > 0) || (motor1_switch && motor1 < 0)) {
653 PORTC &= ~(1 << 3) & ~(1 << 2);
654 } else { // motor1 < 0
664 if (m2_old != motor2) { // update only when changed
667 PORTC |= (1 << 5) | (1 << 4);
668 } else if ((!motor2_switch && motor2 > 0) || (motor2_switch && motor2 < 0)) {
670 PORTC &= ~(1 << 5) & ~(1 << 4);
671 } else { // motor2 < 0
681 if (m3_old != motor3) { // update only when changed
684 PORTC |= (1 << 7) | (1 << 6);
685 } else if ((!motor3_switch && motor3 > 0) || (motor3_switch && motor3 < 0)) {
687 PORTC &= ~(1 << 7) & ~(1 << 6);
688 } else { // motor3 < 0
698 if (m4_old != motor4) { // update only when changed
701 PORTD |= (1 << 3) | (1 << 2);
702 } else if ((!motor4_switch && motor4 > 0) || (motor4_switch && motor4 < 0)) {
704 PORTD &= ~(1 << 3) & ~(1 << 2);
705 } else { // motor4 < 0
717 static void update_pos(void) {
718 static int16_t pos1_last=0;
719 static int16_t pos2_last=0;
720 static int16_t pos3_last=0;
721 static int16_t pos4_last=0;
722 int16_t pos1_diff; // steps
726 float diff_left_m, diff_right_m, angle_diff, translation;
727 float pos_x_diff, pos_y_diff, angle_new;
728 int16_t speed_l, speed_r;
729 float tmp_speed_lin, tmp_speed_rot;
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;
746 angle_new = angle.f + angle_diff;
747 if (angle_new > 2*M_PI) angle_new-=2*M_PI;
748 else if (angle_new < 2*M_PI) angle_new+=2*M_PI;
750 translation = (diff_left_m + diff_right_m)/2.0;
751 pos_x_diff = cos(angle_new)*translation;
752 pos_y_diff = sin(angle_new)*translation;
756 pos_x.f += pos_x_diff;
757 pos_y.f += pos_y_diff;
760 speed_l = (speed1+speed2)/2;
761 speed_r = (speed3+speed4)/2;
762 tmp_speed_lin = (speed_l + speed_r)/(2.0*STEP_PER_M);
763 tmp_speed_rot = (speed_r - speed_l)/(M_PI*WHEEL_DIST*STEP_PER_M);
766 cur_speed_lin = tmp_speed_lin;
767 cur_speed_rot = tmp_speed_rot;
777 static void update_pid(void) {
778 static int16_t eold1=0;
779 static int16_t eold2=0;
780 static int16_t eold3=0;
781 static int16_t eold4=0;
782 static int32_t esum1=0;
783 static int32_t esum2=0;
784 static int32_t esum3=0;
785 static int32_t esum4=0;
787 if (motor1_mode == MOTOR_PID) {
788 if (speed1_wish == 0) {
793 int16_t e = speed1_wish - speed1;
795 motor1 += KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
798 if (motor1 > 255) motor1 = 255;
799 else if (motor1 < -255) motor1 = -255;
802 if (motor2_mode == MOTOR_PID) {
803 if (speed2_wish == 0) {
808 int16_t e = speed2_wish - speed2;
810 motor2 += KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
813 if (motor2 > 255) motor2 = 255;
814 else if (motor2 < -255) motor2 = -255;
817 if (motor3_mode == MOTOR_PID) {
818 if (speed3_wish == 0) {
823 int16_t e = speed3_wish - speed3;
825 motor3 += KP*e + KI*PID_T*esum3 + KD/PID_T*(e - eold3);
828 if (motor3 > 255) motor3 = 255;
829 else if (motor3 < -255) motor3 = -255;
832 if (motor4_mode == MOTOR_PID) {
833 if (speed4_wish == 0) {
838 int16_t e = speed4_wish - speed4;
840 motor4 += KP*e + KI*PID_T*esum4 + KD/PID_T*(e - eold4);
843 if (motor4 > 255) motor4 = 255;
844 else if (motor4 < -255) motor4 = -255;
850 ISR(TIMER1_OVF_vect) {
863 DDRC = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
864 DDRD = (1 << 7) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
875 // Timer 1: Fast PWM inverting mode, Top=256 => 31.25kHz
877 TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << COM1A0) | (1 << COM1B0) | (1 << WGM10);
878 TCCR1B = (1 << WGM12) | (1 << CS10);
883 // Timer 2: Fast PWM inverting mode, Top=256
885 TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << COM21) | (1 << COM20) | (1 << CS20);
889 // Timer 0: Fast PWM inverting mode, Top=256
891 TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << COM01) | (1 << COM00) | (1 << CS00);
894 printf("\r\nStart\r\n");
896 set_sleep_mode(SLEEP_MODE_IDLE);
897 // Enable Timer 1 Overflow Interrupt
898 TIMSK = (1 << TOIE1);
903 case 0xff: // Magic reg that starts the bootloader
904 if (bootloader == 0xa5) {
907 void (*start)(void) = (void*)0x1800;
914 if (cmd_vel.bUpdate) {
915 float speed_wish_right = cmd_vel.angle*M_PI*WHEEL_DIST/2 + cmd_vel.speed;
916 float speed_wish_left = cmd_vel.speed*2-speed_wish_right;
919 speed_wish_left*=STEP_PER_M;
920 speed_wish_right*=STEP_PER_M;
922 speed1_wish = speed_wish_left;
923 speed2_wish = speed_wish_left;
924 speed3_wish = speed_wish_right;
925 speed4_wish = speed_wish_right;
926 motor1_mode = MOTOR_PID;
927 motor2_mode = MOTOR_PID;
928 motor3_mode = MOTOR_PID;
929 motor4_mode = MOTOR_PID;
932 if (run_update >= 312) { // ~100Hz