Marco Oehler
/
Lab3
ROME2 Lab3
IRSensor.cpp@0:6a4d3264c067, 2020-03-24 (annotated)
- Committer:
- oehlemar
- Date:
- Tue Mar 24 08:39:54 2020 +0000
- Revision:
- 0:6a4d3264c067
Lab3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
oehlemar | 0:6a4d3264c067 | 1 | /* |
oehlemar | 0:6a4d3264c067 | 2 | * IRSensor.h |
oehlemar | 0:6a4d3264c067 | 3 | * Copyright (c) 2020, ZHAW |
oehlemar | 0:6a4d3264c067 | 4 | * All rights reserved. |
oehlemar | 0:6a4d3264c067 | 5 | */ |
oehlemar | 0:6a4d3264c067 | 6 | |
oehlemar | 0:6a4d3264c067 | 7 | #include <cmath> |
oehlemar | 0:6a4d3264c067 | 8 | #include "IRSensor.h" |
oehlemar | 0:6a4d3264c067 | 9 | |
oehlemar | 0:6a4d3264c067 | 10 | using namespace std; |
oehlemar | 0:6a4d3264c067 | 11 | |
oehlemar | 0:6a4d3264c067 | 12 | /** |
oehlemar | 0:6a4d3264c067 | 13 | * Creates and initialises the driver to read the distance sensors. |
oehlemar | 0:6a4d3264c067 | 14 | * @param distance the analog input to read a distance value from. |
oehlemar | 0:6a4d3264c067 | 15 | * @param bit0 a digital output to control the multiplexer. |
oehlemar | 0:6a4d3264c067 | 16 | * @param bit1 a digital output to control the multiplexer. |
oehlemar | 0:6a4d3264c067 | 17 | * @param bit2 a digital output to control the multiplexer. |
oehlemar | 0:6a4d3264c067 | 18 | * @param number the number of the sensor. This value must be between 0 and 5. |
oehlemar | 0:6a4d3264c067 | 19 | */ |
oehlemar | 0:6a4d3264c067 | 20 | IRSensor::IRSensor(AnalogIn& distance, DigitalOut& bit0, DigitalOut& bit1, DigitalOut& bit2, int number) : distance(distance), bit0(bit0), bit1(bit1), bit2(bit2) { |
oehlemar | 0:6a4d3264c067 | 21 | |
oehlemar | 0:6a4d3264c067 | 22 | this->number = number; |
oehlemar | 0:6a4d3264c067 | 23 | } |
oehlemar | 0:6a4d3264c067 | 24 | |
oehlemar | 0:6a4d3264c067 | 25 | /** |
oehlemar | 0:6a4d3264c067 | 26 | * Deletes this IRSensor object and releases all allocated resources. |
oehlemar | 0:6a4d3264c067 | 27 | */ |
oehlemar | 0:6a4d3264c067 | 28 | IRSensor::~IRSensor() {} |
oehlemar | 0:6a4d3264c067 | 29 | |
oehlemar | 0:6a4d3264c067 | 30 | /** |
oehlemar | 0:6a4d3264c067 | 31 | * This method reads from the distance sensor. |
oehlemar | 0:6a4d3264c067 | 32 | * @return a distance value, given in [m]. |
oehlemar | 0:6a4d3264c067 | 33 | */ |
oehlemar | 0:6a4d3264c067 | 34 | float IRSensor::read() { |
oehlemar | 0:6a4d3264c067 | 35 | |
oehlemar | 0:6a4d3264c067 | 36 | bit0 = (number >> 0) & 1; |
oehlemar | 0:6a4d3264c067 | 37 | bit1 = (number >> 1) & 1; |
oehlemar | 0:6a4d3264c067 | 38 | bit2 = (number >> 2) & 1; |
oehlemar | 0:6a4d3264c067 | 39 | |
oehlemar | 0:6a4d3264c067 | 40 | float d = -0.58f*sqrt(distance)+0.58f; // calculate the distance in [m] |
oehlemar | 0:6a4d3264c067 | 41 | |
oehlemar | 0:6a4d3264c067 | 42 | return d; |
oehlemar | 0:6a4d3264c067 | 43 | } |
oehlemar | 0:6a4d3264c067 | 44 | |
oehlemar | 0:6a4d3264c067 | 45 | /** |
oehlemar | 0:6a4d3264c067 | 46 | * The empty operator is a shorthand notation of the <code>read()</code> method. |
oehlemar | 0:6a4d3264c067 | 47 | */ |
oehlemar | 0:6a4d3264c067 | 48 | IRSensor::operator float() { |
oehlemar | 0:6a4d3264c067 | 49 | |
oehlemar | 0:6a4d3264c067 | 50 | return read(); |
oehlemar | 0:6a4d3264c067 | 51 | } |
oehlemar | 0:6a4d3264c067 | 52 |