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