]> defiant.homedns.org Git - ros_wild_thumper.git/blobdiff - avr/motor_ctrl/main.c
avr: test i2c fixes for status = TW_NO_INFO
[ros_wild_thumper.git] / avr / motor_ctrl / main.c
index 7781fb4af01654b33ea2a53df04a1bbaf63b7967..f7c9f0ae73822057176994f8082b4660970988bc 100644 (file)
@@ -5,6 +5,10 @@
 #include <avr/io.h>
 #include <avr/interrupt.h>
 #include <avr/sleep.h>
+#include <util/twi.h>
+#include <avr/eeprom.h>
+#include <avr/wdt.h>
+#include <avr/pgmspace.h>
 #include "uart.h"
 
 /*
 #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<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
-#define TWI_RESET TWCR &= ~((1 << TWSTO) | (1 << TWEN)); TWI_ACK
+#define TWI_ACK   TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) | (1<<TWIE)
 #define TWI_NAK   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
+#define TWI_RESET TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWSTO) | (1<<TWEN) | (1<<TWIE);
 #define ENABLE_PWM_MOTOR1  TCCR1A |=  (1 << COM1A1)
 #define ENABLE_PWM_MOTOR2  TCCR1A |=  (1 << COM1B1)
 #define ENABLE_PWM_MOTOR3  TCCR2  |=  (1 << COM21);
@@ -180,6 +185,8 @@ 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=0;
+static volatile uint8_t last_i2c_status = 0;
 
 ISR(TWI_vect)
 {
@@ -188,13 +195,14 @@ ISR(TWI_vect)
        static ufloat_t tmp_speed;
        static ufloat_t tmp_angle;
 
-       switch (TWSR & 0xF8)
+       last_i2c_status = TW_STATUS;
+       switch(TW_STATUS)
        {
-               case 0x60: // start write
+               case TW_SR_SLA_ACK: // start write
                        TWI_ACK;
                        ireg = 0;
                        break;
-               case 0x80: // write
+               case TW_SR_DATA_ACK: // write
                        switch(ireg) {
                                case 0x00: // register select
                                        ireg = TWDR;
@@ -346,6 +354,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
@@ -379,10 +388,10 @@ ISR(TWI_vect)
                                default:
                                        TWI_NAK;
                        }
-                       ireg++;
+                       if (ireg < 0xff) ireg++;
                        break;
-               case 0xA8: // start read
-               case 0xB8: // read
+               case TW_ST_SLA_ACK: // start read
+               case TW_ST_DATA_ACK: // read
                        switch(ireg) {
                                case 0x02: // Motor 1 PWM
                                        TWDR = OCR1A;
@@ -616,6 +625,11 @@ ISR(TWI_vect)
                        }
                        ireg++;
                        break;
+               case TW_SR_STOP:
+                       TWI_ACK;
+                       break;
+               case TW_NO_INFO:
+                       break;
                default:
                        TWI_RESET;
        }
@@ -931,7 +945,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);
@@ -953,7 +968,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 +991,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 +1014,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 +1059,7 @@ int main(void) {
 
        // I2C
        TWAR = 0x50;
-       TWI_RESET;
+       TWI_ACK;
 
        // Motor 1 & 2
        // Also used for PWM frequency TIMER1_FREQ (F_CPU/256)
@@ -1070,7 +1088,7 @@ int main(void) {
        TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << CS00);
        OCR0 = 0;
 
-       printf("\r\nStart\r\n");
+       printf_P(PSTR("\r\nStart\r\n"));
 
        set_sleep_mode(SLEEP_MODE_IDLE);
        // Enable Timer 1 Overflow Interrupt
@@ -1082,10 +1100,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 +1152,18 @@ 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);
+                               }
+                               last_man_update_count = I2C_TIMEOUT_DISABLE;
+                       }
                }
 
                sleep_mode();