X-Git-Url: https://defiant.homedns.org/gitweb/?p=ros_wild_thumper.git;a=blobdiff_plain;f=avr%2Fmotor_ctrl%2Fmain.c;h=a4c984dfa4e7e2cbd0219bc29bc7e2017b045b58;hp=3642d4a8b222e59530eaafb28cb8a287f3dcd4da;hb=5d5f12eaf31287bcbaf01ff211b1366bd48c0658;hpb=f05fa64e03e9892b7a41857f71660f2a60c0a504 diff --git a/avr/motor_ctrl/main.c b/avr/motor_ctrl/main.c index 3642d4a..a4c984d 100644 --- a/avr/motor_ctrl/main.c +++ b/avr/motor_ctrl/main.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "uart.h" /* @@ -95,6 +96,8 @@ * 0xA0 Reset reason * 0xA1 Error status * 0xA2 count test + * 0xA3 last i2c status before boot + * 0xA4 Watchdog enable * free * 0xff Bootloader */ @@ -113,6 +116,7 @@ #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); @@ -959,7 +986,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 +1009,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 +1032,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); @@ -1076,7 +1106,9 @@ int main(void) { TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << CS00); OCR0 = 0; - printf("\r\nStart\r\n"); + printf_P(PSTR("\r\nStart\r\n")); + + last_i2c_status_boot = eeprom_read_byte((uint8_t*)1); set_sleep_mode(SLEEP_MODE_IDLE); // Enable Timer 1 Overflow Interrupt @@ -1085,6 +1117,15 @@ int main(void) { while(1) { switch(ireg) { + case 0xA4: // Watchdog enable + if (watchdog_enable == 1) { + wdt_enable(WDTO_2S); + watchdog_enable = 2; + } else if (watchdog_enable == 0) { + wdt_disable(); + watchdog_enable = 3; + } + break; case 0xff: // Magic reg that starts the bootloader if (bootloader == 0xa5) { cli(); @@ -1140,6 +1181,20 @@ 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; + if (last_man_update_count == 100) { + printf_P(PSTR("I2C State: 0x%x\r\n"), last_i2c_status); + eeprom_write_byte((uint8_t*)1, last_i2c_status); + eeprom_busy_wait(); + } + last_man_update_count = I2C_TIMEOUT_DISABLE; + } } sleep_mode();