Sharp GP2 familly distance sensor library

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

GP2A.cpp

Committer:
haarkon
Date:
2018-05-21
Revision:
2:5e591a5b8edd
Parent:
0:17de10d278c2
Child:
4:4f443a6a6843

File content as of revision 2:5e591a5b8edd:

#include "GP2A.h"

GP2A::GP2A(PinName vmes, float dMin, float dMax, float slope) : _sensor(vmes)
{
    m_dMin = dMin;
    m_dMax = dMax;
    m_slope = slope;
}

double GP2A::getDistance (void)
{
    double vDist = _sensor.read()* 3.3;
    float distance = m_slope/vDist;
    if (distance > m_dMax) return m_dMax;
    if (distance < m_dMin) return m_dMin;
    return distance;
}

double GP2A::getVoltage (void)
{
    return _sensor.read()* 3.3;
}

GP2A::operator double() {
    return getDistance();
}