Browse Source

dfrph: Initial implementation

The driver implements support for the DFRobot pH sensors.  It was
tested with both the standard and Pro versions, calibrated with
standard buffer solutions at pH 4.01 and pH 7.0.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Abhishek Malik <abhishek.malik@intel.com>
Jon Trulson 9 years ago
parent
commit
255d6cbe86

+ 3
- 0
examples/c++/CMakeLists.txt View File

@@ -152,6 +152,7 @@ add_executable (urm37-example urm37.cxx)
152 152
 add_executable (urm37-uart-example urm37-uart.cxx)
153 153
 add_executable (adxrs610-example adxrs610.cxx)
154 154
 add_executable (bma220-example bma220.cxx)
155
+add_executable (dfrph-example dfrph.cxx)
155 156
 
156 157
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
157 158
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -268,6 +269,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/xbee)
268 269
 include_directories (${PROJECT_SOURCE_DIR}/src/urm37)
269 270
 include_directories (${PROJECT_SOURCE_DIR}/src/adxrs610)
270 271
 include_directories (${PROJECT_SOURCE_DIR}/src/bma220)
272
+include_directories (${PROJECT_SOURCE_DIR}/src/dfrph)
271 273
 
272 274
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
273 275
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -421,3 +423,4 @@ target_link_libraries (urm37-example urm37 ${CMAKE_THREAD_LIBS_INIT})
421 423
 target_link_libraries (urm37-uart-example urm37 ${CMAKE_THREAD_LIBS_INIT})
422 424
 target_link_libraries (adxrs610-example adxrs610 ${CMAKE_THREAD_LIBS_INIT})
423 425
 target_link_libraries (bma220-example bma220 ${CMAKE_THREAD_LIBS_INIT})
426
+target_link_libraries (dfrph-example dfrph ${CMAKE_THREAD_LIBS_INIT})

+ 76
- 0
examples/c++/dfrph.cxx View File

@@ -0,0 +1,76 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2015 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 "dfrph.h"
29
+
30
+using namespace std;
31
+
32
+bool shouldRun = true;
33
+
34
+#define DFRPH_AREF   5.0
35
+
36
+void sig_handler(int signo)
37
+{
38
+  if (signo == SIGINT)
39
+    shouldRun = false;
40
+}
41
+
42
+int main()
43
+{
44
+  signal(SIGINT, sig_handler);
45
+
46
+//! [Interesting]
47
+
48
+  // Instantiate a DFRPH sensor on analog pin A0, with an analog
49
+  // reference voltage of DFRPH_AREF
50
+  upm::DFRPH *sensor = new upm::DFRPH(0, DFRPH_AREF);
51
+  
52
+
53
+  // After calibration, set the offset (based on calibration with a pH
54
+  // 7.0 buffer solution).  See the UPM sensor documentation for
55
+  // calibrations instructions.
56
+  sensor->setOffset(0.065);
57
+
58
+  // Every second, sample the pH and output it's corresponding
59
+  // analog voltage.
60
+
61
+  while (shouldRun)
62
+    {
63
+      cout << "Detected volts: " << sensor->volts() << endl;
64
+      cout << "pH value: " << sensor->pH() << endl;
65
+      cout << endl;
66
+
67
+      sleep(1);
68
+    }
69
+
70
+//! [Interesting]
71
+
72
+  cout << "Exiting" << endl;
73
+
74
+  delete sensor;
75
+  return 0;
76
+}

+ 59
- 0
examples/javascript/dfrph.js View File

@@ -0,0 +1,59 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+
4
+/*
5
+ * Author: Jon Trulson <jtrulson@ics.com>
6
+ * Copyright (c) 2015 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
+
29
+var sensorObj = require('jsupm_dfrph');
30
+
31
+// Instantiate a DFRPH sensor on analog pin A0, with an analog
32
+// reference voltage of 5.0
33
+var sensor = new sensorObj.DFRPH(0, 5.0);
34
+
35
+
36
+// After calibration, set the offset (based on calibration with a pH
37
+// 7.0 buffer solution).  See the UPM sensor documentation for
38
+// calibrations instructions.
39
+sensor.setOffset(0.065);
40
+
41
+// Every second, sample the pH and output it's corresponding
42
+// analog voltage.
43
+
44
+setInterval(function()
45
+{
46
+    console.log("Detected volts: " + sensor.volts());
47
+    console.log("pH value: " + sensor.pH());
48
+}, 1000);
49
+
50
+// exit on ^C
51
+process.on('SIGINT', function()
52
+{
53
+    sensor = null;
54
+    sensorObj.cleanUp();
55
+    sensorObj = null;
56
+    console.log("Exiting.");
57
+    process.exit(0);
58
+});
59
+

+ 56
- 0
examples/python/dfrph.py View File

@@ -0,0 +1,56 @@
1
+#!/usr/bin/python
2
+# Author: Jon Trulson <jtrulson@ics.com>
3
+# Copyright (c) 2015 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
+import time, sys, signal, atexit
25
+import pyupm_dfrph as sensorObj
26
+
27
+# Instantiate a DFRPH sensor on analog pin A0, with an analog
28
+# reference voltage of 5.0
29
+sensor = sensorObj.DFRPH(0, 5.0)
30
+
31
+## Exit handlers ##
32
+# This function stops python from printing a stacktrace when you hit control-C
33
+def SIGINTHandler(signum, frame):
34
+	raise SystemExit
35
+
36
+# This function lets you run code on exit
37
+def exitHandler():
38
+	print "Exiting"
39
+	sys.exit(0)
40
+
41
+# Register exit handlers
42
+atexit.register(exitHandler)
43
+signal.signal(signal.SIGINT, SIGINTHandler)
44
+
45
+# After calibration, set the offset (based on calibration with a pH
46
+# 7.0 buffer solution).  See the UPM sensor documentation for
47
+# calibrations instructions.
48
+sensor.setOffset(0.065);
49
+
50
+# Every second, sample the pH and output it's corresponding
51
+# analog voltage.
52
+
53
+while (1):
54
+        print "Detected volts: ", sensor.volts()
55
+        print "pH value: ", sensor.pH()
56
+	time.sleep(1)

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

@@ -0,0 +1,5 @@
1
+set (libname "dfrph")
2
+set (libdescription "upm dfrobot pH sensors")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 74
- 0
src/dfrph/dfrph.cxx View File

@@ -0,0 +1,74 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2015 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 "dfrph.h"
28
+
29
+using namespace std;
30
+using namespace upm;
31
+
32
+DFRPH::DFRPH(int pin, float aref) :
33
+  m_aio(pin)
34
+{
35
+  m_aRes = (1 << m_aio.getBit());
36
+  m_aref = aref;
37
+
38
+  m_offset = 0.0;
39
+}
40
+
41
+DFRPH::~DFRPH()
42
+{
43
+}
44
+
45
+float DFRPH::volts()
46
+{
47
+  int val = m_aio.read();
48
+
49
+  return(val * (m_aref / m_aRes));
50
+}
51
+
52
+void DFRPH::setOffset(float offset)
53
+{
54
+  m_offset = offset;
55
+}
56
+
57
+float DFRPH::pH(unsigned int samples)
58
+{
59
+  if (!samples)
60
+    samples = 1;
61
+
62
+  float sum = 0.0;
63
+
64
+  for (int i=0; i<samples; i++)
65
+    {
66
+      sum += volts();
67
+      usleep(20000);
68
+    }
69
+
70
+  sum /= samples;
71
+
72
+  // 3.5 is a 'magic' DFRobot number. Seems to work though :)
73
+  return (3.5 * sum + m_offset);
74
+}

+ 142
- 0
src/dfrph/dfrph.h View File

@@ -0,0 +1,142 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2015 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 <mraa/aio.hpp>
29
+
30
+namespace upm {
31
+  /**
32
+   * @brief DFRobot pH sensors
33
+   * @defgroup dfrph libupm-dfrph
34
+   * @ingroup dfrobot liquid analog
35
+   */
36
+
37
+  /**
38
+   * @library dfrph
39
+   * @sensor dfrph
40
+   * @comname DFRobot pH Sensors
41
+   * @type liquid
42
+   * @man dfrobot 
43
+   * @web http://www.dfrobot.com/index.php?route=product/product&product_id=1110
44
+   * @con analog
45
+   *
46
+   * @brief API for the DFRobot pH Sensors
47
+   *
48
+   * This sensor family returns an analog voltage proportional to the
49
+   * acidity or alkalinity of a liquid -- it's pH value.
50
+   *
51
+   * This driver was developed using the DFRobot Analog pH meter and
52
+   * the DFRobot Analog pH Meter Pro.
53
+   *
54
+   *
55
+   * Calibration instructions, taken and slightly reworded from the
56
+   *  DFRobot wiki at:
57
+   *  http://dfrobot.com/wiki/index.php/PH_meter%28SKU:_SEN0161%29
58
+   *
59
+   *  1) Connect equipment: the pH electrode is connected to the BNC
60
+   *  connector on the pH meter board, and then the pH meter board is
61
+   *  connected to the analog port 0 of the controller. When the
62
+   *  controller gets power, you will see the blue LED on board is on.
63
+   *
64
+   *  2) Put the pH electrode into the standard solution whose pH
65
+   *  value is 7.00.  Run the dfrph example and note the pH output
66
+   *  value.  Compare the value with 7.00, and calculate the
67
+   *  difference.  This is the value you should supply to the
68
+   *  setOffset() method.
69
+   *
70
+   *  3) Put the pH electrode into the pH standard solution whose
71
+   *  value is 4.00. Then wait about one minute, and adjust the
72
+   *  potentiometer on the interface board.  Let the value stabilise
73
+   *  at around 4.00. At this time,the acidic calibration has been
74
+   *  completed and you can measure the pH value of an acidic
75
+   *  solution.
76
+   *
77
+   *  4) According to the linear characteristics of pH electrode
78
+   *  itself, after the above calibration,you can directly measure the
79
+   *  pH value of the alkaline solution. If you want to get better
80
+   *  accuracy, you can recalibrate it. Alkaline calibration use the
81
+   *  standard solution whose pH value is 9.18.  Also adjust the
82
+   *  potentiometer and let the value stabilise at around 9.18. After
83
+   *  this calibration, you can measure the pH value of an alkaline
84
+   *  solution.
85
+   *
86
+   * @snippet dfrph.cxx Interesting
87
+   */
88
+
89
+  class DFRPH {
90
+  public:
91
+
92
+    /**
93
+     * DFRPH constructor
94
+     *
95
+     * @param pin Analog pin to use
96
+     * @param aref Analog reference voltage; default is 5.0 V
97
+     */
98
+    DFRPH(int pin, float aref=5.0);
99
+
100
+    /**
101
+     * DFRPH destructor
102
+     */
103
+    ~DFRPH();
104
+
105
+    /**
106
+     * Returns the voltage detected on the analog pin
107
+     *
108
+     * @return The detected voltage
109
+     */
110
+    float volts();
111
+
112
+    /**
113
+     * Specifies the offset determined from calibration.  The default
114
+     * is 0.0.
115
+     *
116
+     * @param offset The offset value to use
117
+     */
118
+    void setOffset(float offset);
119
+
120
+    /**
121
+     * Take a number of samples and return the detected pH value.  The
122
+     * default number of samples is 15.
123
+     *
124
+     * @param samples The number of samples to average over, default 15
125
+     * @return The pH value detected
126
+     */
127
+    float pH(unsigned int samples=15);
128
+
129
+  protected:
130
+    mraa::Aio m_aio;
131
+
132
+  private:
133
+    float m_aref;
134
+    // ADC resolution
135
+    int m_aRes;
136
+
137
+    // voltage offset
138
+    float m_offset;
139
+  };
140
+}
141
+
142
+

+ 8
- 0
src/dfrph/javaupm_dfrph.i View File

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

+ 8
- 0
src/dfrph/jsupm_dfrph.i View File

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

+ 9
- 0
src/dfrph/pyupm_dfrph.i View File

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