diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index e8ce8ebc..aab4a2bf 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -116,6 +116,7 @@ add_executable (grovewfs-example grovewfs.cxx) add_executable (isd1820-example isd1820.cxx) add_executable (sx6119-example sx6119.cxx) add_executable (si114x-example si114x.cxx) +add_executable (maxsonarez-example maxsonarez.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) @@ -210,6 +211,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/grovewfs) include_directories (${PROJECT_SOURCE_DIR}/src/isd1820) include_directories (${PROJECT_SOURCE_DIR}/src/sx6119) include_directories (${PROJECT_SOURCE_DIR}/src/si114x) +include_directories (${PROJECT_SOURCE_DIR}/src/maxsonarez) target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT}) @@ -327,3 +329,4 @@ target_link_libraries (grovewfs-example grovewfs ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (isd1820-example isd1820 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (sx6119-example sx6119 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (si114x-example si114x ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (maxsonarez-example maxsonarez ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/c++/maxsonarez.cxx b/examples/c++/maxsonarez.cxx new file mode 100644 index 00000000..3c3d3004 --- /dev/null +++ b/examples/c++/maxsonarez.cxx @@ -0,0 +1,72 @@ +/* + * Author: Jon Trulson + * Copyright (c) 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 +#include +#include +#include "maxsonarez.h" + +using namespace std; + +bool shouldRun = true; + +#define MAXSONAREZ_AREF 5.0 + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + +int main() +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + + // Instantiate a MaxSonarEZ on analog pin A1, with an analog + // reference voltage of MAXSONAREZ_AREF + upm::MAXSONAREZ *sonar = new upm::MAXSONAREZ(1, MAXSONAREZ_AREF); + + // Every second, sample the sonar and output the distance to an + // object in inches. + + // With the EZ3 version, the minimum and maximum ranges seemed to be + // between 6 and 33 inches + + while (shouldRun) + { + cout << "AREF: " << MAXSONAREZ_AREF + << ", distance in inches: " << sonar->inches() << endl; + + sleep(1); + } + +//! [Interesting] + + cout << "Exiting" << endl; + + delete sonar; + return 0; +} diff --git a/examples/javascript/maxsonarez.js b/examples/javascript/maxsonarez.js new file mode 100644 index 00000000..3393b48b --- /dev/null +++ b/examples/javascript/maxsonarez.js @@ -0,0 +1,59 @@ +/*jslint node:true, vars:true, bitwise:true, unparam:true */ +/*jshint unused:true */ +/* +* Author: Jon Trulson +* Copyright (c) 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. +*/ + +// JavaScript example for the MaxSonar-EZ family of ultrasonic range finders + +// analog voltage, usually 3.3 or 5.0 +var MAXSONAREZ_AREF = 5.0; + +var MaxSonarEZ = require('jsupm_maxsonarez'); + +// Instantiate a MaxSonar-EZ on analog pin A1, with an analog +// reference voltage of MAXSONAREZ_AREF +var mySonar = new MaxSonarEZ.MAXSONAREZ(1, MAXSONAREZ_AREF); + +// Every second, sample the sonar and output the distance to an +// object in inches. + +// With the EZ3 version, the minimum and maximum ranges seemed to be +// between 6 and 33 inches + +var myInterval = setInterval(function() +{ + console.log("AREF: " + MAXSONAREZ_AREF + + ", distance in inches: " + mySonar.inches()); +}, 1000); + +// Print message when exiting +process.on('SIGINT', function() +{ + clearInterval(myInterval); + mySonar = null; + MaxSonarEZ.cleanUp(); + MaxSonarEZ = null; + console.log("Exiting..."); + process.exit(0); +}); diff --git a/examples/python/maxsonarez.py b/examples/python/maxsonarez.py new file mode 100644 index 00000000..645ca58b --- /dev/null +++ b/examples/python/maxsonarez.py @@ -0,0 +1,60 @@ +#!/usr/bin/python +# Author: Jon Trulson +# Copyright (c) 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. + +import time, sys, signal, atexit +import pyupm_maxsonarez as MaxSonarEZ + +# Instantiate a MaxSonar-EZ on analog pin A1, with an analog +# reference voltage of MAXSONAREZ_AREF +Sonar = MaxSonarEZ.MAXSONAREZ(1) + +## Exit handlers ## +# This stops python from printing a stacktrace when you hit control-C +def SIGINTHandler(signum, frame): + raise SystemExit + +# This lets you run code on exit, +# including functions from Sonar +def exitHandler(): + print "Exiting" + sys.exit(0) + +# Register exit handlers +atexit.register(exitHandler) +signal.signal(signal.SIGINT, SIGINTHandler) + + +# analog voltage, usually 3.3 or 5.0 +MAXSONAREZ_AREF = 5.0; + +# Every second, sample the sonar and output the distance to an +# object in inches. + +# With the EZ3 version, the minimum and maximum ranges seemed to be +# between 6 and 33 inches + +while (1): + print "AREF: {0}, distance in inches: {1}".format( + MAXSONAREZ_AREF, + Sonar.inches()) + time.sleep(1) diff --git a/src/maxsonarez/CMakeLists.txt b/src/maxsonarez/CMakeLists.txt new file mode 100644 index 00000000..09441bbf --- /dev/null +++ b/src/maxsonarez/CMakeLists.txt @@ -0,0 +1,5 @@ +set (libname "maxsonarez") +set (libdescription "upm MaxSonar EZ family of ultrasonic rangers") +set (module_src ${libname}.cxx) +set (module_h ${libname}.h) +upm_module_init() diff --git a/src/maxsonarez/jsupm_maxsonarez.i b/src/maxsonarez/jsupm_maxsonarez.i new file mode 100644 index 00000000..b0af4c42 --- /dev/null +++ b/src/maxsonarez/jsupm_maxsonarez.i @@ -0,0 +1,8 @@ +%module jsupm_maxsonarez +%include "../upm.i" + +%{ + #include "maxsonarez.h" +%} + +%include "maxsonarez.h" diff --git a/src/maxsonarez/maxsonarez.cxx b/src/maxsonarez/maxsonarez.cxx new file mode 100644 index 00000000..e29453e7 --- /dev/null +++ b/src/maxsonarez/maxsonarez.cxx @@ -0,0 +1,58 @@ +/* + * Author: Jon Trulson + * Copyright (c) 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 + +#include "maxsonarez.h" + +using namespace std; +using namespace upm; + +MAXSONAREZ::MAXSONAREZ(int pin, float aref) +{ + if (!(m_aio = mraa_aio_init(pin))) + { + cerr << __FUNCTION__ << "mraa_aio_init() failed." << endl; + return; + } + + m_aRes = (1 << mraa_aio_get_bit(m_aio)); + m_aref = aref; + + // volt's per inch of this sensor + m_vI = (m_aref / MAXSONAREZ_RES); +} + +MAXSONAREZ::~MAXSONAREZ() +{ + mraa_aio_close(m_aio); +} + +int MAXSONAREZ::inches() +{ + int val = mraa_aio_read(m_aio); + float volts = float(val) * (m_aref / m_aRes); + + return int(volts / m_vI); +} diff --git a/src/maxsonarez/maxsonarez.h b/src/maxsonarez/maxsonarez.h new file mode 100644 index 00000000..494a6465 --- /dev/null +++ b/src/maxsonarez/maxsonarez.h @@ -0,0 +1,97 @@ +/* + * Author: Jon Trulson + * Copyright (c) 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 +#include +#include + +// EZ series is volts/512 +#define MAXSONAREZ_RES 512 + +namespace upm { + /** + * @brief UPM library for the MaxSonar-EZ family of ultrasonic rangers + * @defgroup maxsonarez libupm-maxsonarez + * @ingroup seeed analog sound + */ + + /** + * @library maxsonarez + * @sensor maxsonarez + * @comname LV-MaxSonar-EZ family of ultrasonic rangers + * @altname EZ1, EZ2, EZ3, EZ4 + * @type sound + * @man sparkfun + * @con analog + * + * @brief C++ API for the LV-MaxSonar-EZ family of ultrasonic rangers + * + * This sensor family returns an analog voltage corresponding to the + * distance of an object from the sensor in inches. It has a + * resolution of about 9.7 millivolts per inch with a 5.0 AREF. The + * differences between the various versions (EZ1, EZ2, etc) are + * related to the narrowness of the beam angle. + * + * This class supports these sensors with an analog input only. + * + * This driver was developed using an LV-MaxSonar-EZ3. + * + * @snippet maxsonarez.cxx Interesting + */ + + class MAXSONAREZ { + public: + + /** + * MAXSONAREZ sensor constructor + * + * @param pin analog pin to use + * @param aref the analog reference voltage, default: 5.0 + */ + MAXSONAREZ(int pin, float aref=5.0); + + /** + * MAXSONAREZ Destructor + */ + ~MAXSONAREZ(); + + /** + * get the distance to the object in inches + * + * @return the distance to the object in inches + */ + int inches(); + + private: + mraa_aio_context m_aio; + float m_aref; + // ADC resolution + int m_aRes; + // computed Volts per Inch + float m_vI; + }; +} + + diff --git a/src/maxsonarez/pyupm_maxsonarez.i b/src/maxsonarez/pyupm_maxsonarez.i new file mode 100644 index 00000000..5297d263 --- /dev/null +++ b/src/maxsonarez/pyupm_maxsonarez.i @@ -0,0 +1,9 @@ +%module pyupm_maxsonarez +%include "../upm.i" + +%feature("autodoc", "3"); + +%include "maxsonarez.h" +%{ + #include "maxsonarez.h" +%}