De hele robot in 1 keer bam
Dependencies: mbed QEI Servo HIDScope biquadFilter MODSERIAL FastPWM
main.cpp@3:c0ece64850db, 2019-10-20 (annotated)
- Committer:
- Jellehierck
- Date:
- Sun Oct 20 19:01:21 2019 +0000
- Revision:
- 3:c0ece64850db
- Parent:
- 2:d3e9788ab1b3
- Child:
- 4:09a01d2db8f7
Completely rewrote biquad filters for easier manipulation and implementation of 4th order filters.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IsaRobin | 0:6972d0e91af1 | 1 | //c++ script for filtering of measured EMG signals |
IsaRobin | 0:6972d0e91af1 | 2 | #include "mbed.h" //Base library |
IsaRobin | 0:6972d0e91af1 | 3 | #include "HIDScope.h" // to see if program is working and EMG is filtered properly |
Jellehierck | 2:d3e9788ab1b3 | 4 | // #include "QEI.h"// is needed for the encoder |
Jellehierck | 2:d3e9788ab1b3 | 5 | // #include "MODSERIAL.h"// in order for connection with the pc |
Jellehierck | 2:d3e9788ab1b3 | 6 | #include "BiQuad.h" |
Jellehierck | 2:d3e9788ab1b3 | 7 | // #include "FastPWM.h" |
Jellehierck | 2:d3e9788ab1b3 | 8 | // #include "Arduino.h" //misschien handig omdat we het EMG arduino board gebruiken (?) |
Jellehierck | 2:d3e9788ab1b3 | 9 | // #include "EMGFilters.h" |
IsaRobin | 0:6972d0e91af1 | 10 | #include <vector> // For easy array management |
IsaRobin | 0:6972d0e91af1 | 11 | |
Jellehierck | 2:d3e9788ab1b3 | 12 | // PC serial connection |
Jellehierck | 2:d3e9788ab1b3 | 13 | // MODSERIAL pc(USBTX, USBRX); |
IsaRobin | 0:6972d0e91af1 | 14 | |
IsaRobin | 0:6972d0e91af1 | 15 | //EMG inputs definieren |
IsaRobin | 0:6972d0e91af1 | 16 | AnalogIn emg1_in (A1); //emg van rechterbicep, voor de x-richting |
IsaRobin | 0:6972d0e91af1 | 17 | AnalogIn emg2_in (A2); //emg van linkerbicep, voor de y-richting |
IsaRobin | 0:6972d0e91af1 | 18 | AnalogIn emg3_in (A3); //emg van een derde (nog te bepalen) spier, voor het vernaderen van de richting |
IsaRobin | 0:6972d0e91af1 | 19 | |
IsaRobin | 0:6972d0e91af1 | 20 | //variablen voor EMG |
IsaRobin | 0:6972d0e91af1 | 21 | double emg1; |
IsaRobin | 0:6972d0e91af1 | 22 | double emg2; |
IsaRobin | 0:6972d0e91af1 | 23 | double emg3; |
IsaRobin | 0:6972d0e91af1 | 24 | double notch1; |
IsaRobin | 0:6972d0e91af1 | 25 | double notch2; |
IsaRobin | 0:6972d0e91af1 | 26 | double notch3; |
IsaRobin | 0:6972d0e91af1 | 27 | double highpass1; |
IsaRobin | 0:6972d0e91af1 | 28 | double highpass2; |
IsaRobin | 0:6972d0e91af1 | 29 | double highpass3; |
IsaRobin | 0:6972d0e91af1 | 30 | double lowpass1; |
IsaRobin | 0:6972d0e91af1 | 31 | double lowpass2; |
IsaRobin | 0:6972d0e91af1 | 32 | double lowpass3; |
IsaRobin | 0:6972d0e91af1 | 33 | double rectify1; |
IsaRobin | 0:6972d0e91af1 | 34 | double rectify2; |
IsaRobin | 0:6972d0e91af1 | 35 | double rectify3; |
IsaRobin | 0:6972d0e91af1 | 36 | |
Jellehierck | 3:c0ece64850db | 37 | // Notch filter coefficients (iirnotch Q factor 35 @50Hz) from MATLAB in the following form: |
Jellehierck | 3:c0ece64850db | 38 | // b01 b11 b21 a01 a11 a21 |
Jellehierck | 3:c0ece64850db | 39 | BiQuad bq_notch(0.995636295063941, -1.89829218816065, 0.995636295063941, 1, -1.89829218816065, 0.991272590127882); |
Jellehierck | 1:059cca298369 | 40 | |
Jellehierck | 3:c0ece64850db | 41 | // Highpass filter coefficients (butter 4th order @10Hz cutoff) from MATLAB in the following form: |
Jellehierck | 3:c0ece64850db | 42 | // b01 b11 b21 a01 a11 a21 |
Jellehierck | 3:c0ece64850db | 43 | // b02 b12 b22 a02 a12 a22 |
Jellehierck | 3:c0ece64850db | 44 | BiQuad bq_H1(0.922946103200875, -1.84589220640175, 0.922946103200875, 1, -1.88920703055163, 0.892769008131025); |
Jellehierck | 3:c0ece64850db | 45 | BiQuad bq_H2(1, -2, 1, 1, -1.95046575793011, 0.954143234875078); |
Jellehierck | 3:c0ece64850db | 46 | BiQuadChain bqc_high; // Used to chain two 2nd other filters into a 4th order filter |
IsaRobin | 0:6972d0e91af1 | 47 | |
Jellehierck | 3:c0ece64850db | 48 | // Lowpass filter coefficients (butter 4th order @5Hz cutoff) from MATLAB in the following form: |
Jellehierck | 3:c0ece64850db | 49 | // b01 b11 b21 a01 a11 a21 |
Jellehierck | 3:c0ece64850db | 50 | // b02 b12 b22 a02 a12 a22 |
Jellehierck | 3:c0ece64850db | 51 | BiQuad bq_L1(5.32116245737504e-08, 1.06423249147501e-07, 5.32116245737504e-08, 1, -1.94396715039462, 0.944882378004138); |
Jellehierck | 3:c0ece64850db | 52 | BiQuad bq_L2(1, 2, 1, 1, -1.97586467534468, 0.976794920438162); |
Jellehierck | 3:c0ece64850db | 53 | BiQuadChain bqc_low; // Used to chain two 2nd other filters into a 4th order filter |
Jellehierck | 2:d3e9788ab1b3 | 54 | |
Jellehierck | 2:d3e9788ab1b3 | 55 | void sample() |
Jellehierck | 2:d3e9788ab1b3 | 56 | { |
Jellehierck | 2:d3e9788ab1b3 | 57 | emg1 = emg1_in.read(); |
Jellehierck | 2:d3e9788ab1b3 | 58 | emg2 = emg2_in.read(); |
Jellehierck | 2:d3e9788ab1b3 | 59 | emg3 = emg3_in.read(); |
Jellehierck | 2:d3e9788ab1b3 | 60 | } |
IsaRobin | 0:6972d0e91af1 | 61 | |
IsaRobin | 0:6972d0e91af1 | 62 | //notch filter toepassen |
IsaRobin | 0:6972d0e91af1 | 63 | notch1 = N1.step(emg1); |
IsaRobin | 0:6972d0e91af1 | 64 | notch2 = N2.step(emg2); |
IsaRobin | 0:6972d0e91af1 | 65 | notch3 = N3.step(emg3); |
IsaRobin | 0:6972d0e91af1 | 66 | |
Jellehierck | 2:d3e9788ab1b3 | 67 | //high pass filter |
IsaRobin | 0:6972d0e91af1 | 68 | high1 = H1.step(notch1); |
IsaRobin | 0:6972d0e91af1 | 69 | high2 = H2.step(notch2); |
IsaRobin | 0:6972d0e91af1 | 70 | high3 = H3.step(notch3); |
IsaRobin | 0:6972d0e91af1 | 71 | |
IsaRobin | 0:6972d0e91af1 | 72 | //rectify toepassen, oftewel absolute waarde van EMG signaal nemen |
IsaRobin | 0:6972d0e91af1 | 73 | absolute1 = fabs(high1); |
IsaRobin | 0:6972d0e91af1 | 74 | absolute2 = fabs(high2); |
IsaRobin | 0:6972d0e91af1 | 75 | absolute3 = fabs(high3); |
IsaRobin | 0:6972d0e91af1 | 76 | |
IsaRobin | 0:6972d0e91af1 | 77 | //low pass filter |
IsaRobin | 0:6972d0e91af1 | 78 | low1 = L1.step(absolute1); |
IsaRobin | 0:6972d0e91af1 | 79 | low2 = L2.step(absolute2); |
IsaRobin | 0:6972d0e91af1 | 80 | low3 = L3.step(absolute3); |