mirror of
https://github.com/eclipse/upm.git
synced 2025-07-01 09:21:12 +03:00
HCSR04: Modifying implementation and adding examples
Signed-off-by: Abhishek Malik <abhishek.malik@intel.com>
This commit is contained in:
@ -1,5 +1,8 @@
|
||||
set (libname "hcsr04")
|
||||
set (libdescription "Ultrasonic Distance Measuring Sensor")
|
||||
set (module_src ${libname}.cxx)
|
||||
set (module_hpp ${libname}.hpp)
|
||||
upm_module_init(mraa)
|
||||
upm_mixed_module_init (NAME hcsr04
|
||||
DESCRIPTION "Micropik HCSR04"
|
||||
C_HDR hcsr04.h
|
||||
C_SRC hcsr04.c
|
||||
CPP_HDR hcsr04.hpp
|
||||
CPP_SRC hcsr04.cxx
|
||||
CPP_WRAPS_C
|
||||
REQUIRES mraa utilities-c)
|
||||
|
121
src/hcsr04/hcsr04.c
Normal file
121
src/hcsr04/hcsr04.c
Normal file
@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
||||
* Author: Rafael Neri <rafael.neri@gmail.com>
|
||||
* Author: Jun Kato <i@junkato.jp>
|
||||
* Contributions by: Abhishek Malik <abhishek.malik@intel.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
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#include "upm_utilities.h"
|
||||
#include "hcsr04.h"
|
||||
|
||||
hcsr04_context hcsr04_init(int triggerPin, int echoPin) {
|
||||
// make sure MRAA is initialized
|
||||
int mraa_rv;
|
||||
if ((mraa_rv = mraa_init()) != MRAA_SUCCESS)
|
||||
{
|
||||
printf("%s: mraa_init() failed (%d).\n", __FUNCTION__, mraa_rv);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hcsr04_context dev = (hcsr04_context) malloc(sizeof(struct _hcsr04_context));
|
||||
|
||||
if(!dev) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// initialize the GPIO pins
|
||||
dev->trigPin = mraa_gpio_init(triggerPin);
|
||||
if(!dev->trigPin) {
|
||||
printf("Unable to initialize the trigger pin\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dev->echoPin = mraa_gpio_init(echoPin);
|
||||
if(!dev->echoPin) {
|
||||
printf("Unable to initialize the echo pin\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Setting direction for the GPIO pins
|
||||
if(mraa_gpio_dir(dev->trigPin, MRAA_GPIO_OUT) != MRAA_SUCCESS) {
|
||||
printf("Unable to set the direction of the trigger Pin\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(mraa_gpio_dir(dev->echoPin, MRAA_GPIO_IN) != MRAA_SUCCESS) {
|
||||
printf("Unable to set the direction of the echo Pin\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Setting the trigger pin to logic level 0
|
||||
if(mraa_gpio_write(dev->trigPin, 0) != MRAA_SUCCESS)
|
||||
return NULL;
|
||||
|
||||
// initialize the interrupt counter
|
||||
dev->interruptCounter = 0;
|
||||
|
||||
return dev;
|
||||
}
|
||||
|
||||
void hcsr04_close(hcsr04_context dev) {
|
||||
mraa_gpio_close(dev->trigPin);
|
||||
mraa_gpio_close(dev->echoPin);
|
||||
free(dev);
|
||||
}
|
||||
|
||||
double hcsr04_get_distance(hcsr04_context dev, HCSR04_U unit) {
|
||||
// set value to start cycle right after trigger
|
||||
long cycleLength = 0, sampleTime = 0;
|
||||
struct timeval tv;
|
||||
int reading = 0;
|
||||
|
||||
dev->interruptCounter = 0;
|
||||
gettimeofday(&tv, NULL);
|
||||
|
||||
// The datasheet suggests using cycles of upto 60 ms
|
||||
// being a little libteral and using 70 ms, though a limit
|
||||
// should not be necessary at all
|
||||
cycleLength = (1000000 * tv.tv_sec) + tv.tv_usec + 70000;
|
||||
mraa_gpio_write(dev->trigPin, 1);
|
||||
upm_delay_us(10);
|
||||
mraa_gpio_write(dev->trigPin, 0);
|
||||
|
||||
while(sampleTime < cycleLength) {
|
||||
reading = mraa_gpio_read(dev->echoPin);
|
||||
if(reading == 1 && dev->interruptCounter == 0) {
|
||||
gettimeofday(&tv, NULL);
|
||||
dev->startTime = (1000000 * tv.tv_sec) + tv.tv_usec;
|
||||
dev->interruptCounter++;
|
||||
} else if(reading == 0 && dev->interruptCounter == 1) {
|
||||
gettimeofday(&tv, NULL);
|
||||
dev->endTime = (1000000 * tv.tv_sec) + tv.tv_usec;
|
||||
break;
|
||||
} else {
|
||||
sampleTime = (1000000 * tv.tv_sec) + tv.tv_usec;
|
||||
}
|
||||
}
|
||||
|
||||
if(unit == HCSR04_CM)
|
||||
return ((dev->endTime - dev->startTime)/2)/29.1;
|
||||
else
|
||||
return ((dev->endTime - dev->startTime)/2)/74.1;
|
||||
}
|
@ -26,94 +26,26 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <functional>
|
||||
|
||||
#ifdef JAVACALLBACK
|
||||
#undef JAVACALLBACK
|
||||
#endif
|
||||
|
||||
#include "hcsr04.hpp"
|
||||
|
||||
using namespace upm;
|
||||
|
||||
HCSR04::HCSR04 (int triggerPin, int echoPin) {
|
||||
m_name = "HCSR04";
|
||||
|
||||
m_triggerPinCtx = mraa_gpio_init (triggerPin);
|
||||
if (m_triggerPinCtx == NULL) {
|
||||
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||
": mraa_pwm_init() failed, invalid pin?");
|
||||
return;
|
||||
}
|
||||
|
||||
mraa_gpio_dir(m_triggerPinCtx, MRAA_GPIO_OUT);
|
||||
mraa_gpio_write (m_triggerPinCtx, 0);
|
||||
|
||||
m_echoPinCtx = mraa_gpio_init(echoPin);
|
||||
if (m_echoPinCtx == NULL) {
|
||||
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||
": mraa_gpio_init() failed, invalid pin?");
|
||||
return;
|
||||
}
|
||||
|
||||
mraa_gpio_dir(m_echoPinCtx, MRAA_GPIO_IN);
|
||||
mraa_gpio_isr(m_echoPinCtx, MRAA_GPIO_EDGE_BOTH, &ackEdgeDetected, (void*)this);
|
||||
}
|
||||
|
||||
HCSR04::~HCSR04 () {
|
||||
mraa_result_t error = MRAA_SUCCESS;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
usleep (5);
|
||||
}
|
||||
|
||||
return m_FallingTimeStamp - m_RisingTimeStamp;
|
||||
}
|
||||
|
||||
void
|
||||
HCSR04::ackEdgeDetected (void *ctx) {
|
||||
upm::HCSR04 *This = (upm::HCSR04 *)ctx;
|
||||
struct timeval timer;
|
||||
gettimeofday(&timer, NULL);
|
||||
|
||||
This->m_InterruptCounter++;
|
||||
if (!(This->m_InterruptCounter % 2)) {
|
||||
This->m_FallingTimeStamp = 1000000 * timer.tv_sec + timer.tv_usec;
|
||||
This->m_doWork = 1;
|
||||
} else {
|
||||
This->m_RisingTimeStamp = 1000000 * timer.tv_sec + timer.tv_usec;
|
||||
}
|
||||
}
|
||||
|
||||
double
|
||||
HCSR04::getDistance(int sys)
|
||||
HCSR04::HCSR04 (int triggerPin, int echoPin) :
|
||||
m_hcsr04(hcsr04_init(triggerPin, echoPin))
|
||||
{
|
||||
double _timing = timing();
|
||||
if (sys)
|
||||
{
|
||||
return (_timing/2) / 29.1;
|
||||
} else {
|
||||
return (_timing/2) / 74.1;
|
||||
}
|
||||
if(!m_hcsr04)
|
||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||
": hcsr04_init failed");
|
||||
}
|
||||
|
||||
HCSR04::~HCSR04 ()
|
||||
{
|
||||
hcsr04_close(m_hcsr04);
|
||||
}
|
||||
|
||||
double
|
||||
HCSR04::getDistance(HCSR04_U unit)
|
||||
{
|
||||
return hcsr04_get_distance(m_hcsr04, unit);
|
||||
}
|
||||
|
84
src/hcsr04/hcsr04.h
Normal file
84
src/hcsr04/hcsr04.h
Normal file
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
||||
* Author: Rafael Neri <rafael.neri@gmail.com>
|
||||
* Author: Jun Kato <i@junkato.jp>
|
||||
* Contributions by: Abhishek Malik <abhishek.malik@intel.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
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <mraa/gpio.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
HCSR04_CM = 0,
|
||||
HCSR04_INCH } HCSR04_U;
|
||||
|
||||
/**
|
||||
* @file hcsr04.h
|
||||
* @library hcsr04
|
||||
* @brief C API for the HCSR04 Ultrasonic Ranger sensor
|
||||
*
|
||||
* @include hcsr04.c
|
||||
*/
|
||||
|
||||
typedef struct _hcsr04_context {
|
||||
mraa_gpio_context trigPin;
|
||||
mraa_gpio_context echoPin;
|
||||
int interruptCounter;
|
||||
long startTime;
|
||||
long endTime;
|
||||
} *hcsr04_context;
|
||||
|
||||
/**
|
||||
* HCSR04 Initialization function
|
||||
*
|
||||
* @param triggerPin GPIO pin for trigger
|
||||
* @param echoPin GPIO pin used for output from sensor
|
||||
* @return hcsr04_context
|
||||
*/
|
||||
hcsr04_context hcsr04_init(int triggerPin, int echoPin);
|
||||
|
||||
/**
|
||||
* HCSR04 Close function
|
||||
*
|
||||
* @param dev hcsr04_context pointer
|
||||
*/
|
||||
void hcsr04_close(hcsr04_context dev);
|
||||
|
||||
/**
|
||||
* Function to get the distance from the HCSR04 sensor
|
||||
*
|
||||
* @param unit cm/inches
|
||||
* @return distance in specified unit
|
||||
*/
|
||||
double hcsr04_get_distance(hcsr04_context dev, HCSR04_U unit);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@ -24,14 +24,7 @@
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <mraa/aio.h>
|
||||
#include <mraa/gpio.h>
|
||||
#include <mraa/pwm.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#define CM 1
|
||||
#define INC 0
|
||||
#include "hcsr04.h"
|
||||
|
||||
namespace upm {
|
||||
/**
|
||||
@ -73,37 +66,13 @@ class HCSR04 {
|
||||
/**
|
||||
* Gets the distance from the sensor
|
||||
*
|
||||
* @param sys Selects units for measurement: 0 = inch, 1 = cm
|
||||
* @param unit Selects units for measurement
|
||||
*/
|
||||
double getDistance (int sys);
|
||||
|
||||
|
||||
uint8_t m_doWork; /**< Flag to control blocking function while waiting for a falling-edge interrupt */
|
||||
|
||||
/**
|
||||
* Returns the name of the sensor
|
||||
*/
|
||||
std::string name()
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
double getDistance (HCSR04_U unit);
|
||||
|
||||
private:
|
||||
/**
|
||||
* On each interrupt, this function detects if the interrupt
|
||||
* was falling-edge or rising-edge.
|
||||
*/
|
||||
static void ackEdgeDetected (void *ctx);
|
||||
|
||||
double timing();
|
||||
mraa_gpio_context m_triggerPinCtx;
|
||||
mraa_gpio_context m_echoPinCtx;
|
||||
|
||||
long m_RisingTimeStamp;
|
||||
long m_FallingTimeStamp;
|
||||
uint8_t m_InterruptCounter;
|
||||
|
||||
std::string m_name;
|
||||
};
|
||||
|
||||
hcsr04_context m_hcsr04;
|
||||
HCSR04(const HCSR04& src) { /* do not create copied constructor */ }
|
||||
HCSR04& operator=(const HCSR04&) {return *this;}
|
||||
};
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include "hcsr04.hpp"
|
||||
%}
|
||||
|
||||
%include "hcsr04.h"
|
||||
%include "hcsr04.hpp"
|
||||
|
||||
%pragma(java) jniclasscode=%{
|
||||
|
@ -5,4 +5,5 @@
|
||||
#include "hcsr04.hpp"
|
||||
%}
|
||||
|
||||
%include "hcsr04.h"
|
||||
%include "hcsr04.hpp"
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
%feature("autodoc", "3");
|
||||
|
||||
%include "hcsr04.h"
|
||||
%include "hcsr04.hpp"
|
||||
%{
|
||||
#include "hcsr04.hpp"
|
||||
|
Reference in New Issue
Block a user