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
108 // wheel diameter=12cm, encoder=48cpr, gear ratio=1:47
109 // STEP_PER_M = 48*47/(d*pi)
110 // Left real diameter: 0.12808, Right real diameter: 0.121
111 #define STEP_PER_M 5573.0
112 #define STEP_PER_M_LEFT (STEP_PER_M)
113 #define STEP_PER_M_RIGHT (STEP_PER_M)
114 #define WHEEL_DIST 0.39912 // Measured: 0.252
115 #define PWM_BREAK INT16_MIN
116 #define STALL_LIMIT 140000
117 #define I2C_TIMEOUT_DISABLE 255
119 #define TWI_ACK TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) | (1<<TWIE)
120 #define TWI_NAK TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
121 #define TWI_RESET TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWSTO) | (1<<TWEN) | (1<<TWIE);
122 #define ENABLE_PWM_MOTOR1 TCCR1A |= (1 << COM1A1)
123 #define ENABLE_PWM_MOTOR2 TCCR1A |= (1 << COM1B1)
124 #define ENABLE_PWM_MOTOR3 TCCR2 |= (1 << COM21);
125 #define ENABLE_PWM_MOTOR4 TCCR0 |= (1 << COM01);
126 #define DISABLE_PWM_MOTOR1 TCCR1A &= ~(1 << COM1A1)
127 #define DISABLE_PWM_MOTOR2 TCCR1A &= ~(1 << COM1B1)
128 #define DISABLE_PWM_MOTOR3 TCCR2 &= ~(1 << COM21);
129 #define DISABLE_PWM_MOTOR4 TCCR0 &= ~(1 << COM01);
142 static volatile struct {
146 } cmd_vel = {0, 0, 0};
148 static volatile uint8_t ireg=0;
149 static volatile uint8_t bootloader=0;
150 static volatile int16_t motor1=0; // -255..+255
151 static volatile int16_t motor2=0;
152 static volatile int16_t motor3=0;
153 static volatile int16_t motor4=0;
154 static volatile int16_t pos1=0; // step
155 static volatile int16_t pos2=0;
156 static volatile int16_t pos3=0;
157 static volatile int16_t pos4=0;
158 static volatile enum mode motor1_mode=MOTOR_MANUAL;
159 static volatile enum mode motor2_mode=MOTOR_MANUAL;
160 static volatile enum mode motor3_mode=MOTOR_MANUAL;
161 static volatile enum mode motor4_mode=MOTOR_MANUAL;
162 static volatile uint8_t motor1_switch=1;
163 static volatile uint8_t motor2_switch=1;
164 static volatile uint8_t motor3_switch=0;
165 static volatile uint8_t motor4_switch=0;
166 static volatile int16_t speed1_wish=0; // step/s
167 static volatile int16_t speed2_wish=0;
168 static volatile int16_t speed3_wish=0;
169 static volatile int16_t speed4_wish=0;
170 static volatile int16_t speed1_wish_old=0;
171 static volatile int16_t speed2_wish_old=0;
172 static volatile int16_t speed3_wish_old=0;
173 static volatile int16_t speed4_wish_old=0;
174 static volatile uint8_t run_update=0;
175 static volatile int16_t speed1=0; // step/s
176 static volatile int16_t speed2=0;
177 static volatile int16_t speed3=0;
178 static volatile int16_t speed4=0;
179 static volatile ufloat_t pos_x={0.0};
180 static volatile ufloat_t pos_y={0.0};
181 static volatile ufloat_t angle={0.0};
182 static volatile float cur_speed_lin=0;
183 static volatile float cur_speed_rot=0;
184 static volatile uint8_t count_test=0;
185 static volatile uint8_t front_handicap=0;
186 static volatile uint8_t aft_handicap=0;
187 static volatile uint8_t error_state=0;
188 static volatile uint8_t last_man_update_count=0;
189 static volatile uint8_t last_i2c_status = 0;
193 static uint8_t tmp=0;
194 static int16_t tmp16=0;
195 static ufloat_t tmp_speed;
196 static ufloat_t tmp_angle;
198 last_i2c_status = TW_STATUS;
201 case TW_SR_SLA_ACK: // start write
205 case TW_SR_DATA_ACK: // write
207 case 0x00: // register select
209 ireg--; // because we do ireg++ below
212 case 0x01: // Motor 1 MSB
216 case 0x02: // Motor 1 LSB
217 motor1 = tmp<<8 | TWDR;
218 motor1_mode = MOTOR_MANUAL;
221 case 0x03: // Motor 2 MSB
225 case 0x04: // Motor 2 LSB
226 motor2 = tmp<<8 | TWDR;
227 motor2_mode = MOTOR_MANUAL;
230 case 0x05: // Motor 3 MSB
234 case 0x06: // Motor 3 LSB
235 motor3 = tmp<<8 | TWDR;
236 motor3_mode = MOTOR_MANUAL;
239 case 0x07: // Motor 4 MSB
243 case 0x08: // Motor 4 LSB
244 motor4 = tmp<<8 | TWDR;
245 motor4_mode = MOTOR_MANUAL;
248 case 0x20: // Motor 1 speed wish MSB
252 case 0x21: // Motor 1 speed wish LSB
253 speed1_wish = tmp<<8 | TWDR;
254 motor1_mode = MOTOR_PID;
257 case 0x22: // Motor 2 speed wish MSB
261 case 0x23: // Motor 2 speed wish LSB
262 speed2_wish = tmp<<8 | TWDR;
263 motor2_mode = MOTOR_PID;
266 case 0x24: // Motor 3 speed wish MSB
270 case 0x25: // Motor 3 speed wish LSB
271 speed3_wish = tmp<<8 | TWDR;
272 motor3_mode = MOTOR_PID;
275 case 0x26: // Motor 4 speed wish MSB
279 case 0x27: // Motor 4 speed wish LSB
280 speed4_wish = tmp<<8 | TWDR;
281 motor4_mode = MOTOR_PID;
284 case 0x28: // Left speed wish MSB
288 case 0x29: // Left speed wish
289 tmp_speed.i = tmp_speed.i << 8 | TWDR;
292 case 0x2A: // Left speed wish
293 tmp_speed.i = tmp_speed.i << 8 | TWDR;
296 case 0x2B: // Left speed wish LSB
297 tmp_speed.i = tmp_speed.i << 8 | TWDR;
298 speed1_wish = tmp_speed.f*STEP_PER_M_LEFT;
299 speed2_wish = tmp_speed.f*STEP_PER_M_LEFT;
300 motor1_mode = MOTOR_PID;
301 motor2_mode = MOTOR_PID;
304 case 0x2C: // Right speed wish MSB
308 case 0x2D: // Right speed wish
309 tmp_speed.i = tmp_speed.i << 8 | TWDR;
312 case 0x2E: // Right speed wish
313 tmp_speed.i = tmp_speed.i << 8 | TWDR;
316 case 0x2F: // Right speed wish LSB
317 tmp_speed.i = tmp_speed.i << 8 | TWDR;
318 speed1_wish = tmp_speed.f*STEP_PER_M_RIGHT;
319 speed2_wish = tmp_speed.f*STEP_PER_M_RIGHT;
320 motor1_mode = MOTOR_PID;
321 motor2_mode = MOTOR_PID;
324 case 0x50: // speed wish MSB
328 case 0x51: // speed wish
329 tmp_speed.i = tmp_speed.i << 8 | TWDR;
332 case 0x52: // speed wish
333 tmp_speed.i = tmp_speed.i << 8 | TWDR;
336 case 0x53: // speed wish LSB
337 tmp_speed.i = tmp_speed.i << 8 | TWDR;
338 cmd_vel.speed = tmp_speed.f;
341 case 0x54: // angle wish MSB
345 case 0x55: // angle wish
346 tmp_angle.i = tmp_angle.i << 8 | TWDR;
349 case 0x56: // angle wish
350 tmp_angle.i = tmp_angle.i << 8 | TWDR;
353 case 0x57: // angle wish LSB
354 tmp_angle.i = tmp_angle.i << 8 | TWDR;
355 cmd_vel.angle = tmp_angle.f;
357 last_man_update_count = 0;
360 case 0x90: // Motor 1 switch
361 motor1_switch = TWDR;
364 case 0x91: // Motor 2 switch
365 motor2_switch = TWDR;
368 case 0x92: // Motor 3 switch
369 motor3_switch = TWDR;
372 case 0x93: // Motor 4 switch
373 motor4_switch = TWDR;
376 case 0x94: // Front Handicap
377 front_handicap = TWDR;
381 case 0x95: // Aft Handicap
386 case 0xff: // bootloader
391 if (ireg < 0xff) ireg++;
393 case TW_ST_SLA_ACK: // start read
394 case TW_ST_DATA_ACK: // read
396 case 0x02: // Motor 1 PWM
400 case 0x03: // Dummy to allow continous read
404 case 0x04: // Motor 2 PWM
408 case 0x05: // Dummy to allow continous read
412 case 0x06: // Motor 3 PWM
416 case 0x07: // Dummy to allow continous read
420 case 0x08: // Motor 4 PWM
424 case 0x09: // Dummy to allow continous read
428 case 0x10: // Hall 1 MSB
433 case 0x11: // Hall 1 LSB
437 case 0x12: // Hall 2 MSB
442 case 0x13: // Hall 2 LSB
446 case 0x14: // Hall 3 MSB
451 case 0x15: // Hall 3 LSB
455 case 0x16: // Hall 4 MSB
460 case 0x17: // Hall 4 LSB
464 case 0x20: // Motor 1 speed wish MSB
465 TWDR = speed1_wish>>8;
468 case 0x21: // Motor 1 speed wish LSB
472 case 0x22: // Motor 2 speed wish MSB
473 TWDR = speed2_wish>>8;
476 case 0x23: // Motor 2 speed wish LSB
480 case 0x24: // Motor 3 speed wish MSB
481 TWDR = speed3_wish>>8;
484 case 0x25: // Motor 3 speed wish LSB
488 case 0x26: // Motor 4 speed wish MSB
489 TWDR = speed4_wish>>8;
492 case 0x27: // Motor 4 speed wish LSB
496 case 0x30: // Motor 1 speed MSB
500 case 0x31: // Motor 1 speed LSB
504 case 0x32: // Motor 2 speed MSB
508 case 0x33: // Motor 2 speed LSB
512 case 0x34: // Motor 3 speed MSB
516 case 0x35: // Motor 3 speed LSB
520 case 0x36: // Motor 4 speed MSB
524 case 0x37: // Motor 4 speed LSB
528 case 0x38: // speed MSB
529 tmp_speed.f = cur_speed_lin;
530 TWDR = tmp_speed.i>>24;
534 TWDR = tmp_speed.i>>16;
538 TWDR = tmp_speed.i>>8;
541 case 0x3B: // speed LSB
545 case 0x3C: // angle MSB
546 tmp_angle.f = cur_speed_rot;
547 TWDR = tmp_angle.i>>24;
551 TWDR = tmp_angle.i>>16;
555 TWDR = tmp_angle.i>>8;
558 case 0x3F: // angle LSB
562 case 0x40: // Position x MSB
566 case 0x41: // Position x
570 case 0x42: // Position x
574 case 0x43: // Position x LSB
578 case 0x44: // Position y MSB
582 case 0x45: // Position y
586 case 0x46: // Position y
590 case 0x47: // Position y LSB
594 case 0x48: // Position angle MSB
598 case 0x49: // Position angle
602 case 0x4A: // Position angle
606 case 0x4B: // Position angle LSB
610 case 0xA0: // Reset reason
611 TWDR = MCUCSR & 0x0f;
615 case 0xA1: // Error status
619 case 0xA2: // count test
639 static void update_hall1(void) {
640 unsigned char status = (PINA >> 0) & 0x3;
641 static unsigned char oldstatus=0;
642 unsigned char diff, new;
648 new ^= 0x1; // convert gray to binary
649 diff = oldstatus - new; // difference last - new
650 if (diff & 0x1) { // bit 0 = value (1)
651 oldstatus = new; // store new as next last
652 if (motor1_switch) pos1 += (diff & 2) - 1; // bit 1 = direction (+/-)
653 else pos1 -= (diff & 2) - 1;
658 static void update_hall2(void) {
659 unsigned char status = (PINA >> 4) & 0x3;
660 static unsigned char oldstatus=0;
661 unsigned char diff, new;
667 new ^= 0x1; // convert gray to binary
668 diff = oldstatus - new; // difference last - new
669 if (diff & 0x1) { // bit 0 = value (1)
670 oldstatus = new; // store new as next last
671 if (motor2_switch) pos2 -= (diff & 2) - 1; // bit 1 = direction (+/-)
672 else pos2 += (diff & 2) - 1;
677 static void update_hall3(void) {
678 unsigned char status = (PINA >> 2) & 0x3;
679 static unsigned char oldstatus=0;
680 unsigned char diff, new;
686 new ^= 0x1; // convert gray to binary
687 diff = oldstatus - new; // difference last - new
688 if (diff & 0x1) { // bit 0 = value (1)
689 oldstatus = new; // store new as next last
690 if (motor3_switch) pos3 -= (diff & 2) - 1; // bit 1 = direction (+/-)
691 else pos3 += (diff & 2) - 1;
696 static void update_hall4(void) {
697 unsigned char status = (PINA >> 6) & 0x3;
698 static unsigned char oldstatus=0;
699 unsigned char diff, new;
705 new ^= 0x1; // convert gray to binary
706 diff = oldstatus - new; // difference last - new
707 if (diff & 0x1) { // bit 0 = value (1)
708 oldstatus = new; // store new as next last
709 if (motor4_switch) pos4 += (diff & 2) - 1; // bit 1 = direction (+/-)
710 else pos4 -= (diff & 2) - 1;
715 static void update_motor(void) {
716 static int16_t m1_old=SHRT_MIN;
717 static int16_t m2_old=SHRT_MIN;
718 static int16_t m3_old=SHRT_MIN;
719 static int16_t m4_old=SHRT_MIN;
721 error_state &= 0xf0; // clear lower bits
722 error_state |= ~((PIND & 0x40)>>3 | (PINB & 0x07)) & 0xf;
724 if (m1_old != motor1) { // update only when changed
727 PORTC &= ~(1 << 3) & ~(1 << 2);
729 } else if (motor1 == PWM_BREAK) {
730 PORTC |= (1 << 3) | (1 << 2);
732 } else if ((!motor1_switch && motor1 > 0) || (motor1_switch && motor1 < 0)) {
739 } else { // motor1 < 0
752 if (m2_old != motor2) { // update only when changed
755 PORTC &= ~(1 << 5) & ~(1 << 4);
757 } else if (motor2 == PWM_BREAK) {
758 PORTC |= (1 << 5) | (1 << 4);
760 } else if ((!motor2_switch && motor2 > 0) || (motor2_switch && motor2 < 0)) {
767 } else { // motor2 < 0
780 if (m3_old != motor3) { // update only when changed
783 PORTC &= ~(1 << 7) & ~(1 << 6);
785 } else if (motor3 == PWM_BREAK) {
786 PORTC |= (1 << 7) | (1 << 6);
788 } else if ((!motor3_switch && motor3 > 0) || (motor3_switch && motor3 < 0)) {
795 } else { // motor3 < 0
808 if (m4_old != motor4) { // update only when changed
811 PORTD &= ~(1 << 3) & ~(1 << 2);
813 } else if (motor4 == PWM_BREAK) {
814 PORTD |= (1 << 3) | (1 << 2);
816 } else if ((!motor4_switch && motor4 > 0) || (motor4_switch && motor4 < 0)) {
823 } else { // motor4 < 0
838 static void update_pos(void) {
839 static int16_t pos1_last=0;
840 static int16_t pos2_last=0;
841 static int16_t pos3_last=0;
842 static int16_t pos4_last=0;
843 int16_t pos1_diff; // steps
847 float diff_left_m, diff_right_m, angle_diff, translation;
848 float pos_x_new, pos_y_new, angle_new;
849 float tmp_speed_lin, tmp_speed_rot;
850 int16_t cur_pos1, cur_pos2, cur_pos3, cur_pos4;
851 int16_t new_speed1, new_speed2, new_speed3, new_speed4;
861 pos1_diff = cur_pos1 - pos1_last;
862 pos2_diff = cur_pos2 - pos2_last;
863 pos3_diff = cur_pos3 - pos3_last;
864 pos4_diff = cur_pos4 - pos4_last;
866 new_speed1 = pos1_diff/PID_T;
867 new_speed2 = pos2_diff/PID_T;
868 new_speed3 = pos3_diff/PID_T;
869 new_speed4 = pos4_diff/PID_T;
871 diff_left_m = (pos1_diff + pos2_diff)/(2*STEP_PER_M_LEFT);
872 diff_right_m = (pos3_diff + pos4_diff)/(2*STEP_PER_M_RIGHT);
873 angle_diff = (diff_right_m - diff_left_m) / WHEEL_DIST;
875 angle_new = angle.f + angle_diff;
876 if (angle_new > 2*M_PI) angle_new-=2*M_PI;
877 else if (angle_new < -2*M_PI) angle_new+=2*M_PI;
879 translation = (diff_left_m + diff_right_m)/2.0;
880 pos_x_new = pos_x.f + cos(angle_new)*translation;
881 pos_y_new = pos_y.f + sin(angle_new)*translation;
883 tmp_speed_lin = translation/PID_T;
884 tmp_speed_rot = angle_diff/PID_T;
895 cur_speed_lin = tmp_speed_lin;
896 cur_speed_rot = tmp_speed_rot;
899 pos1_last = cur_pos1;
900 pos2_last = cur_pos2;
901 pos3_last = cur_pos3;
902 pos4_last = cur_pos4;
906 static void update_pid(void) {
907 static int16_t eold1=0;
908 static int16_t eold2=0;
909 static int16_t eold3=0;
910 static int16_t eold4=0;
911 static int32_t esum1=0;
912 static int32_t esum2=0;
913 static int32_t esum3=0;
914 static int32_t esum4=0;
916 // protect motors from damage if stalling
917 if (labs(esum1) > STALL_LIMIT && speed1 == 0) {
919 motor1_mode = MOTOR_MANUAL;
920 error_state |= (1<<4);
923 if (labs(esum2) > STALL_LIMIT && speed2 == 0) {
925 motor2_mode = MOTOR_MANUAL;
926 error_state |= (1<<5);
929 if (labs(esum3) > STALL_LIMIT && speed3 == 0) {
931 motor3_mode = MOTOR_MANUAL;
932 error_state |= (1<<6);
935 if (labs(esum4) > STALL_LIMIT && speed4 == 0) {
937 motor4_mode = MOTOR_MANUAL;
938 error_state |= (1<<7);
942 if (motor1_mode == MOTOR_PID) {
943 if (speed1_wish != speed1_wish_old) {
944 if (abs(speed1_wish - speed1_wish_old) > 500) esum1 = 0;
945 speed1_wish_old = speed1_wish;
948 uint8_t dir_change = (speed1_wish > 0 && speed1 < 0) || (speed1_wish < 0 && speed1 > 0); // Prevent dangerous immediate engine reverse
949 if (speed1_wish == 0 || dir_change) {
952 error_state &= ~(1<<4);
954 int16_t e = speed1_wish - speed1;
956 motor1 = KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
959 if (motor1 > 0 && speed1_wish < 0) motor1=PWM_BREAK;
960 else if (motor1 < 0 && speed1_wish > 0) motor1=PWM_BREAK;
961 else if (motor1 > 255) motor1 = 255;
962 else if (motor1 < -255) motor1 = -255;
965 if (motor2_mode == MOTOR_PID) {
966 if (speed2_wish != speed2_wish_old) {
967 if (abs(speed2_wish - speed2_wish_old) > 500) esum2 = 0;
968 speed2_wish_old = speed2_wish;
971 uint8_t dir_change = (speed2_wish > 0 && speed2 < 0) || (speed2_wish < 0 && speed2 > 0); // Prevent dangerous immediate engine reverse
972 if (speed2_wish == 0 || dir_change) {
975 error_state &= ~(1<<5);
977 int16_t e = speed2_wish - speed2;
979 motor2 = KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
982 if (motor2 > 0 && speed2_wish < 0) motor2=PWM_BREAK;
983 else if (motor2 < 0 && speed2_wish > 0) motor2=PWM_BREAK;
984 else if (motor2 > 255) motor2 = 255;
985 else if (motor2 < -255) motor2 = -255;
988 if (motor3_mode == MOTOR_PID) {
989 if (speed3_wish != speed3_wish_old) {
990 if (abs(speed3_wish - speed3_wish_old) > 500) esum3 = 0;
991 speed3_wish_old = speed3_wish;
994 uint8_t dir_change = (speed3_wish > 0 && speed3 < 0) || (speed3_wish < 0 && speed3 > 0); // Prevent dangerous immediate engine reverse
995 if (speed3_wish == 0 || dir_change) {
998 error_state &= ~(1<<6);
1000 int16_t e = speed3_wish - speed3;
1002 motor3 = KP*e + KI*PID_T*esum3 + KD/PID_T*(e - eold3);
1005 if (motor3 > 0 && speed3_wish < 0) motor3=PWM_BREAK;
1006 else if (motor3 < 0 && speed3_wish > 0) motor3=PWM_BREAK;
1007 else if (motor3 > 255) motor3 = 255;
1008 else if (motor3 < -255) motor3 = -255;
1011 if (motor4_mode == MOTOR_PID) {
1012 if (speed4_wish != speed4_wish_old) {
1013 if (abs(speed4_wish - speed4_wish_old) > 500) esum4 = 0;
1014 speed4_wish_old = speed4_wish;
1017 uint8_t dir_change = (speed4_wish > 0 && speed4 < 0) || (speed4_wish < 0 && speed4 > 0); // Prevent dangerous immediate engine reverse
1018 if (speed4_wish == 0 || dir_change) {
1021 error_state &= ~(1<<7);
1023 int16_t e = speed4_wish - speed4;
1025 motor4 = KP*e + KI*PID_T*esum4 + KD/PID_T*(e - eold4);
1028 if (motor4 > 0 && speed4_wish < 0) motor4=PWM_BREAK;
1029 else if (motor4 < 0 && speed4_wish > 0) motor4=PWM_BREAK;
1030 else if (motor4 > 255) motor4 = 255;
1031 else if (motor4 < -255) motor4 = -255;
1037 ISR(TIMER1_OVF_vect) {
1050 DDRC = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
1051 DDRD = (1 << 7) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
1052 // Pullup Diag/Enable
1053 PORTB = (1 << 0) | (1 << 1) | (1 << 2);
1058 uart_setup_stdout();
1065 // Also used for PWM frequency TIMER1_FREQ (F_CPU/256)
1066 // Timer 1: Fast PWM non-inverting mode, Top=255 => 19.531kHz
1068 //TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10);
1069 // Avoid narrow spike on extreme pwm value 0 by not setting COM1*1
1070 TCCR1A = (1 << WGM10);
1071 TCCR1B = (1 << WGM12) | (1 << CS10);
1076 // Timer 2: Fast PWM non-inverting mode, Top=255
1078 //TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << COM21) | (1 << CS20);
1079 // Avoid narrow spike on extreme pwm value 0 by not setting COM21
1080 TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << CS20);
1084 // Timer 0: Fast PWM non-inverting mode, Top=255
1086 //TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << COM01) | (1 << CS00);
1087 // Avoid narrow spike on extreme pwm value 0 by not setting COM01
1088 TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << CS00);
1091 printf_P(PSTR("\r\nStart\r\n"));
1093 set_sleep_mode(SLEEP_MODE_IDLE);
1094 // Enable Timer 1 Overflow Interrupt
1095 TIMSK = (1 << TOIE1);
1100 case 0xff: // Magic reg that starts the bootloader
1101 if (bootloader == 0xa5) {
1103 // write mark to first area in eeprom
1104 eeprom_write_byte((uint8_t*)0, 123);
1106 // Use watchdog to restart
1107 wdt_enable(WDTO_15MS);
1112 if (cmd_vel.bUpdate) {
1113 float speed_wish_right, speed_wish_left;
1117 speed = cmd_vel.speed;
1118 angle = cmd_vel.angle;
1119 cmd_vel.bUpdate = 0;
1122 speed_wish_right = (angle*WHEEL_DIST)/2 + speed;
1123 speed_wish_left = speed*2-speed_wish_right;
1125 speed_wish_left*=STEP_PER_M_LEFT;
1126 speed_wish_right*=STEP_PER_M_RIGHT;
1128 if (aft_handicap > 0) {
1129 speed1_wish = speed_wish_left * (100-aft_handicap)/100.0;
1130 speed3_wish = speed_wish_right * (100-aft_handicap)/100.0;
1132 speed1_wish = speed_wish_left;
1133 speed3_wish = speed_wish_right;
1135 if (front_handicap > 0) {
1136 speed2_wish = speed_wish_left * (100-front_handicap)/100.0;
1137 speed4_wish = speed_wish_right * (100-front_handicap)/100.0;
1139 speed2_wish = speed_wish_left;
1140 speed4_wish = speed_wish_right;
1142 motor1_mode = MOTOR_PID;
1143 motor2_mode = MOTOR_PID;
1144 motor3_mode = MOTOR_PID;
1145 motor4_mode = MOTOR_PID;
1148 if (run_update >= 195) { // TIMER1_FREQ/195 = ~100Hz
1155 if (last_man_update_count != I2C_TIMEOUT_DISABLE) last_man_update_count++;
1157 if (last_man_update_count >= 100) {
1158 // ~1s without a new i2c command
1161 cmd_vel.bUpdate = 1;
1162 if (last_man_update_count == 100) {
1163 printf_P(PSTR("I2C State: 0x%x\r\n"), last_i2c_status);
1165 last_man_update_count = I2C_TIMEOUT_DISABLE;