-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a09c83
commit 0b3a1c6
Showing
3 changed files
with
190 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,58 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "freertos/FreeRTOS.h" | ||
#include "freertos/task.h" | ||
#include "driver/gpio.h" | ||
#include "esp_err.h" | ||
#include "esp_log.h" | ||
#include "esp_system.h" | ||
|
||
#include "ultrasonic.h" | ||
#include "driver/dac.h" | ||
|
||
#define MAX_DISTANCE_CM 450 // 5m max | ||
|
||
|
||
#define GPIO_TRIGGER 22 | ||
#define GPIO_ECHO 23 | ||
void ultrasonic(void *pvParamters) | ||
{ | ||
ultrasonic_sensor_t sensor = { | ||
.trigger_pin = GPIO_TRIGGER, | ||
.echo_pin = GPIO_ECHO | ||
}; | ||
|
||
ultrasonic_init(&sensor); | ||
|
||
while (true) { | ||
uint32_t distance; | ||
esp_err_t res = ultrasonic_measure_cm(&sensor, MAX_DISTANCE_CM, &distance); | ||
if (res != ESP_OK) { | ||
printf("Error: "); | ||
switch (res) { | ||
case ESP_ERR_ULTRASONIC_PING: | ||
printf("Cannot ping (device is in invalid state)\n"); | ||
break; | ||
case ESP_ERR_ULTRASONIC_PING_TIMEOUT: | ||
printf("Ping timeout (no device found)\n"); | ||
break; | ||
case ESP_ERR_ULTRASONIC_ECHO_TIMEOUT: | ||
printf("Echo timeout (i.e. distance too big)\n"); | ||
break; | ||
default: | ||
printf("%d\n", res); | ||
} | ||
} else { | ||
printf("Distance: %d cm, %.02f m\n", distance, distance / 100.0); | ||
} | ||
vTaskDelay(5000 / portTICK_PERIOD_MS); | ||
} | ||
} | ||
|
||
|
||
void app_main() | ||
{ | ||
|
||
xTaskCreate(ultrasonic, "ultrasonic", 2048, NULL, 5, NULL); | ||
} | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "freertos/FreeRTOS.h" | ||
#include "freertos/task.h" | ||
#include "esp_err.h" | ||
#include "esp_log.h" | ||
#include "esp_system.h" | ||
|
||
#include "ultrasonic.h" | ||
|
||
#define MAX_DISTANCE_CM 450 // 5m max | ||
|
||
|
||
#define GPIO_TRIGGER 22 | ||
#define GPIO_ECHO 23 | ||
void ultrasonic(void *pvParamters) | ||
{ | ||
ultrasonic_sensor_t sensor = { | ||
.trigger_pin = GPIO_TRIGGER, | ||
.echo_pin = GPIO_ECHO | ||
}; | ||
|
||
ultrasonic_init(&sensor); | ||
|
||
while (true) { | ||
uint32_t distance; | ||
esp_err_t res = ultrasonic_measure_cm(&sensor, MAX_DISTANCE_CM, &distance); | ||
if (res != ESP_OK) { | ||
printf("Error: "); | ||
switch (res) { | ||
case ESP_ERR_ULTRASONIC_PING: | ||
printf("Cannot ping (device is in invalid state)\n"); | ||
break; | ||
case ESP_ERR_ULTRASONIC_PING_TIMEOUT: | ||
printf("Ping timeout (no device found)\n"); | ||
break; | ||
case ESP_ERR_ULTRASONIC_ECHO_TIMEOUT: | ||
printf("Echo timeout (i.e. distance too big)\n"); | ||
break; | ||
default: | ||
printf("%d\n", res); | ||
} | ||
} else { | ||
printf("Distance: %ld cm, %.02f m\n", distance, distance / 100.0); | ||
} | ||
vTaskDelay(5000 / portTICK_PERIOD_MS); | ||
} | ||
} | ||
|
||
|
||
void app_main() | ||
{ | ||
|
||
xTaskCreate(ultrasonic, "ultrasonic", 2048, NULL, 5, NULL); | ||
} | ||
|
165 changes: 81 additions & 84 deletions
165
examples/offline_&_deprecated/idf_esp32/main/ultrasonic.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,81 @@ | ||
/** | ||
* @file ultrasonic.c | ||
* | ||
* ESP-IDF driver for ultrasonic range meters, e.g. HC-SR04, HY-SRF05 and so on | ||
* | ||
* Ported from esp-open-rtos | ||
* Copyright (C) 2016, 2018 Ruslan V. Uss <[email protected]> | ||
* BSD Licensed as described in the file LICENSE | ||
*/ | ||
#include "ultrasonic.h" | ||
#include <freertos/FreeRTOS.h> | ||
#include <freertos/task.h> | ||
#include <sys/time.h> | ||
|
||
#define TRIGGER_LOW_DELAY 4 | ||
#define TRIGGER_HIGH_DELAY 10 | ||
#define PING_TIMEOUT 6000 | ||
#define ROUNDTRIP 58 | ||
|
||
static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; | ||
|
||
static inline uint32_t get_time_us() | ||
{ | ||
struct timeval tv; | ||
gettimeofday(&tv, NULL); | ||
return tv.tv_usec; | ||
} | ||
|
||
#define timeout_expired(start, len) ((uint32_t)(get_time_us() - (start)) >= (len)) | ||
|
||
#define RETURN_CRTCAL(MUX, RES) do { portEXIT_CRITICAL(&MUX); return RES; } while(0) | ||
|
||
void ultrasonic_init(const ultrasonic_sensor_t *dev) | ||
{ | ||
gpio_pad_select_gpio( dev->trigger_pin ); | ||
gpio_pad_select_gpio( dev->echo_pin ); | ||
gpio_set_direction(dev->trigger_pin, GPIO_MODE_OUTPUT); | ||
gpio_set_direction(dev->echo_pin, GPIO_MODE_INPUT); | ||
|
||
gpio_set_level(dev->trigger_pin, 0); | ||
} | ||
|
||
esp_err_t ultrasonic_measure_cm(const ultrasonic_sensor_t *dev, uint32_t max_distance, uint32_t *distance) | ||
{ | ||
if (!distance) | ||
return ESP_ERR_INVALID_ARG; | ||
|
||
portENTER_CRITICAL(&mux); | ||
|
||
// Ping: Low for 2..4 us, then high 10 us | ||
gpio_set_level(dev->trigger_pin, 0); | ||
ets_delay_us(TRIGGER_LOW_DELAY); | ||
gpio_set_level(dev->trigger_pin, 1); | ||
ets_delay_us(TRIGGER_HIGH_DELAY); | ||
gpio_set_level(dev->trigger_pin, 0); | ||
|
||
// Previous ping isn't ended | ||
if (gpio_get_level(dev->echo_pin)) | ||
RETURN_CRTCAL(mux, ESP_ERR_ULTRASONIC_PING); | ||
|
||
// Wait for echo | ||
uint32_t start = get_time_us(); | ||
while (!gpio_get_level(dev->echo_pin)) | ||
{ | ||
if (timeout_expired(start, PING_TIMEOUT)) | ||
RETURN_CRTCAL(mux, ESP_ERR_ULTRASONIC_PING_TIMEOUT); | ||
} | ||
|
||
// got echo, measuring | ||
uint32_t echo_start = get_time_us(); | ||
uint32_t time = echo_start; | ||
uint32_t meas_timeout = echo_start + max_distance * ROUNDTRIP; | ||
while (gpio_get_level(dev->echo_pin)) | ||
{ | ||
time = get_time_us(); | ||
if (timeout_expired(echo_start, meas_timeout)) | ||
RETURN_CRTCAL(mux, ESP_ERR_ULTRASONIC_ECHO_TIMEOUT); | ||
} | ||
portEXIT_CRITICAL(&mux); | ||
|
||
*distance = (time - echo_start) / ROUNDTRIP; | ||
|
||
return ESP_OK; | ||
} | ||
/** | ||
* @file ultrasonic.c | ||
* | ||
* ESP-IDF driver for ultrasonic range meters, e.g. HC-SR04, HY-SRF05 and so on | ||
* | ||
* Ported from esp-open-rtos | ||
* Copyright (C) 2016, 2018 Ruslan V. Uss <[email protected]> | ||
* BSD Licensed as described in the file LICENSE | ||
*/ | ||
#include "ultrasonic.h" | ||
#include <freertos/FreeRTOS.h> | ||
#include <freertos/task.h> | ||
#include <sys/time.h> | ||
|
||
#define TRIGGER_LOW_DELAY 4 | ||
#define TRIGGER_HIGH_DELAY 10 | ||
#define PING_TIMEOUT 6000 | ||
#define ROUNDTRIP 58 | ||
|
||
static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; | ||
|
||
static inline uint32_t get_time_us() | ||
{ | ||
struct timeval tv; | ||
gettimeofday(&tv, NULL); | ||
return tv.tv_usec; | ||
} | ||
|
||
#define timeout_expired(start, len) ((uint32_t)(get_time_us() - (start)) >= (len)) | ||
|
||
#define RETURN_CRTCAL(MUX, RES) do { portEXIT_CRITICAL(&MUX); return RES; } while(0) | ||
|
||
void ultrasonic_init(const ultrasonic_sensor_t *dev) | ||
{ | ||
gpio_set_direction(dev->trigger_pin, GPIO_MODE_OUTPUT); | ||
gpio_set_direction(dev->echo_pin, GPIO_MODE_INPUT); | ||
gpio_set_level(dev->trigger_pin, 0); | ||
} | ||
|
||
esp_err_t ultrasonic_measure_cm(const ultrasonic_sensor_t *dev, uint32_t max_distance, uint32_t *distance) | ||
{ | ||
if (!distance) | ||
return ESP_ERR_INVALID_ARG; | ||
|
||
portENTER_CRITICAL(&mux); | ||
|
||
// Ping: Low for 2..4 us, then high 10 us | ||
gpio_set_level(dev->trigger_pin, 0); | ||
esp_rom_delay_us(TRIGGER_LOW_DELAY); | ||
gpio_set_level(dev->trigger_pin, 1); | ||
esp_rom_delay_us(TRIGGER_HIGH_DELAY); | ||
gpio_set_level(dev->trigger_pin, 0); | ||
|
||
// Previous ping isn't ended | ||
if (gpio_get_level(dev->echo_pin)) | ||
RETURN_CRTCAL(mux, ESP_ERR_ULTRASONIC_PING); | ||
|
||
// Wait for echo | ||
uint32_t start = get_time_us(); | ||
while (!gpio_get_level(dev->echo_pin)) | ||
{ | ||
if (timeout_expired(start, PING_TIMEOUT)) | ||
RETURN_CRTCAL(mux, ESP_ERR_ULTRASONIC_PING_TIMEOUT); | ||
} | ||
|
||
// got echo, measuring | ||
uint32_t echo_start = get_time_us(); | ||
uint32_t time = echo_start; | ||
uint32_t meas_timeout = echo_start + max_distance * ROUNDTRIP; | ||
while (gpio_get_level(dev->echo_pin)) | ||
{ | ||
time = get_time_us(); | ||
if (timeout_expired(echo_start, meas_timeout)) | ||
RETURN_CRTCAL(mux, ESP_ERR_ULTRASONIC_ECHO_TIMEOUT); | ||
} | ||
portEXIT_CRITICAL(&mux); | ||
|
||
*distance = (time - echo_start) / ROUNDTRIP; | ||
|
||
return ESP_OK; | ||
} |
102 changes: 51 additions & 51 deletions
102
examples/offline_&_deprecated/idf_esp32/main/ultrasonic.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,51 @@ | ||
/** | ||
* @file ultrasonic.h | ||
* | ||
* ESP-IDF driver for ultrasonic range meters, e.g. HC-SR04, HY-SRF05 and so on | ||
* | ||
* Ported from esp-open-rtos | ||
* Copyright (C) 2016, 2018 Ruslan V. Uss <[email protected]> | ||
* BSD Licensed as described in the file LICENSE | ||
*/ | ||
#ifndef __ULTRASONIC_H__ | ||
#define __ULTRASONIC_H__ | ||
|
||
#include <driver/gpio.h> | ||
#include <driver/dac.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#define ESP_ERR_ULTRASONIC_PING 0x200 | ||
#define ESP_ERR_ULTRASONIC_PING_TIMEOUT 0x201 | ||
#define ESP_ERR_ULTRASONIC_ECHO_TIMEOUT 0x202 | ||
|
||
/** | ||
* Device descriptor | ||
*/ | ||
typedef struct | ||
{ | ||
gpio_num_t trigger_pin; | ||
gpio_num_t echo_pin; | ||
} ultrasonic_sensor_t; | ||
|
||
/** | ||
* Init ranging module | ||
* \param dev Pointer to the device descriptor | ||
*/ | ||
void ultrasonic_init(const ultrasonic_sensor_t *dev); | ||
|
||
/** | ||
* Measure distance | ||
* \param dev Pointer to the device descriptor | ||
* \param max_distance Maximal distance to measure, centimeters | ||
* \return Distance in centimeters or ULTRASONIC_ERROR_xxx if error occured | ||
*/ | ||
esp_err_t ultrasonic_measure_cm(const ultrasonic_sensor_t *dev, uint32_t max_distance, uint32_t *distance); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* __ULTRASONIC_H__ */ | ||
/** | ||
* @file ultrasonic.h | ||
* | ||
* ESP-IDF driver for ultrasonic range meters, e.g. HC-SR04, HY-SRF05 and so on | ||
* | ||
* Ported from esp-open-rtos | ||
* Copyright (C) 2016, 2018 Ruslan V. Uss <[email protected]> | ||
* BSD Licensed as described in the file LICENSE | ||
*/ | ||
#ifndef __ULTRASONIC_H__ | ||
#define __ULTRASONIC_H__ | ||
|
||
#include <driver/gpio.h> | ||
#include "driver/dac_oneshot.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#define ESP_ERR_ULTRASONIC_PING 0x200 | ||
#define ESP_ERR_ULTRASONIC_PING_TIMEOUT 0x201 | ||
#define ESP_ERR_ULTRASONIC_ECHO_TIMEOUT 0x202 | ||
|
||
/** | ||
* Device descriptor | ||
*/ | ||
typedef struct | ||
{ | ||
gpio_num_t trigger_pin; | ||
gpio_num_t echo_pin; | ||
} ultrasonic_sensor_t; | ||
|
||
/** | ||
* Init ranging module | ||
* \param dev Pointer to the device descriptor | ||
*/ | ||
void ultrasonic_init(const ultrasonic_sensor_t *dev); | ||
|
||
/** | ||
* Measure distance | ||
* \param dev Pointer to the device descriptor | ||
* \param max_distance Maximal distance to measure, centimeters | ||
* \return Distance in centimeters or ULTRASONIC_ERROR_xxx if error occured | ||
*/ | ||
esp_err_t ultrasonic_measure_cm(const ultrasonic_sensor_t *dev, uint32_t max_distance, uint32_t *distance); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* __ULTRASONIC_H__ */ |