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: mbed HIDScope BiQuad4th_order biquadFilter MODSERIAL
main.cpp@21:2c26b74a3e48, 2019-10-29 (annotated)
- Committer:
- sanou8
- Date:
- Tue Oct 29 09:51:59 2019 +0000
- Revision:
- 21:2c26b74a3e48
- Parent:
- 20:97059009a491
- Child:
- 22:08b3cd7bec7f
kaulo vo, calibratie alles
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| vsluiter | 0:32bb76391d89 | 1 | #include "mbed.h" | 
| vsluiter | 11:ce72ec658a95 | 2 | #include "HIDScope.h" | 
| sanou8 | 21:2c26b74a3e48 | 3 | #include "FilterDesign.h" | 
| sanou8 | 21:2c26b74a3e48 | 4 | #include "BiQuad.h" | 
| sanou8 | 21:2c26b74a3e48 | 5 | #include "BiQuad4.h" | 
| sanou8 | 21:2c26b74a3e48 | 6 | #include "MODSERIAL.h" | 
| sanou8 | 21:2c26b74a3e48 | 7 | |
| sanou8 | 21:2c26b74a3e48 | 8 | Serial pc(USBTX,USBRX); | 
| sanou8 | 21:2c26b74a3e48 | 9 | DigitalIn button(SW3) ; | 
| sanou8 | 21:2c26b74a3e48 | 10 | |
| vsluiter | 0:32bb76391d89 | 11 | |
| vsluiter | 4:8b298dfada81 | 12 | //Define objects | 
| tomlankhorst | 19:2bf824669684 | 13 | AnalogIn emg0( A0 ); | 
| tomlankhorst | 19:2bf824669684 | 14 | AnalogIn emg1( A1 ); | 
| sanou8 | 21:2c26b74a3e48 | 15 | AnalogIn potmeter1(PTC11); // Input of two potmeters | 
| sanou8 | 21:2c26b74a3e48 | 16 | AnalogIn potmeter2(PTC10); | 
| tomlankhorst | 19:2bf824669684 | 17 | |
| sanou8 | 21:2c26b74a3e48 | 18 | |
| sanou8 | 21:2c26b74a3e48 | 19 | Ticker ticker_calibration; // Ticker to send the EMG signals to screen | 
| sanou8 | 21:2c26b74a3e48 | 20 | Ticker sample_timer; // Ticker for reading out EMG | 
| tomlankhorst | 19:2bf824669684 | 21 | HIDScope scope( 2 ); | 
| tomlankhorst | 18:21d8e7a81cf5 | 22 | DigitalOut led(LED1); | 
| vsluiter | 2:e314bb3b2d99 | 23 | |
| sanou8 | 21:2c26b74a3e48 | 24 | volatile double emg1_filtered; //measured value of the first emg | 
| sanou8 | 21:2c26b74a3e48 | 25 | volatile double emg2_filtered; //measured value of the second emg | 
| sanou8 | 21:2c26b74a3e48 | 26 | volatile double emg1_max ; // calibrated value of first emg | 
| sanou8 | 21:2c26b74a3e48 | 27 | volatile double emg2_max ; | 
| sanou8 | 21:2c26b74a3e48 | 28 | volatile double emg1_cal = 0.1; | 
| sanou8 | 21:2c26b74a3e48 | 29 | |
| sanou8 | 21:2c26b74a3e48 | 30 | // Read EMG | 
| sanou8 | 21:2c26b74a3e48 | 31 | //void EMGread() | 
| sanou8 | 21:2c26b74a3e48 | 32 | //{ | 
| sanou8 | 21:2c26b74a3e48 | 33 | // emg1_filtered = FilterDesign(emg0.read()); | 
| sanou8 | 21:2c26b74a3e48 | 34 | // emg2_filtered = FilterDesign(emg1.read()); | 
| sanou8 | 21:2c26b74a3e48 | 35 | //pc.printf("emg1_cal = %f, emg2_cal = %f \n\r", emg1_filtered, emg2_filtered); | 
| sanou8 | 21:2c26b74a3e48 | 36 | //} | 
| sanou8 | 21:2c26b74a3e48 | 37 | |
| sanou8 | 21:2c26b74a3e48 | 38 | |
| sanou8 | 21:2c26b74a3e48 | 39 | |
| sanou8 | 21:2c26b74a3e48 | 40 | |
| sanou8 | 21:2c26b74a3e48 | 41 | void sample() ; | 
| sanou8 | 21:2c26b74a3e48 | 42 | void EMGcalibration () ; | 
| sanou8 | 21:2c26b74a3e48 | 43 | |
| sanou8 | 21:2c26b74a3e48 | 44 | |
| sanou8 | 21:2c26b74a3e48 | 45 | |
| vsluiter | 0:32bb76391d89 | 46 | |
| vsluiter | 0:32bb76391d89 | 47 | int main() | 
| tomlankhorst | 19:2bf824669684 | 48 | { | 
| tomlankhorst | 14:f83354387756 | 49 | /**Attach the 'sample' function to the timer 'sample_timer'. | 
| tomlankhorst | 19:2bf824669684 | 50 | * this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz | 
| vsluiter | 4:8b298dfada81 | 51 | */ | 
| sanou8 | 21:2c26b74a3e48 | 52 | |
| sanou8 | 21:2c26b74a3e48 | 53 | pc.baud(115200); | 
| sanou8 | 21:2c26b74a3e48 | 54 | //EMGcalibration(); | 
| tomlankhorst | 19:2bf824669684 | 55 | sample_timer.attach(&sample, 0.002); | 
| sanou8 | 21:2c26b74a3e48 | 56 | |
| sanou8 | 21:2c26b74a3e48 | 57 | |
| tomlankhorst | 14:f83354387756 | 58 | /*empty loop, sample() is executed periodically*/ | 
| sanou8 | 21:2c26b74a3e48 | 59 | while(true) { | 
| sanou8 | 21:2c26b74a3e48 | 60 | if(SW3==0){ | 
| sanou8 | 21:2c26b74a3e48 | 61 | EMGcalibration(); | 
| sanou8 | 21:2c26b74a3e48 | 62 | } | 
| sanou8 | 21:2c26b74a3e48 | 63 | if(emg1_filtered >= 0.8){ | 
| sanou8 | 21:2c26b74a3e48 | 64 | led = !led; | 
| sanou8 | 21:2c26b74a3e48 | 65 | } | 
| sanou8 | 21:2c26b74a3e48 | 66 | } | 
| sanou8 | 21:2c26b74a3e48 | 67 | } | 
| sanou8 | 21:2c26b74a3e48 | 68 | |
| sanou8 | 21:2c26b74a3e48 | 69 | |
| sanou8 | 21:2c26b74a3e48 | 70 | |
| sanou8 | 21:2c26b74a3e48 | 71 | |
| sanou8 | 21:2c26b74a3e48 | 72 | |
| sanou8 | 21:2c26b74a3e48 | 73 | |
| sanou8 | 21:2c26b74a3e48 | 74 | void sample() | 
| sanou8 | 21:2c26b74a3e48 | 75 | { | 
| sanou8 | 21:2c26b74a3e48 | 76 | emg1_filtered = FilterDesign(emg0.read()); | 
| sanou8 | 21:2c26b74a3e48 | 77 | emg2_filtered = FilterDesign(emg1.read()); | 
| sanou8 | 21:2c26b74a3e48 | 78 | /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */ | 
| sanou8 | 21:2c26b74a3e48 | 79 | scope.set(0, emg1_filtered ) ; | 
| sanou8 | 21:2c26b74a3e48 | 80 | scope.set(1, emg2_filtered ); | 
| sanou8 | 21:2c26b74a3e48 | 81 | /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) | 
| sanou8 | 21:2c26b74a3e48 | 82 | * Ensure that enough channels are available (HIDScope scope( 2 )) | 
| sanou8 | 21:2c26b74a3e48 | 83 | * Finally, send all channels to the PC at once */ | 
| sanou8 | 21:2c26b74a3e48 | 84 | scope.send(); | 
| sanou8 | 21:2c26b74a3e48 | 85 | /* To indicate that the function is working, the LED is toggled */ | 
| sanou8 | 21:2c26b74a3e48 | 86 | //pc.printf("%f", emg1_filtered) | 
| sanou8 | 21:2c26b74a3e48 | 87 | //led = !led; | 
| sanou8 | 21:2c26b74a3e48 | 88 | } | 
| sanou8 | 21:2c26b74a3e48 | 89 | |
| sanou8 | 21:2c26b74a3e48 | 90 | void EMGcalibration () | 
| sanou8 | 21:2c26b74a3e48 | 91 | { | 
| sanou8 | 21:2c26b74a3e48 | 92 | |
| sanou8 | 21:2c26b74a3e48 | 93 | Timer t; | 
| sanou8 | 21:2c26b74a3e48 | 94 | t.start(); | 
| sanou8 | 21:2c26b74a3e48 | 95 | do { | 
| sanou8 | 21:2c26b74a3e48 | 96 | ticker_calibration.attach(&sample, 0.002); | 
| sanou8 | 21:2c26b74a3e48 | 97 | if(emg1_cal < emg1_filtered){ | 
| sanou8 | 21:2c26b74a3e48 | 98 | emg1_cal = emg1_filtered ; | 
| sanou8 | 21:2c26b74a3e48 | 99 | } | 
| sanou8 | 21:2c26b74a3e48 | 100 | }while(t<10); | 
| vsluiter | 0:32bb76391d89 | 101 | } |