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