Browse Source

gp2y0a: renamed from gp2y0a21yk to indicate support for the gp2y0a family

Additionally, add some error checking in the constructor, and determine
the ADC resolution and use that in the voltage calculation rather than
a hardcoded 1024.0.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Jon Trulson 9 years ago
parent
commit
77be6adf6d

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

@@ -59,7 +59,7 @@ add_executable (enc03r-example enc03r.cxx)
59 59
 add_executable (adc121c021-example adc121c021.cxx)
60 60
 add_executable (ds1307-example ds1307.cxx)
61 61
 add_executable (a110x-example a110x.cxx)
62
-add_executable (gp2y0a21yk-example gp2y0a21yk.cxx)
62
+add_executable (gp2y0a-example gp2y0a.cxx)
63 63
 add_executable (grovemoisture-example grovemoisture.cxx)
64 64
 add_executable (groveehr-example groveehr.cxx)
65 65
 add_executable (ta12200-example ta12200.cxx)
@@ -151,7 +151,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/enc03r)
151 151
 include_directories (${PROJECT_SOURCE_DIR}/src/adc121c021)
152 152
 include_directories (${PROJECT_SOURCE_DIR}/src/ds1307)
153 153
 include_directories (${PROJECT_SOURCE_DIR}/src/a110x)
154
-include_directories (${PROJECT_SOURCE_DIR}/src/gp2y0a21yk)
154
+include_directories (${PROJECT_SOURCE_DIR}/src/gp2y0a)
155 155
 include_directories (${PROJECT_SOURCE_DIR}/src/grovemoisture)
156 156
 include_directories (${PROJECT_SOURCE_DIR}/src/groveehr)
157 157
 include_directories (${PROJECT_SOURCE_DIR}/src/ta12200)
@@ -257,7 +257,7 @@ target_link_libraries (enc03r-example enc03r ${CMAKE_THREAD_LIBS_INIT})
257 257
 target_link_libraries (adc121c021-example adc121c021 ${CMAKE_THREAD_LIBS_INIT})
258 258
 target_link_libraries (ds1307-example ds1307 ${CMAKE_THREAD_LIBS_INIT})
259 259
 target_link_libraries (a110x-example a110x ${CMAKE_THREAD_LIBS_INIT})
260
-target_link_libraries (gp2y0a21yk-example gp2y0a21yk ${CMAKE_THREAD_LIBS_INIT})
260
+target_link_libraries (gp2y0a-example gp2y0a ${CMAKE_THREAD_LIBS_INIT})
261 261
 target_link_libraries (grovemoisture-example grovemoisture ${CMAKE_THREAD_LIBS_INIT})
262 262
 target_link_libraries (groveehr-example groveehr ${CMAKE_THREAD_LIBS_INIT})
263 263
 target_link_libraries (ta12200-example ta12200 ${CMAKE_THREAD_LIBS_INIT})

examples/c++/gp2y0a21yk.cxx → examples/c++/gp2y0a.cxx View File

@@ -25,14 +25,14 @@
25 25
 #include <unistd.h>
26 26
 #include <iostream>
27 27
 #include <signal.h>
28
-#include "gp2y0a21yk.h"
28
+#include "gp2y0a.h"
29 29
 
30 30
 using namespace std;
31 31
 
32 32
 bool shouldRun = true;
33 33
 
34 34
 // analog voltage, usually 3.3 or 5.0
35
-#define GP2Y0A21YK_AREF   5.0
35
+#define GP2Y0A_AREF   5.0
36 36
 #define SAMPLES_PER_QUERY 20
37 37
 
38 38
 void sig_handler(int signo)
@@ -46,21 +46,21 @@ int main()
46 46
   signal(SIGINT, sig_handler);
47 47
 
48 48
 //! [Interesting]
49
-  // Note, for the Grove version of this sensor, due to the way it is wired,
50
-  // you need to plug this into the A0 port, where it will use the available
51
-  // A1 pin for data.
49
+  // Note, for the Grove 80cm version of this sensor, due to the way
50
+  // it is wired, you need to plug this into the A0 port, where it
51
+  // will use the available A1 pin for data.
52 52
 
53
-  // Instantiate a GP2Y0A21YK on analog pin A1
54
-  upm::GP2Y0A21YK *volts = new upm::GP2Y0A21YK(1);
53
+  // Instantiate a GP2Y0A on analog pin A1
54
+  upm::GP2Y0A *volts = new upm::GP2Y0A(1);
55 55
   
56 56
   // The higher the voltage (closer to AREF) the closer the object is.  NOTE:
57 57
   // the measured voltage will probably not exceed 3.3 volts.
58 58
   // Every second, print the averaged voltage value (averaged over 20 samples).
59 59
   while (shouldRun)
60 60
     {
61
-      cout << "AREF: " << GP2Y0A21YK_AREF 
61
+      cout << "AREF: " << GP2Y0A_AREF 
62 62
            << ", Voltage value (higher means closer): " 
63
-           << volts->value(GP2Y0A21YK_AREF, SAMPLES_PER_QUERY) << endl;
63
+           << volts->value(GP2Y0A_AREF, SAMPLES_PER_QUERY) << endl;
64 64
       
65 65
       sleep(1);
66 66
     }

examples/javascript/gp2y0a21yk.js → examples/javascript/gp2y0a.js View File

@@ -1,6 +1,5 @@
1 1
 /*jslint node:true, vars:true, bitwise:true, unparam:true */
2 2
 /*jshint unused:true */
3
-/*global */
4 3
 /*
5 4
 * Author: Zion Orent <zorent@ics.com>
6 5
 * Copyright (c) 2014 Intel Corporation.
@@ -24,33 +23,38 @@
24 23
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 24
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 25
 */
27
-// JavaScript file for GP2Y0A21YK 80cm IR Proximity Sensor
26
+// JavaScript file for GP2Y0A family of IR Proximity Sensors
28 27
 
29 28
 // analog voltage, usually 3.3 or 5.0
30
-var GP2Y0A21YK_AREF = 5.0;
29
+var GP2Y0A_AREF = 5.0;
31 30
 var SAMPLES_PER_QUERY = 20;
32 31
 
33
-var IRProximity = require('jsupm_gp2y0a21yk');
32
+var IRProximity = require('jsupm_gp2y0a');
34 33
 
35
-// Note, for the Grove version of this sensor, due to the way it is wired,
34
+// Note, for the Grove 80cm version of this sensor, due to the way it is wired,
36 35
 // you need to plug this into the A0 port, where it will use the available
37 36
 // A1 pin for data.
38
-// Instantiate a GP2Y0A21YK on analog pin A1
39
-var myVolts = new IRProximity.GP2Y0A21YK(1);
37
+// Instantiate a GP2Y0A on analog pin A1
38
+var myVolts = new IRProximity.GP2Y0A(1);
40 39
 
41 40
 // The higher the voltage (closer to AREF) the closer the object is.
42 41
 // NOTE: The measured voltage will probably not exceed 3.3 volts.
43
-// Every second, print the averaged voltage value (averaged over 20 samples).
44
-setInterval(function()
42
+// Every second, print the averaged voltage value
43
+// (averaged over 20 samples).
44
+var myInterval = setInterval(function()
45 45
 {
46
-	console.log("AREF: " + GP2Y0A21YK_AREF + 
46
+	console.log("AREF: " + GP2Y0A_AREF + 
47 47
                 ", Voltage value (higher means closer): " + 
48
-                myVolts.value(GP2Y0A21YK_AREF, SAMPLES_PER_QUERY));
48
+                myVolts.value(GP2Y0A_AREF, SAMPLES_PER_QUERY));
49 49
 }, 1000);
50 50
 
51 51
 // Print message when exiting
52 52
 process.on('SIGINT', function()
53 53
 {
54
+	clearInterval(myInterval);
55
+	myVolts = null;
56
+	IRProximity.cleanUp();
57
+	IRProximity = null;
54 58
 	console.log("Exiting...");
55 59
 	process.exit(0);
56 60
 });

+ 62
- 0
examples/python/gp2y0a.py View File

@@ -0,0 +1,62 @@
1
+#!/usr/bin/python
2
+# Author: Zion Orent <zorent@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_gp2y0a as upmGp2y0a
26
+
27
+# Note, for the Grove 80cm version of this sensor, due to the way it is wired,
28
+# you need to plug this into the A0 port, where it will use the available
29
+# A1 pin for data.
30
+# Instantiate a GP2Y0A on analog pin A1
31
+myIRProximity = upmGp2y0a.GP2Y0A(1)
32
+
33
+
34
+## Exit handlers ##
35
+# This stops python from printing a stacktrace when you hit control-C
36
+def SIGINTHandler(signum, frame):
37
+	raise SystemExit
38
+
39
+# This lets you run code on exit,
40
+# including functions from myIRProximity
41
+def exitHandler():
42
+	print "Exiting"
43
+	sys.exit(0)
44
+
45
+# Register exit handlers
46
+atexit.register(exitHandler)
47
+signal.signal(signal.SIGINT, SIGINTHandler)
48
+
49
+
50
+# analog voltage, usually 3.3 or 5.0
51
+GP2Y0A_AREF = 5.0;
52
+SAMPLES_PER_QUERY = 20;
53
+
54
+# The higher the voltage (closer to AREF) the closer the object is.
55
+# NOTE: The measured voltage will probably not exceed 3.3 volts.
56
+# Every second, print the averaged voltage value
57
+# (averaged over 20 samples).
58
+while (1):
59
+	print "AREF: {0}, Voltage value (higher means closer): {1}".format(
60
+	GP2Y0A_AREF,
61
+	myIRProximity.value(GP2Y0A_AREF, SAMPLES_PER_QUERY))
62
+	time.sleep(1)

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

@@ -0,0 +1,5 @@
1
+set (libname "gp2y0a")
2
+set (libdescription "upm gp2y0a family of IR distance detectors")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

src/gp2y0a21yk/gp2y0a21yk.cxx → src/gp2y0a/gp2y0a.cxx View File

@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * Author: Jon Trulson <jtrulson@ics.com>
3
- * Copyright (c) 2014 Intel Corporation.
3
+ * Copyright (c) 2015 Intel Corporation.
4 4
  *
5 5
  * Permission is hereby granted, free of charge, to any person obtaining
6 6
  * a copy of this software and associated documentation files (the
@@ -24,23 +24,29 @@
24 24
 
25 25
 #include <iostream>
26 26
 
27
-#include "gp2y0a21yk.h"
27
+#include "gp2y0a.h"
28 28
 
29
+using namespace std;
29 30
 using namespace upm;
30 31
 
31
-GP2Y0A21YK::GP2Y0A21YK(int pin)
32
+GP2Y0A::GP2Y0A(int pin)
32 33
 {
33
-  mraa_init();
34
+  if (!(m_aio = mraa_aio_init(pin)))
35
+    {
36
+      cerr << __FUNCTION__ << "mraa_aio_init() failed." << endl;
37
+      return;
38
+    }
34 39
 
35
-  m_aio = mraa_aio_init(pin);
40
+  // get the ADC resolution
41
+  m_aRes = (1 << mraa_aio_get_bit(m_aio));
36 42
 }
37 43
 
38
-GP2Y0A21YK::~GP2Y0A21YK()
44
+GP2Y0A::~GP2Y0A()
39 45
 {
40 46
   mraa_aio_close(m_aio);
41 47
 }
42 48
 
43
-float GP2Y0A21YK::value(float aref, uint8_t samples)
49
+float GP2Y0A::value(float aref, uint8_t samples)
44 50
 {
45 51
   int val;
46 52
   int sum = 0;
@@ -52,7 +58,7 @@ float GP2Y0A21YK::value(float aref, uint8_t samples)
52 58
     }
53 59
 
54 60
   val = sum / samples;
55
-  float volts = (float)val * aref / 1024.0;
61
+  float volts = float(val) * aref / float(m_aRes);
56 62
 
57 63
   return volts;
58 64
 }

src/gp2y0a21yk/gp2y0a21yk.h → src/gp2y0a/gp2y0a.h View File

@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * Author: Jon Trulson <jtrulson@ics.com>
3
- * Copyright (c) 2014 Intel Corporation.
3
+ * Copyright (c) 2015 Intel Corporation.
4 4
  *
5 5
  * Permission is hereby granted, free of charge, to any person obtaining
6 6
  * a copy of this software and associated documentation files (the
@@ -23,36 +23,49 @@
23 23
  */
24 24
 #pragma once
25 25
 
26
+#include <iostream>
26 27
 #include <string>
27 28
 #include <mraa/aio.h>
28 29
 
29 30
 namespace upm {
31
+  /**
32
+   * @brief UPM library for the GP2Y0A family of IR Proximity Sensors
33
+   * @defgroup gp2y0a libupm-gp2y0a
34
+   * @ingroup seeed analog light
35
+   */
30 36
 
31 37
   /**
32
-   * @brief C++ API for the GP2Y0A21YK 80cm IR Proximity Sensor
38
+   * @sensor gp2y0a
39
+   * @library gp2y0a
40
+   * @name GP2Y0A family of IR Proximity Sensors
41
+   * @category light
42
+   * @manufacturer seeed
43
+   * @connection analog
44
+   *
45
+   * @brief C++ API for the GP2Y0A family of IR Proximity Sensors
33 46
    *
34
-   * UPM module for the GP2Y0A21YK 80cm IR Proximity Sensor.
35
-   * This sensor returns an analog voltage corresponding to the distance 
36
-   * of an object from the sensor.  The object may be 10 cm (4 inches)
37
-   * to 80 cm (30 inches) away.  The voltage is low when objects
47
+   * This sensor family returns an analog voltage corresponding to the distance 
48
+   * of an object from the sensor.  The voltage is lower when objects
38 49
    * are far away; the voltage increases as objects get closer
39
-   * to the sensor.
50
+   * to the sensor.  
40 51
    *
41
-   * @ingroup analog
42
-   * @snippet gp2y0a21yk.cxx Interesting
52
+   * @snippet gp2y0a.cxx Interesting
43 53
    */
44
-  class GP2Y0A21YK {
54
+  class GP2Y0A {
45 55
   public:
56
+
46 57
     /**
47
-     * GP2Y0A21YK sensor constructor
58
+     * GP2Y0A sensor constructor
48 59
      *
49 60
      * @param pin analog pin to use
50 61
      */
51
-    GP2Y0A21YK(int pin);
62
+    GP2Y0A(int pin);
63
+
52 64
     /**
53
-     * GP2Y0A21YK Destructor
65
+     * GP2Y0A Destructor
54 66
      */
55
-    ~GP2Y0A21YK();
67
+    ~GP2Y0A();
68
+
56 69
     /**
57 70
      * Get the averaged voltage value from the sensor
58 71
      *
@@ -64,6 +77,8 @@ namespace upm {
64 77
 
65 78
   private:
66 79
     mraa_aio_context m_aio;
80
+    // ADC resolution
81
+    int m_aRes;
67 82
   };
68 83
 }
69 84
 

+ 8
- 0
src/gp2y0a/jsupm_gp2y0a.i View File

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

+ 9
- 0
src/gp2y0a/pyupm_gp2y0a.i View File

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

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

@@ -1,5 +0,0 @@
1
-set (libname "gp2y0a21yk")
2
-set (libdescription "upm gp2y0a21yk 80cm IR proximity sensor module")
3
-set (module_src ${libname}.cxx)
4
-set (module_h ${libname}.h)
5
-upm_module_init()

+ 0
- 8
src/gp2y0a21yk/jsupm_gp2y0a21yk.i View File

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

+ 0
- 9
src/gp2y0a21yk/pyupm_gp2y0a21yk.i View File

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