Browse Source

ta12200: Initial implementation; Grove Electricity uses this sensor

[renamed from] groveelectricity: Initial implementation

This module adds support for the Grove Electricity sensor.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Sarah Knepper <sarah.knepper@intel.com>
Jon Trulson 10 years ago
parent
commit
4333869b7b

+ 3
- 0
examples/CMakeLists.txt View File

@@ -59,6 +59,7 @@ add_executable (a110x-example a110x.cxx)
59 59
 add_executable (gp2y0a21yk-example gp2y0a21yk.cxx)
60 60
 add_executable (grovemoisture-example grovemoisture.cxx)
61 61
 add_executable (groveehr-example groveehr.cxx)
62
+add_executable (ta12200-example ta12200.cxx)
62 63
 
63 64
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
64 65
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -105,6 +106,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/a110x)
105 106
 include_directories (${PROJECT_SOURCE_DIR}/src/gp2y0a21yk)
106 107
 include_directories (${PROJECT_SOURCE_DIR}/src/grovemoisture)
107 108
 include_directories (${PROJECT_SOURCE_DIR}/src/groveehr)
109
+include_directories (${PROJECT_SOURCE_DIR}/src/ta12200)
108 110
 
109 111
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
110 112
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -167,3 +169,4 @@ target_link_libraries (a110x-example a110x ${CMAKE_THREAD_LIBS_INIT})
167 169
 target_link_libraries (gp2y0a21yk-example gp2y0a21yk ${CMAKE_THREAD_LIBS_INIT})
168 170
 target_link_libraries (grovemoisture-example grovemoisture ${CMAKE_THREAD_LIBS_INIT})
169 171
 target_link_libraries (groveehr-example groveehr ${CMAKE_THREAD_LIBS_INIT})
172
+target_link_libraries (ta12200-example ta12200 ${CMAKE_THREAD_LIBS_INIT})

+ 47
- 0
examples/javascript/ta12200.js View File

@@ -0,0 +1,47 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+/*global */
4
+/*
5
+* Author: Zion Orent <zorent@ics.com>
6
+* Copyright (c) 2014 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
+var electricitySensor = require('jsupm_ta12200');
29
+// Instantiate a TA12-200 sensor on analog pin A0
30
+var myElectricitySensor = new electricitySensor.TA12200(0);
31
+
32
+function getElectricityInfo()
33
+{
34
+	var maxVal = myElectricitySensor.highestValue();
35
+	var current = myElectricitySensor.milliAmps(maxVal);
36
+
37
+	console.log("Max ADC Value: " + maxVal + ", current: " + current + "mA");
38
+}
39
+
40
+setInterval(getElectricityInfo, 100);
41
+
42
+// Print message when exiting
43
+process.on('SIGINT', function()
44
+{
45
+	console.log("Exiting...");
46
+	process.exit(0);
47
+});

+ 65
- 0
examples/ta12200.cxx View File

@@ -0,0 +1,65 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.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 "ta12200.h"
29
+
30
+using namespace std;
31
+
32
+int shouldRun = true;
33
+
34
+void sig_handler(int signo)
35
+{
36
+  if (signo == SIGINT)
37
+    shouldRun = false;
38
+}
39
+
40
+
41
+int main()
42
+{
43
+  signal(SIGINT, sig_handler);
44
+
45
+//! [Interesting]
46
+  // Instantiate a TA12-200 sensor on analog pin A0
47
+  upm::TA12200* electricity = new upm::TA12200(0);
48
+  
49
+  while (shouldRun)
50
+    {
51
+      unsigned int maxVal = electricity->highestValue();
52
+      float current = electricity->milliAmps(maxVal);
53
+
54
+      cout << "Max ADC Value: " << maxVal << ", current: " << current
55
+           << "mA" << endl;
56
+      usleep(100000);
57
+    }
58
+
59
+//! [Interesting]
60
+
61
+  cout << "Exiting..." << endl;
62
+
63
+  delete electricity;
64
+  return 0;
65
+}

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

@@ -0,0 +1,5 @@
1
+set (libname "ta12200")
2
+set (libdescription "upm ta12200 current transformer module")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 8
- 0
src/ta12200/jsupm_ta12200.i View File

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

+ 9
- 0
src/ta12200/pyupm_ta12200.i View File

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

+ 112
- 0
src/ta12200/ta12200.cxx View File

@@ -0,0 +1,112 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.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
+
27
+#include "ta12200.h"
28
+
29
+using namespace upm;
30
+using namespace std;
31
+
32
+TA12200::TA12200(int pin)
33
+{
34
+  initClock();
35
+
36
+  if ( !(m_aio = mraa_aio_init(pin)) )
37
+    {
38
+      cerr << __FUNCTION__ << ": mraa_aio_init() failed" << endl;
39
+      return;
40
+    }
41
+}
42
+
43
+TA12200::~TA12200()
44
+{
45
+  mraa_aio_close(m_aio);
46
+}
47
+
48
+void TA12200::initClock()
49
+{
50
+  gettimeofday(&m_startTime, NULL);
51
+}
52
+
53
+uint32_t TA12200::getMillis()
54
+{
55
+  struct timeval elapsed, now;
56
+  uint32_t elapse;
57
+
58
+  // get current time
59
+  gettimeofday(&now, NULL);
60
+
61
+  // compute the delta since m_startTime
62
+  if( (elapsed.tv_usec = now.tv_usec - m_startTime.tv_usec) < 0 ) 
63
+    {
64
+      elapsed.tv_usec += 1000000;
65
+      elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec - 1;
66
+    } 
67
+  else 
68
+    {
69
+      elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec;
70
+    }
71
+
72
+  elapse = (uint32_t)((elapsed.tv_sec * 1000) + (elapsed.tv_usec / 1000));
73
+
74
+  // never return 0
75
+  if (elapse == 0)
76
+    elapse = 1;
77
+
78
+  return elapse;
79
+}
80
+
81
+
82
+unsigned int TA12200::highestValue()
83
+{
84
+  unsigned int hiVal = 0;
85
+  unsigned int val;
86
+  uint32_t start = getMillis();
87
+
88
+  // 1 second
89
+  while (getMillis() < (start + 1000))
90
+    {
91
+      val = mraa_aio_read(m_aio);
92
+      if (val > hiVal)
93
+        hiVal = val;
94
+    }
95
+        
96
+  return hiVal;
97
+}
98
+
99
+float TA12200::milliAmps(unsigned int val, int res)
100
+{
101
+  float ampCurrent;
102
+  float effectiveVal;
103
+
104
+  // From grove wiki page:
105
+  // minimum_current=1/1024*5/800*2000000/1.414=8.6(mA) 
106
+  // Only for sinusoidal alternating current
107
+  //ampCurrent = float(val) / float(res) * 5.0 / 800.0 * 2000000.0;
108
+  ampCurrent = float(val) / float(res) * 12500.0;
109
+  effectiveVal = ampCurrent/1.414;
110
+  return (effectiveVal);
111
+}
112
+

+ 99
- 0
src/ta12200/ta12200.h View File

@@ -0,0 +1,99 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.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 <iostream>
28
+#include <stdint.h>
29
+#include <sys/time.h>
30
+#include <mraa/aio.h>
31
+
32
+// default ADC resolution. 
33
+#define TA12200_ADC_RES 1024
34
+
35
+namespace upm {
36
+
37
+  /**
38
+   * @brief C++ API for the TA12-200 current transformer
39
+   *
40
+   * UPM module for the TA12-200 current transformer, which is found,
41
+   * for instance, in the Grove Electricity Sensor. 
42
+   * This module can measure AC current moving through a wire at up 
43
+   * to 5A.
44
+   *
45
+   * @ingroup analog
46
+   * @snippet ta12200.cxx Interesting
47
+   */
48
+  class TA12200 {
49
+  public:
50
+    /**
51
+     * TA12200 sensor constructor
52
+     *
53
+     * @param pin analog pin to use
54
+     */
55
+    TA12200(int pin);
56
+
57
+    /**
58
+     * TA12200 Destructor
59
+     */
60
+    ~TA12200();
61
+
62
+    /**
63
+     * Return the number of milliseconds elapsed since initClock()
64
+     * was last called.
65
+     *
66
+     * @return elapsed milliseconds
67
+     */
68
+    uint32_t getMillis();
69
+
70
+    /**
71
+     * Reset the Clock
72
+     *
73
+     */
74
+    void initClock();
75
+
76
+    /**
77
+     * Get the conversion value from the sensor
78
+     *
79
+     * @return the highest value obtained over 1 second of measuring
80
+     */
81
+    unsigned int highestValue();
82
+
83
+    /**
84
+     * Compute the measured voltage
85
+     *
86
+     * @param val value measured by highestValue()
87
+     * @param res ADC resolution
88
+     *
89
+     * @return the measured current in milliamps
90
+     */
91
+    float milliAmps(unsigned int val, int res=TA12200_ADC_RES);
92
+
93
+  private:
94
+    struct timeval m_startTime;
95
+    mraa_aio_context m_aio;
96
+  };
97
+}
98
+
99
+