Example project

Dependencies:   PM2_Libary Eigen

Committer:
pmic
Date:
Tue May 10 10:04:51 2022 +0200
Revision:
37:698d6b73b50c
Child:
40:924bdbc33391
Included nesserary classes for ROME2 example from bleik

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmic 37:698d6b73b50c 1 /*
pmic 37:698d6b73b50c 2 * IRSensor.h
pmic 37:698d6b73b50c 3 * Copyright (c) 2022, ZHAW
pmic 37:698d6b73b50c 4 * All rights reserved.
pmic 37:698d6b73b50c 5 */
pmic 37:698d6b73b50c 6
pmic 37:698d6b73b50c 7 #include <cmath>
pmic 37:698d6b73b50c 8 #include "IRSensor.h"
pmic 37:698d6b73b50c 9
pmic 37:698d6b73b50c 10 using namespace std;
pmic 37:698d6b73b50c 11
pmic 37:698d6b73b50c 12 /**
pmic 37:698d6b73b50c 13 * Creates and initialises the driver to read the distance sensors.
pmic 37:698d6b73b50c 14 * @param distance the analog input to read a distance value from.
pmic 37:698d6b73b50c 15 * @param bit0 a digital output to control the multiplexer.
pmic 37:698d6b73b50c 16 * @param bit1 a digital output to control the multiplexer.
pmic 37:698d6b73b50c 17 * @param bit2 a digital output to control the multiplexer.
pmic 37:698d6b73b50c 18 * @param number the number of the sensor. This value must be between 0 and 5.
pmic 37:698d6b73b50c 19 */
pmic 37:698d6b73b50c 20 IRSensor::IRSensor(AnalogIn& distance, DigitalOut& bit0, DigitalOut& bit1, DigitalOut& bit2, int number) : distance(distance), bit0(bit0), bit1(bit1), bit2(bit2) {
pmic 37:698d6b73b50c 21
pmic 37:698d6b73b50c 22 this->number = number;
pmic 37:698d6b73b50c 23 }
pmic 37:698d6b73b50c 24
pmic 37:698d6b73b50c 25 /**
pmic 37:698d6b73b50c 26 * Deletes this IRSensor object and releases all allocated resources.
pmic 37:698d6b73b50c 27 */
pmic 37:698d6b73b50c 28 IRSensor::~IRSensor() {}
pmic 37:698d6b73b50c 29
pmic 37:698d6b73b50c 30 /**
pmic 37:698d6b73b50c 31 * This method reads from the distance sensor.
pmic 37:698d6b73b50c 32 * @return a distance value, given in [m].
pmic 37:698d6b73b50c 33 */
pmic 37:698d6b73b50c 34 float IRSensor::read() {
pmic 37:698d6b73b50c 35
pmic 37:698d6b73b50c 36 bit0 = (number >> 0) & 1;
pmic 37:698d6b73b50c 37 bit1 = (number >> 1) & 1;
pmic 37:698d6b73b50c 38 bit2 = (number >> 2) & 1;
pmic 37:698d6b73b50c 39
pmic 37:698d6b73b50c 40 float d = 0.09f/(distance+0.001f)-0.03f; // calculate the distance in [m]
pmic 37:698d6b73b50c 41
pmic 37:698d6b73b50c 42 return d;
pmic 37:698d6b73b50c 43 }
pmic 37:698d6b73b50c 44
pmic 37:698d6b73b50c 45 /**
pmic 37:698d6b73b50c 46 * The empty operator is a shorthand notation of the <code>read()</code> method.
pmic 37:698d6b73b50c 47 */
pmic 37:698d6b73b50c 48 IRSensor::operator float() {
pmic 37:698d6b73b50c 49
pmic 37:698d6b73b50c 50 return read();
pmic 37:698d6b73b50c 51 }