Tobis Programm forked to not destroy your golden files

Dependencies:   mbed

Fork of Robocode by PES 2 - Gruppe 1

source/IRSensor.cpp

Committer:
aeschsim
Date:
2017-05-03
Revision:
98:07a8a858e7cd
Parent:
95:5afdfe17300f
Child:
99:78d87027c85b

File content as of revision 98:07a8a858e7cd:

/*
 * IRSensor.cpp
 * Copyright (c) 2016, ZHAW
 * All rights reserved.
 */

#include <cmath>
#include "IRSensor.h"

#define cycles 25

/**
 * Creates an IRSensor object.
 * @param distance an analog input object to read the voltage of the sensor.
 * @param bit0 a digital output to set the first bit of the multiplexer.
 * @param bit1 a digital output to set the second bit of the multiplexer.
 * @param bit2 a digital output to set the third bit of the multiplexer.
 * @param number the number of the sensor, either 0, 1, 2, 3, 4 or 5.
 */
IRSensor::IRSensor(AnalogIn* distance,AnalogIn* distance2, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number)
{
    init(distance,distance2, bit0, bit1, bit2, number);
}


IRSensor::IRSensor()
{
}

void IRSensor::init(AnalogIn* distance,AnalogIn* distance2, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number)
{

    this->distance = distance;  // set local references to objects
    this->distance2 = distance2;
    this->bit0 = bit0;
    this->bit1 = bit1;
    this->bit2 = bit2;

    this->number = number;
}

/**
 * Deletes the IRSensor object.
 */
IRSensor::~IRSensor() {}

/**
read voltage of all sensor for Lowpassfilter
*/
void IRSensor::voltage()
{

    if(number == 3) {
        *bit0 = 0;
        *bit1 = 0;
        *bit2 = 1;
    } else {
        *bit0 = (number >> 0) & 1;
        *bit1 = (number >> 1) & 1;
        *bit2 = (number >> 2) & 1;
    }

    float voltage = 0;
    if(number == 4) {
        voltage = distance2->read();
    } else {
        voltage = distance->read();
    }
    mean = 0.8f*mean + 0.2f*voltage;
}


/**
 * Gets the distance measured with the IR sensor in [m].
 * @return the distance, given in [m].
 */

float IRSensor::read()
{
    float d = 0;
    switch (number) {
        case 0:
            d  = 0.18f/pow((1.5f*mean-0.1f),0.7f)-0.095f;
            break;
        case 1:
            d  = 0.18f/pow((1.5f*mean-0.1f),0.7f)-0.095f;
            break;
        case 2:
            d  = 0.095f/pow((0.94f*mean-0.05f),0.8f)-0.04f;
            break;
        case 3:
            d  = 0.192f/pow((1.8f*mean-0.13f),0.7f)-0.095f;
            break;
        case 4:
            d  = 0.14f/pow((1.2f*mean-0.09f),0.7f)-0.088f;
            break;
        case 5:
            d  = 0.18f/pow((1.55f*mean-0.12f),0.7f)-0.095f;
            break;
        default:
            break;
    }
    return d;
}



/**
 * The empty operator is a shorthand notation of the <code>read()</code> method.
 */
IRSensor::operator float()
{
    return read();
}