Marco Oehler / Mbed 2 deprecated Lab2

Dependencies:   mbed

Committer:
oehlemar
Date:
Mon Mar 09 16:23:04 2020 +0000
Revision:
0:1a972ed770da
LAB2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oehlemar 0:1a972ed770da 1 /*
oehlemar 0:1a972ed770da 2 * IRSensor.h
oehlemar 0:1a972ed770da 3 * Copyright (c) 2020, ZHAW
oehlemar 0:1a972ed770da 4 * All rights reserved.
oehlemar 0:1a972ed770da 5 */
oehlemar 0:1a972ed770da 6
oehlemar 0:1a972ed770da 7 #include <cmath>
oehlemar 0:1a972ed770da 8 #include "IRSensor.h"
oehlemar 0:1a972ed770da 9
oehlemar 0:1a972ed770da 10 using namespace std;
oehlemar 0:1a972ed770da 11
oehlemar 0:1a972ed770da 12 /**
oehlemar 0:1a972ed770da 13 * Creates and initialises the driver to read the distance sensors.
oehlemar 0:1a972ed770da 14 * @param distance the analog input to read a distance value from.
oehlemar 0:1a972ed770da 15 * @param bit0 a digital output to control the multiplexer.
oehlemar 0:1a972ed770da 16 * @param bit1 a digital output to control the multiplexer.
oehlemar 0:1a972ed770da 17 * @param bit2 a digital output to control the multiplexer.
oehlemar 0:1a972ed770da 18 * @param number the number of the sensor. This value must be between 0 and 5.
oehlemar 0:1a972ed770da 19 */
oehlemar 0:1a972ed770da 20 IRSensor::IRSensor(AnalogIn& distance, DigitalOut& bit0, DigitalOut& bit1, DigitalOut& bit2, int number) : distance(distance), bit0(bit0), bit1(bit1), bit2(bit2) {
oehlemar 0:1a972ed770da 21
oehlemar 0:1a972ed770da 22 this->number = number;
oehlemar 0:1a972ed770da 23 }
oehlemar 0:1a972ed770da 24
oehlemar 0:1a972ed770da 25 /**
oehlemar 0:1a972ed770da 26 * Deletes this IRSensor object and releases all allocated resources.
oehlemar 0:1a972ed770da 27 */
oehlemar 0:1a972ed770da 28 IRSensor::~IRSensor() {}
oehlemar 0:1a972ed770da 29
oehlemar 0:1a972ed770da 30 /**
oehlemar 0:1a972ed770da 31 * This method reads from the distance sensor.
oehlemar 0:1a972ed770da 32 * @return a distance value, given in [m].
oehlemar 0:1a972ed770da 33 */
oehlemar 0:1a972ed770da 34 float IRSensor::read() {
oehlemar 0:1a972ed770da 35
oehlemar 0:1a972ed770da 36 bit0 = (number >> 0) & 1;
oehlemar 0:1a972ed770da 37 bit1 = (number >> 1) & 1;
oehlemar 0:1a972ed770da 38 bit2 = (number >> 2) & 1;
oehlemar 0:1a972ed770da 39
oehlemar 0:1a972ed770da 40 float d = -0.58f*sqrt(distance)+0.58f; // calculate the distance in [m]
oehlemar 0:1a972ed770da 41
oehlemar 0:1a972ed770da 42 return d;
oehlemar 0:1a972ed770da 43 }
oehlemar 0:1a972ed770da 44
oehlemar 0:1a972ed770da 45 /**
oehlemar 0:1a972ed770da 46 * The empty operator is a shorthand notation of the <code>read()</code> method.
oehlemar 0:1a972ed770da 47 */
oehlemar 0:1a972ed770da 48 IRSensor::operator float() {
oehlemar 0:1a972ed770da 49
oehlemar 0:1a972ed770da 50 return read();
oehlemar 0:1a972ed770da 51 }
oehlemar 0:1a972ed770da 52