Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
IRSensor.cpp@1:08ca9b208045, 2017-03-31 (annotated)
- Committer:
- solcager
- Date:
- Fri Mar 31 11:00:19 2017 +0000
- Revision:
- 1:08ca9b208045
- Parent:
- 0:646b6cf24af2
P3
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| solcager | 1:08ca9b208045 | 1 | /* |
| solcager | 1:08ca9b208045 | 2 | * IRSensor.cpp |
| solcager | 1:08ca9b208045 | 3 | * Copyright (c) 2017, ZHAW |
| solcager | 1:08ca9b208045 | 4 | * All rights reserved. |
| solcager | 1:08ca9b208045 | 5 | */ |
| favereli | 0:646b6cf24af2 | 6 | |
| favereli | 0:646b6cf24af2 | 7 | #include <cmath> |
| solcager | 1:08ca9b208045 | 8 | #include "IRSensor.h" |
| favereli | 0:646b6cf24af2 | 9 | |
| favereli | 0:646b6cf24af2 | 10 | using namespace std; |
| favereli | 0:646b6cf24af2 | 11 | |
| solcager | 1:08ca9b208045 | 12 | /** |
| solcager | 1:08ca9b208045 | 13 | * Creates an IRSensor object. |
| solcager | 1:08ca9b208045 | 14 | * @param distance an analog input object to read the voltage of the sensor. |
| solcager | 1:08ca9b208045 | 15 | * @param bit0 a digital output to set the first bit of the multiplexer. |
| solcager | 1:08ca9b208045 | 16 | * @param bit1 a digital output to set the second bit of the multiplexer. |
| solcager | 1:08ca9b208045 | 17 | * @param bit2 a digital output to set the third bit of the multiplexer. |
| solcager | 1:08ca9b208045 | 18 | * @param number the number of the sensor, either 0, 1, 2, 3, 4 or 5. |
| solcager | 1:08ca9b208045 | 19 | */ |
| solcager | 1:08ca9b208045 | 20 | IRSensor::IRSensor(AnalogIn& distance, DigitalOut& bit0, DigitalOut& bit1, DigitalOut& bit2, int number) : distance(distance), bit0(bit0), bit1(bit1), bit2(bit2) { |
| solcager | 1:08ca9b208045 | 21 | |
| solcager | 1:08ca9b208045 | 22 | // set local references to objects |
| solcager | 1:08ca9b208045 | 23 | |
| favereli | 0:646b6cf24af2 | 24 | this->number = number; |
| favereli | 0:646b6cf24af2 | 25 | } |
| favereli | 0:646b6cf24af2 | 26 | |
| solcager | 1:08ca9b208045 | 27 | /** |
| solcager | 1:08ca9b208045 | 28 | * Deletes the IRSensor object. |
| solcager | 1:08ca9b208045 | 29 | */ |
| favereli | 0:646b6cf24af2 | 30 | IRSensor::~IRSensor() {} |
| favereli | 0:646b6cf24af2 | 31 | |
| solcager | 1:08ca9b208045 | 32 | /** |
| solcager | 1:08ca9b208045 | 33 | * Gets the distance measured with the IR sensor in [m]. |
| solcager | 1:08ca9b208045 | 34 | * @return the distance, given in [m]. |
| solcager | 1:08ca9b208045 | 35 | */ |
| favereli | 0:646b6cf24af2 | 36 | float IRSensor::read() { |
| solcager | 1:08ca9b208045 | 37 | |
| solcager | 1:08ca9b208045 | 38 | bit0 = (number >> 0) & 1; |
| solcager | 1:08ca9b208045 | 39 | bit1 = (number >> 1) & 1; |
| solcager | 1:08ca9b208045 | 40 | bit2 = (number >> 2) & 1; |
| solcager | 1:08ca9b208045 | 41 | |
| solcager | 1:08ca9b208045 | 42 | float d = -0.58f*sqrt(distance)+0.58f; // calculate the distance in [m] |
| favereli | 0:646b6cf24af2 | 43 | |
| favereli | 0:646b6cf24af2 | 44 | return d; |
| solcager | 1:08ca9b208045 | 45 | } |
| solcager | 1:08ca9b208045 | 46 | |
| solcager | 1:08ca9b208045 | 47 | /** |
| solcager | 1:08ca9b208045 | 48 | * The empty operator is a shorthand notation of the <code>read()</code> method. |
| solcager | 1:08ca9b208045 | 49 | */ |
| solcager | 1:08ca9b208045 | 50 | IRSensor::operator float() { |
| solcager | 1:08ca9b208045 | 51 | |
| solcager | 1:08ca9b208045 | 52 | return read(); |
| solcager | 1:08ca9b208045 | 53 | } |
| solcager | 1:08ca9b208045 | 54 |