Gruppe 3 / Mbed 2 deprecated PES3

Dependencies:   Servo mbed pixy

Fork of PES1 by Gruppe 3

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IRSensor.cpp Source File

IRSensor.cpp

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