to compare with V6, do not change.

Dependencies:   mbed

Committer:
obrie829
Date:
Fri May 26 12:33:39 2017 +0000
Revision:
0:01c109e18d49
RobotV5 in working order

Who changed what in which revision?

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