Sharp GP2 familly distance sensor library

Dependents:   FRC_2018 0hackton_08_06_18 0hackton_08_06_18_publish Kenya_2019 ... more

Files at this revision

API Documentation at this revision

Comitter:
haarkon
Date:
Sun Oct 13 13:35:59 2019 +0000
Parent:
4:4f443a6a6843
Child:
6:36744d1b3127
Commit message:
Added origin parameter to allow a good slope control of the v=f(1/d) slope.

Changed in this revision

GP2A.cpp Show annotated file Show diff for this revision Revisions of this file
GP2A.h Show annotated file Show diff for this revision Revisions of this file
--- a/GP2A.cpp	Thu May 31 17:25:25 2018 +0000
+++ b/GP2A.cpp	Sun Oct 13 13:35:59 2019 +0000
@@ -1,16 +1,17 @@
 #include "GP2A.h"
 
-GP2A::GP2A(PinName vmes, float dMin, float dMax, float slope) : _sensor(vmes)
+GP2A::GP2A(PinName vmes, float dMin, float dMax, float slope, float origin) : _sensor(vmes)
 {
     m_dMin = dMin;
     m_dMax = dMax;
     m_slope = slope;
+    m_origin = origin;
 }
 
 double GP2A::getDistance (void)
 {
     double vDist = (double)_sensor.read()* 3.3;
-    float distance = m_slope/vDist;
+    float distance = m_slope/(vDist-m_origin);
     if (distance > m_dMax) return m_dMax;
     if (distance < m_dMin) return m_dMin;
     return distance;
--- a/GP2A.h	Thu May 31 17:25:25 2018 +0000
+++ b/GP2A.h	Sun Oct 13 13:35:59 2019 +0000
@@ -50,8 +50,10 @@
  * Slope definitions for common GP2A sensors
  */
  
-#define GP2Y0A02YK0F    55.0
-#define GP2Y0A21YK0F    10
+#define GP2Y0A02YK0F_SLOPE    60.0
+#define GP2Y0A21YK0F_SLOPE    0.23625
+#define GP2Y0A02YK0F_ORIGIN   0.0
+#define GP2Y0A21YK0F_ORIGIN   -0.297
 
 /**
  * Includes : Mbed library
@@ -73,9 +75,10 @@
      * @param dMin (double) : the GP2A sensor min distance to mesure
      * @param dMax (double) : the GP2A sensor max distance to mesure
      * @param slope (double) : the slope of the linear part of the graph V = f(1/d)
-     * @note you better use slope value that I've put above
+     * @param origin (double) : the origin (Y for X=0) of the linear pat of the graph V = f(1/d)
+     * @note you better use slope and origin value that I've put above as sybolic constants
      */
-    GP2A(PinName vmes, float dMin, float dMax, float slope);
+    GP2A(PinName vmes, float dMin, float dMax, float slope, float origin = 0);
 
     /**
      * Return the distance to target mesured by sensor in cm
@@ -101,7 +104,7 @@
     
 
 private :
-    float m_dMin, m_dMax, m_slope;
+    float m_dMin, m_dMax, m_slope, m_origin;
 
 protected :