|
@@ -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
|
+}
|