to send emg signal to motor with test programm
Dependencies: HIDScope biquadFilter mbed
Fork of TestProgramm by
main.cpp@25:9c2fc98da6b3, 2017-10-24 (annotated)
- Committer:
- Roytsg
- Date:
- Tue Oct 24 10:11:31 2017 +0000
- Revision:
- 25:9c2fc98da6b3
- Parent:
- 24:fe3825337233
- Child:
- 26:97a8adc9b895
movmean with 12 samples;
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" |
Roytsg | 21:77998ce2c0dd | 3 | #include "BiQuad.h" |
Roytsg | 23:b5a09f96c2d7 | 4 | #include "math.h" |
vsluiter | 0:32bb76391d89 | 5 | |
Roytsg | 24:fe3825337233 | 6 | |
vsluiter | 4:8b298dfada81 | 7 | //Define objects |
tomlankhorst | 19:2bf824669684 | 8 | AnalogIn emg0( A0 ); |
tomlankhorst | 19:2bf824669684 | 9 | AnalogIn emg1( A1 ); |
tomlankhorst | 19:2bf824669684 | 10 | |
tomlankhorst | 14:f83354387756 | 11 | Ticker sample_timer; |
Roytsg | 24:fe3825337233 | 12 | HIDScope scope( 3 ); |
tomlankhorst | 18:21d8e7a81cf5 | 13 | DigitalOut led(LED1); |
vsluiter | 2:e314bb3b2d99 | 14 | |
tomlankhorst | 14:f83354387756 | 15 | /** Sample function |
tomlankhorst | 14:f83354387756 | 16 | * this function samples the emg and sends it to HIDScope |
tomlankhorst | 14:f83354387756 | 17 | **/ |
Roytsg | 24:fe3825337233 | 18 | |
Roytsg | 24:fe3825337233 | 19 | double a1 = 0; |
Roytsg | 24:fe3825337233 | 20 | double a2 = 0; |
Roytsg | 24:fe3825337233 | 21 | double a3 = 0; |
Roytsg | 24:fe3825337233 | 22 | double a4 = 0; |
Roytsg | 24:fe3825337233 | 23 | double a5 = 0; |
Roytsg | 25:9c2fc98da6b3 | 24 | double a6 = 0; |
Roytsg | 25:9c2fc98da6b3 | 25 | double a7 = 0; |
Roytsg | 25:9c2fc98da6b3 | 26 | double a8 = 0; |
Roytsg | 25:9c2fc98da6b3 | 27 | double a9 = 0; |
Roytsg | 25:9c2fc98da6b3 | 28 | double a10 = 0; |
Roytsg | 25:9c2fc98da6b3 | 29 | double a11 = 0; |
Roytsg | 25:9c2fc98da6b3 | 30 | double a12 = 0; |
Roytsg | 24:fe3825337233 | 31 | |
tomlankhorst | 14:f83354387756 | 32 | void sample() |
vsluiter | 2:e314bb3b2d99 | 33 | { |
tomlankhorst | 19:2bf824669684 | 34 | /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */ |
tomlankhorst | 19:2bf824669684 | 35 | scope.set(0, emg0.read() ); |
tomlankhorst | 19:2bf824669684 | 36 | scope.set(1, emg1.read() ); |
tomlankhorst | 19:2bf824669684 | 37 | /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) |
tomlankhorst | 19:2bf824669684 | 38 | * Ensure that enough channels are available (HIDScope scope( 2 )) |
tomlankhorst | 20:97059009a491 | 39 | * Finally, send all channels to the PC at once */ |
vsluiter | 11:ce72ec658a95 | 40 | scope.send(); |
tomlankhorst | 18:21d8e7a81cf5 | 41 | /* To indicate that the function is working, the LED is toggled */ |
tomlankhorst | 18:21d8e7a81cf5 | 42 | led = !led; |
vsluiter | 2:e314bb3b2d99 | 43 | } |
vsluiter | 0:32bb76391d89 | 44 | |
Roytsg | 21:77998ce2c0dd | 45 | BiQuadChain bqc; |
Roytsg | 21:77998ce2c0dd | 46 | BiQuad bq1( 0.6844323315947305,1.368864663189461, 0.6844323315947305,1.2243497755555954,0.5133795508233265); |
Roytsg | 21:77998ce2c0dd | 47 | BiQuad bq2( 0.6844323315947306, -1.3688646631894612, 0.6844323315947306, -1.2243497755555959, 0.5133795508233266); |
Roytsg | 21:77998ce2c0dd | 48 | BiQuad bq3( 0.7566897754116633, -1.2243497755555959, 0.7566897754116633, -1.2243497755555959, 0.5133795508233266); |
Roytsg | 21:77998ce2c0dd | 49 | |
Roytsg | 21:77998ce2c0dd | 50 | Ticker emgSampleTicker; |
Roytsg | 21:77998ce2c0dd | 51 | AnalogIn emg( A0 ); |
Roytsg | 21:77998ce2c0dd | 52 | |
Roytsg | 21:77998ce2c0dd | 53 | void emgSample() { |
Roytsg | 21:77998ce2c0dd | 54 | |
Roytsg | 24:fe3825337233 | 55 | double emgFiltered = bqc.step( emg.read() ); |
Roytsg | 24:fe3825337233 | 56 | double emgabs = abs(emgFiltered); |
Roytsg | 24:fe3825337233 | 57 | scope.set(0, emgFiltered ); |
Roytsg | 24:fe3825337233 | 58 | scope.set(1, emgabs ); |
Roytsg | 24:fe3825337233 | 59 | |
Roytsg | 25:9c2fc98da6b3 | 60 | a12 = a11; |
Roytsg | 25:9c2fc98da6b3 | 61 | a11 = a10; |
Roytsg | 25:9c2fc98da6b3 | 62 | a9 = a8; |
Roytsg | 25:9c2fc98da6b3 | 63 | a8 = a7; |
Roytsg | 25:9c2fc98da6b3 | 64 | a7 = a6; |
Roytsg | 25:9c2fc98da6b3 | 65 | a6 = a5; |
Roytsg | 24:fe3825337233 | 66 | a5 = a4; |
Roytsg | 24:fe3825337233 | 67 | a4 = a3; |
Roytsg | 24:fe3825337233 | 68 | a3 = a2; |
Roytsg | 24:fe3825337233 | 69 | a2 = a1; |
Roytsg | 24:fe3825337233 | 70 | a1 = emgabs; |
Roytsg | 24:fe3825337233 | 71 | |
Roytsg | 24:fe3825337233 | 72 | double moving = a1 + a2 + a3 + a4 + a5; |
Roytsg | 24:fe3825337233 | 73 | double movmean = moving/5; |
Roytsg | 24:fe3825337233 | 74 | scope.set(2, movmean ); |
Roytsg | 24:fe3825337233 | 75 | |
Roytsg | 21:77998ce2c0dd | 76 | scope.send(); |
Roytsg | 21:77998ce2c0dd | 77 | } |
Roytsg | 21:77998ce2c0dd | 78 | |
Roytsg | 21:77998ce2c0dd | 79 | |
vsluiter | 0:32bb76391d89 | 80 | int main() |
tomlankhorst | 19:2bf824669684 | 81 | { |
Roytsg | 21:77998ce2c0dd | 82 | bqc.add( &bq1 ).add( &bq2 ).add( &bq3 ); |
Roytsg | 21:77998ce2c0dd | 83 | emgSampleTicker.attach( &emgSample, 0.002 ); |
tomlankhorst | 14:f83354387756 | 84 | /**Attach the 'sample' function to the timer 'sample_timer'. |
tomlankhorst | 19:2bf824669684 | 85 | * this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz |
vsluiter | 4:8b298dfada81 | 86 | */ |
Roytsg | 21:77998ce2c0dd | 87 | //sample_timer.attach(&sample, 0.01); |
tomlankhorst | 15:0da764eea774 | 88 | |
tomlankhorst | 14:f83354387756 | 89 | /*empty loop, sample() is executed periodically*/ |
tomlankhorst | 15:0da764eea774 | 90 | while(1) {} |
vsluiter | 0:32bb76391d89 | 91 | } |