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