Explorar el Código

mg811: Initial implementation

This driver was developed on a DFRobot CO2 sensor based on the MG811:
http://www.dfrobot.com/index.php?route=product/product&product_id=1023

It seems to *require* very precise calibration at 400ppm and 1000ppm
to be accurate.  It also gets pretty hot (due to the heater) and
consumes significant current.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: sisinty sasmita patra <sisinty.s.patra@intel.com>
Jon Trulson hace 9 años
padre
commit
60cfe88e37

+ 3
- 0
examples/c++/CMakeLists.txt Ver fichero

@@ -134,6 +134,7 @@ add_executable (ak8975-example ak8975.cxx)
134 134
 add_executable (lsm9ds0-example lsm9ds0.cxx)
135 135
 add_executable (eboled-example eboled.cxx)
136 136
 add_executable (hyld9767-example hyld9767.cxx)
137
+add_executable (mg811-example mg811.cxx)
137 138
 
138 139
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
139 140
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -241,6 +242,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/hp20x)
241 242
 include_directories (${PROJECT_SOURCE_DIR}/src/pn532)
242 243
 include_directories (${PROJECT_SOURCE_DIR}/src/lsm9ds0)
243 244
 include_directories (${PROJECT_SOURCE_DIR}/src/hyld9767)
245
+include_directories (${PROJECT_SOURCE_DIR}/src/mg811)
244 246
 
245 247
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
246 248
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -376,3 +378,4 @@ target_link_libraries (ak8975-example mpu9150 ${CMAKE_THREAD_LIBS_INIT})
376 378
 target_link_libraries (lsm9ds0-example lsm9ds0 ${CMAKE_THREAD_LIBS_INIT})
377 379
 target_link_libraries (eboled-example i2clcd ${CMAKE_THREAD_LIBS_INIT})
378 380
 target_link_libraries (hyld9767-example hyld9767 ${CMAKE_THREAD_LIBS_INIT})
381
+target_link_libraries (mg811-example mg811 ${CMAKE_THREAD_LIBS_INIT})

+ 68
- 0
examples/c++/mg811.cxx Ver fichero

@@ -0,0 +1,68 @@
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 <signal.h>
28
+#include "mg811.h"
29
+
30
+using namespace std;
31
+
32
+bool shouldRun = true;
33
+
34
+#define MG811_AREF   5.0
35
+
36
+void sig_handler(int signo)
37
+{
38
+  if (signo == SIGINT)
39
+    shouldRun = false;
40
+}
41
+
42
+int main()
43
+{
44
+  signal(SIGINT, sig_handler);
45
+
46
+//! [Interesting]
47
+
48
+  // Instantiate an MG811 on analog pin A0, and digital pin D2 with an
49
+  // analog reference voltage of MG811_AREF (5.0)
50
+  upm::MG811 *sensor = new upm::MG811(0, 2, MG811_AREF);
51
+  
52
+  // Every tenth of a second, sample the sensor and output it's
53
+  // detected CO2 concentration in parts per million (ppm)
54
+
55
+  while (shouldRun)
56
+    {
57
+      cout << "CO2 concentration in PPM: " << sensor->ppm() << endl;
58
+      
59
+      usleep(100000);
60
+    }
61
+
62
+//! [Interesting]
63
+
64
+  cout << "Exiting" << endl;
65
+
66
+  delete sensor;
67
+  return 0;
68
+}

+ 53
- 0
examples/javascript/mg811.js Ver fichero

@@ -0,0 +1,53 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+
4
+/*
5
+ * Author: Jon Trulson <jtrulson@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
+
29
+var sensorObj = require('jsupm_mg811');
30
+
31
+// Instantiate an MG811 on analog pin A0, and digital pin D2 with an
32
+// analog reference voltage of MG811_AREF (5.0)
33
+
34
+var sensor = new sensorObj.MG811(0, 2, 5.0);
35
+
36
+// Every tenth of a second, sample the sensor and output it's
37
+// detected CO2 concentration in parts per million (ppm)
38
+
39
+setInterval(function()
40
+{
41
+    console.log("CO2 concentration in PPM: " + sensor.ppm());
42
+}, 100);
43
+
44
+// exit on ^C
45
+process.on('SIGINT', function()
46
+{
47
+    sensor = null;
48
+    sensorObj.cleanUp();
49
+    sensorObj = null;
50
+    console.log("Exiting.");
51
+    process.exit(0);
52
+});
53
+

+ 51
- 0
examples/python/mg811.py Ver fichero

@@ -0,0 +1,51 @@
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_mg811 as sensorObj
26
+
27
+# Instantiate an MG811 on analog pin A0, and digital pin D2 with an
28
+# analog reference voltage of MG811_AREF (5.0)
29
+
30
+sensor = sensorObj.MG811(0, 2, 5.0)
31
+
32
+## Exit handlers ##
33
+# This function stops python from printing a stacktrace when you hit control-C
34
+def SIGINTHandler(signum, frame):
35
+	raise SystemExit
36
+
37
+# This function lets you run code on exit
38
+def exitHandler():
39
+	print "Exiting"
40
+	sys.exit(0)
41
+
42
+# Register exit handlers
43
+atexit.register(exitHandler)
44
+signal.signal(signal.SIGINT, SIGINTHandler)
45
+
46
+# Every tenth of a second, sample the sensor and output it's
47
+# detected CO2 concentration in parts per million (ppm)
48
+
49
+while (1):
50
+        print "CO2 concentration in PPM: ", sensor.ppm()
51
+	time.sleep(.1)

+ 5
- 0
src/mg811/CMakeLists.txt Ver fichero

@@ -0,0 +1,5 @@
1
+set (libname "mg811")
2
+set (libdescription "upm DFRobot CO2 sensor")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 8
- 0
src/mg811/javaupm_mg811.i Ver fichero

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

+ 8
- 0
src/mg811/jsupm_mg811.i Ver fichero

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

+ 94
- 0
src/mg811/mg811.cxx Ver fichero

@@ -0,0 +1,94 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2015 Intel Corporation.
4
+ *
5
+ * Thanks to public domain code by Martin Liddament for some useful clues!
6
+ * http://www.veetech.org.uk/CO2_Monitor_Sketch_2_Operation.txt
7
+ * and sandbox electronics (http://sandboxelectronics.com/?p=147).
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining
10
+ * a copy of this software and associated documentation files (the
11
+ * "Software"), to deal in the Software without restriction, including
12
+ * without limitation the rights to use, copy, modify, merge, publish,
13
+ * distribute, sublicense, and/or sell copies of the Software, and to
14
+ * permit persons to whom the Software is furnished to do so, subject to
15
+ * the following conditions:
16
+ *
17
+ * The above copyright notice and this permission notice shall be
18
+ * included in all copies or substantial portions of the Software.
19
+ *
20
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
+ */
28
+
29
+#include <iostream>
30
+#include <math.h>
31
+#include "mg811.h"
32
+
33
+using namespace std;
34
+using namespace upm;
35
+
36
+// voltage gain of the DC amplifier
37
+static const float dcGain = 8.5;
38
+
39
+MG811::MG811(int pin, int dpin, float aref) :
40
+  m_aio(pin), m_gpio(dpin)
41
+{
42
+  m_aRes = m_aio.getBit();
43
+  m_aref = aref;
44
+
45
+  m_gpio.dir(mraa::DIR_IN);
46
+
47
+  // these are just placeholder values (coarsely measured during
48
+  // development), you should determine the appropriate values (in
49
+  // volts) for your environment at the specified concentrations.  Use
50
+  // the getReferenceVoltage() method to get these values at 400ppm
51
+  // and 1000ppm respectively.  Good luck.
52
+  setCalibration(0.5514, 0.370);
53
+}
54
+
55
+MG811::~MG811()
56
+{
57
+}
58
+
59
+float MG811::volts()
60
+{
61
+  int val = m_aio.read();
62
+
63
+  return(float(val) * (m_aref / float(1 << m_aRes)));
64
+}
65
+
66
+void MG811::setCalibration(float ppm400, float ppm1000)
67
+{
68
+  m_zeroPointValue = ppm400;
69
+  m_reactionValue = ppm1000;
70
+}
71
+
72
+float MG811::getReferenceVoltage()
73
+{
74
+  return (volts() / dcGain);
75
+}
76
+
77
+float MG811::ppm()
78
+{
79
+  static const float log400 = log10f(400);
80
+  static const float log1000 = log10f(1000);
81
+
82
+  float val = volts();
83
+
84
+  if ((val / dcGain) >= m_zeroPointValue)
85
+    return 0.0;
86
+  else
87
+    return powf(10.0, ((val/dcGain)-m_zeroPointValue) / 
88
+                      (m_reactionValue / (log400-log1000))+log400);
89
+}
90
+
91
+bool MG811::thresholdReached()
92
+{
93
+  return (m_gpio.read() ? true : false);
94
+}

+ 146
- 0
src/mg811/mg811.h Ver fichero

@@ -0,0 +1,146 @@
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 <mraa/aio.hpp>
29
+#include <mraa/gpio.hpp>
30
+
31
+namespace upm {
32
+  /**
33
+   * @brief DFRobot CO2 Sensor
34
+   * @defgroup mg811 libupm-mg811
35
+   * @ingroup dfrobot analog gas
36
+   */
37
+
38
+  /**
39
+   * @library mg811
40
+   * @sensor mg811
41
+   * @comname DFRobot CO2 Sensor
42
+   * @altname MG811
43
+   * @type gas
44
+   * @man dfrobot
45
+   * @web http://www.dfrobot.com/index.php?route=product/product&product_id=1023
46
+   * @con analog
47
+   *
48
+   * @brief API for the DFRobot CO2 Sensor
49
+   *
50
+   * This sensor returns an an analog voltage that falls as the
51
+   * concentration of CO2 increases.  It contains a heater that must
52
+   * be allowed to 'warm' up the sensor before measurements are stable
53
+   * (hours to days is the recommendation).  It requires that the MCU
54
+   * be powered from an external power supply (not USB) at 5v, since
55
+   * the heater will consume significant current.
56
+   *
57
+   * The sensor should be allowed to 'heat' up for some time before
58
+   * beginning use, typically a couple of hours minimum.  It also
59
+   * needs fairly precise calibration at 400ppm and 1000ppm to return
60
+   * meaningful results.
61
+   *
62
+   * The sensor also incorporates a potentiometer that can be adjusted
63
+   * to specific threshold.  Once that threshold is reached, an LED
64
+   * on the sensor will light, and the digital pin will be driven
65
+   * high.
66
+   *
67
+   * @snippet mg811.cxx Interesting
68
+   */
69
+
70
+  class MG811 {
71
+  public:
72
+
73
+    /**
74
+     * MG811 constructor
75
+     *
76
+     * @param pin Analog pin to use
77
+     * @param dpin Digital pin that indicates threshold
78
+     * @param aref Analog reference voltage; default is 5.0 V
79
+     */
80
+    MG811(int pin, int dpin, float aref=5.0);
81
+
82
+    /**
83
+     * MG811 destructor
84
+     */
85
+    ~MG811();
86
+
87
+    /**
88
+     * Return a cumputed reference voltage to be used in calibration.
89
+     * @return Computed reference voltage
90
+     */
91
+    float getReferenceVoltage();
92
+
93
+    /**
94
+     * Set calibration parameters.  You should measure the reference
95
+     * voltage you get when at CO2 concentrations of 400ppm (ambient)
96
+     * and 1000ppm using the getReferenceVoltage() method.  Then
97
+     * specify those voltages here for more accurate results.
98
+     *
99
+     * @param ppm400 The measured reference voltage at 400 ppm
100
+     * @param ppm40000 The measured reference voltage at 1000 ppm
101
+     */
102
+    void setCalibration(float ppm400, float ppm1000);
103
+
104
+    /**
105
+     * Returns the voltage detected on the analog pin
106
+     *
107
+     * @return The detected voltage
108
+     */
109
+    float volts();
110
+
111
+    /**
112
+     * Returns the computed CO2 concentration in ppm (Parts Per
113
+     * Million).  This method will return 0.0 if the reference voltage
114
+     * is greater than the ppm400 value.  Essentially, ppm values
115
+     * below 400 will be reported as 0.
116
+     *
117
+     * @return The computed CO2 concentration in ppm
118
+     */
119
+    float ppm();
120
+
121
+    /**
122
+     * Read the digital pin and return true if the set threshold has
123
+     * been reached or exceeded.  This threshold is set by adjusting
124
+     * the potentiometer on the sensor.
125
+     *
126
+     * @return true if the threshold has been reached, false otherwise
127
+     */
128
+    bool thresholdReached();
129
+
130
+  protected:
131
+    mraa::Aio m_aio;
132
+    mraa::Gpio m_gpio;
133
+
134
+    // calibration values
135
+    float m_zeroPointValue;
136
+    float m_reactionValue;
137
+
138
+    // ADC resolution
139
+    int m_aRes;
140
+
141
+  private:
142
+    float m_aref;
143
+  };
144
+}
145
+
146
+

+ 9
- 0
src/mg811/pyupm_mg811.i Ver fichero

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