Report ECHO timeouts, add minimum sonar distance

This commit is contained in:
Rihards Skuja 2018-01-22 21:23:27 +02:00
parent cdaaf8719b
commit dc5b0e9995
No known key found for this signature in database
GPG Key ID: 53FA13A3F7F8571B
3 changed files with 7 additions and 7 deletions

View File

@ -20,7 +20,7 @@ enum return_codes {
typedef struct { typedef struct {
double temp; double temp;
uint16_t distance; int32_t distance;
} data_packet_t; } data_packet_t;
// Base station radio MAC address // Base station radio MAC address

View File

@ -17,15 +17,15 @@
#define SPEED_OF_SOUND 343 // 20°C dry air (m/s) #define SPEED_OF_SOUND 343 // 20°C dry air (m/s)
#define MAX_SONAR_RANGE 8 // Trigger + echo (m) #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 SONAR_DELAY 60 // Timeout for return signal
#define TIMER_MAX 65535 // For 16-bit timer #define TIMER_MAX 65535 // For 16-bit timer
#define CYCLES_PER_US (F_CPU / 1000000) #define CYCLES_PER_US (F_CPU / 1000000)
#define SONAR_TIMEOUT (F_CPU * MAX_SONAR_RANGE) / SPEED_OF_SOUND #define SONAR_TIMEOUT (F_CPU * MAX_SONAR_RANGE) / SPEED_OF_SOUND
#define SONAR_ERROR 0 #define ECHO_TIMEOUT -1
void init_sonar(void); void init_sonar(void);
uint16_t read_sonar(void); int32_t read_sonar(void);
#endif /* SONAR_H */ #endif /* SONAR_H */

View File

@ -12,7 +12,7 @@ void init_sonar(void)
CLR(ECHO_DDR, ECHO_BIT); CLR(ECHO_DDR, ECHO_BIT);
} }
uint16_t read_sonar(void) int32_t read_sonar(void)
{ {
// Hold high for 10us while TRIGGER sends 40KHz burst // Hold high for 10us while TRIGGER sends 40KHz burst
SET(TRIG_PORT, TRIG_BIT); SET(TRIG_PORT, TRIG_BIT);
@ -23,7 +23,7 @@ uint16_t read_sonar(void)
// Wait for ECHO to receive TRIGGER burst // Wait for ECHO to receive TRIGGER burst
while (!(CHK(ECHO_PIN, ECHO_BIT))) { while (!(CHK(ECHO_PIN, ECHO_BIT))) {
if (--trig_counter == 0) { 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 // Count cycles until ECHO goes low
while (CHK(ECHO_PIN, ECHO_BIT)) { while (CHK(ECHO_PIN, ECHO_BIT)) {
if (no_of_ticks + TCNT1 > SONAR_TIMEOUT) { if (no_of_ticks + TCNT1 > SONAR_TIMEOUT) {
return SONAR_ERROR; return ECHO_TIMEOUT;
} }
} }
TCCR1B = 0x00; TCCR1B = 0x00;