Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: HIDScope MODSERIAL biquadFilter mbed
Fork of Filter by
Calibrationscript.cpp
- Committer:
- aluminium
- Date:
- 2017-11-01
- Revision:
- 6:b9a84c1cb4f9
- Parent:
- 5:7c8176cdaa1c
- Child:
- 7:7c6a9bb2d30e
File content as of revision 6:b9a84c1cb4f9:
#include "mbed.h"
#include "HIDScope.h" //require the HIDScope library
#include "MODSERIAL.h"
#include "BiQuad.h"
# include "math.h"
// DEFINING
//Define Inputs
AnalogIn emg(A0); //analog of EMG input
InterruptIn button1(PTA4); //test button for starting motor 1
InterruptIn button2(SW2); //FOR DEBUGGING
//Define Outputs
DigitalOut led1(LED_RED);
DigitalOut led2(LED_BLUE);
DigitalOut led3(LED_GREEN); //FOR DEBUGGING
MODSERIAL pc(USBTX,USBRX);
//Define Tickers
Ticker sample_timer; // Taking samples
Ticker LED_timer; // Write the LED
Ticker calibration_timer; // Check when to start calibration
//Define HIDscope
//HIDScope scope(2); //instantize a 2-channel HIDScope object
//Define filters and define the floats which contains the values.
BiQuadChain bqc;
BiQuad bq1_low(0.00182, 0.0036417, 0.0018208, -1.8764998, 0.883783);
BiQuad bq2_high( 0.973868, -1.947737, 0.97386, -1.947737, 0.948429);
BiQuad bq3_notch(0.91859, -1.82269, 0.91859, -1.82269, 0.83718);
double print;
double emgFiltered;
double filteredAbs;
double emg_value;
double onoffsignal;
bool check_calibration=0;
double avg_emg;
// FUNCTIONS
//function for filtering
void filter(){
if(check_calibration==1){
emg_value = emg.read();
emgFiltered = bqc.step( emg_value );
filteredAbs = abs( emgFiltered );
if (avg_emg != 0){
onoffsignal=filteredAbs/avg_emg; //divide the emg signal by the max EMG to calibrate the signal per person
}
// scope.set(0,emg_value); //set emg signal to scope in channel 1
//scope.set(1,onoffsignal); //set filtered signal to scope in channel 2
// scope.send(); //send the signals to the scope
// pc.printf("emg signal %f, filtered signal %f \n",emg_value,onoffsignal);
}
}
//function to check the threshold
void check_emg(){
double filteredAbs_temp;
if(check_calibration==1){
for( int i = 0; i<1000;i++){
filter();
filteredAbs_temp = filteredAbs_temp + onoffsignal;
wait(0.0004);
}
filteredAbs_temp = filteredAbs_temp/1000;
if(filteredAbs_temp<=0.55){ //if signal is lower then 0.5 the blue light goes on
led1.write(1); //led 1 is rood en uit
led2.write(0); //led 2 is blauw en aan
}
else if(filteredAbs_temp > 0.55){ //if signal does not pass threshold value, blue light goes on
led1.write(0);
led2.write(1);
}
}
}
//function to calibrate
int calibration(){
pc.printf("check1\n\r");
// if(button1.read()==false){
led3.write(0);
double signal_verzameling = 0;
for(int n =0; n<5000;n++){
filter();
//read for 5000 samples as calibration
emg_value = emg.read();
emgFiltered = bqc.step( emg_value );
filteredAbs = abs(emgFiltered);
// signal_verzameling[n]= filteredAbs;
signal_verzameling = signal_verzameling + filteredAbs ;
wait(0.0004);
if (n == 4999){
avg_emg = signal_verzameling / n;
pc.printf("avg_emg = %f\n\r",avg_emg);
}
}
led3.write(1);
// double lengte_array = sizeof(signal_verzameling);
// pc.printf("lengte_array = %f\n\r",lengte_array);
// for(int i = 0; i < lengte_array; i++){
// sum_array = sum_array + signal_verzameling[i] ;
// }
//avg_emg = sum_array / lengte_array;
// pc.printf("avg_emg = %f\n\r",avg_emg);
check_calibration=1;
led3.write(1);
// }
return 0;
}
// MAIN
int main(){
pc.baud(115200);
pc.printf("Lampjes zijn langs geweest");
led1.write(1);
led2.write(1);
led3.write(1);
bqc.add( &bq1_low ).add( &bq2_high ).add( &bq3_notch );
// sample_timer.attach(&filter, 0.02); //continously execute the EMG reader and filter
LED_timer.attach(&check_emg, 0.1); //continously execute the motor controller
//calibration_timer.attach(&calibration, 0.002); //ticker to check if EMG is being calibrated
// pc.printf("%d",filteredAbs);
calibration();
while(1){ //while loop to keep system going
}
}
