ksf
Dependents: kinematics_controlv2 kinematics_controlv4 kinematics_control_copyfds Robot_control
signalnumber.cpp
- Committer:
- peterknoben
- Date:
- 2017-11-01
- Revision:
- 4:a79f9f3a9e40
- Parent:
- 3:f8d57796d69b
- Child:
- 5:f3ddc3a7b292
File content as of revision 4:a79f9f3a9e40:
#include "signalnumber.h" #include "mbed.h" //Constants for mean value const int n = 1500; //Window size for the mean value float emg0_filtered[n] = {}, emg2_filtered[n] = {}, emg4_filtered[n]={}; int count1 = 0, count2 = 0, count3 = 0; int countx = 0, county = 0.0, countz = 0.0; float meanL = 0.0, meanR = 0.0, meanM = 0.0; float sum = 0.0; //Constants EMG switch const float LeftMin = 0.20; const float LeftMax = 0.50; /* 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 NumberL = 0, NumberR =0; //const int action =50; SignalNumber::SignalNumber(void) { } /* // 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 sum_math = 0.0; for (int m=0 ; m<n ; m++ ){ sum_math = sum_math + emg_filtered[m]; } float mean_math = sum_math/n; return mean_math; }*/ float SignalNumber::getmeanLeft(const int n, float input){ emg0_filtered[count1] = input; count1++; if (count1 == n){ count1 = 0; } float sum_math = 0.0; for (int m=0 ; m<n ; m++ ){ sum_math = sum_math + emg0_filtered[m]; } float mean_math = sum_math/n; return mean_math; } float SignalNumber::getmeanRight(const int n, float input){ emg2_filtered[count2] = input; count2++; if (count2 == n){ count2 = 0; } float sum_math = 0.0; for (int m=0 ; m<n ; m++ ){ sum_math = sum_math + emg2_filtered[m]; } float mean_math = sum_math/n; return mean_math; } float SignalNumber::getmeanMode(const int n, float input){ emg4_filtered[count3] = input; count3++; if (count3 == n){ count3 = 0; } float sum_math = 0.0; for (int m=0 ; m<n ; m++ ){ sum_math = sum_math + emg4_filtered[m]; } float mean_math = sum_math/n; return mean_math; } float SignalNumber::calibrate(const int n, float input){ float offset = getmeanLeft(n, input); return offset; } int SignalNumber::getnumberLeft(const int n, const int action, float input){ meanL= getmeanLeft(n, input); //Check first case if( meanL< LeftMin ) { if (county <action){ meanL= getmeanLeft(n, input); if(meanL< LeftMin){ county++; } else{ county=0; NumberL=0; } } else{ NumberL = 0; county=0; } } //Check second case else if(meanL >= LeftMin and meanL < LeftMax){ if (county <action){ meanL= getmeanLeft(n, input); if(meanL >= LeftMin and meanL < LeftMax){ county++; } else{ county=0; NumberL=0; } } else{ NumberL = 1; county=0; } } else if( meanL>=LeftMax) { if (county <action){ meanL= getmeanLeft(n, input); if(meanL>=LeftMax){ county++; } else{ county=0; NumberL=0; } } else{ NumberL = 2; county=0; } } else{ county=0; NumberL =0; } return NumberL; } int SignalNumber::getnumberRight(const int n, const int action, float input){ meanR= getmeanRight(n, input); //Check first case if( meanR< LeftMin ) { if (countx <action){ meanR= getmeanRight(n, input); if(meanR< LeftMin){ countx++; } else{ countx=0; NumberR=0; } } else{ NumberR = 0; countx=0; } } //Check second case else if(meanR >= LeftMin and meanR < LeftMax){ if (countx <action){ meanR= getmeanRight(n, input); if(meanR >= LeftMin and meanR < LeftMax){ countx++; } else{ countx=0; NumberR=0; } } else{ NumberR = 1; countx=0; } } else if( meanR >=LeftMax ) { if (countx <action){ meanR= getmeanRight(n, input); if( meanR >=LeftMax ){ countx++; } else{ countx=0; NumberR=0; } } else{ NumberR = 2; countx=0; } } else{ countx=0; NumberR =0; } return NumberR; } //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 (countx <action){ mean = getmean(n, input); if(mean < LeftFastmin){ countx++; } else{ countx=0; Number=0; } } else{ Number = 0; countx=0; } } //Check second case else if(mean <= LeftFastmax and mean > LeftFastmin){ if (countx <action){ mean = getmean(n, input); if(mean <=LeftFastmax and mean>LeftFastmin){ countx++; } else{ countx=0; Number=0; } } else{ Number = 1; countx=0; } } else if( mean <=LeftSlowmax and mean>LeftSlowmin) { if (countx <action){ mean = getmean(n, input); if(mean <=LeftSlowmax and mean>LeftSlowmin){ countx++; } else{ countx=0; Number=0; } } else{ Number = 2; countx=0; } } else if( mean <=RightSlowmax and mean>RightSlowmin) { if (countx <action){ mean = getmean(n, input); if(mean <=RightSlowmax and mean>RightSlowmin){ countx++; } else{ countx=0; Number=0; } } else{ Number = 3; countx=0; } } else if( mean <=RightFastmax and mean>RightFastmin ) { if (countx <action){ mean = getmean(n, input); if(mean <=RightFastmax and mean>RightFastmin){ countx++; } else{ countx=0; Number=0; } } else{ Number = 4; countx=0; } } else{ countx=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 (countx <action){ mean = getmean(n, input); if(mean < LeftFastmin){ countx++; } else{ countx=0; Number=0; } } else{ Number = 0; countx=0; } } return mode; }*/