Selaa lähdekoodia

grovewfs: Initial implementation

This driver implements support for the Grove Water Flow sensor.  It
uses an integrated hall effect sensor, connected to a GPIO configured
as an interrupt, to compute water flow in LPM (Liters Per Minute).

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: John Van Drasek <john.r.van.drasek@intel.com>
Jon Trulson 10 vuotta sitten
vanhempi
commit
ce1e2fd606

+ 3
- 0
examples/c++/CMakeLists.txt Näytä tiedosto

@@ -112,6 +112,7 @@ add_executable (tm1637-example tm1637.cxx)
112 112
 add_executable (zfm20-example zfm20.cxx)
113 113
 add_executable (zfm20-register-example zfm20-register.cxx)
114 114
 add_executable (uln200xa-example uln200xa.cxx)
115
+add_executable (grovewfs-example grovewfs.cxx)
115 116
 
116 117
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
117 118
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -202,6 +203,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/waterlevel)
202 203
 include_directories (${PROJECT_SOURCE_DIR}/src/tm1637)
203 204
 include_directories (${PROJECT_SOURCE_DIR}/src/zfm20)
204 205
 include_directories (${PROJECT_SOURCE_DIR}/src/uln200xa)
206
+include_directories (${PROJECT_SOURCE_DIR}/src/grovewfs)
205 207
 
206 208
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
207 209
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -315,3 +317,4 @@ target_link_libraries (tm1637-example tm1637 ${CMAKE_THREAD_LIBS_INIT})
315 317
 target_link_libraries (zfm20-example zfm20 ${CMAKE_THREAD_LIBS_INIT})
316 318
 target_link_libraries (zfm20-register-example zfm20 ${CMAKE_THREAD_LIBS_INIT})
317 319
 target_link_libraries (uln200xa-example uln200xa ${CMAKE_THREAD_LIBS_INIT})
320
+target_link_libraries (grovewfs-example grovewfs ${CMAKE_THREAD_LIBS_INIT})

+ 78
- 0
examples/c++/grovewfs.cxx Näytä tiedosto

@@ -0,0 +1,78 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+#include <unistd.h>
26
+#include <iostream>
27
+#include <signal.h>
28
+#include "grovewfs.h"
29
+
30
+using namespace std;
31
+
32
+int shouldRun = true;
33
+
34
+void sig_handler(int signo)
35
+{
36
+  if (signo == SIGINT)
37
+    shouldRun = false;
38
+}
39
+
40
+
41
+int main()
42
+{
43
+  signal(SIGINT, sig_handler);
44
+
45
+//! [Interesting]
46
+  // Instantiate a Grove Water Flow Sensor on digital pin D2
47
+  upm::GroveWFS* flow = new upm::GroveWFS(2);
48
+  
49
+  // set the flow counter to 0 and start counting
50
+  flow->clearFlowCounter();
51
+  flow->startFlowCounter();
52
+
53
+  while (shouldRun)
54
+    {
55
+      // we grab these (,illis and flowCount) just for display
56
+      // purposes in this example
57
+      uint32_t millis = flow->getMillis();
58
+      uint32_t flowCount = flow->flowCounter();
59
+
60
+      float fr = flow->flowRate();
61
+
62
+      // output milliseconds passed, flow count, and computed flow rate
63
+      cout << "Millis: " << millis << " Flow Count: " << flowCount;
64
+      cout << " Flow Rate: " << fr << " LPM" << endl;
65
+
66
+      // best to gather data for at least one second for reasonable
67
+      // results.
68
+      sleep(2);
69
+    }
70
+
71
+  flow->stopFlowCounter();
72
+//! [Interesting]
73
+
74
+  cout << "Exiting..." << endl;
75
+
76
+  delete flow;
77
+  return 0;
78
+}

+ 69
- 0
examples/javascript/grovewfs.js Näytä tiedosto

@@ -0,0 +1,69 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+
4
+/*
5
+* Author: Zion Orent <zorent@ics.com>
6
+* Copyright (c) 2015 Intel Corporation.
7
+*
8
+* Permission is hereby granted, free of charge, to any person obtaining
9
+* a copy of this software and associated documentation files (the
10
+* "Software"), to deal in the Software without restriction, including
11
+* without limitation the rights to use, copy, modify, merge, publish,
12
+* distribute, sublicense, and/or sell copies of the Software, and to
13
+* permit persons to whom the Software is furnished to do so, subject to
14
+* the following conditions:
15
+*
16
+* The above copyright notice and this permission notice shall be
17
+* included in all copies or substantial portions of the Software.
18
+*
19
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+*/
27
+
28
+var waterFlow_lib = require('jsupm_grovewfs');
29
+
30
+// Instantiate a Grove Water Flow Sensor on digital pin D2
31
+var myWaterFlow_obj = new waterFlow_lib.GroveWFS(2);
32
+
33
+// set the flow counter to 0 and start counting
34
+myWaterFlow_obj.clearFlowCounter();
35
+myWaterFlow_obj.startFlowCounter();
36
+
37
+
38
+var millis, flowCount, fr;
39
+var myInterval = setInterval(function()
40
+{
41
+	// we grab these (millis and flowCount) just for display
42
+	// purposes in this example
43
+	millis = myWaterFlow_obj.getMillis();
44
+	flowCount = myWaterFlow_obj.flowCounter();
45
+
46
+	fr = myWaterFlow_obj.flowRate();
47
+
48
+	// output milliseconds passed, flow count, and computed flow rate
49
+	outputStr = "Millis: " + millis + " Flow Count: " + flowCount +
50
+	" Flow Rate: " + fr + " LPM";
51
+	console.log(outputStr);
52
+
53
+	// best to gather data for at least one second for reasonable
54
+	// results.
55
+}, 2000);
56
+
57
+
58
+// When exiting: clear interval and print message
59
+process.on('SIGINT', function()
60
+{
61
+	clearInterval(myInterval);
62
+	myWaterFlow_obj.stopFlowCounter();
63
+	myWaterFlow_obj = null
64
+	waterFlow_lib.cleanUp();
65
+	waterFlow_lib = null;
66
+
67
+	console.log("Exiting");
68
+	process.exit(0);
69
+});

+ 64
- 0
examples/python/grovewfs.py Näytä tiedosto

@@ -0,0 +1,64 @@
1
+#!/usr/bin/python
2
+# Author: Zion Orent <zorent@ics.com>
3
+# Copyright (c) 2015 Intel Corporation.
4
+#
5
+# Permission is hereby granted, free of charge, to any person obtaining
6
+# a copy of this software and associated documentation files (the
7
+# "Software"), to deal in the Software without restriction, including
8
+# without limitation the rights to use, copy, modify, merge, publish,
9
+# distribute, sublicense, and/or sell copies of the Software, and to
10
+# permit persons to whom the Software is furnished to do so, subject to
11
+# the following conditions:
12
+#
13
+# The above copyright notice and this permission notice shall be
14
+# included in all copies or substantial portions of the Software.
15
+#
16
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+import time, sys, signal, atexit
25
+import pyupm_grovewfs as upmGrovewfs
26
+
27
+# Instantiate a Grove Water Flow Sensor on digital pin D2
28
+myWaterFlow = upmGrovewfs.GroveWFS(2)
29
+
30
+
31
+## Exit handlers ##
32
+# This stops python from printing a stacktrace when you hit control-C
33
+def SIGINTHandler(signum, frame):
34
+	raise SystemExit
35
+
36
+# This function lets you run code on exit,
37
+# including functions from myWaterFlow
38
+def exitHandler():
39
+	myWaterFlow.stopFlowCounter()
40
+	print "Exiting"
41
+	sys.exit(0)
42
+
43
+# Register exit handlers
44
+atexit.register(exitHandler)
45
+signal.signal(signal.SIGINT, SIGINTHandler)
46
+
47
+
48
+# set the flow counter to 0 and start counting
49
+myWaterFlow.clearFlowCounter()
50
+myWaterFlow.startFlowCounter()
51
+
52
+while (1):
53
+	# we grab these (millis and flowCount) just for display
54
+	# purposes in this example
55
+	millis = myWaterFlow.getMillis()
56
+	flowCount = myWaterFlow.flowCounter()
57
+
58
+	fr = myWaterFlow.flowRate()
59
+
60
+	# output milliseconds passed, flow count, and computed flow rate
61
+	outputStr = "Millis: {0} Flow Count: {1} Flow Rate: {2} LPM".format(
62
+	millis, flowCount, fr)
63
+	print outputStr
64
+	time.sleep(2)

+ 5
- 0
src/grovewfs/CMakeLists.txt Näytä tiedosto

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

+ 121
- 0
src/grovewfs/grovewfs.cxx Näytä tiedosto

@@ -0,0 +1,121 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2015 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+#include <iostream>
26
+
27
+#include "grovewfs.h"
28
+
29
+using namespace upm;
30
+using namespace std;
31
+
32
+GroveWFS::GroveWFS(int pin)
33
+{
34
+  if ( !(m_gpio = mraa_gpio_init(pin)) )
35
+    {
36
+      cerr << __FUNCTION__ << ": mraa_gpio_init() failed" << endl;
37
+      return;
38
+    }
39
+
40
+  mraa_gpio_dir(m_gpio, MRAA_GPIO_IN);
41
+
42
+  initClock();
43
+  m_flowCounter = 0;
44
+  m_isrInstalled = false;
45
+}
46
+
47
+GroveWFS::~GroveWFS()
48
+{
49
+  if (m_isrInstalled)
50
+    stopFlowCounter();
51
+
52
+  mraa_gpio_close(m_gpio);
53
+}
54
+
55
+void GroveWFS::initClock()
56
+{
57
+  gettimeofday(&m_startTime, NULL);
58
+}
59
+
60
+uint32_t GroveWFS::getMillis()
61
+{
62
+  struct timeval elapsed, now;
63
+  uint32_t elapse;
64
+
65
+  // get current time
66
+  gettimeofday(&now, NULL);
67
+
68
+  // compute the delta since m_startTime
69
+  if( (elapsed.tv_usec = now.tv_usec - m_startTime.tv_usec) < 0 ) 
70
+    {
71
+      elapsed.tv_usec += 1000000;
72
+      elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec - 1;
73
+    } 
74
+  else 
75
+    {
76
+      elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec;
77
+    }
78
+
79
+  elapse = (uint32_t)((elapsed.tv_sec * 1000) + (elapsed.tv_usec / 1000));
80
+
81
+  // never return 0
82
+  if (elapse == 0)
83
+    elapse = 1;
84
+
85
+  return elapse;
86
+}
87
+
88
+void GroveWFS::startFlowCounter()
89
+{
90
+  initClock();
91
+  // install our interrupt handler
92
+  mraa_gpio_isr(m_gpio, MRAA_GPIO_EDGE_RISING, 
93
+                &flowISR, this);
94
+
95
+  m_isrInstalled = true;
96
+}
97
+
98
+void GroveWFS::stopFlowCounter()
99
+{
100
+  // remove the interrupt handler
101
+  mraa_gpio_isr_exit(m_gpio);
102
+
103
+  m_isrInstalled = false;
104
+}
105
+
106
+void GroveWFS::flowISR(void *ctx)
107
+{
108
+  upm::GroveWFS *This = (upm::GroveWFS *)ctx;
109
+  This->m_flowCounter++;
110
+}
111
+
112
+float GroveWFS::flowRate()
113
+{
114
+  uint32_t millis = getMillis();
115
+  uint32_t flow = flowCounter();
116
+  
117
+  // 7.5 comes from the seeedstudio page, see the confusing datasheet :)
118
+  float flowRate = (float(flow) * 7.5) / ((float(millis) / 1000.0) * 60.0);
119
+
120
+  return flowRate;
121
+}

+ 137
- 0
src/grovewfs/grovewfs.h Näytä tiedosto

@@ -0,0 +1,137 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2015 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+#pragma once
25
+
26
+#include <string>
27
+#include <stdint.h>
28
+#include <sys/time.h>
29
+#include <mraa/gpio.h>
30
+
31
+namespace upm {
32
+
33
+  /**
34
+   * @brief UPM module for the Grove Water Flow sensor
35
+   * @defgroup grovewfs libupm-grovewfs
36
+   * @ingroup seeed gpio liquid
37
+   */
38
+
39
+  /**
40
+   * @sensor grovewfs
41
+   * @comname Grove Water Flow Sensor
42
+   * @type liquid
43
+   * @man seeed
44
+   * @con gpio
45
+
46
+   * @brief C++ API for the Grove Water Flow Sensor
47
+   *
48
+   * This sensor is used to measure water flow, in LPM (Liters Per
49
+   * Minute).  It incorporates a Hall Effect Sensor.  The UPM module
50
+   * defines an interrupt routine to be triggered on each low pulse,
51
+   * keeping count.  This device requires a 10K pullup resistor for
52
+   * the signal line (yellow wire).  There is a schematic diagram on
53
+   * the seeedstudio site (3/2015):
54
+   * http://www.seeedstudio.com/wiki/index.php?title=G1/2_Water_Flow_sensor
55
+   *
56
+   * However, be careful in wiring this up - the schematic appears to
57
+   * have a bug in it: the lower left connection of the signal line
58
+   * (yellow) to Vcc (red) should not be there.  The sensor may work
59
+   * with this connection, but probably not for very long.
60
+   *
61
+   *
62
+   * @snippet grovewfs.cxx Interesting
63
+   */
64
+  class GroveWFS {
65
+  public:
66
+    /**
67
+     * GroveWFS constructor
68
+     *
69
+     * @param pin digital pin to use
70
+     */
71
+    GroveWFS(int pin);
72
+    /**
73
+     * GroveWFS Destructor
74
+     */
75
+    ~GroveWFS();
76
+    /**
77
+     * Return the number of milliseconds elapsed since initClock()
78
+     * was last called.
79
+     *
80
+     * @return elapsed milliseconds
81
+     */
82
+    uint32_t getMillis();
83
+
84
+    /**
85
+     * Reset the Clock
86
+     *
87
+     */
88
+    void initClock();
89
+
90
+    /**
91
+     * Reset the flow counter to 0.  The flow Counter should be
92
+     * stopped via stopFlowCounter() prior to calling this function.
93
+     *
94
+     */
95
+    void clearFlowCounter() { m_flowCounter = 0; };
96
+
97
+    /**
98
+     * Start the flow counter
99
+     *
100
+     */
101
+    void startFlowCounter();
102
+
103
+    /**
104
+     * Stop the flow counter
105
+     *
106
+     */
107
+    void stopFlowCounter();
108
+
109
+    /**
110
+     * Get the flow Counter
111
+     *
112
+     * @return the flow counter
113
+     */
114
+    uint32_t flowCounter() { return m_flowCounter; };
115
+
116
+    /**
117
+     * Flow Interrupt Service Routine
118
+     *
119
+     */
120
+    static void flowISR(void *ctx);
121
+
122
+    /**
123
+     * Compute the flow rate in liters per minute (LPM)
124
+     *
125
+     * @return the computed flow rate
126
+     */
127
+    float flowRate();
128
+
129
+  private:
130
+    volatile uint32_t m_flowCounter;
131
+    struct timeval m_startTime;
132
+    mraa_gpio_context m_gpio;
133
+    bool m_isrInstalled;
134
+  };
135
+}
136
+
137
+

+ 8
- 0
src/grovewfs/jsupm_grovewfs.i Näytä tiedosto

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

+ 9
- 0
src/grovewfs/pyupm_grovewfs.i Näytä tiedosto

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