totale unit
Dependencies: mbed QEI HIDScope BiQuad4th_order biquadFilter MODSERIAL FastPWM
main.cpp@22:08b3cd7bec7f, 2019-10-29 (annotated)
- Committer:
- lucvandijk
- Date:
- Tue Oct 29 12:11:23 2019 +0000
- Revision:
- 22:08b3cd7bec7f
- Parent:
- 21:2c26b74a3e48
- Child:
- 23:d13db573a875
werkt prima, ipv de led in de main kan de motor komen;
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 ; |
lucvandijk | 22:08b3cd7bec7f | 28 | volatile double emg1_cal = 0.8; |
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 | } |
lucvandijk | 22:08b3cd7bec7f | 63 | |
lucvandijk | 22:08b3cd7bec7f | 64 | led = 1; |
lucvandijk | 22:08b3cd7bec7f | 65 | |
lucvandijk | 22:08b3cd7bec7f | 66 | while(emg1_filtered >= 0.9*emg1_cal){ |
lucvandijk | 22:08b3cd7bec7f | 67 | led = 0; |
lucvandijk | 22:08b3cd7bec7f | 68 | } |
lucvandijk | 22:08b3cd7bec7f | 69 | } |
sanou8 | 21:2c26b74a3e48 | 70 | } |
sanou8 | 21:2c26b74a3e48 | 71 | |
sanou8 | 21:2c26b74a3e48 | 72 | |
sanou8 | 21:2c26b74a3e48 | 73 | |
sanou8 | 21:2c26b74a3e48 | 74 | |
sanou8 | 21:2c26b74a3e48 | 75 | |
sanou8 | 21:2c26b74a3e48 | 76 | |
sanou8 | 21:2c26b74a3e48 | 77 | void sample() |
sanou8 | 21:2c26b74a3e48 | 78 | { |
sanou8 | 21:2c26b74a3e48 | 79 | emg1_filtered = FilterDesign(emg0.read()); |
sanou8 | 21:2c26b74a3e48 | 80 | emg2_filtered = FilterDesign(emg1.read()); |
sanou8 | 21:2c26b74a3e48 | 81 | /* 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 | 82 | scope.set(0, emg1_filtered ) ; |
sanou8 | 21:2c26b74a3e48 | 83 | scope.set(1, emg2_filtered ); |
sanou8 | 21:2c26b74a3e48 | 84 | /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) |
sanou8 | 21:2c26b74a3e48 | 85 | * Ensure that enough channels are available (HIDScope scope( 2 )) |
sanou8 | 21:2c26b74a3e48 | 86 | * Finally, send all channels to the PC at once */ |
sanou8 | 21:2c26b74a3e48 | 87 | scope.send(); |
sanou8 | 21:2c26b74a3e48 | 88 | /* To indicate that the function is working, the LED is toggled */ |
sanou8 | 21:2c26b74a3e48 | 89 | //pc.printf("%f", emg1_filtered) |
sanou8 | 21:2c26b74a3e48 | 90 | //led = !led; |
sanou8 | 21:2c26b74a3e48 | 91 | } |
sanou8 | 21:2c26b74a3e48 | 92 | |
sanou8 | 21:2c26b74a3e48 | 93 | void EMGcalibration () |
sanou8 | 21:2c26b74a3e48 | 94 | { |
sanou8 | 21:2c26b74a3e48 | 95 | |
sanou8 | 21:2c26b74a3e48 | 96 | Timer t; |
sanou8 | 21:2c26b74a3e48 | 97 | t.start(); |
sanou8 | 21:2c26b74a3e48 | 98 | do { |
sanou8 | 21:2c26b74a3e48 | 99 | ticker_calibration.attach(&sample, 0.002); |
sanou8 | 21:2c26b74a3e48 | 100 | if(emg1_cal < emg1_filtered){ |
sanou8 | 21:2c26b74a3e48 | 101 | emg1_cal = emg1_filtered ; |
sanou8 | 21:2c26b74a3e48 | 102 | } |
sanou8 | 21:2c26b74a3e48 | 103 | }while(t<10); |
vsluiter | 0:32bb76391d89 | 104 | } |