6 #include <avr/interrupt.h>
9 #include <avr/eeprom.h>
14 * I2C Register Map (8 Bit)
15 * 0x00 Register select
16 * 0x01 Motor 1 PWM MSB
17 * 0x02 Motor 1 PWM LSB
18 * 0x03 Motor 2 PWM MSB
19 * 0x04 Motor 2 PWM LSB
20 * 0x05 Motor 3 PWM MSB
21 * 0x06 Motor 3 PWM LSB
22 * 0x07 Motor 4 PWM MSB
23 * 0x08 Motor 4 PWM LSB
34 * 0x20 Motor 1 speed wish MSB
35 * 0x21 Motor 1 speed wish LSB
36 * 0x22 Motor 2 speed wish MSB
37 * 0x23 Motor 2 speed wish LSB
38 * 0x24 Motor 3 speed wish MSB
39 * 0x25 Motor 3 speed wish LSB
40 * 0x26 Motor 4 speed wish MSB
41 * 0x27 Motor 4 speed wish LSB
42 * 0x28 Left speed wish (m/s) MSB
43 * 0x29 Left speed wish (m/s)
44 * 0x2A Left speed wish (m/s)
45 * 0x2B Left speed wish (m/s) LSB
46 * 0x2C Right speed wish (m/s) MSB
47 * 0x2D Right speed wish (m/s)
48 * 0x2E Right speed wish (m/s)
49 * 0x2F Right speed wish (m/s) LSB
50 * 0x30 Motor 1 speed MSB
51 * 0x31 Motor 1 speed LSB
52 * 0x32 Motor 2 speed MSB
53 * 0x33 Motor 2 speed LSB
54 * 0x34 Motor 3 speed MSB
55 * 0x35 Motor 3 speed LSB
56 * 0x36 Motor 4 speed MSB
57 * 0x37 Motor 4 speed LSB
58 * 0x38 Speed (m/s) MSB
61 * 0x3B Speed (m/s) LSB
62 * 0x3C Angle (rad/s) MSB
65 * 0x3F Angle (rad/s) LSB
66 * 0x40 Position x (m) MSB
69 * 0x43 Position x (m) LSB
70 * 0x44 Position y (m) MSB
73 * 0x47 Position y (m) LSB
74 * 0x48 Position angle MSB
77 * 0x4B Position angle LSB
79 * 0x50 speed wish (m/s) MSB
80 * 0x51 speed wish (m/s)
81 * 0x52 speed wish (m/s)
82 * 0x53 speed wish (m/s) LSB
83 * 0x54 angle wish (rad/s) MSB
84 * 0x55 angle wish (rad/s)
85 * 0x56 angle wish (rad/s)
86 * 0x57 angle wish (rad/s) LSB
107 // wheel diameter=12cm, encoder=48cpr, gear ratio=1:47
108 // STEP_PER_M = 48*47/(d*pi)
109 // Left real diameter: 0.12808, Right real diameter: 0.121
110 #define STEP_PER_M 5573.0
111 #define STEP_PER_M_LEFT (STEP_PER_M)
112 #define STEP_PER_M_RIGHT (STEP_PER_M)
113 #define WHEEL_DIST 0.39912 // Measured: 0.252
114 #define PWM_BREAK INT16_MIN
115 #define STALL_LIMIT 140000
116 #define I2C_TIMEOUT_DISABLE 255
118 #define TWI_ACK TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) | (1<<TWIE)
119 #define TWI_NAK TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
120 #define TWI_RESET TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWSTO) | (1<<TWEN) | (1<<TWIE);
121 #define ENABLE_PWM_MOTOR1 TCCR1A |= (1 << COM1A1)
122 #define ENABLE_PWM_MOTOR2 TCCR1A |= (1 << COM1B1)
123 #define ENABLE_PWM_MOTOR3 TCCR2 |= (1 << COM21);
124 #define ENABLE_PWM_MOTOR4 TCCR0 |= (1 << COM01);
125 #define DISABLE_PWM_MOTOR1 TCCR1A &= ~(1 << COM1A1)
126 #define DISABLE_PWM_MOTOR2 TCCR1A &= ~(1 << COM1B1)
127 #define DISABLE_PWM_MOTOR3 TCCR2 &= ~(1 << COM21);
128 #define DISABLE_PWM_MOTOR4 TCCR0 &= ~(1 << COM01);
141 static volatile struct {
145 } cmd_vel = {0, 0, 0};
147 static volatile uint8_t ireg=0;
148 static volatile uint8_t bootloader=0;
149 static volatile int16_t motor1=0; // -255..+255
150 static volatile int16_t motor2=0;
151 static volatile int16_t motor3=0;
152 static volatile int16_t motor4=0;
153 static volatile int16_t pos1=0; // step
154 static volatile int16_t pos2=0;
155 static volatile int16_t pos3=0;
156 static volatile int16_t pos4=0;
157 static volatile enum mode motor1_mode=MOTOR_MANUAL;
158 static volatile enum mode motor2_mode=MOTOR_MANUAL;
159 static volatile enum mode motor3_mode=MOTOR_MANUAL;
160 static volatile enum mode motor4_mode=MOTOR_MANUAL;
161 static volatile uint8_t motor1_switch=1;
162 static volatile uint8_t motor2_switch=1;
163 static volatile uint8_t motor3_switch=0;
164 static volatile uint8_t motor4_switch=0;
165 static volatile int16_t speed1_wish=0; // step/s
166 static volatile int16_t speed2_wish=0;
167 static volatile int16_t speed3_wish=0;
168 static volatile int16_t speed4_wish=0;
169 static volatile int16_t speed1_wish_old=0;
170 static volatile int16_t speed2_wish_old=0;
171 static volatile int16_t speed3_wish_old=0;
172 static volatile int16_t speed4_wish_old=0;
173 static volatile uint8_t run_update=0;
174 static volatile int16_t speed1=0; // step/s
175 static volatile int16_t speed2=0;
176 static volatile int16_t speed3=0;
177 static volatile int16_t speed4=0;
178 static volatile ufloat_t pos_x={0.0};
179 static volatile ufloat_t pos_y={0.0};
180 static volatile ufloat_t angle={0.0};
181 static volatile float cur_speed_lin=0;
182 static volatile float cur_speed_rot=0;
183 static volatile uint8_t count_test=0;
184 static volatile uint8_t front_handicap=0;
185 static volatile uint8_t aft_handicap=0;
186 static volatile uint8_t error_state=0;
187 static volatile uint8_t last_man_update_count=I2C_TIMEOUT_DISABLE;
191 static uint8_t tmp=0;
192 static int16_t tmp16=0;
193 static ufloat_t tmp_speed;
194 static ufloat_t tmp_angle;
198 case TW_SR_SLA_ACK: // start write
202 case TW_SR_DATA_ACK: // write
204 case 0x00: // register select
206 ireg--; // because we do ireg++ below
209 case 0x01: // Motor 1 MSB
213 case 0x02: // Motor 1 LSB
214 motor1 = tmp<<8 | TWDR;
215 motor1_mode = MOTOR_MANUAL;
218 case 0x03: // Motor 2 MSB
222 case 0x04: // Motor 2 LSB
223 motor2 = tmp<<8 | TWDR;
224 motor2_mode = MOTOR_MANUAL;
227 case 0x05: // Motor 3 MSB
231 case 0x06: // Motor 3 LSB
232 motor3 = tmp<<8 | TWDR;
233 motor3_mode = MOTOR_MANUAL;
236 case 0x07: // Motor 4 MSB
240 case 0x08: // Motor 4 LSB
241 motor4 = tmp<<8 | TWDR;
242 motor4_mode = MOTOR_MANUAL;
245 case 0x20: // Motor 1 speed wish MSB
249 case 0x21: // Motor 1 speed wish LSB
250 speed1_wish = tmp<<8 | TWDR;
251 motor1_mode = MOTOR_PID;
254 case 0x22: // Motor 2 speed wish MSB
258 case 0x23: // Motor 2 speed wish LSB
259 speed2_wish = tmp<<8 | TWDR;
260 motor2_mode = MOTOR_PID;
263 case 0x24: // Motor 3 speed wish MSB
267 case 0x25: // Motor 3 speed wish LSB
268 speed3_wish = tmp<<8 | TWDR;
269 motor3_mode = MOTOR_PID;
272 case 0x26: // Motor 4 speed wish MSB
276 case 0x27: // Motor 4 speed wish LSB
277 speed4_wish = tmp<<8 | TWDR;
278 motor4_mode = MOTOR_PID;
281 case 0x28: // Left speed wish MSB
285 case 0x29: // Left speed wish
286 tmp_speed.i = tmp_speed.i << 8 | TWDR;
289 case 0x2A: // Left speed wish
290 tmp_speed.i = tmp_speed.i << 8 | TWDR;
293 case 0x2B: // Left speed wish LSB
294 tmp_speed.i = tmp_speed.i << 8 | TWDR;
295 speed1_wish = tmp_speed.f*STEP_PER_M_LEFT;
296 speed2_wish = tmp_speed.f*STEP_PER_M_LEFT;
297 motor1_mode = MOTOR_PID;
298 motor2_mode = MOTOR_PID;
301 case 0x2C: // Right speed wish MSB
305 case 0x2D: // Right speed wish
306 tmp_speed.i = tmp_speed.i << 8 | TWDR;
309 case 0x2E: // Right speed wish
310 tmp_speed.i = tmp_speed.i << 8 | TWDR;
313 case 0x2F: // Right speed wish LSB
314 tmp_speed.i = tmp_speed.i << 8 | TWDR;
315 speed1_wish = tmp_speed.f*STEP_PER_M_RIGHT;
316 speed2_wish = tmp_speed.f*STEP_PER_M_RIGHT;
317 motor1_mode = MOTOR_PID;
318 motor2_mode = MOTOR_PID;
321 case 0x50: // speed wish MSB
325 case 0x51: // speed wish
326 tmp_speed.i = tmp_speed.i << 8 | TWDR;
329 case 0x52: // speed wish
330 tmp_speed.i = tmp_speed.i << 8 | TWDR;
333 case 0x53: // speed wish LSB
334 tmp_speed.i = tmp_speed.i << 8 | TWDR;
335 cmd_vel.speed = tmp_speed.f;
338 case 0x54: // angle wish MSB
342 case 0x55: // angle wish
343 tmp_angle.i = tmp_angle.i << 8 | TWDR;
346 case 0x56: // angle wish
347 tmp_angle.i = tmp_angle.i << 8 | TWDR;
350 case 0x57: // angle wish LSB
351 tmp_angle.i = tmp_angle.i << 8 | TWDR;
352 cmd_vel.angle = tmp_angle.f;
354 last_man_update_count = 0;
357 case 0x90: // Motor 1 switch
358 motor1_switch = TWDR;
361 case 0x91: // Motor 2 switch
362 motor2_switch = TWDR;
365 case 0x92: // Motor 3 switch
366 motor3_switch = TWDR;
369 case 0x93: // Motor 4 switch
370 motor4_switch = TWDR;
373 case 0x94: // Front Handicap
374 front_handicap = TWDR;
378 case 0x95: // Aft Handicap
383 case 0xff: // bootloader
388 if (ireg < 0xff) ireg++;
390 case TW_ST_SLA_ACK: // start read
391 case TW_ST_DATA_ACK: // read
393 case 0x02: // Motor 1 PWM
397 case 0x03: // Dummy to allow continous read
401 case 0x04: // Motor 2 PWM
405 case 0x05: // Dummy to allow continous read
409 case 0x06: // Motor 3 PWM
413 case 0x07: // Dummy to allow continous read
417 case 0x08: // Motor 4 PWM
421 case 0x09: // Dummy to allow continous read
425 case 0x10: // Hall 1 MSB
430 case 0x11: // Hall 1 LSB
434 case 0x12: // Hall 2 MSB
439 case 0x13: // Hall 2 LSB
443 case 0x14: // Hall 3 MSB
448 case 0x15: // Hall 3 LSB
452 case 0x16: // Hall 4 MSB
457 case 0x17: // Hall 4 LSB
461 case 0x20: // Motor 1 speed wish MSB
462 TWDR = speed1_wish>>8;
465 case 0x21: // Motor 1 speed wish LSB
469 case 0x22: // Motor 2 speed wish MSB
470 TWDR = speed2_wish>>8;
473 case 0x23: // Motor 2 speed wish LSB
477 case 0x24: // Motor 3 speed wish MSB
478 TWDR = speed3_wish>>8;
481 case 0x25: // Motor 3 speed wish LSB
485 case 0x26: // Motor 4 speed wish MSB
486 TWDR = speed4_wish>>8;
489 case 0x27: // Motor 4 speed wish LSB
493 case 0x30: // Motor 1 speed MSB
497 case 0x31: // Motor 1 speed LSB
501 case 0x32: // Motor 2 speed MSB
505 case 0x33: // Motor 2 speed LSB
509 case 0x34: // Motor 3 speed MSB
513 case 0x35: // Motor 3 speed LSB
517 case 0x36: // Motor 4 speed MSB
521 case 0x37: // Motor 4 speed LSB
525 case 0x38: // speed MSB
526 tmp_speed.f = cur_speed_lin;
527 TWDR = tmp_speed.i>>24;
531 TWDR = tmp_speed.i>>16;
535 TWDR = tmp_speed.i>>8;
538 case 0x3B: // speed LSB
542 case 0x3C: // angle MSB
543 tmp_angle.f = cur_speed_rot;
544 TWDR = tmp_angle.i>>24;
548 TWDR = tmp_angle.i>>16;
552 TWDR = tmp_angle.i>>8;
555 case 0x3F: // angle LSB
559 case 0x40: // Position x MSB
563 case 0x41: // Position x
567 case 0x42: // Position x
571 case 0x43: // Position x LSB
575 case 0x44: // Position y MSB
579 case 0x45: // Position y
583 case 0x46: // Position y
587 case 0x47: // Position y LSB
591 case 0x48: // Position angle MSB
595 case 0x49: // Position angle
599 case 0x4A: // Position angle
603 case 0x4B: // Position angle LSB
607 case 0xA0: // Reset reason
608 TWDR = MCUCSR & 0x0f;
612 case 0xA1: // Error status
616 case 0xA2: // count test
634 static void update_hall1(void) {
635 unsigned char status = (PINA >> 0) & 0x3;
636 static unsigned char oldstatus=0;
637 unsigned char diff, new;
643 new ^= 0x1; // convert gray to binary
644 diff = oldstatus - new; // difference last - new
645 if (diff & 0x1) { // bit 0 = value (1)
646 oldstatus = new; // store new as next last
647 if (motor1_switch) pos1 += (diff & 2) - 1; // bit 1 = direction (+/-)
648 else pos1 -= (diff & 2) - 1;
653 static void update_hall2(void) {
654 unsigned char status = (PINA >> 4) & 0x3;
655 static unsigned char oldstatus=0;
656 unsigned char diff, new;
662 new ^= 0x1; // convert gray to binary
663 diff = oldstatus - new; // difference last - new
664 if (diff & 0x1) { // bit 0 = value (1)
665 oldstatus = new; // store new as next last
666 if (motor2_switch) pos2 -= (diff & 2) - 1; // bit 1 = direction (+/-)
667 else pos2 += (diff & 2) - 1;
672 static void update_hall3(void) {
673 unsigned char status = (PINA >> 2) & 0x3;
674 static unsigned char oldstatus=0;
675 unsigned char diff, new;
681 new ^= 0x1; // convert gray to binary
682 diff = oldstatus - new; // difference last - new
683 if (diff & 0x1) { // bit 0 = value (1)
684 oldstatus = new; // store new as next last
685 if (motor3_switch) pos3 -= (diff & 2) - 1; // bit 1 = direction (+/-)
686 else pos3 += (diff & 2) - 1;
691 static void update_hall4(void) {
692 unsigned char status = (PINA >> 6) & 0x3;
693 static unsigned char oldstatus=0;
694 unsigned char diff, new;
700 new ^= 0x1; // convert gray to binary
701 diff = oldstatus - new; // difference last - new
702 if (diff & 0x1) { // bit 0 = value (1)
703 oldstatus = new; // store new as next last
704 if (motor4_switch) pos4 += (diff & 2) - 1; // bit 1 = direction (+/-)
705 else pos4 -= (diff & 2) - 1;
710 static void update_motor(void) {
711 static int16_t m1_old=SHRT_MIN;
712 static int16_t m2_old=SHRT_MIN;
713 static int16_t m3_old=SHRT_MIN;
714 static int16_t m4_old=SHRT_MIN;
716 error_state &= 0xf0; // clear lower bits
717 error_state |= ~((PIND & 0x40)>>3 | (PINB & 0x07)) & 0xf;
719 if (m1_old != motor1) { // update only when changed
722 PORTC &= ~(1 << 3) & ~(1 << 2);
724 } else if (motor1 == PWM_BREAK) {
725 PORTC |= (1 << 3) | (1 << 2);
727 } else if ((!motor1_switch && motor1 > 0) || (motor1_switch && motor1 < 0)) {
734 } else { // motor1 < 0
747 if (m2_old != motor2) { // update only when changed
750 PORTC &= ~(1 << 5) & ~(1 << 4);
752 } else if (motor2 == PWM_BREAK) {
753 PORTC |= (1 << 5) | (1 << 4);
755 } else if ((!motor2_switch && motor2 > 0) || (motor2_switch && motor2 < 0)) {
762 } else { // motor2 < 0
775 if (m3_old != motor3) { // update only when changed
778 PORTC &= ~(1 << 7) & ~(1 << 6);
780 } else if (motor3 == PWM_BREAK) {
781 PORTC |= (1 << 7) | (1 << 6);
783 } else if ((!motor3_switch && motor3 > 0) || (motor3_switch && motor3 < 0)) {
790 } else { // motor3 < 0
803 if (m4_old != motor4) { // update only when changed
806 PORTD &= ~(1 << 3) & ~(1 << 2);
808 } else if (motor4 == PWM_BREAK) {
809 PORTD |= (1 << 3) | (1 << 2);
811 } else if ((!motor4_switch && motor4 > 0) || (motor4_switch && motor4 < 0)) {
818 } else { // motor4 < 0
833 static void update_pos(void) {
834 static int16_t pos1_last=0;
835 static int16_t pos2_last=0;
836 static int16_t pos3_last=0;
837 static int16_t pos4_last=0;
838 int16_t pos1_diff; // steps
842 float diff_left_m, diff_right_m, angle_diff, translation;
843 float pos_x_new, pos_y_new, angle_new;
844 float tmp_speed_lin, tmp_speed_rot;
845 int16_t cur_pos1, cur_pos2, cur_pos3, cur_pos4;
846 int16_t new_speed1, new_speed2, new_speed3, new_speed4;
856 pos1_diff = cur_pos1 - pos1_last;
857 pos2_diff = cur_pos2 - pos2_last;
858 pos3_diff = cur_pos3 - pos3_last;
859 pos4_diff = cur_pos4 - pos4_last;
861 new_speed1 = pos1_diff/PID_T;
862 new_speed2 = pos2_diff/PID_T;
863 new_speed3 = pos3_diff/PID_T;
864 new_speed4 = pos4_diff/PID_T;
866 diff_left_m = (pos1_diff + pos2_diff)/(2*STEP_PER_M_LEFT);
867 diff_right_m = (pos3_diff + pos4_diff)/(2*STEP_PER_M_RIGHT);
868 angle_diff = (diff_right_m - diff_left_m) / WHEEL_DIST;
870 angle_new = angle.f + angle_diff;
871 if (angle_new > 2*M_PI) angle_new-=2*M_PI;
872 else if (angle_new < -2*M_PI) angle_new+=2*M_PI;
874 translation = (diff_left_m + diff_right_m)/2.0;
875 pos_x_new = pos_x.f + cos(angle_new)*translation;
876 pos_y_new = pos_y.f + sin(angle_new)*translation;
878 tmp_speed_lin = translation/PID_T;
879 tmp_speed_rot = angle_diff/PID_T;
890 cur_speed_lin = tmp_speed_lin;
891 cur_speed_rot = tmp_speed_rot;
894 pos1_last = cur_pos1;
895 pos2_last = cur_pos2;
896 pos3_last = cur_pos3;
897 pos4_last = cur_pos4;
901 static void update_pid(void) {
902 static int16_t eold1=0;
903 static int16_t eold2=0;
904 static int16_t eold3=0;
905 static int16_t eold4=0;
906 static int32_t esum1=0;
907 static int32_t esum2=0;
908 static int32_t esum3=0;
909 static int32_t esum4=0;
911 // protect motors from damage if stalling
912 if (labs(esum1) > STALL_LIMIT && speed1 == 0) {
914 motor1_mode = MOTOR_MANUAL;
915 error_state |= (1<<4);
918 if (labs(esum2) > STALL_LIMIT && speed2 == 0) {
920 motor2_mode = MOTOR_MANUAL;
921 error_state |= (1<<5);
924 if (labs(esum3) > STALL_LIMIT && speed3 == 0) {
926 motor3_mode = MOTOR_MANUAL;
927 error_state |= (1<<6);
930 if (labs(esum4) > STALL_LIMIT && speed4 == 0) {
932 motor4_mode = MOTOR_MANUAL;
933 error_state |= (1<<7);
937 if (motor1_mode == MOTOR_PID) {
938 if (speed1_wish != speed1_wish_old) {
939 if (abs(speed1_wish - speed1_wish_old) > 500) esum1 = 0;
940 speed1_wish_old = speed1_wish;
943 uint8_t dir_change = (speed1_wish > 0 && speed1 < 0) || (speed1_wish < 0 && speed1 > 0); // Prevent dangerous immediate engine reverse
944 if (speed1_wish == 0 || dir_change) {
947 error_state &= ~(1<<4);
949 int16_t e = speed1_wish - speed1;
951 motor1 = KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
954 if (motor1 > 0 && speed1_wish < 0) motor1=PWM_BREAK;
955 else if (motor1 < 0 && speed1_wish > 0) motor1=PWM_BREAK;
956 else if (motor1 > 255) motor1 = 255;
957 else if (motor1 < -255) motor1 = -255;
960 if (motor2_mode == MOTOR_PID) {
961 if (speed2_wish != speed2_wish_old) {
962 if (abs(speed2_wish - speed2_wish_old) > 500) esum2 = 0;
963 speed2_wish_old = speed2_wish;
966 uint8_t dir_change = (speed2_wish > 0 && speed2 < 0) || (speed2_wish < 0 && speed2 > 0); // Prevent dangerous immediate engine reverse
967 if (speed2_wish == 0 || dir_change) {
970 error_state &= ~(1<<5);
972 int16_t e = speed2_wish - speed2;
974 motor2 = KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
977 if (motor2 > 0 && speed2_wish < 0) motor2=PWM_BREAK;
978 else if (motor2 < 0 && speed2_wish > 0) motor2=PWM_BREAK;
979 else if (motor2 > 255) motor2 = 255;
980 else if (motor2 < -255) motor2 = -255;
983 if (motor3_mode == MOTOR_PID) {
984 if (speed3_wish != speed3_wish_old) {
985 if (abs(speed3_wish - speed3_wish_old) > 500) esum3 = 0;
986 speed3_wish_old = speed3_wish;
989 uint8_t dir_change = (speed3_wish > 0 && speed3 < 0) || (speed3_wish < 0 && speed3 > 0); // Prevent dangerous immediate engine reverse
990 if (speed3_wish == 0 || dir_change) {
993 error_state &= ~(1<<6);
995 int16_t e = speed3_wish - speed3;
997 motor3 = KP*e + KI*PID_T*esum3 + KD/PID_T*(e - eold3);
1000 if (motor3 > 0 && speed3_wish < 0) motor3=PWM_BREAK;
1001 else if (motor3 < 0 && speed3_wish > 0) motor3=PWM_BREAK;
1002 else if (motor3 > 255) motor3 = 255;
1003 else if (motor3 < -255) motor3 = -255;
1006 if (motor4_mode == MOTOR_PID) {
1007 if (speed4_wish != speed4_wish_old) {
1008 if (abs(speed4_wish - speed4_wish_old) > 500) esum4 = 0;
1009 speed4_wish_old = speed4_wish;
1012 uint8_t dir_change = (speed4_wish > 0 && speed4 < 0) || (speed4_wish < 0 && speed4 > 0); // Prevent dangerous immediate engine reverse
1013 if (speed4_wish == 0 || dir_change) {
1016 error_state &= ~(1<<7);
1018 int16_t e = speed4_wish - speed4;
1020 motor4 = KP*e + KI*PID_T*esum4 + KD/PID_T*(e - eold4);
1023 if (motor4 > 0 && speed4_wish < 0) motor4=PWM_BREAK;
1024 else if (motor4 < 0 && speed4_wish > 0) motor4=PWM_BREAK;
1025 else if (motor4 > 255) motor4 = 255;
1026 else if (motor4 < -255) motor4 = -255;
1032 ISR(TIMER1_OVF_vect) {
1045 DDRC = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
1046 DDRD = (1 << 7) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
1047 // Pullup Diag/Enable
1048 PORTB = (1 << 0) | (1 << 1) | (1 << 2);
1053 uart_setup_stdout();
1060 // Also used for PWM frequency TIMER1_FREQ (F_CPU/256)
1061 // Timer 1: Fast PWM non-inverting mode, Top=255 => 19.531kHz
1063 //TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10);
1064 // Avoid narrow spike on extreme pwm value 0 by not setting COM1*1
1065 TCCR1A = (1 << WGM10);
1066 TCCR1B = (1 << WGM12) | (1 << CS10);
1071 // Timer 2: Fast PWM non-inverting mode, Top=255
1073 //TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << COM21) | (1 << CS20);
1074 // Avoid narrow spike on extreme pwm value 0 by not setting COM21
1075 TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << CS20);
1079 // Timer 0: Fast PWM non-inverting mode, Top=255
1081 //TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << COM01) | (1 << CS00);
1082 // Avoid narrow spike on extreme pwm value 0 by not setting COM01
1083 TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << CS00);
1086 printf("\r\nStart\r\n");
1088 set_sleep_mode(SLEEP_MODE_IDLE);
1089 // Enable Timer 1 Overflow Interrupt
1090 TIMSK = (1 << TOIE1);
1095 case 0xff: // Magic reg that starts the bootloader
1096 if (bootloader == 0xa5) {
1098 // write mark to first area in eeprom
1099 eeprom_write_byte((uint8_t*)0, 123);
1101 // Use watchdog to restart
1102 wdt_enable(WDTO_15MS);
1107 if (cmd_vel.bUpdate) {
1108 float speed_wish_right, speed_wish_left;
1112 speed = cmd_vel.speed;
1113 angle = cmd_vel.angle;
1114 cmd_vel.bUpdate = 0;
1117 speed_wish_right = (angle*WHEEL_DIST)/2 + speed;
1118 speed_wish_left = speed*2-speed_wish_right;
1120 speed_wish_left*=STEP_PER_M_LEFT;
1121 speed_wish_right*=STEP_PER_M_RIGHT;
1123 if (aft_handicap > 0) {
1124 speed1_wish = speed_wish_left * (100-aft_handicap)/100.0;
1125 speed3_wish = speed_wish_right * (100-aft_handicap)/100.0;
1127 speed1_wish = speed_wish_left;
1128 speed3_wish = speed_wish_right;
1130 if (front_handicap > 0) {
1131 speed2_wish = speed_wish_left * (100-front_handicap)/100.0;
1132 speed4_wish = speed_wish_right * (100-front_handicap)/100.0;
1134 speed2_wish = speed_wish_left;
1135 speed4_wish = speed_wish_right;
1137 motor1_mode = MOTOR_PID;
1138 motor2_mode = MOTOR_PID;
1139 motor3_mode = MOTOR_PID;
1140 motor4_mode = MOTOR_PID;
1143 if (run_update >= 195) { // TIMER1_FREQ/195 = ~100Hz
1150 if (last_man_update_count != I2C_TIMEOUT_DISABLE) last_man_update_count++;
1152 if (last_man_update_count > 100) {
1153 // ~1s without a new i2c command
1156 cmd_vel.bUpdate = 1;
1157 last_man_update_count = I2C_TIMEOUT_DISABLE;