Browse Source

ecs1030: added new current sensor (works on atmega328 but on Galileo not accurate, need calibration and circuit)

Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
Kiveisha Yevgeniy 10 years ago
parent
commit
5c4de1b926

+ 3
- 0
examples/CMakeLists.txt View File

@@ -28,6 +28,7 @@ add_executable (nrf8001-broadcast-example nrf8001_broadcast.cxx)
28 28
 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
+add_executable (ecs1030-example ecs1030-example.cxx)
31 32
 
32 33
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
33 34
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -53,6 +54,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/max5487)
53 54
 include_directories (${PROJECT_SOURCE_DIR}/src/nrf8001)
54 55
 include_directories (${PROJECT_SOURCE_DIR}/src/lpd8806)
55 56
 include_directories (${PROJECT_SOURCE_DIR}/src/mlx90614)
57
+include_directories (${PROJECT_SOURCE_DIR}/src/ecs1030)
56 58
 
57 59
 target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT})
58 60
 target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT})
@@ -84,3 +86,4 @@ target_link_libraries (nrf8001-broadcast-example nrf8001 ${CMAKE_THREAD_LIBS_INI
84 86
 target_link_libraries (nrf8001-helloworld-example nrf8001 ${CMAKE_THREAD_LIBS_INIT})
85 87
 target_link_libraries (lpd8806-example lpd8806 ${CMAKE_THREAD_LIBS_INIT})
86 88
 target_link_libraries (mlx90614-example mlx90614 ${CMAKE_THREAD_LIBS_INIT})
89
+target_link_libraries (ecs1030-example ecs1030 ${CMAKE_THREAD_LIBS_INIT})

+ 61
- 0
examples/ecs1030-example.cxx View File

@@ -0,0 +1,61 @@
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 <signal.h>
28
+#include <stdlib.h>
29
+#include "ecs1030.h"
30
+
31
+int is_running = 0;
32
+upm::ECS1030 *sensor = NULL;
33
+
34
+void
35
+sig_handler(int signo)
36
+{
37
+    printf("got signal\n");
38
+    if (signo == SIGINT) {
39
+        is_running = 1;
40
+    }
41
+}
42
+
43
+//! [Interesting]
44
+int
45
+main(int argc, char **argv)
46
+{
47
+    sensor = new upm::ECS1030(0);
48
+    signal(SIGINT, sig_handler);
49
+
50
+    while (!is_running) {
51
+        std::cout << "I = " << sensor->getCurrency_A () << ", Power = " << sensor->getPower_A () << std::endl;
52
+        std::cout << "I = " << sensor->getCurrency_B () << ", Power = " << sensor->getPower_B () << std::endl;
53
+    }
54
+
55
+    std::cout << "exiting application" << std::endl;
56
+
57
+    delete sensor;
58
+
59
+    return 0;
60
+}
61
+//! [Interesting]

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

@@ -0,0 +1,5 @@
1
+set (libname "ecs1030")
2
+set (libdescription "Non-invasive current sensor")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 101
- 0
src/ecs1030/ecs1030.cxx View File

@@ -0,0 +1,101 @@
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 <iostream>
26
+#include <unistd.h>
27
+#include <stdlib.h>
28
+
29
+#include "ecs1030.h"
30
+
31
+using namespace upm;
32
+
33
+struct ECS1030Exception : public std::exception {
34
+    std::string message;
35
+    ECS1030Exception (std::string msg) : message (msg) { }
36
+    ~ECS1030Exception () throw () { }
37
+    const char* what() const throw () { return message.c_str(); }
38
+};
39
+
40
+ECS1030::ECS1030 (uint8_t pinNumber) {
41
+    m_dataPinCtx = mraa_aio_init(pinNumber);
42
+    if (m_dataPinCtx == NULL) {
43
+        throw ECS1030Exception ("GPIO failed to initilize");
44
+    }
45
+
46
+    m_calibration = 111.1;
47
+}
48
+
49
+ECS1030::~ECS1030 () {
50
+    mraa_result_t error = MRAA_SUCCESS;
51
+
52
+    error = mraa_aio_close (m_dataPinCtx);
53
+    if (error != MRAA_SUCCESS) {
54
+    }
55
+}
56
+
57
+double
58
+ECS1030::getCurrency_A () {
59
+    int     sensorValue  = 0;
60
+    float   rLoad        = 0;
61
+    float   volt         = 0;
62
+    float   rms          = 0;
63
+
64
+    for (int i = 0; i < NUMBER_OF_SAMPLES; i++) {
65
+        sensorValue = mraa_aio_read (m_dataPinCtx);
66
+        volt = (VOLT_M * sensorValue) - 2.5;
67
+        volt = volt * volt;
68
+        rms = rms + volt;
69
+        usleep (DELAY_MS);
70
+    }
71
+
72
+    rms = rms / (float)NUMBER_OF_SAMPLES;
73
+    rms = sqrt(rms);
74
+    return rms / R_LOAD;
75
+}
76
+
77
+double
78
+ECS1030::getCurrency_B () {
79
+    double sumCurrency    = 0;
80
+
81
+    for (int i = 0; i < NUMBER_OF_SAMPLES; i++) {
82
+        m_lastSample = m_sample;
83
+        m_sample = mraa_aio_read (m_dataPinCtx);
84
+        m_lastFilter = m_filteredSample;
85
+        m_filteredSample = 0.996 * (m_lastFilter + m_sample - m_lastSample);
86
+        sumCurrency += (m_filteredSample * m_filteredSample);
87
+    }
88
+
89
+    double ratio = m_calibration * ((SUPPLYVOLTAGE / 1000.0) / (ADC_RESOLUTION));
90
+    return ( ratio * sqrt(sumCurrency / NUMBER_OF_SAMPLES) );
91
+}
92
+
93
+double
94
+ECS1030::getPower_A () {
95
+    return 220.0 * getCurrency_A ();
96
+}
97
+
98
+double
99
+ECS1030::getPower_B () {
100
+    return 220.0 * getCurrency_B ();
101
+}

+ 97
- 0
src/ecs1030/ecs1030.h View File

@@ -0,0 +1,97 @@
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
+#pragma once
25
+
26
+#include <string>
27
+#include <math.h>
28
+#include <mraa/aio.h>
29
+#include <mraa/gpio.h>
30
+
31
+#define NUMBER_OF_SAMPLES  500
32
+#define ADC_RESOLUTION     1024
33
+#define SUPPLYVOLTAGE      5100
34
+#define CURRENT_RATIO      2000.0
35
+
36
+#define HIGH               1
37
+#define LOW                0
38
+
39
+#define TRUE               HIGH
40
+#define FALSE              LOW
41
+
42
+namespace upm {
43
+    class ECS1030 {
44
+        public:
45
+            static const uint8_t DELAY_MS  = 20000 / NUMBER_OF_SAMPLES; /* 1/50Hz is 20ms period */
46
+            static const uint8_t VOLT_M    = 5.1 / 1023;
47
+            static const uint8_t R_LOAD    = 2000.0 / CURRENT_RATIO;
48
+
49
+            /**
50
+             * Instanciates a ECS1030 (current sensor) object
51
+             *
52
+             * @param pinNumber number of the data pin
53
+             */
54
+            ECS1030 (uint8_t pinNumber);
55
+
56
+            /**
57
+             * ECS1030 object destructor, basicaly it close the GPIO.
58
+             */
59
+            ~ECS1030 ();
60
+
61
+            /**
62
+             * Return currency data for the sampled period
63
+             */
64
+            double getCurrency_A ();
65
+
66
+            /**
67
+             * Return power data for the sampled period
68
+             */
69
+            double getPower_A ();
70
+
71
+            /**
72
+             * Return currency data for the sampled period
73
+             */
74
+            double getCurrency_B ();
75
+
76
+            /**
77
+             * Return power data for the sampled period
78
+             */
79
+            double getPower_B ();
80
+
81
+            /**
82
+             * Return name of the component
83
+             */
84
+            std::string name() {
85
+                return m_name;
86
+            }
87
+        private:
88
+            std::string         m_name;
89
+            mraa_aio_context    m_dataPinCtx;
90
+
91
+            double              m_calibration;
92
+            int                 m_lastSample;
93
+            double              m_lastFilter;
94
+            int                 m_sample;
95
+            double              m_filteredSample;
96
+    };
97
+}

+ 8
- 0
src/ecs1030/jsupm_ecs1030.i View File

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

+ 11
- 0
src/ecs1030/pyupm_ecs1030.i View File

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