Prometheus / IRSensor
Committer:
ZHAW_Prometheus
Date:
Tue Feb 28 15:28:07 2017 +0000
Revision:
0:f4862c7fd394
0.1;

Who changed what in which revision?

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