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