]> defiant.homedns.org Git - ros_wild_thumper.git/blobdiff - avr/motor_ctrl/main.c
base_local_planner: Reduce acc_lim_x for less shaky acceleration
[ros_wild_thumper.git] / avr / motor_ctrl / main.c
index 3642d4a8b222e59530eaafb28cb8a287f3dcd4da..b0d9cbfe00aba102cc72ce4f82173b5eefa30eac 100644 (file)
 #define WHEEL_DIST 0.39912 // Measured: 0.252
 #define PWM_BREAK INT16_MIN
 #define STALL_LIMIT 140000
+#define I2C_TIMEOUT_DISABLE 255
 
 #define TWI_ACK   TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) | (1<<TWIE)
 #define TWI_NAK   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
@@ -183,6 +184,7 @@ static volatile uint8_t count_test=0;
 static volatile uint8_t front_handicap=0;
 static volatile uint8_t aft_handicap=0;
 static volatile uint8_t error_state=0;
+static volatile uint8_t last_man_update_count=I2C_TIMEOUT_DISABLE;
 
 ISR(TWI_vect)
 {
@@ -349,6 +351,7 @@ ISR(TWI_vect)
                                        tmp_angle.i = tmp_angle.i << 8 | TWDR;
                                        cmd_vel.angle = tmp_angle.f;
                                        cmd_vel.bUpdate = 1;
+                                       last_man_update_count = 0;
                                        TWI_ACK;
                                        break;
                                case 0x90: // Motor 1 switch
@@ -382,7 +385,7 @@ ISR(TWI_vect)
                                default:
                                        TWI_NAK;
                        }
-                       ireg++;
+                       if (ireg < 0xff) ireg++;
                        break;
                case TW_ST_SLA_ACK: // start read
                case TW_ST_DATA_ACK: // read
@@ -937,7 +940,8 @@ static void update_pid(void) {
                        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);
@@ -959,7 +963,8 @@ static void update_pid(void) {
                        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);
@@ -981,7 +986,8 @@ static void update_pid(void) {
                        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);
@@ -1003,7 +1009,8 @@ static void update_pid(void) {
                        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);
@@ -1140,6 +1147,15 @@ int main(void) {
                        update_pid();
                        update_motor();
                        count_test++;
+                       if (last_man_update_count != I2C_TIMEOUT_DISABLE) last_man_update_count++;
+
+                       if (last_man_update_count > 100) {
+                               // ~1s without a new i2c command
+                               cmd_vel.speed = 0;
+                               cmd_vel.angle = 0;
+                               cmd_vel.bUpdate = 1;
+                               last_man_update_count = I2C_TIMEOUT_DISABLE;
+                       }
                }
 
                sleep_mode();