X-Git-Url: https://defiant.homedns.org/gitweb/?p=ros_wild_thumper.git;a=blobdiff_plain;f=avr%2Fmotor_ctrl%2Fmain.c;h=b0d9cbfe00aba102cc72ce4f82173b5eefa30eac;hp=7781fb4af01654b33ea2a53df04a1bbaf63b7967;hb=2e1412681f31d74d078d532915017a22281bbcc1;hpb=d55fc1ff8c77515d29610d3940f35965ff202a90 diff --git a/avr/motor_ctrl/main.c b/avr/motor_ctrl/main.c index 7781fb4..b0d9cbf 100644 --- a/avr/motor_ctrl/main.c +++ b/avr/motor_ctrl/main.c @@ -5,6 +5,9 @@ #include #include #include +#include +#include +#include #include "uart.h" /* @@ -110,10 +113,11 @@ #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< 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); @@ -953,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); @@ -975,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); @@ -997,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); @@ -1041,7 +1054,7 @@ int main(void) { // I2C TWAR = 0x50; - TWI_RESET; + TWI_ACK; // Motor 1 & 2 // Also used for PWM frequency TIMER1_FREQ (F_CPU/256) @@ -1082,10 +1095,11 @@ int main(void) { case 0xff: // Magic reg that starts the bootloader if (bootloader == 0xa5) { cli(); - { - void (*start)(void) = (void*)0x1800; - start(); - } + // write mark to first area in eeprom + eeprom_write_byte((uint8_t*)0, 123); + eeprom_busy_wait(); + // Use watchdog to restart + wdt_enable(WDTO_15MS); } break; } @@ -1133,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();