De sensorcontroller van het TLS2 project.

Dependencies:   mbed

main.cpp

Committer:
RichardHoekstra
Date:
2016-11-16
Revision:
3:8e50fc7b82b7
Parent:
2:4c5952cf26d0
Child:
4:512492f73e90

File content as of revision 3:8e50fc7b82b7:

#include "mbed.h"

//I2C settings
    #define SDA D10
    #define SCL D11
    #define sensor_addr 0x93
    #define I2C_BUFFER_SIZE 10
    I2CSlave slave(SDA,SCL);

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[100] = {0}; //[0] is the newest
    float moving_average = 0;
    if(samples > 0 && samples < 100){ //Sanity check
        //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;
    } else {
        return 3.1415926; //Improv error code 
    }
}
//Split an integer into two char
void int_to_2_char(char* arr, int val, int first_element = 0){
    arr[first_element] = val>>8;
    arr[first_element+1] = val&255;
}
//Join two char to make an integer. 
int char2_to_int(char* arr, int first_element = 0){
    return ((arr[first_element]<<8)+arr[first_element+1]);
}
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();
    int respondSensor = 0;
    char buffer[I2C_BUFFER_SIZE] = {0}; //Create the buffer for I2C
    bool buffer_changed = false;
    char data[2];
    slave.address(sensor_addr);
    while(1) {
        //I2C time
        int i = slave.receive();        
        switch (i) {
            case I2CSlave::ReadAddressed:
                //Received a request to be read
                //Irrelevant for now
                switch(respondSensor){
                    case 1: //Druksensor
                        int_to_2_char(data,((int)(sensorVal[0]*65535)));
                        break;
                    case 2: //Flowsensor
                        int_to_2_char(data,((int)(sensorVal[1]*65535)));
                        break;
                    case 3://Temperatuursensor
                        int_to_2_char(data,((int)(sensorVal[2]*65535)));
                        break;                   
                }
                slave.write(data,2);
                break;
            case I2CSlave::WriteGeneral:
                //Received a request to be written to
                slave.read(buffer,I2C_BUFFER_SIZE);
                buffer_changed = true;
                break;
            case I2CSlave::WriteAddressed:
                //Received a request to be written to a specific location
                slave.read(buffer,I2C_BUFFER_SIZE);
                buffer_changed = true;
                break;
        }
        if(buffer_changed == true){
            for(int i=0;i<I2C_BUFFER_SIZE;i++){
                buffer[i] = 0;   
            }
            int command = buffer[0];
            switch(command){
                case 0:
                    //Command is recognized
                    //Write what the command is going to do
                    break;
                case 1:
                    //Command is recognized
                    //Write what the command is going to do
                    break;
                default:
                    //Command is not recognied
                    break;                
            }               
        }        
        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();   
        }
    }
}