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
27 * 0x20 Motor 1 speed wish MSB
28 * 0x21 Motor 1 speed wish LSB
29 * 0x22 Motor 2 speed wish MSB
30 * 0x23 Motor 2 speed wish LSB
31 * 0x28 Left speed wish (m/s) MSB
32 * 0x29 Left speed wish (m/s)
33 * 0x2A Left speed wish (m/s)
34 * 0x2B Left speed wish (m/s) LSB
35 * 0x2C Right speed wish (m/s) MSB
36 * 0x2D Right speed wish (m/s)
37 * 0x2E Right speed wish (m/s)
38 * 0x2F Right speed wish (m/s) LSB
39 * 0x30 Motor 1 speed MSB
40 * 0x31 Motor 1 speed LSB
41 * 0x32 Motor 2 speed MSB
42 * 0x33 Motor 2 speed LSB
43 * 0x38 Speed (m/s) MSB
46 * 0x3B Speed (m/s) LSB
47 * 0x3C Angle (rad/s) MSB
50 * 0x3F Angle (rad/s) LSB
51 * 0x40 Position x (m) MSB
54 * 0x43 Position x (m) LSB
55 * 0x44 Position y (m) MSB
58 * 0x47 Position y (m) LSB
59 * 0x48 Position angle MSB
62 * 0x4B Position angle LSB
64 * 0x50 speed wish (m/s) MSB
65 * 0x51 speed wish (m/s)
66 * 0x52 speed wish (m/s)
67 * 0x53 speed wish (m/s) LSB
68 * 0x54 angle wish (rad/s) MSB
69 * 0x55 angle wish (rad/s)
70 * 0x56 angle wish (rad/s)
71 * 0x57 angle wish (rad/s) LSB
84 #define TWI_ACK TWCR = (1<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
85 #define TWI_RESET TWCR &= ~((1 << TWSTO) | (1 << TWEN)); TWI_ACK
86 #define TWI_NAK TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
92 // wheel diameter=12cm, encoder=48cpr, gear ratio=1:34, real wheel diameter: 0.12454m
93 #define STEP_PER_M_AVG 4171.4
94 #define STEP_PER_M_LEFT (STEP_PER_M_AVG)
95 #define STEP_PER_M_RIGHT (STEP_PER_M_AVG)
96 #define WHEEL_DIST 0.36923 // Real: 0.252
108 static volatile struct {
112 } cmd_vel = {0, 0, 0};
114 static volatile uint8_t ireg=0;
115 static volatile uint8_t bootloader=0;
116 static volatile int16_t motor1=0; // -255..+255
117 static volatile int16_t motor2=0;
118 static volatile int16_t pos1=0; // step
119 static volatile int16_t pos2=0;
120 static volatile int16_t pos3=0;
121 static volatile int16_t pos4=0;
122 static volatile enum mode motor1_mode=MOTOR_MANUAL;
123 static volatile enum mode motor2_mode=MOTOR_MANUAL;
124 static volatile uint8_t motor1_switch=0;
125 static volatile uint8_t motor2_switch=1;
126 static volatile int16_t speed1_wish=0; // step/s
127 static volatile int16_t speed2_wish=0;
128 static volatile uint8_t run_update=0;
129 static volatile int16_t speed1=0; // step/s
130 static volatile int16_t speed2=0;
131 static volatile int16_t speed3=0;
132 static volatile int16_t speed4=0;
133 static volatile int16_t speed_l=0;
134 static volatile int16_t speed_r=0;
135 static volatile ufloat_t pos_x={0.0};
136 static volatile ufloat_t pos_y={0.0};
137 static volatile ufloat_t angle={0.0};
138 static volatile float cur_speed_lin=0;
139 static volatile float cur_speed_rot=0;
140 static volatile uint8_t count_test=0;
141 static volatile uint8_t error_state=0;
145 static uint8_t tmp=0;
146 static int16_t tmp16=0;
147 static ufloat_t tmp_speed;
148 static ufloat_t tmp_angle;
152 case 0x60: // start write
158 case 0x00: // register select
160 ireg--; // because we do ireg++ below
163 case 0x01: // Motor 1 MSB
167 case 0x02: // Motor 1 LSB
168 motor1 = tmp<<8 | TWDR;
169 motor1_mode = MOTOR_MANUAL;
172 case 0x03: // Motor 2 MSB
176 case 0x04: // Motor 2 LSB
177 motor2 = tmp<<8 | TWDR;
178 motor2_mode = MOTOR_MANUAL;
181 case 0x20: // Motor 1 speed wish MSB
185 case 0x21: // Motor 1 speed wish LSB
186 speed1_wish = tmp<<8 | TWDR;
187 motor1_mode = MOTOR_PID;
190 case 0x22: // Motor 2 speed wish MSB
194 case 0x23: // Motor 2 speed wish LSB
195 speed2_wish = tmp<<8 | TWDR;
196 motor2_mode = MOTOR_PID;
199 case 0x28: // Left speed wish MSB
203 case 0x29: // Left speed wish
204 tmp_speed.i = tmp_speed.i << 8 | TWDR;
207 case 0x2A: // Left speed wish
208 tmp_speed.i = tmp_speed.i << 8 | TWDR;
211 case 0x2B: // Left speed wish LSB
212 tmp_speed.i = tmp_speed.i << 8 | TWDR;
213 speed1_wish = tmp_speed.f*STEP_PER_M_LEFT;
214 speed2_wish = tmp_speed.f*STEP_PER_M_LEFT;
215 motor1_mode = MOTOR_PID;
216 motor2_mode = MOTOR_PID;
219 case 0x2C: // Right speed wish MSB
223 case 0x2D: // Right speed wish
224 tmp_speed.i = tmp_speed.i << 8 | TWDR;
227 case 0x2E: // Right speed wish
228 tmp_speed.i = tmp_speed.i << 8 | TWDR;
231 case 0x2F: // Right speed wish LSB
232 tmp_speed.i = tmp_speed.i << 8 | TWDR;
233 speed1_wish = tmp_speed.f*STEP_PER_M_RIGHT;
234 speed2_wish = tmp_speed.f*STEP_PER_M_RIGHT;
235 motor1_mode = MOTOR_PID;
236 motor2_mode = MOTOR_PID;
239 case 0x50: // speed wish MSB
243 case 0x51: // speed wish
244 tmp_speed.i = tmp_speed.i << 8 | TWDR;
247 case 0x52: // speed wish
248 tmp_speed.i = tmp_speed.i << 8 | TWDR;
251 case 0x53: // speed wish LSB
252 tmp_speed.i = tmp_speed.i << 8 | TWDR;
253 cmd_vel.speed = tmp_speed.f;
256 case 0x54: // angle wish MSB
260 case 0x55: // angle wish
261 tmp_angle.i = tmp_angle.i << 8 | TWDR;
264 case 0x56: // angle wish
265 tmp_angle.i = tmp_angle.i << 8 | TWDR;
268 case 0x57: // angle wish LSB
269 tmp_angle.i = tmp_angle.i << 8 | TWDR;
270 cmd_vel.angle = tmp_angle.f;
274 case 0x90: // Motor 1 switch
275 motor1_switch = TWDR;
278 case 0x91: // Motor 2 switch
279 motor2_switch = TWDR;
282 case 0xff: // bootloader
289 case 0xA8: // start read
292 case 0x02: // Motor 1 PWM
296 case 0x04: // Motor 2 PWM
300 case 0x10: // Hall 1 MSB
305 case 0x11: // Hall 1 LSB
309 case 0x12: // Hall 2 MSB
314 case 0x13: // Hall 2 LSB
318 case 0x14: // Hall 3 MSB
323 case 0x15: // Hall 3 LSB
327 case 0x16: // Hall 4 MSB
332 case 0x17: // Hall 4 LSB
336 case 0x20: // Motor 1 speed wish MSB
337 TWDR = speed1_wish>>8;
340 case 0x21: // Motor 1 speed wish LSB
344 case 0x22: // Motor 2 speed wish MSB
345 TWDR = speed2_wish>>8;
348 case 0x23: // Motor 2 speed wish LSB
352 case 0x30: // Motor 1 speed MSB
356 case 0x31: // Motor 1 speed LSB
360 case 0x32: // Motor 2 speed MSB
364 case 0x33: // Motor 2 speed LSB
368 case 0x34: // Motor 3 speed MSB
372 case 0x35: // Motor 3 speed LSB
376 case 0x36: // Motor 4 speed MSB
380 case 0x37: // Motor 4 speed LSB
384 case 0x38: // speed MSB
385 tmp_speed.f = cur_speed_lin;
386 TWDR = tmp_speed.i>>24;
390 TWDR = tmp_speed.i>>16;
394 TWDR = tmp_speed.i>>8;
397 case 0x3B: // speed LSB
401 case 0x3C: // angle MSB
402 tmp_angle.f = cur_speed_rot;
403 TWDR = tmp_angle.i>>24;
407 TWDR = tmp_angle.i>>16;
411 TWDR = tmp_angle.i>>8;
414 case 0x3F: // angle LSB
418 case 0x40: // Position x MSB
422 case 0x41: // Position x
426 case 0x42: // Position x
430 case 0x43: // Position x LSB
434 case 0x44: // Position y MSB
438 case 0x45: // Position y
442 case 0x46: // Position y
446 case 0x47: // Position y LSB
450 case 0x48: // Position angle MSB
454 case 0x49: // Position angle
458 case 0x4A: // Position angle
462 case 0x4B: // Position angle LSB
466 case 0xA0: // Reset reason
467 TWDR = MCUCSR & 0x0f;
471 case 0xA1: // TLE Error status
475 case 0xA2: // count test
490 static void update_hall1(void) {
491 unsigned char status = (PINA >> 0) & 0x3;
492 static unsigned char oldstatus=0;
493 unsigned char diff, new;
499 new ^= 0x1; // convert gray to binary
500 diff = oldstatus - new; // difference last - new
501 if (diff & 0x1) { // bit 0 = value (1)
502 oldstatus = new; // store new as next last
503 if (motor1_switch) pos1 -= (diff & 2) - 1; // bit 1 = direction (+/-)
504 else pos1 += (diff & 2) - 1;
509 static void update_hall2(void) {
510 unsigned char status = (PINA >> 4) & 0x3;
511 static unsigned char oldstatus=0;
512 unsigned char diff, new;
518 new ^= 0x1; // convert gray to binary
519 diff = oldstatus - new; // difference last - new
520 if (diff & 0x1) { // bit 0 = value (1)
521 oldstatus = new; // store new as next last
522 if (motor1_switch) pos2 -= (diff & 2) - 1; // bit 1 = direction (+/-)
523 else pos2 += (diff & 2) - 1;
528 static void update_hall3(void) {
529 unsigned char status = (PINA >> 2) & 0x3;
530 static unsigned char oldstatus=0;
531 unsigned char diff, new;
537 new ^= 0x1; // convert gray to binary
538 diff = oldstatus - new; // difference last - new
539 if (diff & 0x1) { // bit 0 = value (1)
540 oldstatus = new; // store new as next last
541 if (motor2_switch) pos3 += (diff & 2) - 1; // bit 1 = direction (+/-)
542 else pos3 -= (diff & 2) - 1;
547 static void update_hall4(void) {
548 unsigned char status = (PINA >> 6) & 0x3;
549 static unsigned char oldstatus=0;
550 unsigned char diff, new;
556 new ^= 0x1; // convert gray to binary
557 diff = oldstatus - new; // difference last - new
558 if (diff & 0x1) { // bit 0 = value (1)
559 oldstatus = new; // store new as next last
560 if (motor2_switch) pos4 -= (diff & 2) - 1; // bit 1 = direction (+/-)
561 else pos4 += (diff & 2) - 1;
566 static void update_motor(void) {
567 static int16_t m1_old=SHRT_MIN;
568 static int16_t m2_old=SHRT_MIN;
570 error_state = ~(PINB & 0x03);
572 if (motor1_mode == MOTOR_PID && bit_is_set(error_state, 0)) {
573 // if error and running: stop
574 if (m1_old != 0) motor1 = 0;
575 // if we start motor in error state: start with full power
576 else if (motor1 > 0) motor1 = 255;
577 else if (motor1 < 0) motor1 = -255;
579 if (motor2_mode == MOTOR_PID && bit_is_set(error_state, 1)) {
580 // if error and running: stop
581 if (m2_old != 0) motor2 = 0;
582 // if we start motor in error state: start with full power
583 else if (motor2 > 0) motor2 = 255;
584 else if (motor2 < 0) motor2 = -255;
587 if (m1_old != motor1) { // update only when changed
590 PORTC &= ~(1 << 3) & ~(1 << 2);
591 } else if ((!motor1_switch && motor1 > 0) || (motor1_switch && motor1 < 0)) {
595 } else { // motor1 < 0
605 if (m2_old != motor2) { // update only when changed
608 PORTC &= ~(1 << 5) & ~(1 << 4);
609 } else if ((!motor2_switch && motor2 > 0) || (motor2_switch && motor2 < 0)) {
613 } else { // motor2 < 0
625 static void update_pos(void) {
626 static int16_t pos1_last=0;
627 static int16_t pos2_last=0;
628 static int16_t pos3_last=0;
629 static int16_t pos4_last=0;
630 int16_t pos1_diff; // steps
634 float diff_left_m, diff_right_m, angle_diff, translation;
635 float pos_x_new, pos_y_new, angle_new;
636 float tmp_speed_lin, tmp_speed_rot;
637 int16_t cur_pos1, cur_pos2, cur_pos3, cur_pos4;
638 int16_t new_speed1, new_speed2, new_speed3, new_speed4;
648 pos1_diff = cur_pos1 - pos1_last;
649 pos2_diff = cur_pos2 - pos2_last;
650 pos3_diff = cur_pos3 - pos3_last;
651 pos4_diff = cur_pos4 - pos4_last;
653 new_speed1 = pos1_diff/PID_T;
654 new_speed2 = pos2_diff/PID_T;
655 new_speed3 = pos3_diff/PID_T;
656 new_speed4 = pos4_diff/PID_T;
658 diff_left_m = (pos1_diff + pos2_diff)/(2*STEP_PER_M_LEFT);
659 diff_right_m = (pos3_diff + pos4_diff)/(2*STEP_PER_M_RIGHT);
660 angle_diff = (diff_right_m - diff_left_m) / WHEEL_DIST;
662 angle_new = angle.f + angle_diff;
663 if (angle_new > 2*M_PI) angle_new-=2*M_PI;
664 else if (angle_new < -2*M_PI) angle_new+=2*M_PI;
666 translation = (diff_left_m + diff_right_m)/2.0;
667 pos_x_new = pos_x.f + cos(angle_new)*translation;
668 pos_y_new = pos_y.f + sin(angle_new)*translation;
670 speed_l = (new_speed1+new_speed2)/2;
671 speed_r = (new_speed3+new_speed4)/2;
672 tmp_speed_lin = (speed_l + speed_r)/(2.0*STEP_PER_M_AVG);
673 tmp_speed_rot = (speed_r - speed_l)/(M_PI*WHEEL_DIST*STEP_PER_M_AVG);
684 cur_speed_lin = tmp_speed_lin;
685 cur_speed_rot = tmp_speed_rot;
688 pos1_last = cur_pos1;
689 pos2_last = cur_pos2;
690 pos3_last = cur_pos3;
691 pos4_last = cur_pos4;
695 static void update_pid(void) {
696 static int16_t eold1=0;
697 static int16_t eold2=0;
698 static int32_t esum1=0;
699 static int32_t esum2=0;
701 if (motor1_mode == MOTOR_PID) {
702 if (speed1_wish == 0) {
707 int16_t e = speed1_wish - speed_l;
709 motor1 = KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
712 if (motor1 > 255) motor1 = 255;
713 else if (motor1 < -255) motor1 = -255;
716 if (motor2_mode == MOTOR_PID) {
717 if (speed2_wish == 0) {
722 int16_t e = speed2_wish - speed_r;
724 motor2 = KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
727 if (motor2 > 255) motor2 = 255;
728 else if (motor2 < -255) motor2 = -255;
734 ISR(TIMER1_OVF_vect) {
747 DDRC = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
748 DDRD = (1 << 7) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
759 // Timer 1: Fast PWM non-inverting mode, Top=256 => 15.625kHz
761 TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10);
762 TCCR1B = (1 << WGM12) | (1 << CS10);
766 printf("\r\nStart\r\n");
768 set_sleep_mode(SLEEP_MODE_IDLE);
769 // Enable Timer 1 Overflow Interrupt
770 TIMSK = (1 << TOIE1);
775 case 0xff: // Magic reg that starts the bootloader
776 if (bootloader == 0xa5) {
779 void (*start)(void) = (void*)0x1800;
786 if (cmd_vel.bUpdate) {
787 float speed_wish_right, speed_wish_left;
791 speed = cmd_vel.speed;
792 angle = cmd_vel.angle;
796 speed_wish_right = angle*M_PI*WHEEL_DIST/2 + speed;
797 speed_wish_left = speed*2-speed_wish_right;
799 speed_wish_left*=STEP_PER_M_LEFT;
800 speed_wish_right*=STEP_PER_M_RIGHT;
802 speed1_wish = speed_wish_left;
803 speed2_wish = speed_wish_right;
804 motor1_mode = MOTOR_PID;
805 motor2_mode = MOTOR_PID;
808 if (run_update >= 156) { // ~100Hz