Eindelijk!!!!!

Fork of SignalNumber2 by Peter Knoben

signalnumber.cpp

Committer:
peterknoben
Date:
2017-11-01
Revision:
3:f8d57796d69b
Parent:
2:1a677b57ce81
Child:
4:a79f9f3a9e40

File content as of revision 3:f8d57796d69b:

#include "signalnumber.h"
#include "mbed.h"


//Constants for mean value
const int n = 400;                   //Window size for the mean value
float emg_filtered[n] = {}; 
int count = 0;
int count2 = 0;
float mean = 0.0;
float sum = 0.0;


//Constants EMG switch
const float LeftFastmin=0.075;
const float LeftFastmax=0.15;
const float LeftSlowmin=0.2;
const float LeftSlowmax=0.35;
const float RightSlowmin=0.4;
const float RightSlowmax=0.65;
const float RightFastmin=0.7;
const float RightFastmax=10.5;
int Number = 0;
//const int action =50;


SignalNumber::SignalNumber(void)
{

}



//Determine the sum of the last "n" amount of numbers.
float SignalNumber::getsum(float array[], const int n){
    float sum_math = 0.0;
    for (int m=0 ; m<n ; m++ ){
        sum_math = sum_math + array[m];
    }
    return sum_math;   
}



// Determine the mean value of the last "n" amount of numbers.
float SignalNumber::getmean(const int n, float input){
    emg_filtered[count] = input;
    count++;
    if (count == n){
        count = 0;
    }
    float mean_math = getsum(emg_filtered,n)/n;
    return mean_math;
}
/*
float SignalNumber::getmean(float array[], const int n, float input){
    float sum_math = 0;
    
    
}
*/
float SignalNumber::calibrate(const int n, float input){
    float offset = getmean(n, input);
    return offset; 
} 

//Determine the right signal number
/*
There will be checked in which range the mean value lies. If the value lies 
within this range for "action" samples the switch will be changed to the right
mode. If the mean value changes outside of the region the switch will be set to 
0, this is the resting state. Also the counter will be reset.
n is the amount of samples in the window
action is the amount of same mean values 
*/
int SignalNumber::getnumber(const int n, const int action, float input){
    mean = getmean(n, input);
    //Check first case
    if( mean < LeftFastmin ) {
        if (count2 <action){
            mean = getmean(n, input);
            if(mean < LeftFastmin){
                count2++;
            }
            else{
                count2=0;
                Number=0;
            } 
        }
        else{
            Number = 0;
            count2=0;
        }
    }
     //Check second case  
     else if(mean <= LeftFastmax and mean > LeftFastmin){
        if (count2 <action){
            mean = getmean(n, input);
            if(mean <=LeftFastmax and mean>LeftFastmin){
                count2++;
            }
            else{
                count2=0;
                Number=0;
            } 
        }
        else{
            Number = 1;
            count2=0;
        }
    }
     else if( mean <=LeftSlowmax and mean>LeftSlowmin) {
        if (count2 <action){
            mean = getmean(n, input);
            if(mean <=LeftSlowmax and mean>LeftSlowmin){
                count2++;
            }
            else{
                count2=0;
                Number=0;
            } 
        }
        else{
            Number = 2;
            count2=0;
        }
     }
     else if( mean <=RightSlowmax and mean>RightSlowmin) {
        if (count2 <action){
            mean = getmean(n, input);
            if(mean <=RightSlowmax and mean>RightSlowmin){
                count2++;
            }
            else{
                count2=0;
                Number=0;
            } 
        }
        else{
            Number = 3;
            count2=0;
        }
     }
     else if( mean <=RightFastmax and mean>RightFastmin ) {
        if (count2 <action){
            mean = getmean(n, input);
            if(mean <=RightFastmax and mean>RightFastmin){
                count2++;
            }
            else{
                count2=0;
                Number=0;
            } 
        }
        else{
            Number = 4;
            count2=0;
        }
    }
    else{
        count2=0;
        Number =0;
    }
    return Number;
}       


// In progress
int SignalNumber::getmode(const int n, const int action, float input){
    int mode;
    mean = getmean(n, input);
    //Check first case
    if( mean < LeftFastmin ) {
        if (count2 <action){
            mean = getmean(n, input);
            if(mean < LeftFastmin){
                count2++;
            }
            else{
                count2=0;
                Number=0;
            } 
        }
        else{
            Number = 0;
            count2=0;
        }
    }
    return mode;
}