Browse Source

mq9:: added new sensor

Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
Kiveisha Yevgeniy 10 years ago
parent
commit
4eb6c4e19d
9 changed files with 405 additions and 0 deletions
  1. 3
    0
      examples/CMakeLists.txt
  2. 74
    0
      examples/mq9-example.cxx
  3. 5
    0
      src/gas/CMakeLists.txt
  4. 105
    0
      src/gas/gas.cxx
  5. 104
    0
      src/gas/gas.h
  6. 9
    0
      src/gas/jsupm_gas.i
  7. 33
    0
      src/gas/mq9.cxx
  8. 60
    0
      src/gas/mq9.h
  9. 12
    0
      src/gas/pyupm_gas.i

+ 3
- 0
examples/CMakeLists.txt View File

@@ -29,6 +29,7 @@ add_executable (nrf8001-helloworld-example nrf8001_helloworld.cxx)
29 29
 add_executable (lpd8806-example lpd8806-example.cxx)
30 30
 add_executable (mlx90614-example mlx90614-example.cxx)
31 31
 add_executable (ecs1030-example ecs1030-example.cxx)
32
+add_executable (mq9-example mq9-example.cxx)
32 33
 
33 34
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
34 35
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -55,6 +56,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/nrf8001)
55 56
 include_directories (${PROJECT_SOURCE_DIR}/src/lpd8806)
56 57
 include_directories (${PROJECT_SOURCE_DIR}/src/mlx90614)
57 58
 include_directories (${PROJECT_SOURCE_DIR}/src/ecs1030)
59
+include_directories (${PROJECT_SOURCE_DIR}/src/gas)
58 60
 
59 61
 target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT})
60 62
 target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT})
@@ -87,3 +89,4 @@ target_link_libraries (nrf8001-helloworld-example nrf8001 ${CMAKE_THREAD_LIBS_IN
87 89
 target_link_libraries (lpd8806-example lpd8806 ${CMAKE_THREAD_LIBS_INIT})
88 90
 target_link_libraries (mlx90614-example mlx90614 ${CMAKE_THREAD_LIBS_INIT})
89 91
 target_link_libraries (ecs1030-example ecs1030 ${CMAKE_THREAD_LIBS_INIT})
92
+target_link_libraries (mq9-example gas ${CMAKE_THREAD_LIBS_INIT})

+ 74
- 0
examples/mq9-example.cxx View File

@@ -0,0 +1,74 @@
1
+/*
2
+ * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.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 "mq9.h"
28
+#include <signal.h>
29
+#include <stdlib.h>
30
+#include <sys/time.h>
31
+
32
+int is_running = 0;
33
+uint16_t buffer [128];
34
+upm::MQ9 *sensor = NULL;
35
+
36
+void
37
+sig_handler(int signo)
38
+{
39
+    printf("got signal\n");
40
+    if (signo == SIGINT) {
41
+        is_running = 1;
42
+    }
43
+}
44
+
45
+//! [Interesting]
46
+int
47
+main(int argc, char **argv)
48
+{
49
+    sensor = new upm::MQ9(0);
50
+    signal(SIGINT, sig_handler);
51
+
52
+    thresholdContext ctx;
53
+    ctx.averageReading = 0;
54
+    ctx.runningAverage = 0;
55
+    ctx.averagedOver   = 2;
56
+
57
+    while (!is_running) {
58
+        int len = sensor->getSampledWindow (2, 128, buffer);
59
+        if (len) {
60
+            int thresh = sensor->findThreshold (&ctx, 30, buffer, len);
61
+            sensor->printGraph(&ctx, 5);
62
+            if (thresh) {
63
+                // do something ....
64
+            }
65
+        }
66
+    }
67
+
68
+    std::cout << "exiting application" << std::endl;
69
+
70
+    delete sensor;
71
+
72
+    return 0;
73
+}
74
+//! [Interesting]

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

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

+ 105
- 0
src/gas/gas.cxx View File

@@ -0,0 +1,105 @@
1
+/*
2
+ * Author: Brendan Le Foll <brendan.le.foll@intel.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 <iostream>
26
+#include <unistd.h>
27
+#include <stdlib.h>
28
+#include <functional>
29
+#include <string.h>
30
+#include "gas.h"
31
+
32
+using namespace upm;
33
+
34
+Gas::Gas(int gasPin) {
35
+    // initialise analog gas input
36
+    m_gasCtx = mraa_aio_init(gasPin);
37
+}
38
+
39
+Gas::~Gas() {
40
+    // close analog input
41
+    mraa_result_t error;
42
+    error = mraa_aio_close(m_gasCtx);
43
+    if (error != MRAA_SUCCESS) {
44
+        mraa_result_print(error);
45
+    }
46
+}
47
+
48
+int
49
+Gas::getSampledWindow (unsigned int freqMS, unsigned int numberOfSamples,
50
+                            uint16_t * buffer) {
51
+    int sampleIdx = 0;
52
+
53
+    // must have freq
54
+    if (!freqMS) {
55
+        return 0;
56
+    }
57
+
58
+    // too much samples
59
+    if (numberOfSamples > 0xFFFFFF) {
60
+        return 0;
61
+    }
62
+
63
+    while (sampleIdx < numberOfSamples) {
64
+        buffer[sampleIdx++] = mraa_aio_read (m_gasCtx);
65
+        usleep(freqMS * 1000);
66
+    }
67
+
68
+    return sampleIdx;
69
+}
70
+
71
+int
72
+Gas::findThreshold (thresholdContext* ctx, unsigned int threshold,
73
+                                uint16_t * buffer, unsigned int len) {
74
+    long sum = 0;
75
+    for (unsigned int i = 0; i < len; i++) {
76
+        sum += buffer[i];
77
+    }
78
+
79
+    ctx->averageReading = sum / len;
80
+    ctx->runningAverage = (((ctx->averagedOver-1) * ctx->runningAverage) + ctx->averageReading) / ctx->averagedOver;
81
+
82
+    if (ctx->runningAverage > threshold) {
83
+        return ctx->runningAverage;
84
+    } else {
85
+        return 0;
86
+    }
87
+}
88
+
89
+int
90
+Gas::getSampledData (thresholdContext* ctx) {
91
+    return ctx->runningAverage;
92
+}
93
+
94
+int
95
+Gas::getSample (thresholdContext* ctx) {
96
+    return mraa_aio_read (m_gasCtx);
97
+}
98
+
99
+void
100
+Gas::printGraph (thresholdContext* ctx, uint8_t resolution) {
101
+    std::cout << "(" << ctx->runningAverage << ") | ";
102
+    for (int i = 0; i < ctx->runningAverage / resolution; i++)
103
+        std::cout << "*";
104
+    std::cout << std::endl;
105
+}

+ 104
- 0
src/gas/gas.h View File

@@ -0,0 +1,104 @@
1
+/*
2
+ * Author: Brendan Le Foll <brendan.le.foll@intel.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
+#pragma once
25
+
26
+#include <string>
27
+#include <mraa/gpio.h>
28
+#include <mraa/aio.h>
29
+
30
+struct thresholdContext {
31
+    long averageReading;
32
+    long runningAverage;
33
+    int averagedOver;
34
+};
35
+
36
+namespace upm {
37
+
38
+/**
39
+ * @brief C++ API for Gas sensors
40
+ *
41
+ * This file defines the Gas Analog sensors
42
+ */
43
+class Gas {
44
+    public:
45
+        /**
46
+         * Instanciates a Gas object
47
+         *
48
+         * @param gasPin pin where gas is connected
49
+         */
50
+        Gas(int gasPin);
51
+
52
+        /**
53
+         * Gas object destructor
54
+         */
55
+        ~Gas();
56
+
57
+        /**
58
+         * Get samples from gas sensor according to provided window and
59
+         * number of samples
60
+         *
61
+         * @param freqMS time between each sample (in microseconds)
62
+         * @param numberOfSamples number of sample to sample for this window
63
+         * @param buffer bufer with sampled data
64
+         */
65
+        virtual int getSampledWindow (unsigned int freqMS, unsigned int numberOfSamples, uint16_t * buffer);
66
+
67
+        /**
68
+         * Given sampled buffer this method will return TRUE/FALSE if threshold
69
+         * was reached
70
+         *
71
+         * @param ctx threshold context
72
+         * @param threshold sample threshold
73
+         * @param buffer buffer with samples
74
+         * @param len bufer len
75
+         */
76
+        virtual int findThreshold (thresholdContext* ctx, unsigned int threshold, uint16_t * buffer, unsigned int len);
77
+
78
+        /**
79
+         * Return avarage data for the sampled window
80
+         *
81
+         * @param ctx threshold context
82
+         */
83
+        virtual int getSampledData (thresholdContext* ctx);
84
+
85
+        /**
86
+         * Return one sample from the sensor
87
+         *
88
+         * @param ctx threshold context
89
+         */
90
+        virtual int getSample (thresholdContext* ctx);
91
+
92
+        /**
93
+         *
94
+         * Print running average of threshold context
95
+         *
96
+         * @param ctx threshold context
97
+         */
98
+        virtual void printGraph (thresholdContext* ctx, uint8_t resolution);
99
+
100
+    protected:
101
+        mraa_aio_context    m_gasCtx;
102
+};
103
+
104
+}

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

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

+ 33
- 0
src/gas/mq9.cxx View File

@@ -0,0 +1,33 @@
1
+/*
2
+ * Author: Brendan Le Foll <brendan.le.foll@intel.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 "mq9.h"
26
+
27
+using namespace upm;
28
+
29
+MQ9::MQ9 (int gasPin) : Gas (gasPin) {
30
+}
31
+
32
+MQ9::~MQ9 () {
33
+}

+ 60
- 0
src/gas/mq9.h View File

@@ -0,0 +1,60 @@
1
+/*
2
+ * Author: Brendan Le Foll <brendan.le.foll@intel.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
+#pragma once
25
+
26
+#include <iostream>
27
+#include <string>
28
+#include "gas.h"
29
+
30
+namespace upm {
31
+    /**
32
+     * @brief C++ API for MQ9 gas sensor
33
+     *
34
+     * @snippet mq9-example.cxx Interesting
35
+     */
36
+    class MQ9 : public Gas {
37
+        public:
38
+            /**
39
+             * Jhd1313m1 constructor
40
+             *
41
+             * @param gasPin analog pin where sensor connected
42
+             */
43
+            MQ9 (int gasPin);
44
+
45
+            /**
46
+             * MQ9 destructor
47
+             */
48
+            ~MQ9 ();
49
+
50
+            /**
51
+             * Return name of the component
52
+             */
53
+            std::string name()
54
+            {
55
+                return m_name;
56
+            }
57
+        private:
58
+            std::string m_name;
59
+    };
60
+}

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

@@ -0,0 +1,12 @@
1
+%module pyupm_gas
2
+%include "../upm.i"
3
+%include "../carrays_uint16_t.i"
4
+
5
+%include "stdint.i"
6
+
7
+%feature("autodoc", "3");
8
+
9
+%include "gas.h"
10
+%{
11
+    #include "gas.h"
12
+%}