Merge pull request #235 from UncleRus/extras/ultrasonic
Driver for ultrasonic range meters + example
This commit is contained in:
		
						commit
						0a8c61ffb7
					
				
					 5 changed files with 179 additions and 0 deletions
				
			
		
							
								
								
									
										9
									
								
								extras/ultrasonic/component.mk
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								extras/ultrasonic/component.mk
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,9 @@
 | 
			
		|||
# Component makefile for extras/ultrasonic
 | 
			
		||||
 | 
			
		||||
# expected anyone using this driver includes it as 'ultrasonic/ultrasonic.h'
 | 
			
		||||
INC_DIRS += $(ultrasonic_ROOT)..
 | 
			
		||||
 | 
			
		||||
# args for passing into compile rule generation
 | 
			
		||||
ultrasonic_SRC_DIR = $(ultrasonic_ROOT)
 | 
			
		||||
 | 
			
		||||
$(eval $(call component_compile_rules,ultrasonic))
 | 
			
		||||
							
								
								
									
										58
									
								
								extras/ultrasonic/ultrasonic.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								extras/ultrasonic/ultrasonic.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,58 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Driver for ultrasonic range meters, e.g. HC-SR04, HY-SRF05 and so on
 | 
			
		||||
 *
 | 
			
		||||
 * Part of esp-open-rtos
 | 
			
		||||
 * Copyright (C) 2016 Ruslan V. Uss <unclerus@gmail.com>
 | 
			
		||||
 * BSD Licensed as described in the file LICENSE
 | 
			
		||||
 */
 | 
			
		||||
#include "ultrasonic.h"
 | 
			
		||||
#include <esp/gpio.h>
 | 
			
		||||
#include <espressif/esp_common.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
#define TRIGGER_LOW_DELAY 4
 | 
			
		||||
#define TRIGGER_HIGH_DELAY 10
 | 
			
		||||
#define PING_TIMEOUT 6000
 | 
			
		||||
#define ROUNDTRIP 58
 | 
			
		||||
 | 
			
		||||
void ultrasoinc_init(const ultrasonic_sensor_t *dev)
 | 
			
		||||
{
 | 
			
		||||
    gpio_enable(dev->trigger_pin, GPIO_OUTPUT);
 | 
			
		||||
    gpio_enable(dev->echo_pin, GPIO_INPUT);
 | 
			
		||||
    gpio_write(dev->trigger_pin, false);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int32_t ultrasoinc_measure_cm(const ultrasonic_sensor_t *dev, uint32_t max_distance)
 | 
			
		||||
{
 | 
			
		||||
    // Ping: Low for 2..4 us, then high 10 us
 | 
			
		||||
    gpio_write(dev->trigger_pin, false);
 | 
			
		||||
    sdk_os_delay_us(TRIGGER_LOW_DELAY);
 | 
			
		||||
    gpio_write(dev->trigger_pin, true);
 | 
			
		||||
    sdk_os_delay_us(TRIGGER_HIGH_DELAY);
 | 
			
		||||
    gpio_write(dev->trigger_pin, false);
 | 
			
		||||
 | 
			
		||||
    // Previous ping isn't ended
 | 
			
		||||
    if (gpio_read(dev->echo_pin))
 | 
			
		||||
        return ULTRASONIC_ERROR_PING;
 | 
			
		||||
 | 
			
		||||
    // Wait for echo
 | 
			
		||||
    uint32_t timeout = sdk_system_get_time() + PING_TIMEOUT;
 | 
			
		||||
    while (!gpio_read(dev->echo_pin))
 | 
			
		||||
    {
 | 
			
		||||
        if (sdk_system_get_time() >= timeout)
 | 
			
		||||
            return ULTRASONIC_ERROR_PING_TIMEOUT;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // got echo, measuring
 | 
			
		||||
    uint32_t echo_start = sdk_system_get_time();
 | 
			
		||||
    uint32_t time = echo_start;
 | 
			
		||||
    timeout = echo_start + max_distance * ROUNDTRIP;
 | 
			
		||||
    while (gpio_read(dev->echo_pin))
 | 
			
		||||
    {
 | 
			
		||||
        time = sdk_system_get_time();
 | 
			
		||||
        if (time >= timeout)
 | 
			
		||||
            return ULTRASONIC_ERROR_ECHO_TIMEOUT;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return (time - echo_start) / ROUNDTRIP;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										48
									
								
								extras/ultrasonic/ultrasonic.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								extras/ultrasonic/ultrasonic.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,48 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Driver for ultrasonic range meters, e.g. HC-SR04, HY-SRF05 and so on
 | 
			
		||||
 *
 | 
			
		||||
 * Part of esp-open-rtos
 | 
			
		||||
 * Copyright (C) 2016 Ruslan V. Uss <unclerus@gmail.com>
 | 
			
		||||
 * BSD Licensed as described in the file LICENSE
 | 
			
		||||
 */
 | 
			
		||||
#ifndef EXTRAS_ULTRASONIC_H_
 | 
			
		||||
#define EXTRAS_ULTRASONIC_H_
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
extern "C" {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define ULTRASONIC_ERROR_PING         (-1)
 | 
			
		||||
#define ULTRASONIC_ERROR_PING_TIMEOUT (-2)
 | 
			
		||||
#define ULTRASONIC_ERROR_ECHO_TIMEOUT (-3)
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Device descriptor
 | 
			
		||||
 */
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
    uint8_t trigger_pin;
 | 
			
		||||
    uint8_t echo_pin;
 | 
			
		||||
} ultrasonic_sensor_t;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Init ranging module
 | 
			
		||||
 * \param dev Pointer to the device descriptor
 | 
			
		||||
 */
 | 
			
		||||
void ultrasoinc_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
 | 
			
		||||
 */
 | 
			
		||||
int32_t ultrasoinc_measure_cm(const ultrasonic_sensor_t *dev, uint32_t max_distance);
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif /* EXTRAS_ULTRASONIC_H_ */
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue