P1

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IRSensor.cpp Source File

IRSensor.cpp

00001 /*
00002  * IRSensor.h
00003  * Copyright (c) 2020, ZHAW
00004  * All rights reserved.
00005  */
00006 
00007 #include <cmath>
00008 #include "IRSensor.h"
00009 
00010 using namespace std;
00011 
00012 /**
00013  * Creates and initialises the driver to read the distance sensors.
00014  * @param distance the analog input to read a distance value from.
00015  * @param bit0 a digital output to control the multiplexer.
00016  * @param bit1 a digital output to control the multiplexer.
00017  * @param bit2 a digital output to control the multiplexer.
00018  * @param number the number of the sensor. This value must be between 0 and 5.
00019  */
00020 IRSensor::IRSensor(AnalogIn& distance, DigitalOut& bit0, DigitalOut& bit1, DigitalOut& bit2, int number) : distance(distance), bit0(bit0), bit1(bit1), bit2(bit2) {
00021     
00022     this->number = number;
00023 }
00024 
00025 /**
00026  * Deletes this IRSensor object and releases all allocated resources.
00027  */
00028 IRSensor::~IRSensor() {}
00029 
00030 /**
00031  * This method reads from the distance sensor.
00032  * @return a distance value, given in [m].
00033  */
00034 float IRSensor::read() {
00035 
00036     // bitte implementieren!
00037 
00038     float d;
00039     switch(number) {
00040         case 0:
00041             bit0 = 0; // Wahl des Sensors mit dem Multiplexer (Sensor hinten)
00042             bit1 = 0;
00043             bit2 = 0;
00044             break;
00045         case 1:
00046             bit0 = 1; // Wahl des Sensors mit dem Multiplexer (Sensor hinten)
00047             bit1 = 0;
00048             bit2 = 0;
00049             break;
00050         case 2:
00051             bit0 = 0; // Wahl des Sensors mit dem Multiplexer (Sensor hinten)
00052             bit1 = 1;
00053             bit2 = 0;
00054             break;
00055         case 3:
00056             bit0 = 1; // Wahl des Sensors mit dem Multiplexer (Sensor hinten)
00057             bit1 = 1;
00058             bit2 = 0;
00059             break;
00060         case 4:
00061             bit0 = 0; // Wahl des Sensors mit dem Multiplexer (Sensor hinten)
00062             bit1 = 0;
00063             bit2 = 1;
00064             break;
00065         case 5:
00066             bit0 = 1; // Wahl des Sensors mit dem Multiplexer (Sensor hinten)
00067             bit1 = 0;
00068             bit2 = 1;
00069             break;
00070             
00071             }
00072             
00073     d = -0.58f*sqrt(distance)+0.58f; // Lesen der Distanz in [m]
00074 return d;
00075 }