P2 halbfertig

Fork of Library by St Knz

Committer:
kueenste
Date:
Sat Mar 10 13:47:28 2018 +0000
Revision:
2:6dd39662e6e5
Parent:
0:bb408887ab78
funktioniert soso

Who changed what in which revision?

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