ROME_P5

Dependencies:   mbed

Committer:
Inaueadr
Date:
Fri Apr 27 08:47:34 2018 +0000
Revision:
0:29be10cb0afc
Hallo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Inaueadr 0:29be10cb0afc 1 /*
Inaueadr 0:29be10cb0afc 2 * IRSensor.cpp
Inaueadr 0:29be10cb0afc 3 * Copyright (c) 2018, ZHAW
Inaueadr 0:29be10cb0afc 4 * All rights reserved.
Inaueadr 0:29be10cb0afc 5 */
Inaueadr 0:29be10cb0afc 6
Inaueadr 0:29be10cb0afc 7 #include <cmath>
Inaueadr 0:29be10cb0afc 8 #include "IRSensor.h"
Inaueadr 0:29be10cb0afc 9
Inaueadr 0:29be10cb0afc 10 using namespace std;
Inaueadr 0:29be10cb0afc 11
Inaueadr 0:29be10cb0afc 12 /**
Inaueadr 0:29be10cb0afc 13 * Creates an IRSensor object.
Inaueadr 0:29be10cb0afc 14 * @param distance an analog input object to read the voltage of the sensor.
Inaueadr 0:29be10cb0afc 15 * @param bit0 a digital output to set the first bit of the multiplexer.
Inaueadr 0:29be10cb0afc 16 * @param bit1 a digital output to set the second bit of the multiplexer.
Inaueadr 0:29be10cb0afc 17 * @param bit2 a digital output to set the third bit of the multiplexer.
Inaueadr 0:29be10cb0afc 18 * @param number the number of the sensor, either 0, 1, 2, 3, 4 or 5.
Inaueadr 0:29be10cb0afc 19 */
Inaueadr 0:29be10cb0afc 20 IRSensor::IRSensor(AnalogIn& distance, DigitalOut& bit0, DigitalOut& bit1, DigitalOut& bit2, int number) : distance(distance), bit0(bit0), bit1(bit1), bit2(bit2) {
Inaueadr 0:29be10cb0afc 21
Inaueadr 0:29be10cb0afc 22 // set local references to objects
Inaueadr 0:29be10cb0afc 23
Inaueadr 0:29be10cb0afc 24 this->number = number;
Inaueadr 0:29be10cb0afc 25 }
Inaueadr 0:29be10cb0afc 26
Inaueadr 0:29be10cb0afc 27 /**
Inaueadr 0:29be10cb0afc 28 * Deletes the IRSensor object.
Inaueadr 0:29be10cb0afc 29 */
Inaueadr 0:29be10cb0afc 30 IRSensor::~IRSensor() {}
Inaueadr 0:29be10cb0afc 31
Inaueadr 0:29be10cb0afc 32 /**
Inaueadr 0:29be10cb0afc 33 * Gets the distance measured with the IR sensor in [m].
Inaueadr 0:29be10cb0afc 34 * @return the distance, given in [m].
Inaueadr 0:29be10cb0afc 35 */
Inaueadr 0:29be10cb0afc 36 float IRSensor::read() {
Inaueadr 0:29be10cb0afc 37
Inaueadr 0:29be10cb0afc 38 bit0 = (number >> 0) & 1;
Inaueadr 0:29be10cb0afc 39 bit1 = (number >> 1) & 1;
Inaueadr 0:29be10cb0afc 40 bit2 = (number >> 2) & 1;
Inaueadr 0:29be10cb0afc 41
Inaueadr 0:29be10cb0afc 42 float d = -0.58f*sqrt(distance)+0.58f; // calculate the distance in [m]
Inaueadr 0:29be10cb0afc 43
Inaueadr 0:29be10cb0afc 44 return d;
Inaueadr 0:29be10cb0afc 45 }
Inaueadr 0:29be10cb0afc 46
Inaueadr 0:29be10cb0afc 47 /**
Inaueadr 0:29be10cb0afc 48 * The empty operator is a shorthand notation of the <code>read()</code> method.
Inaueadr 0:29be10cb0afc 49 */
Inaueadr 0:29be10cb0afc 50 IRSensor::operator float() {
Inaueadr 0:29be10cb0afc 51
Inaueadr 0:29be10cb0afc 52 return read();
Inaueadr 0:29be10cb0afc 53 }
Inaueadr 0:29be10cb0afc 54
Inaueadr 0:29be10cb0afc 55