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@4:285fb7d84088, 2017-10-31 (annotated)
- Committer:
- aluminium
- Date:
- Tue Oct 31 10:17:15 2017 +0000
- Revision:
- 4:285fb7d84088
- Parent:
- 2:8f6fca2179f2
- Child:
- 5:7c8176cdaa1c
eerste deel met avrage berekenen
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DAkoetsier | 2:8f6fca2179f2 | 1 | #include "mbed.h" |
DAkoetsier | 2:8f6fca2179f2 | 2 | #include "HIDScope.h" //require the HIDScope library |
DAkoetsier | 2:8f6fca2179f2 | 3 | #include "MODSERIAL.h" |
DAkoetsier | 2:8f6fca2179f2 | 4 | #include "BiQuad.h" |
DAkoetsier | 2:8f6fca2179f2 | 5 | |
DAkoetsier | 2:8f6fca2179f2 | 6 | // DEFINING |
DAkoetsier | 2:8f6fca2179f2 | 7 | //Define Inputs |
DAkoetsier | 2:8f6fca2179f2 | 8 | AnalogIn emg(A0); //analog of EMG input |
DAkoetsier | 2:8f6fca2179f2 | 9 | InterruptIn button1(PTA4); //test button for starting motor 1 |
DAkoetsier | 2:8f6fca2179f2 | 10 | InterruptIn button2(SW2); //FOR DEBUGGING |
DAkoetsier | 2:8f6fca2179f2 | 11 | |
DAkoetsier | 2:8f6fca2179f2 | 12 | //Define Outputs |
DAkoetsier | 2:8f6fca2179f2 | 13 | DigitalOut led1(LED_RED); |
DAkoetsier | 2:8f6fca2179f2 | 14 | DigitalOut led2(LED_BLUE); |
DAkoetsier | 2:8f6fca2179f2 | 15 | DigitalOut led3(LED_GREEN); //FOR DEBUGGING |
DAkoetsier | 2:8f6fca2179f2 | 16 | MODSERIAL pc(USBTX,USBRX); |
DAkoetsier | 2:8f6fca2179f2 | 17 | |
DAkoetsier | 2:8f6fca2179f2 | 18 | |
DAkoetsier | 2:8f6fca2179f2 | 19 | //Define Tickers |
DAkoetsier | 2:8f6fca2179f2 | 20 | Ticker sample_timer; // Taking samples |
DAkoetsier | 2:8f6fca2179f2 | 21 | Ticker LED_timer; // Write the LED |
DAkoetsier | 2:8f6fca2179f2 | 22 | Ticker calibration_timer; // Check when to start calibration |
DAkoetsier | 2:8f6fca2179f2 | 23 | //MODSERIAL pc(USBTX,USBRX); |
DAkoetsier | 2:8f6fca2179f2 | 24 | |
DAkoetsier | 2:8f6fca2179f2 | 25 | //Define HIDscope |
aluminium | 4:285fb7d84088 | 26 | //HIDScope scope(2); //instantize a 2-channel HIDScope object |
DAkoetsier | 2:8f6fca2179f2 | 27 | |
DAkoetsier | 2:8f6fca2179f2 | 28 | |
DAkoetsier | 2:8f6fca2179f2 | 29 | //Define filters and define the floats which contains the values. |
DAkoetsier | 2:8f6fca2179f2 | 30 | BiQuadChain bqc; |
DAkoetsier | 2:8f6fca2179f2 | 31 | BiQuad bq1_low(1.34871e-3, 2.69742e-3, 1.34871e-3, -1.89346, 0.89885); |
DAkoetsier | 2:8f6fca2179f2 | 32 | BiQuad bq2_high(0.96508, -1.93016, 0.96508, -1.92894, 0.93138); |
DAkoetsier | 2:8f6fca2179f2 | 33 | BiQuad bq3_notch(0.91859, -1.82269, 0.91859, -1.82269, 0.83718); |
DAkoetsier | 2:8f6fca2179f2 | 34 | |
DAkoetsier | 2:8f6fca2179f2 | 35 | double emgFiltered; |
DAkoetsier | 2:8f6fca2179f2 | 36 | double filteredAbs; |
DAkoetsier | 2:8f6fca2179f2 | 37 | double emg_value; |
DAkoetsier | 2:8f6fca2179f2 | 38 | double onoffsignal; |
DAkoetsier | 2:8f6fca2179f2 | 39 | double maxcal=0; |
DAkoetsier | 2:8f6fca2179f2 | 40 | bool aboveTreshold=0; |
DAkoetsier | 2:8f6fca2179f2 | 41 | |
DAkoetsier | 2:8f6fca2179f2 | 42 | // FUNCTIONS |
DAkoetsier | 2:8f6fca2179f2 | 43 | //function for filtering |
DAkoetsier | 2:8f6fca2179f2 | 44 | |
DAkoetsier | 2:8f6fca2179f2 | 45 | void filter(){ |
DAkoetsier | 2:8f6fca2179f2 | 46 | if(aboveTreshold==1){ |
DAkoetsier | 2:8f6fca2179f2 | 47 | |
DAkoetsier | 2:8f6fca2179f2 | 48 | emg_value = emg.read(); |
DAkoetsier | 2:8f6fca2179f2 | 49 | emgFiltered = bqc.step( emg_value ); |
DAkoetsier | 2:8f6fca2179f2 | 50 | filteredAbs = abs( emgFiltered ); |
DAkoetsier | 2:8f6fca2179f2 | 51 | |
DAkoetsier | 2:8f6fca2179f2 | 52 | onoffsignal=filteredAbs/maxcal; //divide the emg signal by the max EMG to calibrate the signal per person |
aluminium | 4:285fb7d84088 | 53 | // scope.set(0,emg_value); //set emg signal to scope in channel 1 |
aluminium | 4:285fb7d84088 | 54 | // scope.set(1,onoffsignal); //set filtered signal to scope in channel 2 |
aluminium | 4:285fb7d84088 | 55 | // scope.send(); //send the signals to the scope |
DAkoetsier | 2:8f6fca2179f2 | 56 | // pc.printf("emg signal %f, filtered signal %f \n",emg_value,onoffsignal); |
DAkoetsier | 2:8f6fca2179f2 | 57 | } |
DAkoetsier | 2:8f6fca2179f2 | 58 | } |
DAkoetsier | 2:8f6fca2179f2 | 59 | |
DAkoetsier | 2:8f6fca2179f2 | 60 | //function to check the threshold |
DAkoetsier | 2:8f6fca2179f2 | 61 | void checkthreshold(){ |
DAkoetsier | 2:8f6fca2179f2 | 62 | if(aboveTreshold==1){ //if signal passes threshold value, red light goes on |
DAkoetsier | 2:8f6fca2179f2 | 63 | if(onoffsignal<=0.25){ |
DAkoetsier | 2:8f6fca2179f2 | 64 | led1.write(1); |
DAkoetsier | 2:8f6fca2179f2 | 65 | led2.write(0); |
DAkoetsier | 2:8f6fca2179f2 | 66 | } |
DAkoetsier | 2:8f6fca2179f2 | 67 | else if(onoffsignal >= 0.5){ //if signal does not pass threshold value, blue light goes on |
DAkoetsier | 2:8f6fca2179f2 | 68 | led1.write(0); |
DAkoetsier | 2:8f6fca2179f2 | 69 | led2.write(1); |
DAkoetsier | 2:8f6fca2179f2 | 70 | } |
DAkoetsier | 2:8f6fca2179f2 | 71 | } |
DAkoetsier | 2:8f6fca2179f2 | 72 | } |
DAkoetsier | 2:8f6fca2179f2 | 73 | |
DAkoetsier | 2:8f6fca2179f2 | 74 | //function to calibrate |
aluminium | 4:285fb7d84088 | 75 | void calibration(){ |
aluminium | 4:285fb7d84088 | 76 | double sum_array; |
DAkoetsier | 2:8f6fca2179f2 | 77 | if(button1.read()==false){ |
DAkoetsier | 2:8f6fca2179f2 | 78 | led3.write(0); |
aluminium | 4:285fb7d84088 | 79 | double signal_verzameling[5000]; |
DAkoetsier | 2:8f6fca2179f2 | 80 | for(int n =0; n<5000;n++){ //read for 5000 samples as calibration |
DAkoetsier | 2:8f6fca2179f2 | 81 | emg_value = emg.read(); |
DAkoetsier | 2:8f6fca2179f2 | 82 | emgFiltered = bqc.step( emg_value ); |
DAkoetsier | 2:8f6fca2179f2 | 83 | filteredAbs = abs( emgFiltered ); |
DAkoetsier | 2:8f6fca2179f2 | 84 | |
DAkoetsier | 2:8f6fca2179f2 | 85 | double signalmeasure = filteredAbs; |
aluminium | 4:285fb7d84088 | 86 | signal_verzameling[n]= signalmeasure; |
DAkoetsier | 2:8f6fca2179f2 | 87 | } |
aluminium | 4:285fb7d84088 | 88 | double lengte_array = sizeof(signal_verzameling); |
aluminium | 4:285fb7d84088 | 89 | |
aluminium | 4:285fb7d84088 | 90 | |
aluminium | 4:285fb7d84088 | 91 | for(int i = 1; i < lengte_array; i++){ |
aluminium | 4:285fb7d84088 | 92 | //^^should be 0 |
aluminium | 4:285fb7d84088 | 93 | double temp_a = signal_verzameling[i]; |
aluminium | 4:285fb7d84088 | 94 | sum_array += temp_a ; |
aluminium | 4:285fb7d84088 | 95 | } |
aluminium | 4:285fb7d84088 | 96 | double avg = sum_array / lengte_array; |
aluminium | 4:285fb7d84088 | 97 | |
aluminium | 4:285fb7d84088 | 98 | |
aluminium | 4:285fb7d84088 | 99 | |
aluminium | 4:285fb7d84088 | 100 | |
aluminium | 4:285fb7d84088 | 101 | |
DAkoetsier | 2:8f6fca2179f2 | 102 | aboveTreshold=1; |
DAkoetsier | 2:8f6fca2179f2 | 103 | led3.write(1); |
DAkoetsier | 2:8f6fca2179f2 | 104 | } |
DAkoetsier | 2:8f6fca2179f2 | 105 | } |
DAkoetsier | 2:8f6fca2179f2 | 106 | |
DAkoetsier | 2:8f6fca2179f2 | 107 | // MAIN |
DAkoetsier | 2:8f6fca2179f2 | 108 | |
DAkoetsier | 2:8f6fca2179f2 | 109 | int main(){ |
DAkoetsier | 2:8f6fca2179f2 | 110 | // pc.baud(115200); |
DAkoetsier | 2:8f6fca2179f2 | 111 | |
DAkoetsier | 2:8f6fca2179f2 | 112 | // pc.printf("Lampjes zijn langs geweest"); |
DAkoetsier | 2:8f6fca2179f2 | 113 | led1.write(1); |
DAkoetsier | 2:8f6fca2179f2 | 114 | led2.write(1); |
DAkoetsier | 2:8f6fca2179f2 | 115 | led3.write(1); |
DAkoetsier | 2:8f6fca2179f2 | 116 | |
aluminium | 4:285fb7d84088 | 117 | |
aluminium | 4:285fb7d84088 | 118 | |
DAkoetsier | 2:8f6fca2179f2 | 119 | bqc.add( &bq1_low ).add( &bq2_high ).add( &bq3_notch ); |
DAkoetsier | 2:8f6fca2179f2 | 120 | |
DAkoetsier | 2:8f6fca2179f2 | 121 | sample_timer.attach(&filter, 0.002); //continously execute the EMG reader and filter |
DAkoetsier | 2:8f6fca2179f2 | 122 | LED_timer.attach(&checkthreshold, 0.002); //continously execute the motor controller |
DAkoetsier | 2:8f6fca2179f2 | 123 | calibration_timer.attach(&calibration, 0.002); //ticker to check if EMG is being calibrated |
DAkoetsier | 2:8f6fca2179f2 | 124 | // pc.printf("%d",filteredAbs); |
DAkoetsier | 2:8f6fca2179f2 | 125 | |
DAkoetsier | 2:8f6fca2179f2 | 126 | |
DAkoetsier | 2:8f6fca2179f2 | 127 | |
DAkoetsier | 2:8f6fca2179f2 | 128 | while(1){ //while loop to keep system going |
DAkoetsier | 2:8f6fca2179f2 | 129 | |
DAkoetsier | 2:8f6fca2179f2 | 130 | } |
DAkoetsier | 2:8f6fca2179f2 | 131 | } |