hcsr04: fixes driver and addresses issue #207

Signed-off-by: Rafael Neri <rafael.neri@gmail.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Rafael Neri 2015-08-27 22:39:09 -03:00 committed by Mihai Tudor Panu
parent 0bc1930cf5
commit 5a62602a11
4 changed files with 56 additions and 32 deletions

View File

@ -18,7 +18,7 @@ add_executable (nrf24l01-transmitter-example nrf24l01-transmitter.cxx)
add_executable (nrf24l01-receiver-example nrf24l01-receiver.cxx)
add_executable (nrf24l01-broadcast-example nrf24l01-broadcast.cxx)
add_executable (es08a-example es08a.cxx)
add_executable (son-hcsr04-example hcsr04.cxx)
add_executable (hcsr04-example hcsr04.cxx)
add_executable (ssd1308-oled-example ssd1308-oled.cxx)
add_executable (ssd1327-oled-example ssd1327-oled.cxx)
add_executable (max44000-example max44000.cxx)
@ -269,7 +269,7 @@ target_link_libraries (nrf24l01-transmitter-example nrf24l01 ${CMAKE_THREAD_LIBS
target_link_libraries (nrf24l01-receiver-example nrf24l01 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (nrf24l01-broadcast-example nrf24l01 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (es08a-example servo ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (son-hcsr04-example hcsr04 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (hcsr04-example hcsr04 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (ssd1308-oled-example i2clcd ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (ssd1327-oled-example i2clcd ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (max44000-example max44000 ${CMAKE_THREAD_LIBS_INIT})

View File

@ -50,11 +50,17 @@ interrupt (void * args) {
int
main(int argc, char **argv)
{
sonar = new upm::HCSR04(5, 7, &interrupt);
sonar = new upm::HCSR04(5, 6, &interrupt);
signal(SIGINT, sig_handler);
printf ("width = %d\n", sonar->getDistance());
std::cout << "exiting application" << std::endl;
sleep(1);
for(;;){
std::cout << "get distance" << std::endl;
double distance = sonar->getDistance(CM);
std::cout << "distance " << distance << std::endl;
sleep(5);
}
delete sonar;

View File

@ -1,6 +1,7 @@
/*
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
* Copyright (c) 2014 Intel Corporation.
* Author: Rafael Neri <rafael.neri@gmail.com>
* Copyright (c) 2014-2015 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -42,13 +43,17 @@ HCSR04::HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *)) {
mraa_result_t error = MRAA_SUCCESS;
m_name = "HCSR04";
m_pwmTriggerCtx = mraa_pwm_init (triggerPin);
if (m_pwmTriggerCtx == NULL) {
std::cout << "PWM context is NULL" << std::endl;
mraa_init();
m_triggerPinCtx = mraa_gpio_init (triggerPin);
if (m_triggerPinCtx == NULL) {
fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", triggerPin);
exit (1);
}
mraa_init();
mraa_gpio_dir(m_triggerPinCtx, MRAA_GPIO_OUT);
mraa_gpio_write (m_triggerPinCtx, 0);
m_echoPinCtx = mraa_gpio_init(echoPin);
if (m_echoPinCtx == NULL) {
fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", echoPin);
@ -62,24 +67,27 @@ HCSR04::HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *)) {
HCSR04::~HCSR04 () {
mraa_result_t error = MRAA_SUCCESS;
mraa_pwm_close (m_pwmTriggerCtx);
error = mraa_gpio_close (m_triggerPinCtx);
if (error != MRAA_SUCCESS) {
mraa_result_print (error);
}
error = mraa_gpio_close (m_echoPinCtx);
if (error != MRAA_SUCCESS) {
mraa_result_print (error);
}
}
int
HCSR04::getDistance () {
mraa_pwm_enable (m_pwmTriggerCtx, 1);
mraa_pwm_period_us (m_pwmTriggerCtx, MAX_PERIOD);
mraa_pwm_pulsewidth_us (m_pwmTriggerCtx, TRIGGER_PULSE);
mraa_pwm_enable (m_pwmTriggerCtx, 0);
double
HCSR04::timing() {
mraa_gpio_write (m_triggerPinCtx, 1);
usleep(10);
mraa_gpio_write (m_triggerPinCtx, 0);
m_doWork = 0;
m_InterruptCounter = 0;
while (!m_doWork) {
sleep (1);
usleep (5);
}
return m_FallingTimeStamp - m_RisingTimeStamp;
@ -98,3 +106,15 @@ HCSR04::ackEdgeDetected () {
m_RisingTimeStamp = 1000000 * timer.tv_sec + timer.tv_usec;
}
}
double
HCSR04::getDistance(int sys)
{
double _timing = timing();
if (sys)
{
return (_timing/2) / 29.1;
} else {
return (_timing/2) / 74.1;
}
}

View File

@ -1,6 +1,7 @@
/*
/*
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
* Copyright (c) 2014 Intel Corporation.
* Author: Rafael Neri <rafael.neri@gmail.com>
* Copyright (c) 2014-2015 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -33,11 +34,8 @@
#include "../IsrCallback.h"
#endif
#define HIGH 1
#define LOW 0
#define MAX_PERIOD 7968
#define TRIGGER_PULSE 10
#define CM 1
#define INC 0
namespace upm {
/**
@ -62,9 +60,6 @@ namespace upm {
*/
class HCSR04 {
public:
#if defined(SWIGJAVA) || defined(JAVACALLBACK)
HCSR04 (uint8_t triggerPin, uint8_t echoPin, IsrCallback *cb);
#else
/**
* Instantiates an HCSR04 object
*
@ -73,6 +68,9 @@ class HCSR04 {
* @param fptr Function pointer to handle rising-edge and
* falling-edge interrupts
*/
#if defined(SWIGJAVA) || defined(JAVACALLBACK)
HCSR04 (uint8_t triggerPin, uint8_t echoPin, IsrCallback *cb);
#else
HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *));
#endif
/**
@ -83,7 +81,7 @@ class HCSR04 {
/**
* Gets the distance from the sensor
*/
int getDistance ();
double getDistance (int sys);
/**
* On each interrupt, this function detects if the interrupt
@ -106,10 +104,10 @@ class HCSR04 {
#if defined(SWIGJAVA) || defined(JAVACALLBACK)
HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *));
#endif
mraa_pwm_context m_pwmTriggerCtx;
mraa_gpio_context m_echoPinCtx;
double timing();
mraa_gpio_context m_triggerPinCtx;
mraa_gpio_context m_echoPinCtx;
uint8_t m_waitEcho;
long m_RisingTimeStamp;
long m_FallingTimeStamp;
uint8_t m_InterruptCounter;