1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Driver for the SRF02 Ultrasonic Ranger

This commit is contained in:
Zakaria Kasmi 2013-08-12 12:07:47 +02:00 committed by Oleg Hahm
parent 1acfee5991
commit 7084bcad22
4 changed files with 165 additions and 2 deletions

View File

@ -26,15 +26,19 @@ endif
ifneq (,$(findstring gps_ublox,$(USEMODULE)))
DIRS += gps_ublox
endif
ifneq (,$(findstring srf02,$(USEMODULE)))
DIRS += srf02
endif
all:
@for i in $(DIRS) ; do $(MAKE) -C $$i ; done ;
@for i in $(DIRS) ; do "$(MAKE)" -C $$i ; done ;
include $(RIOTBASE)/Makefile.base
# remove compilation products
clean::
@for i in $(DIRS) ; do $(MAKE) -C $$i clean ; done ;
@for i in $(DIRS) ; do "$(MAKE)" -C $$i clean ; done ;

View File

@ -0,0 +1,66 @@
/*
* sarf02-ultrasonic-sensor.h - Definitions for the SRF02 ultrasonic ranger based on 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 definitions for the SRF02 ultrasonic ranger using the LPC2387 chip.
* The connection between the SRF02 and the LPC2387 is based on the i2c interface.
*
* @author Freie Universität Berlin, Computer Systems & Telematics
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
* @version $Revision: 3854 $
*
* @note $Id: sarf02-ultrasonic-sensor.h 3854 2013-06-20 13:30:01Z zkasmi $
*
*/
#ifndef SRF02_ULTRASONIC_SENSOR_I2C_H_
#define SRF02_ULTRASONIC_SENSOR_I2C_H_
#include "i2c.h"
/* define the SRF02 registers*/
#define SRF02_DEFAULT_ADDR 112
#define SRF02_COMMAND_REG 0x0
#define SRF02_RANGE_HIGH_BYTE 0x2
#define SRF02_RANGE_LOW_BYTE 0x3
#define SRF02_REAL_RANGING_MODE_CM 0x51
/* Define the used I2C Interface */
//#define SRF02_I2C_INTERFACE I2C0 // P0.27 SDA0, P0.28 SCL0
//#define SRF02_I2C_INTERFACE I2C1_0 // P0.0 SDA1, P0.1 SCL1
//#define SRF02_I2C_INTERFACE I2C1_1 // P0.19 SDA1, P0.20 SCL1
#define SRF02_I2C_INTERFACE I2C2 // P0.10 SDA2, P0.11 SCL2
#define SRF02_EXIT_MSG 0
#define SRF02_RANGING_MSG 1
#define SRF02_SLEEP_MSG 2
#define SRF02_WEAKUP_MSG 3
/**
* @brief Initialize the SRF02 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 SRF02 is successfully initialized, otherwise false.
*/
bool srf02_init(uint8_t i2c_interface, uint32_t baud_rate);
/**
* @brief Start a continuous sampling of the distance measures.
* This function prints the distance values in centimeters over the rs232 interface.
*
*/
void srf02_start_ranging(void);
#endif /* SRF02_ULTRASONIC_SENSOR_I2C_H_ */

5
drivers/srf02/Makefile Normal file
View File

@ -0,0 +1,5 @@
INCLUDES = -I$(RIOTBASE)/core/include -I../include/ -I$(RIOTBASE)/sys/include
MODULE =srf02
include $(MAKEBASE)/Makefile.base

View File

@ -0,0 +1,88 @@
/*
* sarf02-ultrasonic-sensor.c - Driver for the SRF02 ultrasonic sensor 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 for the SRF02 ultrasonic ranger using the LPC2387 chip.
* The connection between the LPC2387 and the SRF08 is based on the i2c-interface.
*
* @author Freie Universität Berlin, Computer Systems & Telematics
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
* @version $Revision: 3854 $
*
* @note $Id: sarf02-ultrasonic-sensor.c 3854 2013-06-05 13:30:01Z zkasmi $
*
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <msg.h>
#include <vtimer.h>
#include <timex.h>
#include <thread.h>
#include "hwtimer.h"
#include "srf02-ultrasonic-sensor.h"
#include "i2c.h"
bool srf02_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 srf02_start_ranging(void) {
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] = SRF02_REAL_RANGING_MODE_CM;
while (1) {
status = i2c_write(SRF02_I2C_INTERFACE, SRF02_DEFAULT_ADDR,
SRF02_COMMAND_REG, tx_buff, reg_size);
if(!status){
puts("Write the ranging command to the i2c-interface is failed");
break;
}
hwtimer_wait(HWTIMER_TICKS(65000));
status = i2c_read(SRF02_I2C_INTERFACE, SRF02_DEFAULT_ADDR,
SRF02_RANGE_HIGH_BYTE, rx_buff, reg_size);
if(!status){
puts("Read the distance from the i2c-interface is failed");
break;
}
range_high_byte = rx_buff[0];
status = i2c_read(SRF02_I2C_INTERFACE, SRF02_DEFAULT_ADDR,
SRF02_RANGE_LOW_BYTE, rx_buff, reg_size);
range_low_byte = rx_buff[0];
distance = (range_high_byte << 8) | range_low_byte;
printf("distance = %lu cm\n", distance);
//printf("%u | %u\n", range_high_byte, range_low_byte);
hwtimer_wait(HWTIMER_TICKS(50000));
}
puts("The SRF02 range sampling is ended!!");
}