Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hc_sr04: Improve accuracy by measuring microseconds #367

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions applications/plugins/hc_sr04/hc_sr04.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Ported and modified by @xMasterX

#include <furi.h>
#include <furi_hal.h>
#include <furi_hal_power.h>
#include <furi_hal_console.h>
#include <gui/gui.h>
Expand All @@ -26,7 +27,7 @@ typedef struct {
NotificationApp* notification;
bool have_5v;
bool measurement_made;
uint32_t echo; // ms
uint32_t echo; // us
float distance; // meters
} PluginState;

Expand Down Expand Up @@ -72,7 +73,7 @@ static void render_callback(Canvas* const canvas, void* ctx) {

FuriString* str_buf;
str_buf = furi_string_alloc();
furi_string_printf(str_buf, "Echo: %ld ms", plugin_state->echo);
furi_string_printf(str_buf, "Echo: %ld us", plugin_state->echo);

canvas_draw_str_aligned(
canvas, 8, 38, AlignLeft, AlignTop, furi_string_get_cstr(str_buf));
Expand Down Expand Up @@ -110,9 +111,11 @@ static void hc_sr04_state_init(PluginState* const plugin_state) {
}
}

float hc_sr04_ms_to_m(uint32_t ms) {
const float speed_sound_m_per_s = 343.0f;
const float time_s = ms / 1e3f;
float hc_sr04_us_to_m(uint32_t us) {
//speed of sound for 20°C, 50% relative humidity
//331.3 + 20 * 0.606 + 50 * 0.0124 = 0.034404
const float speed_sound_m_per_s = 344.04f;
const float time_s = us / 1e6f;
const float total_dist = time_s * speed_sound_m_per_s;
return total_dist / 2.0f;
}
Expand Down Expand Up @@ -147,27 +150,23 @@ static void hc_sr04_measure(PluginState* const plugin_state) {
furi_delay_ms(10);
furi_hal_gpio_write(&gpio_usart_tx, false);

// TODO change from furi_get_tick(), which returns ms,
// to DWT->CYCCNT, which is a more precise counter with
// us precision (see furi_hal_cortex_delay_us)

const uint32_t start = furi_get_tick();

while(furi_get_tick() - start < timeout_ms && furi_hal_gpio_read(&gpio_usart_rx))
;
while(furi_get_tick() - start < timeout_ms && !furi_hal_gpio_read(&gpio_usart_rx))
;

const uint32_t pulse_start = furi_get_tick();
const uint32_t pulse_start = DWT->CYCCNT;

while(furi_get_tick() - start < timeout_ms && furi_hal_gpio_read(&gpio_usart_rx))
;
const uint32_t pulse_end = DWT->CYCCNT;

const uint32_t pulse_end = furi_get_tick();
//FURI_CRITICAL_EXIT();

plugin_state->echo = pulse_end - pulse_start;
plugin_state->distance = hc_sr04_ms_to_m(pulse_end - pulse_start);
plugin_state->echo = (pulse_end - pulse_start) / furi_hal_cortex_instructions_per_microsecond();
plugin_state->distance = hc_sr04_us_to_m(plugin_state->echo);
plugin_state->measurement_made = true;

//furi_hal_light_set(LightRed, 0x00);
Expand Down Expand Up @@ -270,4 +269,4 @@ int32_t hc_sr04_app() {
delete_mutex(&state_mutex);

return 0;
}
}