diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index 1dd1daeb..0732862d 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -324,6 +324,7 @@ add_example (sht1x) add_example (ms5803) add_example (ims) add_example (ecezo) +add_example (mb704x) # These are special cases where you specify example binary, source file and module(s) include_directories (${PROJECT_SOURCE_DIR}/src) diff --git a/examples/c++/mb704x.cxx b/examples/c++/mb704x.cxx new file mode 100644 index 00000000..ebcb9241 --- /dev/null +++ b/examples/c++/mb704x.cxx @@ -0,0 +1,70 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 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 + +using namespace std; +using namespace upm; + +bool shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + +int main() +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + + // Instantiate a MB704X sensor using default parameters (bus 0, + // address 112) + upm::MB704X *sensor = new upm::MB704X(); + + while (shouldRun) + { + cout << "Range: " + << sensor->getRange() + << " cm" + << endl; + + upm_delay_ms(500); + } + + + cout << "Exiting..." << endl; + + delete sensor; + +//! [Interesting] + + return 0; +} diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index ce36f793..dd08f195 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -140,6 +140,7 @@ add_example (my9221) add_example (ms5803) add_example (ims) add_example (ecezo) +add_example (mb704x) # Custom examples add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps) diff --git a/examples/c/mb704x.c b/examples/c/mb704x.c new file mode 100644 index 00000000..6a00752f --- /dev/null +++ b/examples/c/mb704x.c @@ -0,0 +1,77 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 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 + +bool shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + +int main() +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + + // Instantiate a MB704X sensor on i2c bus 0, address 112. + mb704x_context sensor = mb704x_init(0, 112); + + if (!sensor) + { + printf("mb704x_init() failed.\n"); + return 1; + } + + while (shouldRun) + { + // this will take about 1 second to complete + int range = mb704x_get_range(sensor); + if (range < 0) + { + printf("Error getting range.\n"); + } + else + { + printf("Range: %d cm\n", range); + } + + upm_delay_ms(500); + } + + printf("Exiting\n"); + + mb704x_close(sensor); + +//! [Interesting] + + return 0; +} diff --git a/examples/java/CMakeLists.txt b/examples/java/CMakeLists.txt index 2f87451d..19f919be 100644 --- a/examples/java/CMakeLists.txt +++ b/examples/java/CMakeLists.txt @@ -159,6 +159,7 @@ add_example(SHT1X_Example sht1x) add_example(MS5803_Example ms5803) add_example(ECEZO_Example ecezo) add_example(IMS_Example ims) +add_example(MB704X_Example mb704x) add_example_with_path(Jhd1313m1_lcdSample lcd i2clcd) add_example_with_path(Jhd1313m1Sample lcd i2clcd) diff --git a/examples/java/MB704X_Example.java b/examples/java/MB704X_Example.java new file mode 100644 index 00000000..c3262d46 --- /dev/null +++ b/examples/java/MB704X_Example.java @@ -0,0 +1,48 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 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 upm_mb704x.MB704X; + +public class MB704X_Example +{ + public static void main(String[] args) throws InterruptedException + { +// ! [Interesting] + + // Instantiate a MB704X sensor using default parameters (bus 0, + // address 112) + MB704X sensor = new MB704X(); + + while (true) + { + System.out.println("Range: " + + sensor.getRange() + + " cm"); + + Thread.sleep(500); + } + +// ! [Interesting] + } +} diff --git a/examples/javascript/mb704x.js b/examples/javascript/mb704x.js new file mode 100644 index 00000000..e37acaa0 --- /dev/null +++ b/examples/javascript/mb704x.js @@ -0,0 +1,47 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 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. + */ + +var sensorObj = require('jsupm_mb704x'); + +// Instantiate a MB704X sensor using default parameters (bus 0, +// address 112) +var sensor = new sensorObj.MB704X(); + +setInterval(function() +{ + console.log("Range: " + + sensor.getRange() + + " cm"); + +}, 500); + +// exit on ^C +process.on('SIGINT', function() +{ + sensor = null; + sensorObj.cleanUp(); + sensorObj = null; + console.log("Exiting."); + process.exit(0); +}); diff --git a/examples/python/mb704x.py b/examples/python/mb704x.py new file mode 100755 index 00000000..706d9dd4 --- /dev/null +++ b/examples/python/mb704x.py @@ -0,0 +1,56 @@ +#!/usr/bin/python +# Author: Jon Trulson +# Copyright (c) 2016 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. + +from __future__ import print_function +import time, sys, signal, atexit +from upm import pyupm_mb704x as sensorObj + +def main(): + # Instantiate a MB704X sensor using default parameters (bus 0, + # address 112) + sensor = sensorObj.MB704X(); + + ## Exit handlers ## + # This function stops python from printing a stacktrace when you + # hit control-C + def SIGINTHandler(signum, frame): + raise SystemExit + + # This function lets you run code on exit + def exitHandler(): + print("Exiting") + sys.exit(0) + + # Register exit handlers + atexit.register(exitHandler) + signal.signal(signal.SIGINT, SIGINTHandler) + + while (1): + print("Range: " + + str(sensor.getRange()) + + " cm") + + time.sleep(.5) + +if __name__ == '__main__': + main() diff --git a/src/mb704x/CMakeLists.txt b/src/mb704x/CMakeLists.txt new file mode 100644 index 00000000..88fd7109 --- /dev/null +++ b/src/mb704x/CMakeLists.txt @@ -0,0 +1,9 @@ +upm_mixed_module_init (NAME mb704x + DESCRIPTION "MB7040/7047 I2C MaxSonar WR Ultrasonic Ranger" + C_HDR mb704x.h + C_SRC mb704x.c + CPP_HDR mb704x.hpp + CPP_SRC mb704x.cxx + FTI_SRC mb704x_fti.c + CPP_WRAPS_C + REQUIRES mraa) diff --git a/src/mb704x/javaupm_mb704x.i b/src/mb704x/javaupm_mb704x.i new file mode 100644 index 00000000..0c80da73 --- /dev/null +++ b/src/mb704x/javaupm_mb704x.i @@ -0,0 +1,21 @@ +%module javaupm_mb704x +%include "../upm.i" +%include "std_string.i" +%include "stdint.i" +%include "typemaps.i" + +%include "mb704x.hpp" +%{ + #include "mb704x.hpp" +%} + +%pragma(java) jniclasscode=%{ + static { + try { + System.loadLibrary("javaupm_mb704x"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. \n" + e); + System.exit(1); + } + } +%} diff --git a/src/mb704x/jsupm_mb704x.i b/src/mb704x/jsupm_mb704x.i new file mode 100644 index 00000000..c86ac5ad --- /dev/null +++ b/src/mb704x/jsupm_mb704x.i @@ -0,0 +1,8 @@ +%module jsupm_mb704x +%include "../upm.i" +%include "std_string.i" + +%include "mb704x.hpp" +%{ + #include "mb704x.hpp" +%} diff --git a/src/mb704x/mb704x.c b/src/mb704x/mb704x.c new file mode 100644 index 00000000..085d4eee --- /dev/null +++ b/src/mb704x/mb704x.c @@ -0,0 +1,115 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 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 + +#include "mb704x.h" + +// The delay to wait (in ms) for a ranging command to complete before +// requesting the range data. +#define MB704X_RANGE_DELAY (100) + +// range command +#define MB704X_CMD_RANGE (81) + +mb704x_context mb704x_init(unsigned int bus, int addr) +{ + // 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; + } + + mb704x_context dev = + (mb704x_context)malloc(sizeof(struct _mb704x_context)); + + if (!dev) + return NULL; + + // zero out context + memset((void *)dev, 0, sizeof(struct _mb704x_context)); + + // initialize the MRAA contexts + + if (!(dev->i2c = mraa_i2c_init(bus))) + { + printf("%s: mraa_i2c_init() failed.\n", __FUNCTION__); + mb704x_close(dev); + return NULL; + } + + if (mraa_i2c_address(dev->i2c, (uint8_t)addr)) + { + printf("%s: mraa_i2c_address() failed.\n", __FUNCTION__); + mb704x_close(dev); + return NULL; + } + + // max power up delay + upm_delay_ms(135); + + return dev; +} + +void mb704x_close(mb704x_context dev) +{ + assert(dev != NULL); + + if (dev->i2c) + mraa_i2c_stop(dev->i2c); + + free(dev); +} + +int mb704x_get_range(const mb704x_context dev) +{ + assert(dev != NULL); + + // first, send the range command. + uint8_t cmd = MB704X_CMD_RANGE; + if (mraa_i2c_write(dev->i2c, &cmd, 1)) + { + printf("%s: mraa_i2c_write() failed.\n", __FUNCTION__); + return -1; + } + + // delay for measurement + upm_delay_ms(MB704X_RANGE_DELAY); + + // At this point, we should be able to do our read of the data + uint8_t buffer[2]; + if (mraa_i2c_read(dev->i2c, buffer, 2) != 2) + { + printf("%s: mraa_i2c_read() failed.\n", __FUNCTION__); + return -1; + } + + return ((buffer[0] << 8) | buffer[1]); +} diff --git a/src/mb704x/mb704x.cxx b/src/mb704x/mb704x.cxx new file mode 100644 index 00000000..725ad3cf --- /dev/null +++ b/src/mb704x/mb704x.cxx @@ -0,0 +1,49 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 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 "mb704x.hpp" + +using namespace upm; +using namespace std; + +MB704X::MB704X(unsigned int bus, unsigned int addr) : + m_mb704x(mb704x_init(bus, addr)) +{ + if (!m_mb704x) + throw std::runtime_error(string(__FUNCTION__) + + ": mb704x_*_init() failed"); +} + +MB704X::~MB704X() +{ + mb704x_close(m_mb704x); +} + +int MB704X::getRange() +{ + return mb704x_get_range(m_mb704x); +} diff --git a/src/mb704x/mb704x.h b/src/mb704x/mb704x.h new file mode 100644 index 00000000..5b98d303 --- /dev/null +++ b/src/mb704x/mb704x.h @@ -0,0 +1,78 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 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 +#include + +#ifdef __cplusplus +extern "C" { +#endif + + /** + * @file mb704x.h + * @library mb704x + * @brief C API for the MB704x MaxSonar-WR Ultrasonic Ranger + * + * @include mb704x.c + */ + + /** + * Device context + */ + typedef struct _mb704x_context { + mraa_i2c_context i2c; + } *mb704x_context; + + /** + * MB704X Initializer + * + * @param bus Specify which the I2C bus to use. + * @param addr Specify the I2C address to use. The default is 112. + * @return an initialized device context on success, NULL on error. + */ + mb704x_context mb704x_init(unsigned int bus, int addr); + + /** + * MB704X sensor close function + */ + void mb704x_close(mb704x_context dev); + + /** + * Query the device for a range reading. The range will be + * reported in centimeters (cm). + * + * @param dev Device context + * @return Measured range, -1 on error. The range is reported in + * centimeters (cm). + */ + int mb704x_get_range(const mb704x_context dev); + + +#ifdef __cplusplus +} +#endif diff --git a/src/mb704x/mb704x.hpp b/src/mb704x/mb704x.hpp new file mode 100644 index 00000000..138107c4 --- /dev/null +++ b/src/mb704x/mb704x.hpp @@ -0,0 +1,94 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 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 +#include + +#include "mb704x.h" + +namespace upm { + /** + * @brief API for the MB704x MaxSonar-WR Ultrasonic Ranger + * @defgroup mb704x libupm-mb704x + * @ingroup i2c sound + */ + + /** + * @library mb704x + * @sensor mb704x + * @comname MB704x MaxSonar-WR Ultrasonic Ranger + * @altname MB7040 MB7047 MB7247 + * @type sound + * @man maxbotix + * @con i2c + * @web http://www.maxbotix.com/Ultrasonic_Sensors/I2C_Distance_Sensors.htm + * + * @brief API for the MB704x MaxSonar-WR Ultrasonic Ranger + * + * This driver was tested with the MB704x MaxSonar-WR Ultrasonic + * Ranger using the long distance, weather resistant horn. + * Depending on your sensor type and horn, tanges from 20cm to + * 750cm are possible. + * + * @snippet mb704x.cxx Interesting + */ + + class MB704X { + public: + + /** + * Initialize a device context. + * + * @param bus Specify which the I2C bus to use. The default is 0. + * @param addr Specify the I2C address to use. The default is 112. + */ + MB704X(unsigned int bus=0, unsigned int addr=112); + + /** + * MB704X object destructor + */ + ~MB704X(); + + /** + * Query the device for a range reading. The range will be + * reported in centimeters (cm). + * + * @param dev Device context + * @return Measured range, -1 on error. The range is reported in + * centimeters (cm). + */ + int getRange(); + + + protected: + // mb704x device context + mb704x_context m_mb704x; + + private: + }; +} diff --git a/src/mb704x/mb704x_fti.c b/src/mb704x/mb704x_fti.c new file mode 100644 index 00000000..059674e6 --- /dev/null +++ b/src/mb704x/mb704x_fti.c @@ -0,0 +1,113 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 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 "mb704x.h" +#include "upm_fti.h" + +/** + * This file implements the Function Table Interface (FTI) for this sensor + */ + +const char upm_mb704x_name[] = "MB704X"; +const char upm_mb704x_description[] = "MB704x MaxSonar-WR Ultrasonic Ranger"; +const upm_protocol_t upm_mb704x_protocol[] = {UPM_I2C}; +const upm_sensor_t upm_mb704x_category[] = {UPM_DISTANCE}; + +// forward declarations +const void* upm_mb704x_get_ft(upm_sensor_t sensor_type); +void* upm_mb704x_init_name(); +void upm_mb704x_close(void *dev); +upm_result_t upm_mb704x_get_distance(void *dev, float *value, + upm_distance_u unit); + +const upm_sensor_descriptor_t upm_es08a_get_descriptor() +{ + upm_sensor_descriptor_t usd; + + usd.name = upm_mb704x_name; + usd.description = upm_mb704x_description; + usd.protocol_size = 1; + usd.protocol = upm_mb704x_protocol; + usd.category_size = 1; + usd.category = upm_mb704x_category; + + return usd; +} + +static const upm_sensor_ft ft = +{ + .upm_sensor_init_name = &upm_mb704x_init_name, + .upm_sensor_close = &upm_mb704x_close, +}; + +static const upm_distance_ft dft = +{ + .upm_distance_get_value = upm_mb704x_get_distance +}; + +const void* upm_mb704x_get_ft(upm_sensor_t sensor_type) +{ + switch(sensor_type) + { + case UPM_SENSOR: + return &ft; + + case UPM_DISTANCE: + return &dft; + + default: + return NULL; + } +} + +void *upm_mb704x_init_name() +{ + return NULL; +} + +void upm_mb704x_close(void *dev) +{ + mb704x_close((mb704x_context)dev); +} + +upm_result_t upm_mb704x_get_distance(void *dev, float *value, + upm_distance_u unit) +{ + int rv; + + if ((rv = mb704x_get_range((mb704x_context)dev)) < 0) + return UPM_ERROR_OPERATION_FAILED; + + switch(unit) + { + case CENTIMETER: + *value = (float)rv; + case INCH: + *value = ((float)rv * 0.393701); + default: + return UPM_ERROR_INVALID_PARAMETER; + } + + return UPM_SUCCESS; +} diff --git a/src/mb704x/pyupm_mb704x.i b/src/mb704x/pyupm_mb704x.i new file mode 100644 index 00000000..eaec2c8a --- /dev/null +++ b/src/mb704x/pyupm_mb704x.i @@ -0,0 +1,12 @@ +// Include doxygen-generated documentation +%include "pyupm_doxy2swig.i" +%module pyupm_mb704x +%include "../upm.i" +%include "std_string.i" + +%feature("autodoc", "3"); + +%include "mb704x.hpp" +%{ + #include "mb704x.hpp" +%}