![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Roboter
IRSensor.cpp@0:306a2438de17, 2017-03-21 (annotated)
- Committer:
- itslinear
- Date:
- Tue Mar 21 12:42:01 2017 +0000
- Revision:
- 0:306a2438de17
hoiii
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
itslinear | 0:306a2438de17 | 1 | #include <cmath> |
itslinear | 0:306a2438de17 | 2 | #include "IRSensor.h" |
itslinear | 0:306a2438de17 | 3 | |
itslinear | 0:306a2438de17 | 4 | |
itslinear | 0:306a2438de17 | 5 | /** |
itslinear | 0:306a2438de17 | 6 | * Creates an IRSensor object. |
itslinear | 0:306a2438de17 | 7 | * @param distance an analog input object to read the voltage of the sensor. |
itslinear | 0:306a2438de17 | 8 | * @param bit0 a digital output to set the first bit of the multiplexer. |
itslinear | 0:306a2438de17 | 9 | * @param bit1 a digital output to set the second bit of the multiplexer. |
itslinear | 0:306a2438de17 | 10 | * @param bit2 a digital output to set the third bit of the multiplexer. |
itslinear | 0:306a2438de17 | 11 | * @param number the number of the sensor, either 0, 1, 2, 3, 4 or 5. |
itslinear | 0:306a2438de17 | 12 | */ |
itslinear | 0:306a2438de17 | 13 | IRSensor::IRSensor(AnalogIn* distance, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number) |
itslinear | 0:306a2438de17 | 14 | { |
itslinear | 0:306a2438de17 | 15 | init(distance, bit0, bit1, bit2, number); |
itslinear | 0:306a2438de17 | 16 | } |
itslinear | 0:306a2438de17 | 17 | |
itslinear | 0:306a2438de17 | 18 | |
itslinear | 0:306a2438de17 | 19 | IRSensor::IRSensor() |
itslinear | 0:306a2438de17 | 20 | { |
itslinear | 0:306a2438de17 | 21 | } |
itslinear | 0:306a2438de17 | 22 | |
itslinear | 0:306a2438de17 | 23 | void IRSensor::init(AnalogIn* distance, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number) |
itslinear | 0:306a2438de17 | 24 | { |
itslinear | 0:306a2438de17 | 25 | |
itslinear | 0:306a2438de17 | 26 | this->distance = distance; // set local references to objects |
itslinear | 0:306a2438de17 | 27 | this->bit0 = bit0; |
itslinear | 0:306a2438de17 | 28 | this->bit1 = bit1; |
itslinear | 0:306a2438de17 | 29 | this->bit2 = bit2; |
itslinear | 0:306a2438de17 | 30 | |
itslinear | 0:306a2438de17 | 31 | this->number = number; |
itslinear | 0:306a2438de17 | 32 | } |
itslinear | 0:306a2438de17 | 33 | |
itslinear | 0:306a2438de17 | 34 | |
itslinear | 0:306a2438de17 | 35 | /** |
itslinear | 0:306a2438de17 | 36 | * Deletes the IRSensor object. |
itslinear | 0:306a2438de17 | 37 | */ |
itslinear | 0:306a2438de17 | 38 | IRSensor::~IRSensor() {} |
itslinear | 0:306a2438de17 | 39 | |
itslinear | 0:306a2438de17 | 40 | /** |
itslinear | 0:306a2438de17 | 41 | * Gets the distance measured with the IR sensor in [m]. |
itslinear | 0:306a2438de17 | 42 | * @return the distance, given in [m]. |
itslinear | 0:306a2438de17 | 43 | */ |
itslinear | 0:306a2438de17 | 44 | float IRSensor::read() |
itslinear | 0:306a2438de17 | 45 | { |
itslinear | 0:306a2438de17 | 46 | *bit0 = (number >> 0) & 1; |
itslinear | 0:306a2438de17 | 47 | *bit1 = (number >> 1) & 1; |
itslinear | 0:306a2438de17 | 48 | *bit2 = (number >> 2) & 1; |
itslinear | 0:306a2438de17 | 49 | |
itslinear | 0:306a2438de17 | 50 | float d = -0.38f*sqrt(distance->read())+0.38f; // calculate the distance in [m] |
itslinear | 0:306a2438de17 | 51 | return d; |
itslinear | 0:306a2438de17 | 52 | } |
itslinear | 0:306a2438de17 | 53 | |
itslinear | 0:306a2438de17 | 54 | /** |
itslinear | 0:306a2438de17 | 55 | * The empty operator is a shorthand notation of the <code>read()</code> method. |
itslinear | 0:306a2438de17 | 56 | */ |
itslinear | 0:306a2438de17 | 57 | IRSensor::operator float() |
itslinear | 0:306a2438de17 | 58 | { |
itslinear | 0:306a2438de17 | 59 | |
itslinear | 0:306a2438de17 | 60 | return read(); |
itslinear | 0:306a2438de17 | 61 | } |