Browse Source

grovevdiv: Initial implementation

This module implements support for the Grove Voltage Divider.
jrvandr: Removed unecessary mraa_init() from grovevdiv.cxx

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: John Van Drasek <john.r.van.drasek@intel.com>
Jon Trulson 10 years ago
parent
commit
8d3e29a695

+ 3
- 0
examples/CMakeLists.txt View File

@@ -61,6 +61,7 @@ add_executable (grovemoisture-example grovemoisture.cxx)
61 61
 add_executable (groveehr-example groveehr.cxx)
62 62
 add_executable (ta12200-example ta12200.cxx)
63 63
 add_executable (grovelinefinder-example grovelinefinder.cxx)
64
+add_executable (grovevdiv-example grovevdiv.cxx)
64 65
 
65 66
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
66 67
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -109,6 +110,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/grovemoisture)
109 110
 include_directories (${PROJECT_SOURCE_DIR}/src/groveehr)
110 111
 include_directories (${PROJECT_SOURCE_DIR}/src/ta12200)
111 112
 include_directories (${PROJECT_SOURCE_DIR}/src/grovelinefinder)
113
+include_directories (${PROJECT_SOURCE_DIR}/src/grovevdiv)
112 114
 
113 115
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
114 116
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -173,3 +175,4 @@ target_link_libraries (grovemoisture-example grovemoisture ${CMAKE_THREAD_LIBS_I
173 175
 target_link_libraries (groveehr-example groveehr ${CMAKE_THREAD_LIBS_INIT})
174 176
 target_link_libraries (ta12200-example ta12200 ${CMAKE_THREAD_LIBS_INIT})
175 177
 target_link_libraries (grovelinefinder-example grovelinefinder ${CMAKE_THREAD_LIBS_INIT})
178
+target_link_libraries (grovevdiv-example grovevdiv ${CMAKE_THREAD_LIBS_INIT})

+ 67
- 0
examples/grovevdiv.cxx View File

@@ -0,0 +1,67 @@
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 "grovevdiv.h"
29
+
30
+using namespace std;
31
+
32
+bool 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 Grove Voltage Divider sensor on analog pin A0
47
+  upm::GroveVDiv* vDiv = new upm::GroveVDiv(0);
48
+
49
+  // collect data and output measured voltage according to the setting
50
+  // of the scaling switch (3 or 10)
51
+  while (shouldRun)
52
+    {
53
+      unsigned int val = vDiv->value(100);
54
+      float gain3val = vDiv->computedValue(3, val);
55
+      float gain10val = vDiv->computedValue(10, val);
56
+      cout << "ADC value: " << val << " Gain 3: " << gain3val 
57
+           << "v Gain 10: " << gain10val << "v" << endl;
58
+
59
+      sleep(1);
60
+    }
61
+//! [Interesting]
62
+
63
+  cout << "Exiting..." << endl;
64
+
65
+  delete vDiv;
66
+  return 0;
67
+}

+ 50
- 0
examples/javascript/grovevdiv.js View File

@@ -0,0 +1,50 @@
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 voltageDivider = require('jsupm_grovevdiv');
29
+// Instantiate a Grove Voltage Divider sensor on analog pin A0
30
+var myVoltageDivider = new voltageDivider.GroveVDiv(0);
31
+
32
+// collect data and output measured voltage according to the setting
33
+// of the scaling switch (3 or 10)
34
+function getVoltageInfo()
35
+{
36
+	var val = myVoltageDivider.value(100);
37
+	var gain3val = myVoltageDivider.computedValue(3, val);
38
+	var gain10val = myVoltageDivider.computedValue(10, val);
39
+	console.log("ADC value: " + val + " Gain 3: " + gain3val 
40
+				+ "v Gain 10: " + gain10val + "v");
41
+}
42
+
43
+setInterval(getVoltageInfo, 1000);
44
+
45
+// Print message when exiting
46
+process.on('SIGINT', function()
47
+{
48
+	console.log("Exiting...");
49
+	process.exit(0);
50
+});

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

@@ -0,0 +1,5 @@
1
+set (libname "grovevdiv")
2
+set (libdescription "upm grove voltage divider module")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 64
- 0
src/grovevdiv/grovevdiv.cxx View File

@@ -0,0 +1,64 @@
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 "grovevdiv.h"
28
+
29
+using namespace upm;
30
+using namespace std;
31
+
32
+GroveVDiv::GroveVDiv(int pin)
33
+{
34
+  if ( !(m_aio = mraa_aio_init(pin)) )
35
+    {
36
+      cerr << __FUNCTION__ << ": mraa_aio_init() failed" << endl;
37
+      return;
38
+    }
39
+}
40
+
41
+GroveVDiv::~GroveVDiv()
42
+{
43
+  mraa_aio_close(m_aio);
44
+}
45
+
46
+unsigned int GroveVDiv::value(unsigned int samples)
47
+{
48
+  unsigned int sum = 0;
49
+
50
+  for (int i=0; i<samples; i++)
51
+    {
52
+      sum += mraa_aio_read(m_aio);
53
+      usleep(2000);
54
+    }
55
+        
56
+  return (sum / samples);
57
+}
58
+
59
+float GroveVDiv::computedValue(uint8_t gain, uint16_t val, int vref, int res)
60
+{
61
+  return ((float(gain) * float(val) * float(vref) / float(res)) / 1000.0);
62
+
63
+}
64
+

+ 87
- 0
src/grovevdiv/grovevdiv.h View File

@@ -0,0 +1,87 @@
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 <mraa/aio.h>
30
+
31
+// ref voltage in millivolts
32
+#define GROVEVDIV_VREF  4980
33
+
34
+// default ADC resolution. 
35
+#define GROVEVDIV_ADC_RES 1024
36
+
37
+namespace upm {
38
+
39
+  /**
40
+   * @brief C++ API for the Grove Voltage Divider Sensor
41
+   *
42
+   * UPM module for the Grove Voltage Divider Sensor
43
+   *
44
+   * @ingroup grove analog
45
+   * @snippet grovevdiv.cxx Interesting
46
+   */
47
+  class GroveVDiv {
48
+  public:
49
+    /**
50
+     * Grove Voltage Divider sensor constructor
51
+     *
52
+     * @param pin analog pin to use
53
+     */
54
+    GroveVDiv(int pin);
55
+
56
+    /**
57
+     * Grove Voltage Divider Destructor
58
+     */
59
+    ~GroveVDiv();
60
+
61
+    /**
62
+     * Get the conversion value from the sensor
63
+     *
64
+     * @param samples specifies how many samples to average over
65
+     * @return the averaged ADC conversion value
66
+     */
67
+    unsigned int value(unsigned int samples);
68
+
69
+    /**
70
+     * Compute the measured voltage
71
+     *
72
+     * @param gain gain switch, either 3 or 10 for grove
73
+     * @param val measured voltage (from value())
74
+     * @param vref reference voltage in millivolts
75
+     * @param res ADC resolution
76
+     *
77
+     * @return the measured voltage
78
+     */
79
+    float computedValue(uint8_t gain, uint16_t val, int vref=GROVEVDIV_VREF, 
80
+                        int res=GROVEVDIV_ADC_RES);
81
+
82
+  private:
83
+    mraa_aio_context m_aio;
84
+  };
85
+}
86
+
87
+

+ 8
- 0
src/grovevdiv/jsupm_grovevdiv.i View File

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

+ 9
- 0
src/grovevdiv/pyupm_grovevdiv.i View File

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