mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
groveultrasonic: Initial implementation
This module was developed and tested on a Grove Ultrasonic Ranger module. http://www.seeedstudio.com/wiki/Grove_-_Ultrasonic_Ranger Signed-off-by: Jun Kato <i@junkato.jp> Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
parent
dc97adf578
commit
2eb6ebd3bd
@ -154,6 +154,7 @@ add_executable (adxrs610-example adxrs610.cxx)
|
|||||||
add_executable (bma220-example bma220.cxx)
|
add_executable (bma220-example bma220.cxx)
|
||||||
add_executable (dfrph-example dfrph.cxx)
|
add_executable (dfrph-example dfrph.cxx)
|
||||||
add_executable (mcp9808-example mcp9808.cxx)
|
add_executable (mcp9808-example mcp9808.cxx)
|
||||||
|
add_executable (groveultrasonic-example groveultrasonic.cxx)
|
||||||
|
|
||||||
include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
|
include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
|
||||||
include_directories (${PROJECT_SOURCE_DIR}/src/grove)
|
include_directories (${PROJECT_SOURCE_DIR}/src/grove)
|
||||||
@ -272,6 +273,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/adxrs610)
|
|||||||
include_directories (${PROJECT_SOURCE_DIR}/src/bma220)
|
include_directories (${PROJECT_SOURCE_DIR}/src/bma220)
|
||||||
include_directories (${PROJECT_SOURCE_DIR}/src/dfrph)
|
include_directories (${PROJECT_SOURCE_DIR}/src/dfrph)
|
||||||
include_directories (${PROJECT_SOURCE_DIR}/src/mcp9808)
|
include_directories (${PROJECT_SOURCE_DIR}/src/mcp9808)
|
||||||
|
include_directories (${PROJECT_SOURCE_DIR}/src/groveultrasonic)
|
||||||
|
|
||||||
target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
|
target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
|
||||||
target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
|
target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
|
||||||
@ -427,3 +429,4 @@ target_link_libraries (adxrs610-example adxrs610 ${CMAKE_THREAD_LIBS_INIT})
|
|||||||
target_link_libraries (bma220-example bma220 ${CMAKE_THREAD_LIBS_INIT})
|
target_link_libraries (bma220-example bma220 ${CMAKE_THREAD_LIBS_INIT})
|
||||||
target_link_libraries (dfrph-example dfrph ${CMAKE_THREAD_LIBS_INIT})
|
target_link_libraries (dfrph-example dfrph ${CMAKE_THREAD_LIBS_INIT})
|
||||||
target_link_libraries (mcp9808-example mcp9808 ${CMAKE_THREAD_LIBS_INIT})
|
target_link_libraries (mcp9808-example mcp9808 ${CMAKE_THREAD_LIBS_INIT})
|
||||||
|
target_link_libraries (groveultrasonic-example groveultrasonic ${CMAKE_THREAD_LIBS_INIT})
|
||||||
|
58
examples/c++/groveultrasonic.cxx
Normal file
58
examples/c++/groveultrasonic.cxx
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* Author: Jun Kato <i@junkato.jp>
|
||||||
|
* Copyright (c) 2015 Jun Kato.
|
||||||
|
*
|
||||||
|
* Thanks to Seeed Studio for a working arduino sketch
|
||||||
|
*
|
||||||
|
* 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 <unistd.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include "groveultrasonic.h"
|
||||||
|
#include <signal.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
upm::GroveUltraSonic *sonar = NULL;
|
||||||
|
|
||||||
|
void
|
||||||
|
sig_handler(int signo)
|
||||||
|
{
|
||||||
|
printf("got signal\n");
|
||||||
|
if (signo == SIGINT) {
|
||||||
|
sonar->m_doWork = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
signal(SIGINT, sig_handler);
|
||||||
|
//! [Interesting]
|
||||||
|
// upm::GroveUltraSonic *sonar = NULL;
|
||||||
|
sonar = new upm::GroveUltraSonic(2);
|
||||||
|
printf("width = %d\n", sonar->getDistance());
|
||||||
|
delete sonar;
|
||||||
|
//! [Interesting]
|
||||||
|
printf("exiting application\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
45
examples/javascript/groveultrasonic.js
Normal file
45
examples/javascript/groveultrasonic.js
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* Author: Jun Kato <i@junkato.jp>
|
||||||
|
* Copyright (c) 2015 Jun Kato.
|
||||||
|
*
|
||||||
|
* Thanks to Seeed Studio for a working arduino sketch
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var ultrasonic = require("jsupm_groveultrasonic");
|
||||||
|
var sensor = new ultrasonic.GroveUltraSonic(2);
|
||||||
|
|
||||||
|
var myInterval = setInterval(function()
|
||||||
|
{
|
||||||
|
var travelTime = sensor.getDistance();
|
||||||
|
if (travelTime > 0) {
|
||||||
|
var distance = (travelTime / 29 / 2).toFixed(3);
|
||||||
|
console.log("distance: " + distance + " [cm]");
|
||||||
|
}
|
||||||
|
}, 200);
|
||||||
|
|
||||||
|
// When exiting: clear interval and print message
|
||||||
|
process.on('SIGINT', function()
|
||||||
|
{
|
||||||
|
clearInterval(myInterval);
|
||||||
|
console.log("Exiting...");
|
||||||
|
process.exit(0);
|
||||||
|
});
|
5
src/groveultrasonic/CMakeLists.txt
Normal file
5
src/groveultrasonic/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
set (libname "groveultrasonic")
|
||||||
|
set (libdescription "upm grove ultrasonic proximity sensor")
|
||||||
|
set (module_src ${libname}.cxx)
|
||||||
|
set (module_h ${libname}.h)
|
||||||
|
upm_module_init()
|
104
src/groveultrasonic/groveultrasonic.cxx
Normal file
104
src/groveultrasonic/groveultrasonic.cxx
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* Author: Jun Kato <i@junkato.jp>
|
||||||
|
* Copyright (c) 2015 Jun Kato.
|
||||||
|
*
|
||||||
|
* Thanks to Seeed Studio for a working arduino sketch
|
||||||
|
*
|
||||||
|
* 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 <iostream>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
#include "groveultrasonic.h"
|
||||||
|
|
||||||
|
using namespace upm;
|
||||||
|
|
||||||
|
GroveUltraSonic::GroveUltraSonic (uint8_t pin) {
|
||||||
|
mraa_result_t error = MRAA_SUCCESS;
|
||||||
|
m_name = "GroveUltraSonic";
|
||||||
|
|
||||||
|
mraa_init();
|
||||||
|
|
||||||
|
// setup pin
|
||||||
|
m_pinCtx = mraa_gpio_init(pin);
|
||||||
|
if (m_pinCtx == NULL) {
|
||||||
|
fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", pin);
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
mraa_gpio_use_mmaped(m_pinCtx, 1);
|
||||||
|
mraa_gpio_isr (m_pinCtx, MRAA_GPIO_EDGE_BOTH,
|
||||||
|
&signalISR, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
GroveUltraSonic::~GroveUltraSonic () {
|
||||||
|
|
||||||
|
// close pin
|
||||||
|
mraa_gpio_isr_exit(m_pinCtx);
|
||||||
|
mraa_gpio_close (m_pinCtx);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
GroveUltraSonic::getDistance () {
|
||||||
|
|
||||||
|
// output trigger signal
|
||||||
|
mraa_gpio_dir(m_pinCtx, MRAA_GPIO_OUT);
|
||||||
|
mraa_gpio_write(m_pinCtx, LOW);
|
||||||
|
usleep(2);
|
||||||
|
mraa_gpio_write(m_pinCtx, HIGH);
|
||||||
|
usleep(5);
|
||||||
|
mraa_gpio_write(m_pinCtx, LOW);
|
||||||
|
|
||||||
|
// wait for the pulse,
|
||||||
|
m_doWork = 0;
|
||||||
|
m_InterruptCounter = 0;
|
||||||
|
mraa_gpio_dir(m_pinCtx, MRAA_GPIO_IN);
|
||||||
|
|
||||||
|
// though do not wait over 25 [ms].
|
||||||
|
int timer = 0;
|
||||||
|
while (!m_doWork && timer++ < 5) {
|
||||||
|
// in 25 [ms], sound travels 25000 / 29 / 2 = 431 [cm],
|
||||||
|
// which is more than 400 [cm], the max distance mesurable with this sensor.
|
||||||
|
usleep(5 * 1000); // 5 [ms]
|
||||||
|
}
|
||||||
|
|
||||||
|
// calc diff
|
||||||
|
long diff = m_FallingTimeStamp.tv_usec - m_RisingTimeStamp.tv_usec;
|
||||||
|
diff += (m_FallingTimeStamp.tv_sec - m_RisingTimeStamp.tv_sec) * 1000000;
|
||||||
|
return timer >= 5 ? 0 : diff;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
GroveUltraSonic::signalISR(void *ctx) {
|
||||||
|
upm::GroveUltraSonic *This = (upm::GroveUltraSonic *)ctx;
|
||||||
|
This->ackEdgeDetected();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
GroveUltraSonic::ackEdgeDetected () {
|
||||||
|
if (++m_InterruptCounter % 2 == 0) {
|
||||||
|
gettimeofday(&m_FallingTimeStamp, NULL);
|
||||||
|
m_doWork = 1;
|
||||||
|
} else {
|
||||||
|
gettimeofday(&m_RisingTimeStamp, NULL);
|
||||||
|
}
|
||||||
|
}
|
107
src/groveultrasonic/groveultrasonic.h
Normal file
107
src/groveultrasonic/groveultrasonic.h
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
/*
|
||||||
|
* Author: Jun Kato <i@junkato.jp>
|
||||||
|
* Copyright (c) 2015 Jun Kato.
|
||||||
|
*
|
||||||
|
* Thanks to Seeed Studio for a working arduino sketch
|
||||||
|
*
|
||||||
|
* 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 <string>
|
||||||
|
#include <mraa/aio.h>
|
||||||
|
#include <mraa/gpio.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
#define HIGH 1
|
||||||
|
#define LOW 0
|
||||||
|
|
||||||
|
#define MAX_PERIOD 7968
|
||||||
|
#define TRIGGER_PULSE 10
|
||||||
|
|
||||||
|
namespace upm {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Grove ultrasonic sensor library
|
||||||
|
* @defgroup groveultrasonic libupm-groveultrasonic
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief C++ API for Grove ultrasonic ranging component
|
||||||
|
*
|
||||||
|
* This file defines the GroveUltraSonic C++ interface for libgroveultrasonic
|
||||||
|
*
|
||||||
|
* @ingroup groveultrasonic gpio
|
||||||
|
*/
|
||||||
|
class GroveUltraSonic {
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Instanciates a GroveUltraSonic object
|
||||||
|
*
|
||||||
|
* @param pin pin for triggering the sensor for distance and for receiving pulse response
|
||||||
|
*/
|
||||||
|
GroveUltraSonic (uint8_t pin);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GroveUltraSonic object destructor.
|
||||||
|
*/
|
||||||
|
~GroveUltraSonic ();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the distance from the sensor.
|
||||||
|
*/
|
||||||
|
int getDistance ();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return name of the component
|
||||||
|
*/
|
||||||
|
std::string name()
|
||||||
|
{
|
||||||
|
return m_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ISR for the pulse signal
|
||||||
|
*/
|
||||||
|
static void signalISR(void *ctx);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flag to controll blocking function while waiting for falling edge interrupt
|
||||||
|
*/
|
||||||
|
uint8_t m_doWork;
|
||||||
|
|
||||||
|
private:
|
||||||
|
mraa_gpio_context m_pinCtx;
|
||||||
|
|
||||||
|
uint8_t m_InterruptCounter;
|
||||||
|
struct timeval m_RisingTimeStamp;
|
||||||
|
struct timeval m_FallingTimeStamp;
|
||||||
|
|
||||||
|
std::string m_name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On each interrupt this function will detect if the interrupt
|
||||||
|
* was falling edge or rising.
|
||||||
|
* Should be called from the interrupt handler.
|
||||||
|
*/
|
||||||
|
void ackEdgeDetected ();
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
8
src/groveultrasonic/jsupm_groveultrasonic.i
Normal file
8
src/groveultrasonic/jsupm_groveultrasonic.i
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
%module jsupm_groveultrasonic
|
||||||
|
%include "../upm.i"
|
||||||
|
|
||||||
|
%{
|
||||||
|
#include "groveultrasonic.h"
|
||||||
|
%}
|
||||||
|
|
||||||
|
%include "groveultrasonic.h"
|
9
src/groveultrasonic/pyupm_groveultrasonic.i
Normal file
9
src/groveultrasonic/pyupm_groveultrasonic.i
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
%module pyupm_groveultrasonic
|
||||||
|
%include "../upm.i"
|
||||||
|
|
||||||
|
%feature("autodoc", "3");
|
||||||
|
|
||||||
|
%include "groveultrasonic.h"
|
||||||
|
%{
|
||||||
|
#include "groveultrasonic.h"
|
||||||
|
%}
|
Loading…
x
Reference in New Issue
Block a user