diff --git a/common/include/common.h b/common/include/common.h index 7e2d79a..3547f35 100644 --- a/common/include/common.h +++ b/common/include/common.h @@ -20,7 +20,7 @@ enum return_codes { typedef struct { double temp; - uint16_t distance; + int32_t distance; } data_packet_t; // Base station radio MAC address diff --git a/robot/include/sonar.h b/robot/include/sonar.h index 8d22b48..b8ad087 100644 --- a/robot/include/sonar.h +++ b/robot/include/sonar.h @@ -17,15 +17,15 @@ #define SPEED_OF_SOUND 343 // 20°C dry air (m/s) #define MAX_SONAR_RANGE 8 // Trigger + echo (m) +#define MIN_SONAR_RANGE 3 // Minimum distance that is still reliable #define SONAR_DELAY 60 // Timeout for return signal #define TIMER_MAX 65535 // For 16-bit timer #define CYCLES_PER_US (F_CPU / 1000000) #define SONAR_TIMEOUT (F_CPU * MAX_SONAR_RANGE) / SPEED_OF_SOUND -#define SONAR_ERROR 0 - +#define ECHO_TIMEOUT -1 void init_sonar(void); -uint16_t read_sonar(void); +int32_t read_sonar(void); #endif /* SONAR_H */ diff --git a/robot/src/sonar.c b/robot/src/sonar.c index aeaba41..8db9d31 100644 --- a/robot/src/sonar.c +++ b/robot/src/sonar.c @@ -12,7 +12,7 @@ void init_sonar(void) CLR(ECHO_DDR, ECHO_BIT); } -uint16_t read_sonar(void) +int32_t read_sonar(void) { // Hold high for 10us while TRIGGER sends 40KHz burst SET(TRIG_PORT, TRIG_BIT); @@ -23,7 +23,7 @@ uint16_t read_sonar(void) // Wait for ECHO to receive TRIGGER burst while (!(CHK(ECHO_PIN, ECHO_BIT))) { if (--trig_counter == 0) { - return SONAR_ERROR; + return ECHO_TIMEOUT; } } @@ -36,7 +36,7 @@ uint16_t read_sonar(void) // Count cycles until ECHO goes low while (CHK(ECHO_PIN, ECHO_BIT)) { if (no_of_ticks + TCNT1 > SONAR_TIMEOUT) { - return SONAR_ERROR; + return ECHO_TIMEOUT; } } TCCR1B = 0x00;