Mechatronics Robotics / Mbed 2 deprecated robotV5

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IRSensor.cpp Source File

IRSensor.cpp

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