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