Browse Source

mq7: Initial implementation

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Jon Trulson 9 years ago
parent
commit
d7bc084565

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

@@ -42,6 +42,7 @@ add_executable (ecs1030-example ecs1030.cxx)
42 42
 add_executable (mq2-example mq2.cxx)
43 43
 add_executable (mq3-example mq3.cxx)
44 44
 add_executable (mq5-example mq5.cxx)
45
+add_executable (mq7-example mq7.cxx)
45 46
 add_executable (mq9-example mq9.cxx)
46 47
 add_executable (tp401-example tp401.cxx)
47 48
 add_executable (tcs3414cs-example tcs3414cs.cxx)
@@ -297,6 +298,7 @@ target_link_libraries (ecs1030-example ecs1030 ${CMAKE_THREAD_LIBS_INIT})
297 298
 target_link_libraries (mq2-example gas ${CMAKE_THREAD_LIBS_INIT})
298 299
 target_link_libraries (mq3-example gas ${CMAKE_THREAD_LIBS_INIT})
299 300
 target_link_libraries (mq5-example gas ${CMAKE_THREAD_LIBS_INIT})
301
+target_link_libraries (mq7-example gas ${CMAKE_THREAD_LIBS_INIT})
300 302
 target_link_libraries (mq9-example gas ${CMAKE_THREAD_LIBS_INIT})
301 303
 target_link_libraries (tp401-example gas ${CMAKE_THREAD_LIBS_INIT})
302 304
 target_link_libraries (tcs3414cs-example tcs3414cs ${CMAKE_THREAD_LIBS_INIT})

+ 81
- 0
examples/c++/mq7.cxx View File

@@ -0,0 +1,81 @@
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 <unistd.h>
26
+#include <iostream>
27
+#include "mq7.h"
28
+#include <signal.h>
29
+#include <stdlib.h>
30
+#include <sys/time.h>
31
+
32
+bool shouldRun = true;
33
+
34
+using namespace std;
35
+
36
+void
37
+sig_handler(int signo)
38
+{
39
+  if (signo == SIGINT) 
40
+    {
41
+      shouldRun = false;
42
+    }
43
+}
44
+
45
+//! [Interesting]
46
+int main(int argc, char **argv)
47
+{
48
+  // Attach gas sensor to Analog A0
49
+  upm::MQ7 *sensor = new upm::MQ7(0);
50
+  signal(SIGINT, sig_handler);
51
+  
52
+  uint16_t buffer [128];
53
+
54
+  thresholdContext ctx;
55
+  ctx.averageReading = 0;
56
+  ctx.runningAverage = 0;
57
+  ctx.averagedOver   = 2;
58
+  
59
+  // Infinite loop, ends when script is cancelled
60
+  // Repeatedly, take a sample every 2 microseconds;
61
+  // find the average of 128 samples; and
62
+  // print a running graph of asteriskss as averages
63
+  while (shouldRun) 
64
+    {
65
+      int len = sensor->getSampledWindow (2, 128, buffer);
66
+      if (len) {
67
+        int thresh = sensor->findThreshold (&ctx, 30, buffer, len);
68
+        sensor->printGraph(&ctx, 5);
69
+        if (thresh) {
70
+          // do something ....
71
+        }
72
+      }
73
+    }
74
+  
75
+  cout << "Exiting" << endl;
76
+  
77
+  delete sensor;
78
+  
79
+  return 0;
80
+}
81
+//! [Interesting]

+ 60
- 0
examples/javascript/mq7.js View File

@@ -0,0 +1,60 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+/*
4
+ * Author: Jon Trulson <jtrulson@ics.com>
5
+ * Copyright (c) 2015 Intel Corporation.
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 upmGAS = require("jsupm_gas");
28
+
29
+// Attach gas sensor to Analog A0
30
+var sensor = new upmGAS.MQ7(0);
31
+
32
+var threshContext = new upmGAS.thresholdContext;
33
+threshContext.averageReading = 0;
34
+threshContext.runningAverage = 0;
35
+threshContext.averagedOver = 2;
36
+
37
+// Infinite loop, ends when script is cancelled
38
+// Repeatedly, take a sample every 2 microseconds;
39
+// find the average of 128 samples; and
40
+// print a running graph of asteriskss as averages
41
+
42
+while(1)
43
+{
44
+    var buffer = new upmGAS.uint16Array(128);
45
+    var len = sensor.getSampledWindow(2, 128, buffer);
46
+    if (len)
47
+    {
48
+        var thresh = sensor.findThreshold(threshContext, 30, buffer, len);
49
+        sensor.printGraph(threshContext, 5);
50
+        //if (thresh)
51
+        //    console.log("Threshold is " + thresh);
52
+    }
53
+}
54
+
55
+// Print message when exiting
56
+process.on('SIGINT', function()
57
+{
58
+	console.log("Exiting...");
59
+	process.exit(0);
60
+});

+ 62
- 0
examples/python/mq7.py View File

@@ -0,0 +1,62 @@
1
+#!/usr/bin/python
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
+import time, sys, signal, atexit
25
+import pyupm_gas as upmGAS
26
+
27
+# Attach gas sensor to Analog A0
28
+sensor = upmGAS.MQ7(0)
29
+
30
+
31
+## Exit handlers ##
32
+# This function 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, including functions from sensor
37
+def exitHandler():
38
+	print "Exiting"
39
+	sys.exit(0)
40
+
41
+# Register exit handlers
42
+atexit.register(exitHandler)
43
+signal.signal(signal.SIGINT, SIGINTHandler)
44
+
45
+
46
+threshContext = upmGAS.thresholdContext()
47
+threshContext.averageReading = 0
48
+threshContext.runningAverage = 0
49
+threshContext.averagedOver = 2
50
+
51
+# Infinite loop, ends when script is cancelled
52
+# Repeatedly, take a sample every 2 microseconds;
53
+# find the average of 128 samples; and
54
+# print a running graph of asteriskss as averages
55
+mybuffer = upmGAS.uint16Array(128)
56
+while(1):
57
+	samplelen = sensor.getSampledWindow(2, 128, mybuffer)
58
+	if samplelen:
59
+		thresh = sensor.findThreshold(threshContext, 30, mybuffer, samplelen)
60
+		sensor.printGraph(threshContext, 5)
61
+#		if(thresh):
62
+#			print "Threshold is ", thresh

+ 2
- 2
src/gas/CMakeLists.txt View File

@@ -1,5 +1,5 @@
1 1
 set (libname "gas")
2 2
 set (libdescription "Gas sensors")
3
-set (module_src ${libname}.cxx mq2.cxx mq3.cxx mq5.cxx mq9.cxx tp401.cxx)
4
-set (module_h ${libname}.h mq2.h mq3.h mq5.h mq9.h tp401.h)
3
+set (module_src ${libname}.cxx mq2.cxx mq3.cxx mq5.cxx mq7.cxx mq9.cxx tp401.cxx)
4
+set (module_h ${libname}.h mq2.h mq3.h mq5.h mq7.h mq9.h tp401.h)
5 5
 upm_module_init()

+ 2
- 0
src/gas/javaupm_gas.i View File

@@ -7,6 +7,7 @@
7 7
     #include "mq2.h"
8 8
     #include "mq3.h"
9 9
     #include "mq5.h"
10
+    #include "mq7.h"
10 11
     #include "mq9.h"
11 12
     #include "tp401.h"
12 13
 %}
@@ -45,5 +46,6 @@
45 46
 %include "mq2.h"
46 47
 %include "mq3.h"
47 48
 %include "mq5.h"
49
+%include "mq7.h"
48 50
 %include "mq9.h"
49 51
 %include "tp401.h"

+ 5
- 0
src/gas/jsupm_gas.i View File

@@ -28,6 +28,11 @@
28 28
     #include "mq5.h"
29 29
 %}
30 30
 
31
+%include "mq7.h"
32
+%{
33
+    #include "mq7.h"
34
+%}
35
+
31 36
 %include "mq9.h"
32 37
 %{
33 38
     #include "mq9.h"

+ 35
- 0
src/gas/mq7.cxx View File

@@ -0,0 +1,35 @@
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 "mq7.h"
26
+
27
+using namespace upm;
28
+
29
+MQ7::MQ7 (int gasPin) : Gas (gasPin)
30
+{
31
+}
32
+
33
+MQ7::~MQ7 ()
34
+{
35
+}

+ 75
- 0
src/gas/mq7.h View File

@@ -0,0 +1,75 @@
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 <iostream>
27
+#include <string>
28
+#include "gas.h"
29
+
30
+namespace upm {
31
+  /**
32
+   * @library gas
33
+   * @sensor mq7
34
+   * @comname DFRobot MQ7 CO Gas Sensor
35
+   * @type gaseous
36
+   * @man dfrobot
37
+   * @con analog
38
+   *
39
+   * @brief API for the DFRobot MQ7 CO Gas Sensor
40
+   *
41
+   * The Grove MQ7 Gas Sensor module is useful for detecting Carbon
42
+   * Monoxide (CO) concentrations in the air.  It has a detection
43
+   * range of 20-2000 ppm.
44
+   *
45
+   * For optimum use, it requires calibration after a pre-heat time of
46
+   * 48 hours.  See the datasheet for details.
47
+   *
48
+   * @image html mq3-9.jpeg
49
+   * @snippet mq7.cxx Interesting
50
+   */
51
+    class MQ7 : public Gas {
52
+        public:
53
+            /**
54
+             * MQ7 constructor
55
+             *
56
+             * @param gasPin Analog pin where the sensor is connected
57
+             */
58
+            MQ7 (int gasPin);
59
+
60
+            /**
61
+             * MQ7 destructor
62
+             */
63
+            ~MQ7 ();
64
+
65
+            /**
66
+             * Returns the name of the sensor
67
+             */
68
+            std::string name()
69
+            {
70
+                return m_name;
71
+            }
72
+        private:
73
+            std::string m_name;
74
+    };
75
+}

+ 5
- 0
src/gas/pyupm_gas.i View File

@@ -30,6 +30,11 @@
30 30
     #include "mq5.h"
31 31
 %}
32 32
 
33
+%include "mq7.h"
34
+%{
35
+    #include "mq7.h"
36
+%}
37
+
33 38
 %include "mq9.h"
34 39
 %{
35 40
     #include "mq9.h"