TeamSurface / Library

Fork of Library by St Knz

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IRSensor.cpp Source File

IRSensor.cpp

00001 /*
00002  * IRSensor.cpp
00003  * Copyright (c) 2018, ZHAW
00004  * All rights reserved.
00005  */
00006 
00007 #include <cmath>
00008 #include "IRSensor.h"
00009 
00010 using namespace std;
00011 
00012 /**
00013  * Creates an IRSensor object.
00014  * @param distance an analog input object to read the voltage of the sensor.
00015  * @param bit0 a digital output to set the first bit of the multiplexer.
00016  * @param bit1 a digital output to set the second bit of the multiplexer.
00017  * @param bit2 a digital output to set the third bit of the multiplexer.
00018  * @param number the number of the sensor, either 0, 1, 2, 3, 4 or 5.
00019  */
00020 IRSensor::IRSensor(AnalogIn& distance, DigitalOut& bit0, DigitalOut& bit1, DigitalOut& bit2, int number) : distance(distance), bit0(bit0), bit1(bit1), bit2(bit2) {
00021     
00022     // set local references to objects
00023     
00024     this->number = number;
00025 }
00026 
00027 /**
00028  * Deletes the IRSensor object.
00029  */
00030 IRSensor::~IRSensor() {}
00031 
00032 /**
00033  * Gets the distance measured with the IR sensor in [m].
00034  * @return the distance, given in [m].
00035  */
00036 float IRSensor::read() {
00037     
00038     bit0 = (number >> 0) & 1;
00039     bit1 = (number >> 1) & 1;
00040     bit2 = (number >> 2) & 1;
00041     
00042     float d = -0.58f*sqrt(distance)+0.58f;  // calculate the distance in [m]
00043     
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