Marco Oehler / Mbed 2 deprecated Lab2

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IRSensor.cpp Source File

IRSensor.cpp

00001 /*
00002  * IRSensor.h
00003  * Copyright (c) 2020, ZHAW
00004  * All rights reserved.
00005  */
00006 
00007 #include <cmath>
00008 #include "IRSensor.h"
00009 
00010 using namespace std;
00011 
00012 /**
00013  * Creates and initialises the driver to read the distance sensors.
00014  * @param distance the analog input to read a distance value from.
00015  * @param bit0 a digital output to control the multiplexer.
00016  * @param bit1 a digital output to control the multiplexer.
00017  * @param bit2 a digital output to control the multiplexer.
00018  * @param number the number of the sensor. This value must be between 0 and 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     this->number = number;
00023 }
00024 
00025 /**
00026  * Deletes this IRSensor object and releases all allocated resources.
00027  */
00028 IRSensor::~IRSensor() {}
00029 
00030 /**
00031  * This method reads from the distance sensor.
00032  * @return a distance value, given in [m].
00033  */
00034 float IRSensor::read() {
00035 
00036     bit0 = (number >> 0) & 1;
00037     bit1 = (number >> 1) & 1;
00038     bit2 = (number >> 2) & 1;
00039     
00040     float d = -0.58f*sqrt(distance)+0.58f; // calculate the distance in [m]
00041     
00042     return d;
00043 }
00044 
00045 /**
00046  * The empty operator is a shorthand notation of the <code>read()</code> method.
00047  */
00048 IRSensor::operator float() {
00049     
00050     return read();
00051 }
00052