diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index 0d549f31..1d4a95e3 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -154,6 +154,7 @@ add_executable (adxrs610-example adxrs610.cxx) add_executable (bma220-example bma220.cxx) add_executable (dfrph-example dfrph.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/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/dfrph) 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 (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 (dfrph-example dfrph ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (mcp9808-example mcp9808 ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (groveultrasonic-example groveultrasonic ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/c++/groveultrasonic.cxx b/examples/c++/groveultrasonic.cxx new file mode 100644 index 00000000..96a0fef0 --- /dev/null +++ b/examples/c++/groveultrasonic.cxx @@ -0,0 +1,58 @@ +/* + * Author: Jun Kato + * 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 +#include +#include "groveultrasonic.h" +#include +#include +#include + +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; +} diff --git a/examples/javascript/groveultrasonic.js b/examples/javascript/groveultrasonic.js new file mode 100644 index 00000000..e2b58041 --- /dev/null +++ b/examples/javascript/groveultrasonic.js @@ -0,0 +1,45 @@ +/* + * Author: Jun Kato + * 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); +}); diff --git a/src/groveultrasonic/CMakeLists.txt b/src/groveultrasonic/CMakeLists.txt new file mode 100644 index 00000000..3144f437 --- /dev/null +++ b/src/groveultrasonic/CMakeLists.txt @@ -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() diff --git a/src/groveultrasonic/groveultrasonic.cxx b/src/groveultrasonic/groveultrasonic.cxx new file mode 100644 index 00000000..161da33c --- /dev/null +++ b/src/groveultrasonic/groveultrasonic.cxx @@ -0,0 +1,104 @@ +/* + * Author: Jun Kato + * 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 +#include +#include +#include + +#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); + } +} diff --git a/src/groveultrasonic/groveultrasonic.h b/src/groveultrasonic/groveultrasonic.h new file mode 100644 index 00000000..c16de755 --- /dev/null +++ b/src/groveultrasonic/groveultrasonic.h @@ -0,0 +1,107 @@ +/* + * Author: Jun Kato + * 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 +#include +#include +#include + +#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 (); +}; + +} diff --git a/src/groveultrasonic/jsupm_groveultrasonic.i b/src/groveultrasonic/jsupm_groveultrasonic.i new file mode 100644 index 00000000..6993b015 --- /dev/null +++ b/src/groveultrasonic/jsupm_groveultrasonic.i @@ -0,0 +1,8 @@ +%module jsupm_groveultrasonic +%include "../upm.i" + +%{ + #include "groveultrasonic.h" +%} + +%include "groveultrasonic.h" diff --git a/src/groveultrasonic/pyupm_groveultrasonic.i b/src/groveultrasonic/pyupm_groveultrasonic.i new file mode 100644 index 00000000..bc3df677 --- /dev/null +++ b/src/groveultrasonic/pyupm_groveultrasonic.i @@ -0,0 +1,9 @@ +%module pyupm_groveultrasonic +%include "../upm.i" + +%feature("autodoc", "3"); + +%include "groveultrasonic.h" +%{ + #include "groveultrasonic.h" +%}