Browse Source

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>
Jun Kato 9 years ago
parent
commit
2eb6ebd3bd

+ 3
- 0
examples/c++/CMakeLists.txt View File

@@ -154,6 +154,7 @@ add_executable (adxrs610-example adxrs610.cxx)
154 154
 add_executable (bma220-example bma220.cxx)
155 155
 add_executable (dfrph-example dfrph.cxx)
156 156
 add_executable (mcp9808-example mcp9808.cxx)
157
+add_executable (groveultrasonic-example groveultrasonic.cxx)
157 158
 
158 159
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
159 160
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -272,6 +273,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/adxrs610)
272 273
 include_directories (${PROJECT_SOURCE_DIR}/src/bma220)
273 274
 include_directories (${PROJECT_SOURCE_DIR}/src/dfrph)
274 275
 include_directories (${PROJECT_SOURCE_DIR}/src/mcp9808)
276
+include_directories (${PROJECT_SOURCE_DIR}/src/groveultrasonic)
275 277
 
276 278
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
277 279
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -427,3 +429,4 @@ target_link_libraries (adxrs610-example adxrs610 ${CMAKE_THREAD_LIBS_INIT})
427 429
 target_link_libraries (bma220-example bma220 ${CMAKE_THREAD_LIBS_INIT})
428 430
 target_link_libraries (dfrph-example dfrph ${CMAKE_THREAD_LIBS_INIT})
429 431
 target_link_libraries (mcp9808-example mcp9808 ${CMAKE_THREAD_LIBS_INIT})
432
+target_link_libraries (groveultrasonic-example groveultrasonic ${CMAKE_THREAD_LIBS_INIT})

+ 58
- 0
examples/c++/groveultrasonic.cxx View File

@@ -0,0 +1,58 @@
1
+/*
2
+ * Author: Jun Kato <i@junkato.jp>
3
+ * Copyright (c) 2015 Jun Kato.
4
+ *
5
+ * Thanks to Seeed Studio for a working arduino sketch
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining
8
+ * a copy of this software and associated documentation files (the
9
+ * "Software"), to deal in the Software without restriction, including
10
+ * without limitation the rights to use, copy, modify, merge, publish,
11
+ * distribute, sublicense, and/or sell copies of the Software, and to
12
+ * permit persons to whom the Software is furnished to do so, subject to
13
+ * the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be
16
+ * included in all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+ */
26
+
27
+#include <unistd.h>
28
+#include <iostream>
29
+#include "groveultrasonic.h"
30
+#include <signal.h>
31
+#include <stdlib.h>
32
+#include <sys/time.h>
33
+
34
+upm::GroveUltraSonic *sonar = NULL;
35
+
36
+void
37
+sig_handler(int signo)
38
+{
39
+    printf("got signal\n");
40
+    if (signo == SIGINT) {
41
+      sonar->m_doWork = 1;
42
+    }
43
+}
44
+
45
+int
46
+main(int argc, char **argv)
47
+{
48
+  signal(SIGINT, sig_handler);
49
+//! [Interesting]
50
+  // upm::GroveUltraSonic *sonar = NULL;
51
+  sonar = new upm::GroveUltraSonic(2);
52
+  printf("width = %d\n", sonar->getDistance());
53
+  delete sonar;
54
+//! [Interesting]
55
+  printf("exiting application\n");
56
+
57
+  return 0;
58
+}

+ 45
- 0
examples/javascript/groveultrasonic.js View File

@@ -0,0 +1,45 @@
1
+/*
2
+ * Author: Jun Kato <i@junkato.jp>
3
+ * Copyright (c) 2015 Jun Kato.
4
+ *
5
+ * Thanks to Seeed Studio for a working arduino sketch
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining
8
+ * a copy of this software and associated documentation files (the
9
+ * "Software"), to deal in the Software without restriction, including
10
+ * without limitation the rights to use, copy, modify, merge, publish,
11
+ * distribute, sublicense, and/or sell copies of the Software, and to
12
+ * permit persons to whom the Software is furnished to do so, subject to
13
+ * the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be
16
+ * included in all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+ */
26
+
27
+var ultrasonic = require("jsupm_groveultrasonic");
28
+var sensor = new ultrasonic.GroveUltraSonic(2);
29
+
30
+var myInterval = setInterval(function()
31
+{
32
+  var travelTime = sensor.getDistance();
33
+  if (travelTime > 0) {
34
+    var distance = (travelTime / 29 / 2).toFixed(3);
35
+    console.log("distance: " + distance + " [cm]");
36
+  }
37
+}, 200);
38
+
39
+// When exiting: clear interval and print message
40
+process.on('SIGINT', function()
41
+{
42
+  clearInterval(myInterval);
43
+  console.log("Exiting...");
44
+  process.exit(0);
45
+});

+ 5
- 0
src/groveultrasonic/CMakeLists.txt View File

@@ -0,0 +1,5 @@
1
+set (libname "groveultrasonic")
2
+set (libdescription "upm grove ultrasonic proximity sensor")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 104
- 0
src/groveultrasonic/groveultrasonic.cxx View File

@@ -0,0 +1,104 @@
1
+/*
2
+ * Author: Jun Kato <i@junkato.jp>
3
+ * Copyright (c) 2015 Jun Kato.
4
+ *
5
+ * Thanks to Seeed Studio for a working arduino sketch
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining
8
+ * a copy of this software and associated documentation files (the
9
+ * "Software"), to deal in the Software without restriction, including
10
+ * without limitation the rights to use, copy, modify, merge, publish,
11
+ * distribute, sublicense, and/or sell copies of the Software, and to
12
+ * permit persons to whom the Software is furnished to do so, subject to
13
+ * the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be
16
+ * included in all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+ */
26
+
27
+#include <iostream>
28
+#include <unistd.h>
29
+#include <stdlib.h>
30
+#include <functional>
31
+
32
+#include "groveultrasonic.h"
33
+
34
+using namespace upm;
35
+
36
+GroveUltraSonic::GroveUltraSonic (uint8_t pin) {
37
+    mraa_result_t error = MRAA_SUCCESS;
38
+    m_name = "GroveUltraSonic";
39
+
40
+    mraa_init();
41
+
42
+    // setup pin
43
+    m_pinCtx = mraa_gpio_init(pin);
44
+    if (m_pinCtx == NULL) {
45
+        fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", pin);
46
+        exit (1);
47
+    }
48
+    mraa_gpio_use_mmaped(m_pinCtx, 1);
49
+    mraa_gpio_isr (m_pinCtx, MRAA_GPIO_EDGE_BOTH,
50
+                   &signalISR, this);
51
+}
52
+
53
+GroveUltraSonic::~GroveUltraSonic () {
54
+
55
+    // close pin
56
+    mraa_gpio_isr_exit(m_pinCtx);
57
+    mraa_gpio_close (m_pinCtx);
58
+}
59
+
60
+int
61
+GroveUltraSonic::getDistance () {
62
+
63
+    // output trigger signal
64
+    mraa_gpio_dir(m_pinCtx, MRAA_GPIO_OUT);
65
+    mraa_gpio_write(m_pinCtx, LOW);
66
+    usleep(2);
67
+    mraa_gpio_write(m_pinCtx, HIGH);
68
+    usleep(5);
69
+    mraa_gpio_write(m_pinCtx, LOW);
70
+
71
+    // wait for the pulse,
72
+    m_doWork = 0;
73
+    m_InterruptCounter = 0;
74
+    mraa_gpio_dir(m_pinCtx, MRAA_GPIO_IN);
75
+
76
+    // though do not wait over 25 [ms].
77
+    int timer = 0;
78
+    while (!m_doWork && timer++ < 5) {
79
+        // in 25 [ms], sound travels 25000 / 29 / 2 = 431 [cm],
80
+        // which is more than 400 [cm], the max distance mesurable with this sensor.
81
+        usleep(5 * 1000); // 5 [ms]
82
+    }
83
+
84
+    // calc diff
85
+    long diff = m_FallingTimeStamp.tv_usec - m_RisingTimeStamp.tv_usec;
86
+    diff += (m_FallingTimeStamp.tv_sec - m_RisingTimeStamp.tv_sec) * 1000000;
87
+    return timer >= 5 ? 0 : diff;
88
+}
89
+
90
+void
91
+GroveUltraSonic::signalISR(void *ctx) {
92
+    upm::GroveUltraSonic *This = (upm::GroveUltraSonic *)ctx;
93
+    This->ackEdgeDetected();
94
+}
95
+
96
+void
97
+GroveUltraSonic::ackEdgeDetected () {
98
+    if (++m_InterruptCounter % 2 == 0) {
99
+        gettimeofday(&m_FallingTimeStamp, NULL);
100
+        m_doWork = 1;
101
+    } else {
102
+        gettimeofday(&m_RisingTimeStamp, NULL);
103
+    }
104
+}

+ 107
- 0
src/groveultrasonic/groveultrasonic.h View File

@@ -0,0 +1,107 @@
1
+/*
2
+ * Author: Jun Kato <i@junkato.jp>
3
+ * Copyright (c) 2015 Jun Kato.
4
+ *
5
+ * Thanks to Seeed Studio for a working arduino sketch
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining
8
+ * a copy of this software and associated documentation files (the
9
+ * "Software"), to deal in the Software without restriction, including
10
+ * without limitation the rights to use, copy, modify, merge, publish,
11
+ * distribute, sublicense, and/or sell copies of the Software, and to
12
+ * permit persons to whom the Software is furnished to do so, subject to
13
+ * the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be
16
+ * included in all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+ */
26
+#pragma once
27
+
28
+#include <string>
29
+#include <mraa/aio.h>
30
+#include <mraa/gpio.h>
31
+#include <sys/time.h>
32
+
33
+#define HIGH                   1
34
+#define LOW                    0
35
+
36
+#define MAX_PERIOD             7968
37
+#define TRIGGER_PULSE          10
38
+
39
+namespace upm {
40
+
41
+/**
42
+ * @brief Grove ultrasonic sensor library
43
+ * @defgroup groveultrasonic libupm-groveultrasonic
44
+ */
45
+
46
+/**
47
+ * @brief C++ API for Grove ultrasonic ranging component
48
+ *
49
+ * This file defines the GroveUltraSonic C++ interface for libgroveultrasonic
50
+ *
51
+ * @ingroup groveultrasonic gpio
52
+ */
53
+class GroveUltraSonic {
54
+    public:
55
+        /**
56
+         * Instanciates a GroveUltraSonic object
57
+         *
58
+         * @param pin pin for triggering the sensor for distance and for receiving pulse response
59
+         */
60
+        GroveUltraSonic (uint8_t pin);
61
+
62
+        /**
63
+         * GroveUltraSonic object destructor.
64
+         */
65
+        ~GroveUltraSonic ();
66
+
67
+        /**
68
+         * Get the distance from the sensor.
69
+         */
70
+        int getDistance ();
71
+
72
+        /**
73
+         * Return name of the component
74
+         */
75
+        std::string name()
76
+        {
77
+            return m_name;
78
+        }
79
+
80
+        /**
81
+         * ISR for the pulse signal
82
+         */
83
+        static void signalISR(void *ctx);
84
+
85
+        /**
86
+         * Flag to controll blocking function while waiting for falling edge interrupt
87
+         */
88
+        uint8_t m_doWork;
89
+
90
+    private:
91
+        mraa_gpio_context m_pinCtx;
92
+
93
+        uint8_t m_InterruptCounter;
94
+        struct timeval m_RisingTimeStamp;
95
+        struct timeval m_FallingTimeStamp;
96
+
97
+        std::string m_name;
98
+
99
+        /**
100
+         * On each interrupt this function will detect if the interrupt
101
+         * was falling edge or rising.
102
+         * Should be called from the interrupt handler.
103
+         */
104
+        void ackEdgeDetected ();
105
+};
106
+
107
+}

+ 8
- 0
src/groveultrasonic/jsupm_groveultrasonic.i View File

@@ -0,0 +1,8 @@
1
+%module jsupm_groveultrasonic
2
+%include "../upm.i"
3
+
4
+%{
5
+    #include "groveultrasonic.h"
6
+%}
7
+
8
+%include "groveultrasonic.h"

+ 9
- 0
src/groveultrasonic/pyupm_groveultrasonic.i View File

@@ -0,0 +1,9 @@
1
+%module pyupm_groveultrasonic
2
+%include "../upm.i"
3
+
4
+%feature("autodoc", "3");
5
+
6
+%include "groveultrasonic.h"
7
+%{
8
+    #include "groveultrasonic.h"
9
+%}