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