misc avr fixes
[ros_wild_thumper.git] / avr / motor_ctrl / main.c
index 3642d4a8b222e59530eaafb28cb8a287f3dcd4da..ec9965f68800c26812a9eadf04da5e67dd3cec81 100644 (file)
@@ -382,7 +382,7 @@ ISR(TWI_vect)
                                default:
                                        TWI_NAK;
                        }
                                default:
                                        TWI_NAK;
                        }
-                       ireg++;
+                       if (ireg < 0xff) ireg++;
                        break;
                case TW_ST_SLA_ACK: // start read
                case TW_ST_DATA_ACK: // read
                        break;
                case TW_ST_SLA_ACK: // start read
                case TW_ST_DATA_ACK: // read
@@ -937,7 +937,8 @@ static void update_pid(void) {
                        speed1_wish_old = speed1_wish;
                }
 
                        speed1_wish_old = speed1_wish;
                }
 
-               if (speed1_wish == 0) {
+               uint8_t dir_change = (speed1_wish > 0 && speed1 < 0) || (speed1_wish < 0 && speed1 > 0); // Prevent dangerous immediate engine reverse
+               if (speed1_wish == 0 || dir_change) {
                        motor1 = 0;
                        eold1 = 0;
                        error_state &= ~(1<<4);
                        motor1 = 0;
                        eold1 = 0;
                        error_state &= ~(1<<4);
@@ -959,7 +960,8 @@ static void update_pid(void) {
                        speed2_wish_old = speed2_wish;
                }
 
                        speed2_wish_old = speed2_wish;
                }
 
-               if (speed2_wish == 0) {
+               uint8_t dir_change = (speed2_wish > 0 && speed2 < 0) || (speed2_wish < 0 && speed2 > 0); // Prevent dangerous immediate engine reverse
+               if (speed2_wish == 0 || dir_change) {
                        motor2 = 0;
                        eold2 = 0;
                        error_state &= ~(1<<5);
                        motor2 = 0;
                        eold2 = 0;
                        error_state &= ~(1<<5);
@@ -981,7 +983,8 @@ static void update_pid(void) {
                        speed3_wish_old = speed3_wish;
                }
 
                        speed3_wish_old = speed3_wish;
                }
 
-               if (speed3_wish == 0) {
+               uint8_t dir_change = (speed3_wish > 0 && speed3 < 0) || (speed3_wish < 0 && speed3 > 0); // Prevent dangerous immediate engine reverse
+               if (speed3_wish == 0 || dir_change) {
                        motor3 = 0;
                        eold3 = 0;
                        error_state &= ~(1<<6);
                        motor3 = 0;
                        eold3 = 0;
                        error_state &= ~(1<<6);
@@ -1003,7 +1006,8 @@ static void update_pid(void) {
                        speed4_wish_old = speed4_wish;
                }
 
                        speed4_wish_old = speed4_wish;
                }
 
-               if (speed4_wish == 0) {
+               uint8_t dir_change = (speed4_wish > 0 && speed4 < 0) || (speed4_wish < 0 && speed4 > 0); // Prevent dangerous immediate engine reverse
+               if (speed4_wish == 0 || dir_change) {
                        motor4 = 0;
                        eold4 = 0;
                        error_state &= ~(1<<7);
                        motor4 = 0;
                        eold4 = 0;
                        error_state &= ~(1<<7);