Sharp GP2 familly distance sensor library

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GP2A.cpp Source File

GP2A.cpp

00001 #include "GP2A.h"
00002 
00003 GP2A::GP2A(PinName vmes, float dMin, float dMax, float slope, float origin) : _sensor(vmes)
00004 {
00005     m_dMin = dMin;
00006     m_dMax = dMax;
00007     m_slope = slope;
00008     m_origin = origin;
00009 }
00010 
00011 double GP2A::getDistance (void)
00012 {
00013     double vDist = (double)_sensor.read()* 3.3;
00014     float distance = m_slope/(vDist-m_origin);
00015     if (distance > m_dMax) return m_dMax;
00016     if (distance < m_dMin) return m_dMin;
00017     return distance;
00018 }
00019 
00020 double GP2A::getVoltage (void)
00021 {
00022     return (double)_sensor.read()* 3.3;
00023 }
00024 
00025 GP2A::operator double() {
00026     return getDistance();
00027 }