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-receiver-example nrf24l01-receiver.cxx)
add_executable (nrf24l01-broadcast-example nrf24l01-broadcast.cxx) add_executable (nrf24l01-broadcast-example nrf24l01-broadcast.cxx)
add_executable (es08a-example es08a.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 (ssd1308-oled-example ssd1308-oled.cxx)
add_executable (ssd1327-oled-example ssd1327-oled.cxx) add_executable (ssd1327-oled-example ssd1327-oled.cxx)
add_executable (max44000-example max44000.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-receiver-example nrf24l01 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (nrf24l01-broadcast-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 (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 (ssd1308-oled-example i2clcd ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (ssd1327-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}) target_link_libraries (max44000-example max44000 ${CMAKE_THREAD_LIBS_INIT})

View File

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

View File

@ -1,6 +1,7 @@
/* /*
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com> * 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 * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * 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; mraa_result_t error = MRAA_SUCCESS;
m_name = "HCSR04"; m_name = "HCSR04";
m_pwmTriggerCtx = mraa_pwm_init (triggerPin); mraa_init();
if (m_pwmTriggerCtx == NULL) {
std::cout << "PWM context is NULL" << std::endl; 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); exit (1);
} }
mraa_init(); mraa_gpio_dir(m_triggerPinCtx, MRAA_GPIO_OUT);
mraa_gpio_write (m_triggerPinCtx, 0);
m_echoPinCtx = mraa_gpio_init(echoPin); m_echoPinCtx = mraa_gpio_init(echoPin);
if (m_echoPinCtx == NULL) { if (m_echoPinCtx == NULL) {
fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", echoPin); 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 () { HCSR04::~HCSR04 () {
mraa_result_t error = MRAA_SUCCESS; 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); error = mraa_gpio_close (m_echoPinCtx);
if (error != MRAA_SUCCESS) { if (error != MRAA_SUCCESS) {
mraa_result_print (error); mraa_result_print (error);
} }
} }
int double
HCSR04::getDistance () { HCSR04::timing() {
mraa_pwm_enable (m_pwmTriggerCtx, 1); mraa_gpio_write (m_triggerPinCtx, 1);
mraa_pwm_period_us (m_pwmTriggerCtx, MAX_PERIOD); usleep(10);
mraa_pwm_pulsewidth_us (m_pwmTriggerCtx, TRIGGER_PULSE); mraa_gpio_write (m_triggerPinCtx, 0);
mraa_pwm_enable (m_pwmTriggerCtx, 0);
m_doWork = 0; m_doWork = 0;
m_InterruptCounter = 0; m_InterruptCounter = 0;
while (!m_doWork) { while (!m_doWork) {
sleep (1); usleep (5);
} }
return m_FallingTimeStamp - m_RisingTimeStamp; return m_FallingTimeStamp - m_RisingTimeStamp;
@ -98,3 +106,15 @@ HCSR04::ackEdgeDetected () {
m_RisingTimeStamp = 1000000 * timer.tv_sec + timer.tv_usec; 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> * 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 * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
@ -33,11 +34,8 @@
#include "../IsrCallback.h" #include "../IsrCallback.h"
#endif #endif
#define HIGH 1 #define CM 1
#define LOW 0 #define INC 0
#define MAX_PERIOD 7968
#define TRIGGER_PULSE 10
namespace upm { namespace upm {
/** /**
@ -62,9 +60,6 @@ namespace upm {
*/ */
class HCSR04 { class HCSR04 {
public: public:
#if defined(SWIGJAVA) || defined(JAVACALLBACK)
HCSR04 (uint8_t triggerPin, uint8_t echoPin, IsrCallback *cb);
#else
/** /**
* Instantiates an HCSR04 object * Instantiates an HCSR04 object
* *
@ -73,6 +68,9 @@ class HCSR04 {
* @param fptr Function pointer to handle rising-edge and * @param fptr Function pointer to handle rising-edge and
* falling-edge interrupts * 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 *)); HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *));
#endif #endif
/** /**
@ -83,7 +81,7 @@ class HCSR04 {
/** /**
* Gets the distance from the sensor * Gets the distance from the sensor
*/ */
int getDistance (); double getDistance (int sys);
/** /**
* On each interrupt, this function detects if the interrupt * On each interrupt, this function detects if the interrupt
@ -106,10 +104,10 @@ class HCSR04 {
#if defined(SWIGJAVA) || defined(JAVACALLBACK) #if defined(SWIGJAVA) || defined(JAVACALLBACK)
HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *)); HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *));
#endif #endif
mraa_pwm_context m_pwmTriggerCtx; double timing();
mraa_gpio_context m_echoPinCtx; mraa_gpio_context m_triggerPinCtx;
mraa_gpio_context m_echoPinCtx;
uint8_t m_waitEcho;
long m_RisingTimeStamp; long m_RisingTimeStamp;
long m_FallingTimeStamp; long m_FallingTimeStamp;
uint8_t m_InterruptCounter; uint8_t m_InterruptCounter;