IRSensor.cpp@1:6709a7dfda65, 2017-05-20 (annotated)
- Committer:
- ZHAW_Prometheus
- Date:
- Sat May 20 21:46:08 2017 +0000
- Revision:
- 1:6709a7dfda65
- Parent:
- 0:667a753ef94f
Vers. 20.05.2017 23:45
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ZHAW_Prometheus | 0:667a753ef94f | 1 | #include "mbed.h" |
ZHAW_Prometheus | 0:667a753ef94f | 2 | #include <cmath> |
ZHAW_Prometheus | 0:667a753ef94f | 3 | #include "IRSensor.h" |
ZHAW_Prometheus | 0:667a753ef94f | 4 | |
ZHAW_Prometheus | 0:667a753ef94f | 5 | |
ZHAW_Prometheus | 0:667a753ef94f | 6 | IRSensor::IRSensor(AnalogIn* distance, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number) |
ZHAW_Prometheus | 0:667a753ef94f | 7 | { |
ZHAW_Prometheus | 0:667a753ef94f | 8 | init(distance, bit0, bit1, bit2, number); |
ZHAW_Prometheus | 0:667a753ef94f | 9 | } |
ZHAW_Prometheus | 0:667a753ef94f | 10 | |
ZHAW_Prometheus | 0:667a753ef94f | 11 | |
ZHAW_Prometheus | 0:667a753ef94f | 12 | IRSensor::IRSensor() |
ZHAW_Prometheus | 0:667a753ef94f | 13 | { |
ZHAW_Prometheus | 0:667a753ef94f | 14 | } |
ZHAW_Prometheus | 0:667a753ef94f | 15 | |
ZHAW_Prometheus | 0:667a753ef94f | 16 | void IRSensor::init(AnalogIn* distance, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number) |
ZHAW_Prometheus | 0:667a753ef94f | 17 | { |
ZHAW_Prometheus | 0:667a753ef94f | 18 | |
ZHAW_Prometheus | 0:667a753ef94f | 19 | this->distance = distance; // set local references to objects |
ZHAW_Prometheus | 0:667a753ef94f | 20 | this->bit0 = bit0; |
ZHAW_Prometheus | 0:667a753ef94f | 21 | this->bit1 = bit1; |
ZHAW_Prometheus | 0:667a753ef94f | 22 | this->bit2 = bit2; |
ZHAW_Prometheus | 0:667a753ef94f | 23 | |
ZHAW_Prometheus | 0:667a753ef94f | 24 | this->number = number; |
ZHAW_Prometheus | 0:667a753ef94f | 25 | } |
ZHAW_Prometheus | 0:667a753ef94f | 26 | |
ZHAW_Prometheus | 0:667a753ef94f | 27 | |
ZHAW_Prometheus | 0:667a753ef94f | 28 | /** |
ZHAW_Prometheus | 0:667a753ef94f | 29 | * Deletes the IRSensor object. |
ZHAW_Prometheus | 0:667a753ef94f | 30 | */ |
ZHAW_Prometheus | 0:667a753ef94f | 31 | IRSensor::~IRSensor() {} |
ZHAW_Prometheus | 0:667a753ef94f | 32 | |
ZHAW_Prometheus | 0:667a753ef94f | 33 | /** |
ZHAW_Prometheus | 0:667a753ef94f | 34 | * Gets the distance measured with the IR sensor in [m]. |
ZHAW_Prometheus | 0:667a753ef94f | 35 | * @return the distance, given in [m]. |
ZHAW_Prometheus | 0:667a753ef94f | 36 | */ |
ZHAW_Prometheus | 0:667a753ef94f | 37 | float IRSensor::read() |
ZHAW_Prometheus | 0:667a753ef94f | 38 | { |
ZHAW_Prometheus | 0:667a753ef94f | 39 | *bit0 = (number >> 0) & 1; |
ZHAW_Prometheus | 0:667a753ef94f | 40 | *bit1 = (number >> 1) & 1; |
ZHAW_Prometheus | 0:667a753ef94f | 41 | *bit2 = (number >> 2) & 1; |
ZHAW_Prometheus | 0:667a753ef94f | 42 | |
ZHAW_Prometheus | 0:667a753ef94f | 43 | float d = -0.58f*sqrt(distance->read())+0.58f; // calculate the distance in [m] |
ZHAW_Prometheus | 0:667a753ef94f | 44 | return d; |
ZHAW_Prometheus | 0:667a753ef94f | 45 | } |
ZHAW_Prometheus | 0:667a753ef94f | 46 | |
ZHAW_Prometheus | 0:667a753ef94f | 47 | /** |
ZHAW_Prometheus | 0:667a753ef94f | 48 | * The empty operator is a shorthand notation of the <code>read()</code> method. |
ZHAW_Prometheus | 0:667a753ef94f | 49 | */ |
ZHAW_Prometheus | 0:667a753ef94f | 50 | IRSensor::operator float() |
ZHAW_Prometheus | 0:667a753ef94f | 51 | { |
ZHAW_Prometheus | 0:667a753ef94f | 52 | return read(); |
ZHAW_Prometheus | 0:667a753ef94f | 53 | } |
ZHAW_Prometheus | 0:667a753ef94f | 54 | |
ZHAW_Prometheus | 0:667a753ef94f | 55 | |
ZHAW_Prometheus | 0:667a753ef94f | 56 | |
ZHAW_Prometheus | 0:667a753ef94f | 57 |