mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Driver for the SRF08 Ultrasonic Ranger
This commit is contained in:
parent
7084bcad22
commit
2c3fce7212
@ -29,6 +29,9 @@ endif
|
||||
ifneq (,$(findstring srf02,$(USEMODULE)))
|
||||
DIRS += srf02
|
||||
endif
|
||||
ifneq (,$(findstring srf08,$(USEMODULE)))
|
||||
DIRS += srf08
|
||||
endif
|
||||
|
||||
all:
|
||||
@for i in $(DIRS) ; do "$(MAKE)" -C $$i ; done ;
|
||||
|
101
drivers/include/srf08-ultrasonic-sensor.h
Normal file
101
drivers/include/srf08-ultrasonic-sensor.h
Normal file
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* sarf08-ultrasonic-sensor.h - Definitions for the SRF02 ultrasonic ranger using the LPC2387 chip.
|
||||
* Copyright (C) 2013 Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
||||
*
|
||||
* This source code is licensed under the LGPLv2 license,
|
||||
* See the file LICENSE for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @internal
|
||||
* @brief Driver definitions for the SRF08 ultrasonic ranger using the LPC2387 chip. The communication
|
||||
* between the LPC23 chip and SRF08 is via the i2c interface
|
||||
*
|
||||
* @author Freie Universität Berlin, Computer Systems & Telematics
|
||||
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
||||
* @version $Revision: 3854 $
|
||||
*
|
||||
* @note $Id: sarf08-ultrasonic-sensor.h 3854 2013-06-21 12:30:01Z zkasmi $
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef SRF08_ULTRASONIC_SENSOR_I2C_H_
|
||||
#define SRF08_ULTRASONIC_SENSOR_I2C_H_
|
||||
|
||||
/* define the SRF02 registers*/
|
||||
#define SRF08_DEFAULT_ADDR 112
|
||||
#define SRF08_COMMAND_REG 0x0
|
||||
#define SRF08_RANGE_HIGH_BYTE 0x2
|
||||
#define SRF08_RANGE_LOW_BYTE 0x3
|
||||
#define SRF08_REAL_RANGING_MODE_CM 0x51
|
||||
#define SRF08_RANGE_REG 0x2
|
||||
#define SRF08_GAIN_REG 0x1
|
||||
|
||||
#define MAX_REGISTER_NUMBER 35
|
||||
|
||||
/* Define the used I2C Interface */
|
||||
//#define I2C_INTERFACE I2C0 // P0.27 SDA0, P0.28 SCL0
|
||||
//#define I2C_INTERFACE I2C1_0 // P0.0 SDA1, P0.1 SCL1
|
||||
//#define I2C_INTERFACE I2C1_1 // P0.19 SDA1, P0.20 SCL1
|
||||
#define SRF08_I2C_INTERFACE I2C2 // P0.10 SDA2, P0.11 SCL2
|
||||
|
||||
|
||||
/**
|
||||
* @brief Initialize the SRF08 ultrasonic sensor.
|
||||
*
|
||||
* @param[in] i2c_interface the i2c interface, several interfaces can be selected: i2c0, i2c1 and i2c2.
|
||||
* @param[in] baud_rate the baud rate.
|
||||
*
|
||||
*@return true if the SRF08 is successfully initialized, otherwise false.
|
||||
*
|
||||
*/
|
||||
|
||||
bool srf08_init(uint8_t i2c_interface, uint32_t baud_rate);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Start a continuous distance ranging with the SRF08 ultrasonic range-finder.
|
||||
* The ranging results are given in centimeters over the RS232-Interface.
|
||||
*
|
||||
*/
|
||||
void srf08_start_ranging(void);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set the maximum range of the SRF08.
|
||||
*
|
||||
* @param[in] max_range the adjusted maximal range is: max_range = (max_rangex43mm) + 43mm.
|
||||
*
|
||||
*/
|
||||
void srf08_set_range(uint8_t max_range);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set the maximum of the analog stages.
|
||||
*
|
||||
* @param[in] max_gain the maximal gain value.
|
||||
*
|
||||
*/
|
||||
void srf08_set_gain(uint8_t max_gain);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get the maximal range.
|
||||
*
|
||||
*
|
||||
* @return the maximal range value.
|
||||
*/
|
||||
uint8_t srf08_get_range(void);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get the maximal analog gain.
|
||||
*
|
||||
*
|
||||
* @return the maximal gain value.
|
||||
*/
|
||||
uint8_t srf08_get_gain(void);
|
||||
|
||||
#endif /* SRF08_ULTRASONIC_SENSOR_I2C_H_ */
|
5
drivers/srf08/Makefile
Normal file
5
drivers/srf08/Makefile
Normal file
@ -0,0 +1,5 @@
|
||||
INCLUDES = -I$(RIOTBASE)/core/include -I../include/ -I$(RIOTBASE)/sys/include
|
||||
MODULE =srf08
|
||||
include $(MAKEBASE)/Makefile.base
|
||||
|
||||
|
141
drivers/srf08/srf08-ultrasonic-sensor.c
Normal file
141
drivers/srf08/srf08-ultrasonic-sensor.c
Normal file
@ -0,0 +1,141 @@
|
||||
/*
|
||||
* srf08-ultrasonic-sensor.c - Driver for the SRF08 ultrasonic ranger and the LPC2387 chip via the i2c interface.
|
||||
* Copyright (C) 2013 Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
||||
*
|
||||
* This source code is licensed under the LGPLv2 license,
|
||||
* See the file LICENSE for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @internal
|
||||
* @brief Driver for the SRF08 ultrasonic ranger and the LPC2387 chip using the i2c interface.
|
||||
*
|
||||
* @author Freie Universität Berlin, Computer Systems & Telematics
|
||||
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
||||
* @version $Revision: 3854 $
|
||||
*
|
||||
* @note $Id: srf08-ultrasonic-sensor.c 3854 2013-06-21 15:30:01Z zkasmi $
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include "hwtimer.h"
|
||||
#include "srf08-ultrasonic-sensor.h"
|
||||
#include "i2c.h"
|
||||
|
||||
|
||||
bool srf08_init(uint8_t i2c_interface, uint32_t baud_rate) {
|
||||
if (i2c_initialize(i2c_interface, (uint32_t) I2CMASTER, 0, baud_rate, NULL)
|
||||
== false) /* initialize I2C */
|
||||
{
|
||||
puts("fatal error! happened in i2cInitialize() \n");
|
||||
while (1)
|
||||
; /* Fatal error */
|
||||
} else {
|
||||
i2c_enable_pull_up_resistor(i2c_interface);
|
||||
//i2c_disable_pull_up_resistor(i2c_interface);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void srf08_set_range(uint8_t max_range) {
|
||||
uint8_t tx_buff[1];
|
||||
tx_buff[0] = max_range;
|
||||
i2c_write(SRF08_I2C_INTERFACE, SRF08_DEFAULT_ADDR, SRF08_RANGE_REG, tx_buff,
|
||||
1);
|
||||
}
|
||||
|
||||
void srf08_set_gain(uint8_t gain) {
|
||||
uint8_t tx_buff[1];
|
||||
tx_buff[0] = gain;
|
||||
i2c_write(SRF08_I2C_INTERFACE, SRF08_DEFAULT_ADDR, SRF08_GAIN_REG, tx_buff,
|
||||
1);
|
||||
}
|
||||
|
||||
uint8_t srf08_get_range(void) {
|
||||
uint8_t rx_buff[1];
|
||||
i2c_read(SRF08_I2C_INTERFACE, SRF08_DEFAULT_ADDR, SRF08_RANGE_REG, rx_buff,
|
||||
1);
|
||||
|
||||
return rx_buff[0];
|
||||
}
|
||||
|
||||
uint8_t srf08_get_gain(void) {
|
||||
uint8_t rx_buff[1];
|
||||
i2c_read(SRF08_I2C_INTERFACE, SRF08_DEFAULT_ADDR, SRF08_GAIN_REG, rx_buff,
|
||||
1);
|
||||
|
||||
return rx_buff[0];
|
||||
}
|
||||
|
||||
void srf08_start_ranging(void) {
|
||||
puts("Ultrasonic SRF08 engine is started");
|
||||
bool status = false;
|
||||
uint8_t reg_size = 1;
|
||||
uint8_t range_high_byte = 0;
|
||||
uint8_t range_low_byte = 0;
|
||||
uint32_t distance = 0;
|
||||
uint8_t rx_buff[reg_size];
|
||||
uint8_t tx_buff[reg_size];
|
||||
tx_buff[0] = SRF08_REAL_RANGING_MODE_CM;
|
||||
uint8_t register_location;
|
||||
|
||||
//wait due to calibration
|
||||
hwtimer_wait(HWTIMER_TICKS(700000));
|
||||
printf("Actual range = %d\n", srf08_get_range());
|
||||
printf("Actual gain = %d\n", srf08_get_gain());
|
||||
|
||||
|
||||
while (1) {
|
||||
|
||||
status = i2c_write(SRF08_I2C_INTERFACE, SRF08_DEFAULT_ADDR,
|
||||
SRF08_COMMAND_REG, tx_buff, reg_size);
|
||||
if (!status) {
|
||||
puts("Write the ranging command to the i2c-interface is failed");
|
||||
break;
|
||||
}
|
||||
|
||||
hwtimer_wait(HWTIMER_TICKS(70000));
|
||||
|
||||
// Read all echo buffers
|
||||
for (register_location = 2; register_location < MAX_REGISTER_NUMBER;
|
||||
register_location += 2) {
|
||||
|
||||
//read the high echo byte
|
||||
status = i2c_read(SRF08_I2C_INTERFACE, SRF08_DEFAULT_ADDR,
|
||||
register_location, rx_buff, reg_size);
|
||||
if (!status) {
|
||||
puts(
|
||||
"Read the the high echo byte from the i2c-interface is failed");
|
||||
break;
|
||||
}
|
||||
range_high_byte = rx_buff[0];
|
||||
|
||||
//read the low echo byte
|
||||
status = i2c_read(SRF08_I2C_INTERFACE, SRF08_DEFAULT_ADDR,
|
||||
register_location + 1, rx_buff, reg_size);
|
||||
if (!status) {
|
||||
puts(
|
||||
"Read the the low echo byte from the i2c-interface is failed");
|
||||
break;
|
||||
}
|
||||
|
||||
range_low_byte = rx_buff[0];
|
||||
|
||||
if ((range_high_byte == 0) && (range_low_byte == 0)) {
|
||||
break;
|
||||
} else {
|
||||
distance = (range_high_byte << 8) | range_low_byte;
|
||||
printf("distance = %lu cm , echo%d\n", distance, register_location/2);
|
||||
}
|
||||
hwtimer_wait(HWTIMER_TICKS(500000));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user