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
113 // wheel diameter=12cm, encoder=48cpr, gear ratio=1:47
114 // STEP_PER_M = 48*47/(d*pi)
115 // Left real diameter: 0.12808, Right real diameter: 0.121
116 #define STEP_PER_M 5573.0
117 #define STEP_PER_M_LEFT (STEP_PER_M)
118 #define STEP_PER_M_RIGHT (STEP_PER_M)
119 #define WHEEL_DIST 0.39912 // Measured: 0.252
120 #define PWM_BREAK INT16_MIN
121 #define STALL_LIMIT 140000
122 #define I2C_TIMEOUT_DISABLE 255
124 #define TWI_ACK TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) | (1<<TWIE)
125 #define TWI_NAK TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
126 #define TWI_RESET TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWSTO) | (1<<TWEN) | (1<<TWIE);
127 #define ENABLE_PWM_MOTOR1 TCCR1A |= (1 << COM1A1)
128 #define ENABLE_PWM_MOTOR2 TCCR1A |= (1 << COM1B1)
129 #define ENABLE_PWM_MOTOR3 TCCR2 |= (1 << COM21);
130 #define ENABLE_PWM_MOTOR4 TCCR0 |= (1 << COM01);
131 #define DISABLE_PWM_MOTOR1 TCCR1A &= ~(1 << COM1A1)
132 #define DISABLE_PWM_MOTOR2 TCCR1A &= ~(1 << COM1B1)
133 #define DISABLE_PWM_MOTOR3 TCCR2 &= ~(1 << COM21);
134 #define DISABLE_PWM_MOTOR4 TCCR0 &= ~(1 << COM01);
147 static volatile struct {
151 } cmd_vel = {0, 0, 0};
153 static volatile uint8_t ireg=0;
154 static volatile uint8_t bootloader=0;
155 static volatile int16_t motor1=0; // -255..+255
156 static volatile int16_t motor2=0;
157 static volatile int16_t motor3=0;
158 static volatile int16_t motor4=0;
159 static volatile int16_t pos1=0; // step
160 static volatile int16_t pos2=0;
161 static volatile int16_t pos3=0;
162 static volatile int16_t pos4=0;
163 static volatile enum mode motor1_mode=MOTOR_MANUAL;
164 static volatile enum mode motor2_mode=MOTOR_MANUAL;
165 static volatile enum mode motor3_mode=MOTOR_MANUAL;
166 static volatile enum mode motor4_mode=MOTOR_MANUAL;
167 static volatile uint8_t motor1_switch=1;
168 static volatile uint8_t motor2_switch=1;
169 static volatile uint8_t motor3_switch=0;
170 static volatile uint8_t motor4_switch=0;
171 static volatile int16_t speed1_wish=0; // step/s
172 static volatile int16_t speed2_wish=0;
173 static volatile int16_t speed3_wish=0;
174 static volatile int16_t speed4_wish=0;
175 static volatile int16_t speed1_wish_old=0;
176 static volatile int16_t speed2_wish_old=0;
177 static volatile int16_t speed3_wish_old=0;
178 static volatile int16_t speed4_wish_old=0;
179 static volatile uint8_t run_update=0;
180 static volatile uint8_t sound_update=0;
181 static volatile uint8_t sound_max=0;
182 static volatile int16_t speed1=0; // step/s
183 static volatile int16_t speed2=0;
184 static volatile int16_t speed3=0;
185 static volatile int16_t speed4=0;
186 static volatile ufloat_t pos_x={0.0};
187 static volatile ufloat_t pos_y={0.0};
188 static volatile ufloat_t angle={0.0};
189 static volatile float cur_speed_lin=0;
190 static volatile float cur_speed_rot=0;
191 static volatile uint8_t count_test=0;
192 static volatile uint8_t front_handicap=0;
193 static volatile uint8_t aft_handicap=0;
194 static volatile uint8_t error_state=0;
195 static volatile uint8_t last_man_update_count=0;
196 static volatile uint8_t last_i2c_status = 0;
197 static uint8_t last_i2c_status_boot = 0;
198 static volatile uint8_t watchdog_enable = 0;
200 static void set_motor4_sound(uint16_t);
204 static uint8_t tmp=0;
205 static int16_t tmp16=0;
206 static ufloat_t tmp_speed;
207 static ufloat_t tmp_angle;
209 last_i2c_status = TW_STATUS;
212 case TW_SR_SLA_ACK: // start write
216 case TW_SR_DATA_ACK: // write
218 case 0x00: // register select
220 ireg--; // because we do ireg++ below
223 case 0x01: // Motor 1 MSB
227 case 0x02: // Motor 1 LSB
228 motor1 = tmp<<8 | TWDR;
229 motor1_mode = MOTOR_MANUAL;
232 case 0x03: // Motor 2 MSB
236 case 0x04: // Motor 2 LSB
237 motor2 = tmp<<8 | TWDR;
238 motor2_mode = MOTOR_MANUAL;
241 case 0x05: // Motor 3 MSB
245 case 0x06: // Motor 3 LSB
246 motor3 = tmp<<8 | TWDR;
247 motor3_mode = MOTOR_MANUAL;
250 case 0x07: // Motor 4 MSB
254 case 0x08: // Motor 4 LSB
255 motor4 = tmp<<8 | TWDR;
256 motor4_mode = MOTOR_MANUAL;
259 case 0x20: // Motor 1 speed wish MSB
263 case 0x21: // Motor 1 speed wish LSB
264 speed1_wish = tmp<<8 | TWDR;
265 motor1_mode = MOTOR_PID;
268 case 0x22: // Motor 2 speed wish MSB
272 case 0x23: // Motor 2 speed wish LSB
273 speed2_wish = tmp<<8 | TWDR;
274 motor2_mode = MOTOR_PID;
277 case 0x24: // Motor 3 speed wish MSB
281 case 0x25: // Motor 3 speed wish LSB
282 speed3_wish = tmp<<8 | TWDR;
283 motor3_mode = MOTOR_PID;
286 case 0x26: // Motor 4 speed wish MSB
290 case 0x27: // Motor 4 speed wish LSB
291 speed4_wish = tmp<<8 | TWDR;
292 motor4_mode = MOTOR_PID;
295 case 0x28: // Left speed wish MSB
299 case 0x29: // Left speed wish
300 tmp_speed.i = tmp_speed.i << 8 | TWDR;
303 case 0x2A: // Left speed wish
304 tmp_speed.i = tmp_speed.i << 8 | TWDR;
307 case 0x2B: // Left speed wish LSB
308 tmp_speed.i = tmp_speed.i << 8 | TWDR;
309 speed1_wish = tmp_speed.f*STEP_PER_M_LEFT;
310 speed2_wish = tmp_speed.f*STEP_PER_M_LEFT;
311 motor1_mode = MOTOR_PID;
312 motor2_mode = MOTOR_PID;
315 case 0x2C: // Right speed wish MSB
319 case 0x2D: // Right speed wish
320 tmp_speed.i = tmp_speed.i << 8 | TWDR;
323 case 0x2E: // Right speed wish
324 tmp_speed.i = tmp_speed.i << 8 | TWDR;
327 case 0x2F: // Right speed wish LSB
328 tmp_speed.i = tmp_speed.i << 8 | TWDR;
329 speed1_wish = tmp_speed.f*STEP_PER_M_RIGHT;
330 speed2_wish = tmp_speed.f*STEP_PER_M_RIGHT;
331 motor1_mode = MOTOR_PID;
332 motor2_mode = MOTOR_PID;
335 case 0x50: // speed wish MSB
339 case 0x51: // speed wish
340 tmp_speed.i = tmp_speed.i << 8 | TWDR;
343 case 0x52: // speed wish
344 tmp_speed.i = tmp_speed.i << 8 | TWDR;
347 case 0x53: // speed wish LSB
348 tmp_speed.i = tmp_speed.i << 8 | TWDR;
349 cmd_vel.speed = tmp_speed.f;
352 case 0x54: // angle wish MSB
356 case 0x55: // angle wish
357 tmp_angle.i = tmp_angle.i << 8 | TWDR;
360 case 0x56: // angle wish
361 tmp_angle.i = tmp_angle.i << 8 | TWDR;
364 case 0x57: // angle wish LSB
365 tmp_angle.i = tmp_angle.i << 8 | TWDR;
366 cmd_vel.angle = tmp_angle.f;
368 last_man_update_count = 0;
371 case 0x90: // Motor 1 switch
372 motor1_switch = TWDR;
375 case 0x91: // Motor 2 switch
376 motor2_switch = TWDR;
379 case 0x92: // Motor 3 switch
380 motor3_switch = TWDR;
383 case 0x93: // Motor 4 switch
384 motor4_switch = TWDR;
387 case 0x94: // Front Handicap
388 front_handicap = TWDR;
392 case 0x95: // Aft Handicap
397 case 0xA4: // Watchdog enable
398 watchdog_enable = TWDR;
401 case 0xB0: // Sound freq MSB
405 case 0xB1: // Sound freq LSB
406 tmp16 = tmp16<<8 | TWDR;
407 set_motor4_sound(tmp16);
410 case 0xff: // bootloader
415 if (ireg < 0xff) ireg++;
417 case TW_ST_SLA_ACK: // start read
418 case TW_ST_DATA_ACK: // read
420 case 0x02: // Motor 1 PWM
424 case 0x03: // Dummy to allow continous read
428 case 0x04: // Motor 2 PWM
432 case 0x05: // Dummy to allow continous read
436 case 0x06: // Motor 3 PWM
440 case 0x07: // Dummy to allow continous read
444 case 0x08: // Motor 4 PWM
448 case 0x09: // Dummy to allow continous read
452 case 0x10: // Hall 1 MSB
457 case 0x11: // Hall 1 LSB
461 case 0x12: // Hall 2 MSB
466 case 0x13: // Hall 2 LSB
470 case 0x14: // Hall 3 MSB
475 case 0x15: // Hall 3 LSB
479 case 0x16: // Hall 4 MSB
484 case 0x17: // Hall 4 LSB
488 case 0x20: // Motor 1 speed wish MSB
489 TWDR = speed1_wish>>8;
492 case 0x21: // Motor 1 speed wish LSB
496 case 0x22: // Motor 2 speed wish MSB
497 TWDR = speed2_wish>>8;
500 case 0x23: // Motor 2 speed wish LSB
504 case 0x24: // Motor 3 speed wish MSB
505 TWDR = speed3_wish>>8;
508 case 0x25: // Motor 3 speed wish LSB
512 case 0x26: // Motor 4 speed wish MSB
513 TWDR = speed4_wish>>8;
516 case 0x27: // Motor 4 speed wish LSB
520 case 0x30: // Motor 1 speed MSB
524 case 0x31: // Motor 1 speed LSB
528 case 0x32: // Motor 2 speed MSB
532 case 0x33: // Motor 2 speed LSB
536 case 0x34: // Motor 3 speed MSB
540 case 0x35: // Motor 3 speed LSB
544 case 0x36: // Motor 4 speed MSB
548 case 0x37: // Motor 4 speed LSB
552 case 0x38: // speed MSB
553 tmp_speed.f = cur_speed_lin;
554 TWDR = tmp_speed.i>>24;
558 TWDR = tmp_speed.i>>16;
562 TWDR = tmp_speed.i>>8;
565 case 0x3B: // speed LSB
569 case 0x3C: // angle MSB
570 tmp_angle.f = cur_speed_rot;
571 TWDR = tmp_angle.i>>24;
575 TWDR = tmp_angle.i>>16;
579 TWDR = tmp_angle.i>>8;
582 case 0x3F: // angle LSB
586 case 0x40: // Position x MSB
590 case 0x41: // Position x
594 case 0x42: // Position x
598 case 0x43: // Position x LSB
602 case 0x44: // Position y MSB
606 case 0x45: // Position y
610 case 0x46: // Position y
614 case 0x47: // Position y LSB
618 case 0x48: // Position angle MSB
622 case 0x49: // Position angle
626 case 0x4A: // Position angle
630 case 0x4B: // Position angle LSB
634 case 0xA0: // Reset reason
635 TWDR = MCUCSR & 0x0f;
639 case 0xA1: // Error status
643 case 0xA2: // count test
646 case 0xA3: // last i2c status before boot
647 TWDR = last_i2c_status_boot;
649 case 0xA4: // Watchdog enable
650 TWDR = watchdog_enable;
667 if (watchdog_enable == 2) {
673 static void update_hall1(void) {
674 unsigned char status = (PINA >> 0) & 0x3;
675 static unsigned char oldstatus=0;
676 unsigned char diff, new;
682 new ^= 0x1; // convert gray to binary
683 diff = oldstatus - new; // difference last - new
684 if (diff & 0x1) { // bit 0 = value (1)
685 oldstatus = new; // store new as next last
686 if (motor1_switch) pos1 += (diff & 2) - 1; // bit 1 = direction (+/-)
687 else pos1 -= (diff & 2) - 1;
692 static void update_hall2(void) {
693 unsigned char status = (PINA >> 4) & 0x3;
694 static unsigned char oldstatus=0;
695 unsigned char diff, new;
701 new ^= 0x1; // convert gray to binary
702 diff = oldstatus - new; // difference last - new
703 if (diff & 0x1) { // bit 0 = value (1)
704 oldstatus = new; // store new as next last
705 if (motor2_switch) pos2 -= (diff & 2) - 1; // bit 1 = direction (+/-)
706 else pos2 += (diff & 2) - 1;
711 static void update_hall3(void) {
712 unsigned char status = (PINA >> 2) & 0x3;
713 static unsigned char oldstatus=0;
714 unsigned char diff, new;
720 new ^= 0x1; // convert gray to binary
721 diff = oldstatus - new; // difference last - new
722 if (diff & 0x1) { // bit 0 = value (1)
723 oldstatus = new; // store new as next last
724 if (motor3_switch) pos3 -= (diff & 2) - 1; // bit 1 = direction (+/-)
725 else pos3 += (diff & 2) - 1;
730 static void update_hall4(void) {
731 unsigned char status = (PINA >> 6) & 0x3;
732 static unsigned char oldstatus=0;
733 unsigned char diff, new;
739 new ^= 0x1; // convert gray to binary
740 diff = oldstatus - new; // difference last - new
741 if (diff & 0x1) { // bit 0 = value (1)
742 oldstatus = new; // store new as next last
743 if (motor4_switch) pos4 += (diff & 2) - 1; // bit 1 = direction (+/-)
744 else pos4 -= (diff & 2) - 1;
749 static void update_motor(void) {
750 static int16_t m1_old=SHRT_MIN;
751 static int16_t m2_old=SHRT_MIN;
752 static int16_t m3_old=SHRT_MIN;
753 static int16_t m4_old=SHRT_MIN;
755 error_state &= 0xf0; // clear lower bits
756 error_state |= ~((PIND & 0x40)>>3 | (PINB & 0x07)) & 0xf;
758 if (m1_old != motor1) { // update only when changed
761 PORTC &= ~(1 << 3) & ~(1 << 2);
763 } else if (motor1 == PWM_BREAK) {
764 PORTC |= (1 << 3) | (1 << 2);
766 } else if ((!motor1_switch && motor1 > 0) || (motor1_switch && motor1 < 0)) {
773 } else { // motor1 < 0
786 if (m2_old != motor2) { // update only when changed
789 PORTC &= ~(1 << 5) & ~(1 << 4);
791 } else if (motor2 == PWM_BREAK) {
792 PORTC |= (1 << 5) | (1 << 4);
794 } else if ((!motor2_switch && motor2 > 0) || (motor2_switch && motor2 < 0)) {
801 } else { // motor2 < 0
814 if (m3_old != motor3) { // update only when changed
817 PORTC &= ~(1 << 7) & ~(1 << 6);
819 } else if (motor3 == PWM_BREAK) {
820 PORTC |= (1 << 7) | (1 << 6);
822 } else if ((!motor3_switch && motor3 > 0) || (motor3_switch && motor3 < 0)) {
829 } else { // motor3 < 0
842 if (m4_old != motor4) { // update only when changed
845 PORTD &= ~(1 << 3) & ~(1 << 2);
847 } else if (motor4 == PWM_BREAK) {
848 PORTD |= (1 << 3) | (1 << 2);
850 } else if ((!motor4_switch && motor4 > 0) || (motor4_switch && motor4 < 0)) {
857 } else { // motor4 < 0
872 static void update_pos(void) {
873 static int16_t pos1_last=0;
874 static int16_t pos2_last=0;
875 static int16_t pos3_last=0;
876 static int16_t pos4_last=0;
877 int16_t pos1_diff; // steps
881 float diff_left_m, diff_right_m, angle_diff, translation;
882 float pos_x_new, pos_y_new, angle_new;
883 float tmp_speed_lin, tmp_speed_rot;
884 int16_t cur_pos1, cur_pos2, cur_pos3, cur_pos4;
885 int16_t new_speed1, new_speed2, new_speed3, new_speed4;
895 pos1_diff = cur_pos1 - pos1_last;
896 pos2_diff = cur_pos2 - pos2_last;
897 pos3_diff = cur_pos3 - pos3_last;
898 pos4_diff = cur_pos4 - pos4_last;
900 new_speed1 = pos1_diff/PID_T;
901 new_speed2 = pos2_diff/PID_T;
902 new_speed3 = pos3_diff/PID_T;
903 new_speed4 = pos4_diff/PID_T;
905 diff_left_m = (pos1_diff + pos2_diff)/(2*STEP_PER_M_LEFT);
906 diff_right_m = (pos3_diff + pos4_diff)/(2*STEP_PER_M_RIGHT);
907 angle_diff = (diff_right_m - diff_left_m) / WHEEL_DIST;
909 angle_new = angle.f + angle_diff;
910 if (angle_new > 2*M_PI) angle_new-=2*M_PI;
911 else if (angle_new < -2*M_PI) angle_new+=2*M_PI;
913 translation = (diff_left_m + diff_right_m)/2.0;
914 pos_x_new = pos_x.f + cos(angle_new)*translation;
915 pos_y_new = pos_y.f + sin(angle_new)*translation;
917 tmp_speed_lin = translation/PID_T;
918 tmp_speed_rot = angle_diff/PID_T;
929 cur_speed_lin = tmp_speed_lin;
930 cur_speed_rot = tmp_speed_rot;
933 pos1_last = cur_pos1;
934 pos2_last = cur_pos2;
935 pos3_last = cur_pos3;
936 pos4_last = cur_pos4;
940 static void update_pid(void) {
941 static int16_t eold1=0;
942 static int16_t eold2=0;
943 static int16_t eold3=0;
944 static int16_t eold4=0;
945 static int32_t esum1=0;
946 static int32_t esum2=0;
947 static int32_t esum3=0;
948 static int32_t esum4=0;
950 // protect motors from damage if stalling
951 if (labs(esum1) > STALL_LIMIT && speed1 == 0) {
953 motor1_mode = MOTOR_MANUAL;
954 error_state |= (1<<4);
957 if (labs(esum2) > STALL_LIMIT && speed2 == 0) {
959 motor2_mode = MOTOR_MANUAL;
960 error_state |= (1<<5);
963 if (labs(esum3) > STALL_LIMIT && speed3 == 0) {
965 motor3_mode = MOTOR_MANUAL;
966 error_state |= (1<<6);
969 if (labs(esum4) > STALL_LIMIT && speed4 == 0) {
971 motor4_mode = MOTOR_MANUAL;
972 error_state |= (1<<7);
976 if (motor1_mode == MOTOR_PID) {
977 if (speed1_wish != speed1_wish_old) {
978 if (abs(speed1_wish - speed1_wish_old) > 500) esum1 = 0;
979 speed1_wish_old = speed1_wish;
982 uint8_t dir_change = (speed1_wish > 0 && speed1 < 0) || (speed1_wish < 0 && speed1 > 0); // Prevent dangerous immediate engine reverse
983 if (speed1_wish == 0 || dir_change) {
986 error_state &= ~(1<<4);
988 int16_t e = speed1_wish - speed1;
990 motor1 = KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
993 if (motor1 > 0 && speed1_wish < 0) motor1=PWM_BREAK;
994 else if (motor1 < 0 && speed1_wish > 0) motor1=PWM_BREAK;
995 else if (motor1 > 255) motor1 = 255;
996 else if (motor1 < -255) motor1 = -255;
999 if (motor2_mode == MOTOR_PID) {
1000 if (speed2_wish != speed2_wish_old) {
1001 if (abs(speed2_wish - speed2_wish_old) > 500) esum2 = 0;
1002 speed2_wish_old = speed2_wish;
1005 uint8_t dir_change = (speed2_wish > 0 && speed2 < 0) || (speed2_wish < 0 && speed2 > 0); // Prevent dangerous immediate engine reverse
1006 if (speed2_wish == 0 || dir_change) {
1009 error_state &= ~(1<<5);
1011 int16_t e = speed2_wish - speed2;
1013 motor2 = KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
1016 if (motor2 > 0 && speed2_wish < 0) motor2=PWM_BREAK;
1017 else if (motor2 < 0 && speed2_wish > 0) motor2=PWM_BREAK;
1018 else if (motor2 > 255) motor2 = 255;
1019 else if (motor2 < -255) motor2 = -255;
1022 if (motor3_mode == MOTOR_PID) {
1023 if (speed3_wish != speed3_wish_old) {
1024 if (abs(speed3_wish - speed3_wish_old) > 500) esum3 = 0;
1025 speed3_wish_old = speed3_wish;
1028 uint8_t dir_change = (speed3_wish > 0 && speed3 < 0) || (speed3_wish < 0 && speed3 > 0); // Prevent dangerous immediate engine reverse
1029 if (speed3_wish == 0 || dir_change) {
1032 error_state &= ~(1<<6);
1034 int16_t e = speed3_wish - speed3;
1036 motor3 = KP*e + KI*PID_T*esum3 + KD/PID_T*(e - eold3);
1039 if (motor3 > 0 && speed3_wish < 0) motor3=PWM_BREAK;
1040 else if (motor3 < 0 && speed3_wish > 0) motor3=PWM_BREAK;
1041 else if (motor3 > 255) motor3 = 255;
1042 else if (motor3 < -255) motor3 = -255;
1045 if (motor4_mode == MOTOR_PID) {
1046 if (speed4_wish != speed4_wish_old) {
1047 if (abs(speed4_wish - speed4_wish_old) > 500) esum4 = 0;
1048 speed4_wish_old = speed4_wish;
1051 uint8_t dir_change = (speed4_wish > 0 && speed4 < 0) || (speed4_wish < 0 && speed4 > 0); // Prevent dangerous immediate engine reverse
1052 if (speed4_wish == 0 || dir_change) {
1055 error_state &= ~(1<<7);
1057 int16_t e = speed4_wish - speed4;
1059 motor4 = KP*e + KI*PID_T*esum4 + KD/PID_T*(e - eold4);
1062 if (motor4 > 0 && speed4_wish < 0) motor4=PWM_BREAK;
1063 else if (motor4 < 0 && speed4_wish > 0) motor4=PWM_BREAK;
1064 else if (motor4 > 255) motor4 = 255;
1065 else if (motor4 < -255) motor4 = -255;
1071 ISR(TIMER1_OVF_vect) {
1080 ISR(TIMER0_OVF_vect) {
1082 if (sound_update >= sound_max) {
1089 static void set_motor4_sound(uint16_t hz) {
1092 sound_max = 19608/hz/2;
1094 // Enable Timer 0 Overflow Interrupt
1095 TIMSK |= (1 << TOIE0);
1096 last_man_update_count = I2C_TIMEOUT_DISABLE;
1098 // Disable Timer 0 Overflow Interrupt
1099 TIMSK &= ~(1 << TOIE0);
1108 DDRC = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
1109 DDRD = (1 << 7) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
1110 // Pullup Diag/Enable
1111 PORTB = (1 << 0) | (1 << 1) | (1 << 2);
1116 uart_setup_stdout();
1123 // Also used for PWM frequency TIMER1_FREQ (F_CPU/256)
1124 // Timer 1: Fast PWM non-inverting mode, Top=255 => 19.531kHz
1126 //TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10);
1127 // Avoid narrow spike on extreme pwm value 0 by not setting COM1*1
1128 TCCR1A = (1 << WGM10);
1129 TCCR1B = (1 << WGM12) | (1 << CS10);
1134 // Timer 2: Fast PWM non-inverting mode, Top=255
1136 //TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << COM21) | (1 << CS20);
1137 // Avoid narrow spike on extreme pwm value 0 by not setting COM21
1138 TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << CS20);
1142 // Timer 0: Fast PWM non-inverting mode, Top=255
1144 //TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << COM01) | (1 << CS00);
1145 // Avoid narrow spike on extreme pwm value 0 by not setting COM01
1146 TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << CS00);
1149 printf_P(PSTR("\r\nStart\r\n"));
1151 last_i2c_status_boot = eeprom_read_byte((uint8_t*)1);
1153 set_sleep_mode(SLEEP_MODE_IDLE);
1154 // Enable Timer 1 Overflow Interrupt
1155 TIMSK = (1 << TOIE1);
1160 case 0xA4: // Watchdog enable
1161 if (watchdog_enable == 1) {
1162 wdt_enable(WDTO_2S);
1163 watchdog_enable = 2;
1164 } else if (watchdog_enable == 0) {
1166 watchdog_enable = 3;
1169 case 0xff: // Magic reg that starts the bootloader
1170 if (bootloader == 0xa5) {
1172 // write mark to first area in eeprom
1173 eeprom_write_byte((uint8_t*)0, 123);
1175 // Use watchdog to restart
1176 wdt_enable(WDTO_15MS);
1181 if (cmd_vel.bUpdate) {
1182 float speed_wish_right, speed_wish_left;
1186 speed = cmd_vel.speed;
1187 angle = cmd_vel.angle;
1188 cmd_vel.bUpdate = 0;
1191 speed_wish_right = (angle*WHEEL_DIST)/2 + speed;
1192 speed_wish_left = speed*2-speed_wish_right;
1194 speed_wish_left*=STEP_PER_M_LEFT;
1195 speed_wish_right*=STEP_PER_M_RIGHT;
1197 if (aft_handicap > 0) {
1198 speed1_wish = speed_wish_left * (100-aft_handicap)/100.0;
1199 speed3_wish = speed_wish_right * (100-aft_handicap)/100.0;
1201 speed1_wish = speed_wish_left;
1202 speed3_wish = speed_wish_right;
1204 if (front_handicap > 0) {
1205 speed2_wish = speed_wish_left * (100-front_handicap)/100.0;
1206 speed4_wish = speed_wish_right * (100-front_handicap)/100.0;
1208 speed2_wish = speed_wish_left;
1209 speed4_wish = speed_wish_right;
1211 motor1_mode = MOTOR_PID;
1212 motor2_mode = MOTOR_PID;
1213 motor3_mode = MOTOR_PID;
1214 motor4_mode = MOTOR_PID;
1217 if (run_update >= 195) { // TIMER1_FREQ/195 = ~100Hz
1224 if (last_man_update_count != I2C_TIMEOUT_DISABLE) {
1225 last_man_update_count++;
1227 if (last_man_update_count >= 100) {
1228 // ~1s without a new i2c command
1231 cmd_vel.bUpdate = 1;
1232 if (last_man_update_count == 100) {
1233 printf_P(PSTR("I2C State: 0x%x\r\n"), last_i2c_status);
1234 eeprom_write_byte((uint8_t*)1, last_i2c_status);
1237 last_man_update_count = I2C_TIMEOUT_DISABLE;