6 #include <avr/interrupt.h>
9 #include <avr/eeprom.h>
11 #include <avr/pgmspace.h>
15 * I2C Register Map (8 Bit)
16 * 0x00 Register select
17 * 0x01 Motor 1 PWM MSB
18 * 0x02 Motor 1 PWM LSB
19 * 0x03 Motor 2 PWM MSB
20 * 0x04 Motor 2 PWM LSB
21 * 0x05 Motor 3 PWM MSB
22 * 0x06 Motor 3 PWM LSB
23 * 0x07 Motor 4 PWM MSB
24 * 0x08 Motor 4 PWM LSB
35 * 0x20 Motor 1 speed wish MSB
36 * 0x21 Motor 1 speed wish LSB
37 * 0x22 Motor 2 speed wish MSB
38 * 0x23 Motor 2 speed wish LSB
39 * 0x24 Motor 3 speed wish MSB
40 * 0x25 Motor 3 speed wish LSB
41 * 0x26 Motor 4 speed wish MSB
42 * 0x27 Motor 4 speed wish LSB
43 * 0x28 Left speed wish (m/s) MSB
44 * 0x29 Left speed wish (m/s)
45 * 0x2A Left speed wish (m/s)
46 * 0x2B Left speed wish (m/s) LSB
47 * 0x2C Right speed wish (m/s) MSB
48 * 0x2D Right speed wish (m/s)
49 * 0x2E Right speed wish (m/s)
50 * 0x2F Right speed wish (m/s) LSB
51 * 0x30 Motor 1 speed MSB
52 * 0x31 Motor 1 speed LSB
53 * 0x32 Motor 2 speed MSB
54 * 0x33 Motor 2 speed LSB
55 * 0x34 Motor 3 speed MSB
56 * 0x35 Motor 3 speed LSB
57 * 0x36 Motor 4 speed MSB
58 * 0x37 Motor 4 speed LSB
59 * 0x38 Speed (m/s) MSB
62 * 0x3B Speed (m/s) LSB
63 * 0x3C Angle (rad/s) MSB
66 * 0x3F Angle (rad/s) LSB
67 * 0x40 Position x (m) MSB
70 * 0x43 Position x (m) LSB
71 * 0x44 Position y (m) MSB
74 * 0x47 Position y (m) LSB
75 * 0x48 Position angle MSB
78 * 0x4B Position angle LSB
80 * 0x50 speed wish (m/s) MSB
81 * 0x51 speed wish (m/s)
82 * 0x52 speed wish (m/s)
83 * 0x53 speed wish (m/s) LSB
84 * 0x54 angle wish (rad/s) MSB
85 * 0x55 angle wish (rad/s)
86 * 0x56 angle wish (rad/s)
87 * 0x57 angle wish (rad/s) LSB
99 * 0xA3 last i2c status before boot
100 * 0xA4 Watchdog enable
110 // wheel diameter=12cm, encoder=48cpr, gear ratio=1:47
111 // STEP_PER_M = 48*47/(d*pi)
112 // Left real diameter: 0.12808, Right real diameter: 0.121
113 #define STEP_PER_M 5573.0
114 #define STEP_PER_M_LEFT (STEP_PER_M)
115 #define STEP_PER_M_RIGHT (STEP_PER_M)
116 #define WHEEL_DIST 0.39912 // Measured: 0.252
117 #define PWM_BREAK INT16_MIN
118 #define STALL_LIMIT 140000
119 #define I2C_TIMEOUT_DISABLE 255
121 #define TWI_ACK TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) | (1<<TWIE)
122 #define TWI_NAK TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
123 #define TWI_RESET TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWSTO) | (1<<TWEN) | (1<<TWIE);
124 #define ENABLE_PWM_MOTOR1 TCCR1A |= (1 << COM1A1)
125 #define ENABLE_PWM_MOTOR2 TCCR1A |= (1 << COM1B1)
126 #define ENABLE_PWM_MOTOR3 TCCR2 |= (1 << COM21);
127 #define ENABLE_PWM_MOTOR4 TCCR0 |= (1 << COM01);
128 #define DISABLE_PWM_MOTOR1 TCCR1A &= ~(1 << COM1A1)
129 #define DISABLE_PWM_MOTOR2 TCCR1A &= ~(1 << COM1B1)
130 #define DISABLE_PWM_MOTOR3 TCCR2 &= ~(1 << COM21);
131 #define DISABLE_PWM_MOTOR4 TCCR0 &= ~(1 << COM01);
144 static volatile struct {
148 } cmd_vel = {0, 0, 0};
150 static volatile uint8_t ireg=0;
151 static volatile uint8_t bootloader=0;
152 static volatile int16_t motor1=0; // -255..+255
153 static volatile int16_t motor2=0;
154 static volatile int16_t motor3=0;
155 static volatile int16_t motor4=0;
156 static volatile int16_t pos1=0; // step
157 static volatile int16_t pos2=0;
158 static volatile int16_t pos3=0;
159 static volatile int16_t pos4=0;
160 static volatile enum mode motor1_mode=MOTOR_MANUAL;
161 static volatile enum mode motor2_mode=MOTOR_MANUAL;
162 static volatile enum mode motor3_mode=MOTOR_MANUAL;
163 static volatile enum mode motor4_mode=MOTOR_MANUAL;
164 static volatile uint8_t motor1_switch=1;
165 static volatile uint8_t motor2_switch=1;
166 static volatile uint8_t motor3_switch=0;
167 static volatile uint8_t motor4_switch=0;
168 static volatile int16_t speed1_wish=0; // step/s
169 static volatile int16_t speed2_wish=0;
170 static volatile int16_t speed3_wish=0;
171 static volatile int16_t speed4_wish=0;
172 static volatile int16_t speed1_wish_old=0;
173 static volatile int16_t speed2_wish_old=0;
174 static volatile int16_t speed3_wish_old=0;
175 static volatile int16_t speed4_wish_old=0;
176 static volatile uint8_t run_update=0;
177 static volatile int16_t speed1=0; // step/s
178 static volatile int16_t speed2=0;
179 static volatile int16_t speed3=0;
180 static volatile int16_t speed4=0;
181 static volatile ufloat_t pos_x={0.0};
182 static volatile ufloat_t pos_y={0.0};
183 static volatile ufloat_t angle={0.0};
184 static volatile float cur_speed_lin=0;
185 static volatile float cur_speed_rot=0;
186 static volatile uint8_t count_test=0;
187 static volatile uint8_t front_handicap=0;
188 static volatile uint8_t aft_handicap=0;
189 static volatile uint8_t error_state=0;
190 static volatile uint8_t last_man_update_count=0;
191 static volatile uint8_t last_i2c_status = 0;
192 static uint8_t last_i2c_status_boot = 0;
193 static volatile uint8_t watchdog_enable = 0;
197 static uint8_t tmp=0;
198 static int16_t tmp16=0;
199 static ufloat_t tmp_speed;
200 static ufloat_t tmp_angle;
202 last_i2c_status = TW_STATUS;
205 case TW_SR_SLA_ACK: // start write
209 case TW_SR_DATA_ACK: // write
211 case 0x00: // register select
213 ireg--; // because we do ireg++ below
216 case 0x01: // Motor 1 MSB
220 case 0x02: // Motor 1 LSB
221 motor1 = tmp<<8 | TWDR;
222 motor1_mode = MOTOR_MANUAL;
225 case 0x03: // Motor 2 MSB
229 case 0x04: // Motor 2 LSB
230 motor2 = tmp<<8 | TWDR;
231 motor2_mode = MOTOR_MANUAL;
234 case 0x05: // Motor 3 MSB
238 case 0x06: // Motor 3 LSB
239 motor3 = tmp<<8 | TWDR;
240 motor3_mode = MOTOR_MANUAL;
243 case 0x07: // Motor 4 MSB
247 case 0x08: // Motor 4 LSB
248 motor4 = tmp<<8 | TWDR;
249 motor4_mode = MOTOR_MANUAL;
252 case 0x20: // Motor 1 speed wish MSB
256 case 0x21: // Motor 1 speed wish LSB
257 speed1_wish = tmp<<8 | TWDR;
258 motor1_mode = MOTOR_PID;
261 case 0x22: // Motor 2 speed wish MSB
265 case 0x23: // Motor 2 speed wish LSB
266 speed2_wish = tmp<<8 | TWDR;
267 motor2_mode = MOTOR_PID;
270 case 0x24: // Motor 3 speed wish MSB
274 case 0x25: // Motor 3 speed wish LSB
275 speed3_wish = tmp<<8 | TWDR;
276 motor3_mode = MOTOR_PID;
279 case 0x26: // Motor 4 speed wish MSB
283 case 0x27: // Motor 4 speed wish LSB
284 speed4_wish = tmp<<8 | TWDR;
285 motor4_mode = MOTOR_PID;
288 case 0x28: // Left speed wish MSB
292 case 0x29: // Left speed wish
293 tmp_speed.i = tmp_speed.i << 8 | TWDR;
296 case 0x2A: // Left speed wish
297 tmp_speed.i = tmp_speed.i << 8 | TWDR;
300 case 0x2B: // Left speed wish LSB
301 tmp_speed.i = tmp_speed.i << 8 | TWDR;
302 speed1_wish = tmp_speed.f*STEP_PER_M_LEFT;
303 speed2_wish = tmp_speed.f*STEP_PER_M_LEFT;
304 motor1_mode = MOTOR_PID;
305 motor2_mode = MOTOR_PID;
308 case 0x2C: // Right speed wish MSB
312 case 0x2D: // Right speed wish
313 tmp_speed.i = tmp_speed.i << 8 | TWDR;
316 case 0x2E: // Right speed wish
317 tmp_speed.i = tmp_speed.i << 8 | TWDR;
320 case 0x2F: // Right speed wish LSB
321 tmp_speed.i = tmp_speed.i << 8 | TWDR;
322 speed1_wish = tmp_speed.f*STEP_PER_M_RIGHT;
323 speed2_wish = tmp_speed.f*STEP_PER_M_RIGHT;
324 motor1_mode = MOTOR_PID;
325 motor2_mode = MOTOR_PID;
328 case 0x50: // speed wish MSB
332 case 0x51: // speed wish
333 tmp_speed.i = tmp_speed.i << 8 | TWDR;
336 case 0x52: // speed wish
337 tmp_speed.i = tmp_speed.i << 8 | TWDR;
340 case 0x53: // speed wish LSB
341 tmp_speed.i = tmp_speed.i << 8 | TWDR;
342 cmd_vel.speed = tmp_speed.f;
345 case 0x54: // angle wish MSB
349 case 0x55: // angle wish
350 tmp_angle.i = tmp_angle.i << 8 | TWDR;
353 case 0x56: // angle wish
354 tmp_angle.i = tmp_angle.i << 8 | TWDR;
357 case 0x57: // angle wish LSB
358 tmp_angle.i = tmp_angle.i << 8 | TWDR;
359 cmd_vel.angle = tmp_angle.f;
361 last_man_update_count = 0;
364 case 0x90: // Motor 1 switch
365 motor1_switch = TWDR;
368 case 0x91: // Motor 2 switch
369 motor2_switch = TWDR;
372 case 0x92: // Motor 3 switch
373 motor3_switch = TWDR;
376 case 0x93: // Motor 4 switch
377 motor4_switch = TWDR;
380 case 0x94: // Front Handicap
381 front_handicap = TWDR;
385 case 0x95: // Aft Handicap
390 case 0xA4: // Watchdog enable
391 watchdog_enable = TWDR;
394 case 0xff: // bootloader
399 if (ireg < 0xff) ireg++;
401 case TW_ST_SLA_ACK: // start read
402 case TW_ST_DATA_ACK: // read
404 case 0x02: // Motor 1 PWM
408 case 0x03: // Dummy to allow continous read
412 case 0x04: // Motor 2 PWM
416 case 0x05: // Dummy to allow continous read
420 case 0x06: // Motor 3 PWM
424 case 0x07: // Dummy to allow continous read
428 case 0x08: // Motor 4 PWM
432 case 0x09: // Dummy to allow continous read
436 case 0x10: // Hall 1 MSB
441 case 0x11: // Hall 1 LSB
445 case 0x12: // Hall 2 MSB
450 case 0x13: // Hall 2 LSB
454 case 0x14: // Hall 3 MSB
459 case 0x15: // Hall 3 LSB
463 case 0x16: // Hall 4 MSB
468 case 0x17: // Hall 4 LSB
472 case 0x20: // Motor 1 speed wish MSB
473 TWDR = speed1_wish>>8;
476 case 0x21: // Motor 1 speed wish LSB
480 case 0x22: // Motor 2 speed wish MSB
481 TWDR = speed2_wish>>8;
484 case 0x23: // Motor 2 speed wish LSB
488 case 0x24: // Motor 3 speed wish MSB
489 TWDR = speed3_wish>>8;
492 case 0x25: // Motor 3 speed wish LSB
496 case 0x26: // Motor 4 speed wish MSB
497 TWDR = speed4_wish>>8;
500 case 0x27: // Motor 4 speed wish LSB
504 case 0x30: // Motor 1 speed MSB
508 case 0x31: // Motor 1 speed LSB
512 case 0x32: // Motor 2 speed MSB
516 case 0x33: // Motor 2 speed LSB
520 case 0x34: // Motor 3 speed MSB
524 case 0x35: // Motor 3 speed LSB
528 case 0x36: // Motor 4 speed MSB
532 case 0x37: // Motor 4 speed LSB
536 case 0x38: // speed MSB
537 tmp_speed.f = cur_speed_lin;
538 TWDR = tmp_speed.i>>24;
542 TWDR = tmp_speed.i>>16;
546 TWDR = tmp_speed.i>>8;
549 case 0x3B: // speed LSB
553 case 0x3C: // angle MSB
554 tmp_angle.f = cur_speed_rot;
555 TWDR = tmp_angle.i>>24;
559 TWDR = tmp_angle.i>>16;
563 TWDR = tmp_angle.i>>8;
566 case 0x3F: // angle LSB
570 case 0x40: // Position x MSB
574 case 0x41: // Position x
578 case 0x42: // Position x
582 case 0x43: // Position x LSB
586 case 0x44: // Position y MSB
590 case 0x45: // Position y
594 case 0x46: // Position y
598 case 0x47: // Position y LSB
602 case 0x48: // Position angle MSB
606 case 0x49: // Position angle
610 case 0x4A: // Position angle
614 case 0x4B: // Position angle LSB
618 case 0xA0: // Reset reason
619 TWDR = MCUCSR & 0x0f;
623 case 0xA1: // Error status
627 case 0xA2: // count test
630 case 0xA3: // last i2c status before boot
631 TWDR = last_i2c_status_boot;
633 case 0xA4: // Watchdog enable
634 TWDR = watchdog_enable;
651 if (watchdog_enable == 2) {
657 static void update_hall1(void) {
658 unsigned char status = (PINA >> 0) & 0x3;
659 static unsigned char oldstatus=0;
660 unsigned char diff, new;
666 new ^= 0x1; // convert gray to binary
667 diff = oldstatus - new; // difference last - new
668 if (diff & 0x1) { // bit 0 = value (1)
669 oldstatus = new; // store new as next last
670 if (motor1_switch) pos1 += (diff & 2) - 1; // bit 1 = direction (+/-)
671 else pos1 -= (diff & 2) - 1;
676 static void update_hall2(void) {
677 unsigned char status = (PINA >> 4) & 0x3;
678 static unsigned char oldstatus=0;
679 unsigned char diff, new;
685 new ^= 0x1; // convert gray to binary
686 diff = oldstatus - new; // difference last - new
687 if (diff & 0x1) { // bit 0 = value (1)
688 oldstatus = new; // store new as next last
689 if (motor2_switch) pos2 -= (diff & 2) - 1; // bit 1 = direction (+/-)
690 else pos2 += (diff & 2) - 1;
695 static void update_hall3(void) {
696 unsigned char status = (PINA >> 2) & 0x3;
697 static unsigned char oldstatus=0;
698 unsigned char diff, new;
704 new ^= 0x1; // convert gray to binary
705 diff = oldstatus - new; // difference last - new
706 if (diff & 0x1) { // bit 0 = value (1)
707 oldstatus = new; // store new as next last
708 if (motor3_switch) pos3 -= (diff & 2) - 1; // bit 1 = direction (+/-)
709 else pos3 += (diff & 2) - 1;
714 static void update_hall4(void) {
715 unsigned char status = (PINA >> 6) & 0x3;
716 static unsigned char oldstatus=0;
717 unsigned char diff, new;
723 new ^= 0x1; // convert gray to binary
724 diff = oldstatus - new; // difference last - new
725 if (diff & 0x1) { // bit 0 = value (1)
726 oldstatus = new; // store new as next last
727 if (motor4_switch) pos4 += (diff & 2) - 1; // bit 1 = direction (+/-)
728 else pos4 -= (diff & 2) - 1;
733 static void update_motor(void) {
734 static int16_t m1_old=SHRT_MIN;
735 static int16_t m2_old=SHRT_MIN;
736 static int16_t m3_old=SHRT_MIN;
737 static int16_t m4_old=SHRT_MIN;
739 error_state &= 0xf0; // clear lower bits
740 error_state |= ~((PIND & 0x40)>>3 | (PINB & 0x07)) & 0xf;
742 if (m1_old != motor1) { // update only when changed
745 PORTC &= ~(1 << 3) & ~(1 << 2);
747 } else if (motor1 == PWM_BREAK) {
748 PORTC |= (1 << 3) | (1 << 2);
750 } else if ((!motor1_switch && motor1 > 0) || (motor1_switch && motor1 < 0)) {
757 } else { // motor1 < 0
770 if (m2_old != motor2) { // update only when changed
773 PORTC &= ~(1 << 5) & ~(1 << 4);
775 } else if (motor2 == PWM_BREAK) {
776 PORTC |= (1 << 5) | (1 << 4);
778 } else if ((!motor2_switch && motor2 > 0) || (motor2_switch && motor2 < 0)) {
785 } else { // motor2 < 0
798 if (m3_old != motor3) { // update only when changed
801 PORTC &= ~(1 << 7) & ~(1 << 6);
803 } else if (motor3 == PWM_BREAK) {
804 PORTC |= (1 << 7) | (1 << 6);
806 } else if ((!motor3_switch && motor3 > 0) || (motor3_switch && motor3 < 0)) {
813 } else { // motor3 < 0
826 if (m4_old != motor4) { // update only when changed
829 PORTD &= ~(1 << 3) & ~(1 << 2);
831 } else if (motor4 == PWM_BREAK) {
832 PORTD |= (1 << 3) | (1 << 2);
834 } else if ((!motor4_switch && motor4 > 0) || (motor4_switch && motor4 < 0)) {
841 } else { // motor4 < 0
856 static void update_pos(void) {
857 static int16_t pos1_last=0;
858 static int16_t pos2_last=0;
859 static int16_t pos3_last=0;
860 static int16_t pos4_last=0;
861 int16_t pos1_diff; // steps
865 float diff_left_m, diff_right_m, angle_diff, translation;
866 float pos_x_new, pos_y_new, angle_new;
867 float tmp_speed_lin, tmp_speed_rot;
868 int16_t cur_pos1, cur_pos2, cur_pos3, cur_pos4;
869 int16_t new_speed1, new_speed2, new_speed3, new_speed4;
879 pos1_diff = cur_pos1 - pos1_last;
880 pos2_diff = cur_pos2 - pos2_last;
881 pos3_diff = cur_pos3 - pos3_last;
882 pos4_diff = cur_pos4 - pos4_last;
884 new_speed1 = pos1_diff/PID_T;
885 new_speed2 = pos2_diff/PID_T;
886 new_speed3 = pos3_diff/PID_T;
887 new_speed4 = pos4_diff/PID_T;
889 diff_left_m = (pos1_diff + pos2_diff)/(2*STEP_PER_M_LEFT);
890 diff_right_m = (pos3_diff + pos4_diff)/(2*STEP_PER_M_RIGHT);
891 angle_diff = (diff_right_m - diff_left_m) / WHEEL_DIST;
893 angle_new = angle.f + angle_diff;
894 if (angle_new > 2*M_PI) angle_new-=2*M_PI;
895 else if (angle_new < -2*M_PI) angle_new+=2*M_PI;
897 translation = (diff_left_m + diff_right_m)/2.0;
898 pos_x_new = pos_x.f + cos(angle_new)*translation;
899 pos_y_new = pos_y.f + sin(angle_new)*translation;
901 tmp_speed_lin = translation/PID_T;
902 tmp_speed_rot = angle_diff/PID_T;
913 cur_speed_lin = tmp_speed_lin;
914 cur_speed_rot = tmp_speed_rot;
917 pos1_last = cur_pos1;
918 pos2_last = cur_pos2;
919 pos3_last = cur_pos3;
920 pos4_last = cur_pos4;
924 static void update_pid(void) {
925 static int16_t eold1=0;
926 static int16_t eold2=0;
927 static int16_t eold3=0;
928 static int16_t eold4=0;
929 static int32_t esum1=0;
930 static int32_t esum2=0;
931 static int32_t esum3=0;
932 static int32_t esum4=0;
934 // protect motors from damage if stalling
935 if (labs(esum1) > STALL_LIMIT && speed1 == 0) {
937 motor1_mode = MOTOR_MANUAL;
938 error_state |= (1<<4);
941 if (labs(esum2) > STALL_LIMIT && speed2 == 0) {
943 motor2_mode = MOTOR_MANUAL;
944 error_state |= (1<<5);
947 if (labs(esum3) > STALL_LIMIT && speed3 == 0) {
949 motor3_mode = MOTOR_MANUAL;
950 error_state |= (1<<6);
953 if (labs(esum4) > STALL_LIMIT && speed4 == 0) {
955 motor4_mode = MOTOR_MANUAL;
956 error_state |= (1<<7);
960 if (motor1_mode == MOTOR_PID) {
961 if (speed1_wish != speed1_wish_old) {
962 if (abs(speed1_wish - speed1_wish_old) > 500) esum1 = 0;
963 speed1_wish_old = speed1_wish;
966 uint8_t dir_change = (speed1_wish > 0 && speed1 < 0) || (speed1_wish < 0 && speed1 > 0); // Prevent dangerous immediate engine reverse
967 if (speed1_wish == 0 || dir_change) {
970 error_state &= ~(1<<4);
972 int16_t e = speed1_wish - speed1;
974 motor1 = KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
977 if (motor1 > 0 && speed1_wish < 0) motor1=PWM_BREAK;
978 else if (motor1 < 0 && speed1_wish > 0) motor1=PWM_BREAK;
979 else if (motor1 > 255) motor1 = 255;
980 else if (motor1 < -255) motor1 = -255;
983 if (motor2_mode == MOTOR_PID) {
984 if (speed2_wish != speed2_wish_old) {
985 if (abs(speed2_wish - speed2_wish_old) > 500) esum2 = 0;
986 speed2_wish_old = speed2_wish;
989 uint8_t dir_change = (speed2_wish > 0 && speed2 < 0) || (speed2_wish < 0 && speed2 > 0); // Prevent dangerous immediate engine reverse
990 if (speed2_wish == 0 || dir_change) {
993 error_state &= ~(1<<5);
995 int16_t e = speed2_wish - speed2;
997 motor2 = KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
1000 if (motor2 > 0 && speed2_wish < 0) motor2=PWM_BREAK;
1001 else if (motor2 < 0 && speed2_wish > 0) motor2=PWM_BREAK;
1002 else if (motor2 > 255) motor2 = 255;
1003 else if (motor2 < -255) motor2 = -255;
1006 if (motor3_mode == MOTOR_PID) {
1007 if (speed3_wish != speed3_wish_old) {
1008 if (abs(speed3_wish - speed3_wish_old) > 500) esum3 = 0;
1009 speed3_wish_old = speed3_wish;
1012 uint8_t dir_change = (speed3_wish > 0 && speed3 < 0) || (speed3_wish < 0 && speed3 > 0); // Prevent dangerous immediate engine reverse
1013 if (speed3_wish == 0 || dir_change) {
1016 error_state &= ~(1<<6);
1018 int16_t e = speed3_wish - speed3;
1020 motor3 = KP*e + KI*PID_T*esum3 + KD/PID_T*(e - eold3);
1023 if (motor3 > 0 && speed3_wish < 0) motor3=PWM_BREAK;
1024 else if (motor3 < 0 && speed3_wish > 0) motor3=PWM_BREAK;
1025 else if (motor3 > 255) motor3 = 255;
1026 else if (motor3 < -255) motor3 = -255;
1029 if (motor4_mode == MOTOR_PID) {
1030 if (speed4_wish != speed4_wish_old) {
1031 if (abs(speed4_wish - speed4_wish_old) > 500) esum4 = 0;
1032 speed4_wish_old = speed4_wish;
1035 uint8_t dir_change = (speed4_wish > 0 && speed4 < 0) || (speed4_wish < 0 && speed4 > 0); // Prevent dangerous immediate engine reverse
1036 if (speed4_wish == 0 || dir_change) {
1039 error_state &= ~(1<<7);
1041 int16_t e = speed4_wish - speed4;
1043 motor4 = KP*e + KI*PID_T*esum4 + KD/PID_T*(e - eold4);
1046 if (motor4 > 0 && speed4_wish < 0) motor4=PWM_BREAK;
1047 else if (motor4 < 0 && speed4_wish > 0) motor4=PWM_BREAK;
1048 else if (motor4 > 255) motor4 = 255;
1049 else if (motor4 < -255) motor4 = -255;
1055 ISR(TIMER1_OVF_vect) {
1068 DDRC = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
1069 DDRD = (1 << 7) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
1070 // Pullup Diag/Enable
1071 PORTB = (1 << 0) | (1 << 1) | (1 << 2);
1076 uart_setup_stdout();
1083 // Also used for PWM frequency TIMER1_FREQ (F_CPU/256)
1084 // Timer 1: Fast PWM non-inverting mode, Top=255 => 19.531kHz
1086 //TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10);
1087 // Avoid narrow spike on extreme pwm value 0 by not setting COM1*1
1088 TCCR1A = (1 << WGM10);
1089 TCCR1B = (1 << WGM12) | (1 << CS10);
1094 // Timer 2: Fast PWM non-inverting mode, Top=255
1096 //TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << COM21) | (1 << CS20);
1097 // Avoid narrow spike on extreme pwm value 0 by not setting COM21
1098 TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << CS20);
1102 // Timer 0: Fast PWM non-inverting mode, Top=255
1104 //TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << COM01) | (1 << CS00);
1105 // Avoid narrow spike on extreme pwm value 0 by not setting COM01
1106 TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << CS00);
1109 printf_P(PSTR("\r\nStart\r\n"));
1111 last_i2c_status_boot = eeprom_read_byte((uint8_t*)1);
1113 set_sleep_mode(SLEEP_MODE_IDLE);
1114 // Enable Timer 1 Overflow Interrupt
1115 TIMSK = (1 << TOIE1);
1120 case 0xA4: // Watchdog enable
1121 if (watchdog_enable == 1) {
1122 wdt_enable(WDTO_2S);
1123 watchdog_enable = 2;
1124 } else if (watchdog_enable == 0) {
1126 watchdog_enable = 3;
1129 case 0xff: // Magic reg that starts the bootloader
1130 if (bootloader == 0xa5) {
1132 // write mark to first area in eeprom
1133 eeprom_write_byte((uint8_t*)0, 123);
1135 // Use watchdog to restart
1136 wdt_enable(WDTO_15MS);
1141 if (cmd_vel.bUpdate) {
1142 float speed_wish_right, speed_wish_left;
1146 speed = cmd_vel.speed;
1147 angle = cmd_vel.angle;
1148 cmd_vel.bUpdate = 0;
1151 speed_wish_right = (angle*WHEEL_DIST)/2 + speed;
1152 speed_wish_left = speed*2-speed_wish_right;
1154 speed_wish_left*=STEP_PER_M_LEFT;
1155 speed_wish_right*=STEP_PER_M_RIGHT;
1157 if (aft_handicap > 0) {
1158 speed1_wish = speed_wish_left * (100-aft_handicap)/100.0;
1159 speed3_wish = speed_wish_right * (100-aft_handicap)/100.0;
1161 speed1_wish = speed_wish_left;
1162 speed3_wish = speed_wish_right;
1164 if (front_handicap > 0) {
1165 speed2_wish = speed_wish_left * (100-front_handicap)/100.0;
1166 speed4_wish = speed_wish_right * (100-front_handicap)/100.0;
1168 speed2_wish = speed_wish_left;
1169 speed4_wish = speed_wish_right;
1171 motor1_mode = MOTOR_PID;
1172 motor2_mode = MOTOR_PID;
1173 motor3_mode = MOTOR_PID;
1174 motor4_mode = MOTOR_PID;
1177 if (run_update >= 195) { // TIMER1_FREQ/195 = ~100Hz
1184 if (last_man_update_count != I2C_TIMEOUT_DISABLE) last_man_update_count++;
1186 if (last_man_update_count >= 100) {
1187 // ~1s without a new i2c command
1190 cmd_vel.bUpdate = 1;
1191 if (last_man_update_count == 100) {
1192 printf_P(PSTR("I2C State: 0x%x\r\n"), last_i2c_status);
1193 eeprom_write_byte((uint8_t*)1, last_i2c_status);
1196 last_man_update_count = I2C_TIMEOUT_DISABLE;