Working, Clean

Dependents:   ShowItv2 ShowIt_robot_control

Fork of SignalNumber by Dustin Berendsen

signalnumber.cpp

Committer:
peterknoben
Date:
2017-10-31
Revision:
1:15543c143a63
Parent:
0:5f8dee4d4b09
Child:
2:1a677b57ce81

File content as of revision 1:15543c143a63:

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


//Constants for mean value
const int n = 400;                   //Window size for the mean value
float emg0_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=1.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){
    emg0_filtered[count] = input;
    count++;
    if (count == n){
        count = 0;
    }
    float mean_math = getsum(emg0_filtered,n)/n;
    return mean_math;
}


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.
*/
int SignalNumber::getnumber(const int n, const int action, double input, float k){
    mean = getmean(n, input)*k;
    //Check first case
    if( mean < LeftFastmin ) {
        if (count2 <action){
            mean = getmean(n, input)*k;
            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)*k;
            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)*k;
            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)*k;
            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)*k;
            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, double input, float k){
    int mode;
    mean = getmean(n, input)*k;
    //Check first case
    if( mean < LeftFastmin ) {
        if (count2 <action){
            mean = getmean(n, input)*k;
            if(mean < LeftFastmin){
                count2++;
            }
            else{
                count2=0;
                Number=0;
            } 
        }
        else{
            Number = 0;
            count2=0;
        }
    }
    return mode;
}