Prometheus / IRSensorLib

Dependents:   Prom_Roebi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IRSensor.cpp Source File

IRSensor.cpp

00001 #include "mbed.h"
00002 #include <cmath>
00003 #include "IRSensor.h"
00004 
00005 
00006 IRSensor::IRSensor(AnalogIn* distance, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number)
00007 {
00008     init(distance, bit0, bit1, bit2, number);
00009 }
00010 
00011 
00012 IRSensor::IRSensor()
00013 {
00014 }
00015 
00016 void IRSensor::init(AnalogIn* distance, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number)
00017 {
00018 
00019     this->distance = distance;  // set local references to objects
00020     this->bit0 = bit0;
00021     this->bit1 = bit1;
00022     this->bit2 = bit2;
00023 
00024     this->number = number;
00025 }
00026 
00027 
00028 /**
00029  * Deletes the IRSensor object.
00030  */
00031 IRSensor::~IRSensor() {}
00032 
00033 /**
00034  * Gets the distance measured with the IR sensor in [m].
00035  * @return the distance, given in [m].
00036  */
00037 float IRSensor::read()
00038 {
00039     *bit0 = (number >> 0) & 1;
00040     *bit1 = (number >> 1) & 1;
00041     *bit2 = (number >> 2) & 1;
00042 
00043     float d = -0.58f*sqrt(distance->read())+0.58f;  // calculate the distance in [m]
00044     return d;
00045 }
00046 
00047 /**
00048  * The empty operator is a shorthand notation of the <code>read()</code> method.
00049  */
00050 IRSensor::operator float()
00051 {
00052     return read();
00053 }
00054 
00055 
00056     
00057