De sensorcontroller van het TLS2 project.

Dependencies:   mbed

main.cpp

Committer:
RichardHoekstra
Date:
2016-11-14
Revision:
1:c9fae063e6f3
Parent:
0:eea3cc9d2701
Child:
2:4c5952cf26d0

File content as of revision 1:c9fae063e6f3:

#include "mbed.h"


/* De sensor controller heeft de volgende taken
    1. Sensoren uitlezen
        1. De frequentie moet per sensor aanpasbaar zijn.
    2. Reageren op I2C data requests
    3. Indien mogelijk meerdere samples nemen van de hoogste prioriteit sensor
    
    De volgende 'dingen' moeten kunnen worden ingesteld
        1. 'Hoofd'sensor
        2. Sample rate _per_ sensor
        3. Moving Average van 'Hoofd'sensor
*/


void i2c_request(){
    //To be written    
}

        
float sensorVal[3] = {0}; 
//Index 0: druksensor waarde
//Index 1: flowsensor waarde
//Index 2: temperatuursenor waarde
#define druksensor 0
#define flowsensor 1
#define tempsensor 2

bool pressure_is_main = true; //Determine the most important sensor as in, on which value is the motor controller regulating
bool smoothing = true; //Determine to activate the moving average         



float calc_moving_average(float val, int samples = 10){
    static float sample_arr[samples] = {0}; //[0] is the newest
    float moving_average = 0;
    //Put the new val into the sample_arr and push out the oldest one
    for(int i=samples-1; i>0; i--){
        //[9]<-[8]<-[7]
        sample_arr[i] = sample_arr[i-1];
    }
    sample_arr[0] = val;
    //Calculate the moving average
    for(int i=0; i<samples; i++){
        moving_average += sample_arr[i]   
    }
    return moving_average/(float)samples;
}

int main() {
    //Pins
    AnalogIn drukSensor(A0);
    AnalogIn flowSensor(A1);
    AnalogIn tempSensor(A2);
    
    //Sample time
    int tick_ms_druksensor    = 10,     //100 Hz
        tick_ms_flowsensor    = 10,     //100 Hz
        tick_ms_tempsensor    = 1000;   //1 Hz 

    //mbed ondersteund onneindig veel timers
    Timer   t_druk,
            t_flow,
            t_temp;

    t_druk.start();
    t_flow.start();
    t_temp.start();
    while(1) {
        if(t_druk.read_ms() >= tick_ms_druksensor){
            //Lees de druksensor uit            
            if(pressure_is_main == true && smoothing == true){
                calc_moving_average(drukSensor.read());
            } else {
                sensorVal[druksensor] = drukSensor.read();   
            }
            t_druk.reset(); 
        }
        if(t_flow.read_ms() >= tick_ms_flowsensor){
            //Lees de flowsensor uit
            if(pressure_is_main == false && smoothing == true){
                calc_moving_average(flowSensor.read());
            } else {
                sensorVal[flowsensor] = flowSensor.read();
            }
            t_flow.reset();    
        }
        if(t_temp.read_ms() >= tick_ms_tempsensor){
            //Lees de temperatuursensor uit 
            sensorVal[tempsensor] = tempSensor.read();
            t_temp.reset();   
        }
    }
}