now with PID controler XXXD
Dependencies: HIDScope MODSERIAL QEI biquadFilter mbed
Fork of another_try_from_scratch_on_emg by
main.cpp@36:959f4719ab3a, 2016-10-28 (annotated)
- Committer:
- daniQQue
- Date:
- Fri Oct 28 11:37:33 2016 +0000
- Revision:
- 36:959f4719ab3a
- Parent:
- 22:eb8411807cca
direction motor 0 and 1 and different pole works
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
daniQQue | 0:34c739fcc3e0 | 1 | //libraries |
daniQQue | 0:34c739fcc3e0 | 2 | #include "mbed.h" |
daniQQue | 0:34c739fcc3e0 | 3 | #include "HIDScope.h" |
daniQQue | 14:5b17697cf775 | 4 | #include "BiQuad.h" |
daniQQue | 0:34c739fcc3e0 | 5 | |
daniQQue | 0:34c739fcc3e0 | 6 | //Define objects |
daniQQue | 21:9b8be3dff01a | 7 | AnalogIn emg_biceps_right_in( A0 ); //analog in to get EMG biceps (r) in to c++ |
daniQQue | 21:9b8be3dff01a | 8 | AnalogIn emg_triceps_right_in(A1); //analog in to get EMG triceps (r) in to c++ |
daniQQue | 21:9b8be3dff01a | 9 | //AnalogIn emg_biceps_left_in (A2); //analog in to get EMG biceps (l) in to c++ |
daniQQue | 0:34c739fcc3e0 | 10 | Ticker sample_timer; //ticker |
daniQQue | 21:9b8be3dff01a | 11 | HIDScope scope(6); //open 3 channels in hidscope |
daniQQue | 8:cd0cb71b69f2 | 12 | DigitalOut richting_motor1(D4); //motor1 direction output |
daniQQue | 8:cd0cb71b69f2 | 13 | PwmOut pwm_motor1(D5); //motor1 velocity output |
daniQQue | 21:9b8be3dff01a | 14 | DigitalOut led(LED_GREEN); //led included to check where code is |
daniQQue | 0:34c739fcc3e0 | 15 | |
daniQQue | 0:34c739fcc3e0 | 16 | //define variables |
daniQQue | 21:9b8be3dff01a | 17 | //other |
daniQQue | 21:9b8be3dff01a | 18 | int onoffsignal_rightarm=0; // on/off signal: 1; biceps activation, 0: nothing, -1, triceps activation |
daniQQue | 36:959f4719ab3a | 19 | double cut_off_value_biceps =0.04; //gespecificeerd door floortje |
daniQQue | 36:959f4719ab3a | 20 | double cut_off_value_triceps=-0.02; //gespecificeerd door floortje |
daniQQue | 21:9b8be3dff01a | 21 | double signal_right_arm; |
daniQQue | 21:9b8be3dff01a | 22 | |
daniQQue | 21:9b8be3dff01a | 23 | //biceps arm 1, right arm |
daniQQue | 15:bb4a6c7836d8 | 24 | double emg_biceps_right; |
daniQQue | 15:bb4a6c7836d8 | 25 | double emg_filtered_high_biceps_right; |
daniQQue | 15:bb4a6c7836d8 | 26 | double emg_abs_biceps_right; |
daniQQue | 15:bb4a6c7836d8 | 27 | double emg_filtered_biceps_right; |
daniQQue | 21:9b8be3dff01a | 28 | double emg_filtered_high_notch_1_biceps_right; |
daniQQue | 21:9b8be3dff01a | 29 | double emg_filtered_high_notch_1_2_biceps_right; |
daniQQue | 5:688b1b5530d8 | 30 | |
daniQQue | 21:9b8be3dff01a | 31 | //triceps arm 1, right arm |
daniQQue | 21:9b8be3dff01a | 32 | double emg_triceps_right; |
daniQQue | 21:9b8be3dff01a | 33 | double emg_filtered_high_triceps_right; |
daniQQue | 21:9b8be3dff01a | 34 | double emg_abs_triceps_right; |
daniQQue | 21:9b8be3dff01a | 35 | double emg_filtered_triceps_right; |
daniQQue | 21:9b8be3dff01a | 36 | double emg_filtered_high_notch_1_triceps_right; |
daniQQue | 21:9b8be3dff01a | 37 | double emg_filtered_high_notch_1_2_triceps_right; |
daniQQue | 21:9b8be3dff01a | 38 | |
daniQQue | 21:9b8be3dff01a | 39 | //before abs filtering |
daniQQue | 22:eb8411807cca | 40 | BiQuad filterhigh_b1(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01); |
daniQQue | 22:eb8411807cca | 41 | BiQuad filternotch1_b1 (9.9115e-01, -1.8853e+00, 9.9115e-01 ,-1.8909e+00 , 9.9103e-01); |
daniQQue | 22:eb8411807cca | 42 | BiQuad filternotch2_b1(1.0000e+00, -1.9022e+00, 1.0000e+00, -1.8965e+00 , 9.9127e-01); |
daniQQue | 22:eb8411807cca | 43 | BiQuad filterhigh_t1(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01); |
daniQQue | 22:eb8411807cca | 44 | BiQuad filternotch1_t1 (9.9115e-01, -1.8853e+00, 9.9115e-01 ,-1.8909e+00 , 9.9103e-01); |
daniQQue | 22:eb8411807cca | 45 | BiQuad filternotch2_t1(1.0000e+00, -1.9022e+00, 1.0000e+00, -1.8965e+00 , 9.9127e-01); |
daniQQue | 21:9b8be3dff01a | 46 | |
daniQQue | 21:9b8be3dff01a | 47 | //after abs filtering |
daniQQue | 22:eb8411807cca | 48 | BiQuad filterlow_b1 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01); |
daniQQue | 22:eb8411807cca | 49 | BiQuad filterlow_t1 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01); |
daniQQue | 10:7255b59224cc | 50 | |
daniQQue | 21:9b8be3dff01a | 51 | //functions which are called in ticker to sample the analog signal |
daniQQue | 0:34c739fcc3e0 | 52 | void filter(){ |
daniQQue | 21:9b8be3dff01a | 53 | //biceps right arm read+filtering |
daniQQue | 15:bb4a6c7836d8 | 54 | emg_biceps_right=emg_biceps_right_in.read(); //read the emg value from the elektrodes |
daniQQue | 22:eb8411807cca | 55 | emg_filtered_high_biceps_right= filterhigh_b1.step(emg_biceps_right); |
daniQQue | 22:eb8411807cca | 56 | emg_filtered_high_notch_1_biceps_right=filternotch1_b1.step(emg_filtered_high_biceps_right); |
daniQQue | 22:eb8411807cca | 57 | emg_filtered_high_notch_1_2_biceps_right=filternotch2_b1.step(emg_filtered_high_notch_1_biceps_right); |
daniQQue | 21:9b8be3dff01a | 58 | emg_abs_biceps_right=fabs(emg_filtered_high_notch_1_2_biceps_right); //fabs because float |
daniQQue | 22:eb8411807cca | 59 | emg_filtered_biceps_right=filterlow_b1.step(emg_abs_biceps_right); |
daniQQue | 21:9b8be3dff01a | 60 | |
daniQQue | 21:9b8be3dff01a | 61 | //triceps right arm read+filtering |
daniQQue | 21:9b8be3dff01a | 62 | emg_triceps_right=emg_triceps_right_in.read(); //read the emg value from the elektrodes |
daniQQue | 22:eb8411807cca | 63 | emg_filtered_high_triceps_right= filterhigh_t1.step(emg_triceps_right); |
daniQQue | 22:eb8411807cca | 64 | emg_filtered_high_notch_1_triceps_right=filternotch1_t1.step(emg_filtered_high_triceps_right); |
daniQQue | 22:eb8411807cca | 65 | emg_filtered_high_notch_1_2_triceps_right=filternotch2_t1.step(emg_filtered_high_notch_1_triceps_right); |
daniQQue | 21:9b8be3dff01a | 66 | emg_abs_triceps_right=fabs(emg_filtered_high_notch_1_2_triceps_right); //fabs because float |
daniQQue | 22:eb8411807cca | 67 | emg_filtered_triceps_right=filterlow_t1.step(emg_abs_triceps_right); |
daniQQue | 7:42d0e38196f1 | 68 | |
daniQQue | 21:9b8be3dff01a | 69 | //signal substraction of filter biceps and triceps. Biceps +,triceps - |
daniQQue | 21:9b8be3dff01a | 70 | signal_right_arm=emg_filtered_biceps_right-emg_filtered_triceps_right; |
daniQQue | 21:9b8be3dff01a | 71 | |
daniQQue | 21:9b8be3dff01a | 72 | //creating of on/off signal with the created on/off signals, with if statement |
daniQQue | 21:9b8be3dff01a | 73 | if (signal_right_arm>cut_off_value_biceps) |
daniQQue | 21:9b8be3dff01a | 74 | {onoffsignal_rightarm=1;} |
daniQQue | 7:42d0e38196f1 | 75 | |
daniQQue | 21:9b8be3dff01a | 76 | else if (signal_right_arm<cut_off_value_triceps) |
daniQQue | 21:9b8be3dff01a | 77 | { |
daniQQue | 21:9b8be3dff01a | 78 | onoffsignal_rightarm=-1; |
daniQQue | 21:9b8be3dff01a | 79 | } |
daniQQue | 21:9b8be3dff01a | 80 | |
daniQQue | 21:9b8be3dff01a | 81 | else |
daniQQue | 21:9b8be3dff01a | 82 | {onoffsignal_rightarm=0;} |
daniQQue | 7:42d0e38196f1 | 83 | |
daniQQue | 0:34c739fcc3e0 | 84 | //send signals to scope |
daniQQue | 21:9b8be3dff01a | 85 | scope.set(0, emg_filtered_biceps_right); //set emg signal to scope in channel 0 |
daniQQue | 21:9b8be3dff01a | 86 | scope.set(1, emg_filtered_triceps_right); // set emg signal to scope in channel 1 |
daniQQue | 21:9b8be3dff01a | 87 | scope.set(2, signal_right_arm); // set emg signal to scope in channel 2 |
daniQQue | 21:9b8be3dff01a | 88 | scope.set(3, onoffsignal_rightarm); // set emg signal to scope in channel 3 |
daniQQue | 21:9b8be3dff01a | 89 | scope.set(4, emg_biceps_right); |
daniQQue | 21:9b8be3dff01a | 90 | scope.set(5, emg_triceps_right); |
daniQQue | 0:34c739fcc3e0 | 91 | scope.send(); //send all the signals to the scope |
daniQQue | 0:34c739fcc3e0 | 92 | } |
daniQQue | 0:34c739fcc3e0 | 93 | |
daniQQue | 0:34c739fcc3e0 | 94 | //program |
daniQQue | 0:34c739fcc3e0 | 95 | |
daniQQue | 0:34c739fcc3e0 | 96 | int main() |
daniQQue | 0:34c739fcc3e0 | 97 | { |
daniQQue | 0:34c739fcc3e0 | 98 | sample_timer.attach(&filter, 0.001); //continously execute the EMG reader and filter, it ensures that filter and sampling is executed every 1/frequency seconds |
daniQQue | 4:7d9ca9c1dcce | 99 | |
daniQQue | 0:34c739fcc3e0 | 100 | //endless loop |
daniQQue | 0:34c739fcc3e0 | 101 | |
daniQQue | 0:34c739fcc3e0 | 102 | while(1) |
daniQQue | 8:cd0cb71b69f2 | 103 | { |
daniQQue | 21:9b8be3dff01a | 104 | if (onoffsignal_rightarm==1) |
daniQQue | 8:cd0cb71b69f2 | 105 | { |
daniQQue | 36:959f4719ab3a | 106 | richting_motor1 = 0; //motordirection (ccw) |
daniQQue | 10:7255b59224cc | 107 | pwm_motor1 = 1; //motorspeed 1 |
daniQQue | 8:cd0cb71b69f2 | 108 | |
daniQQue | 8:cd0cb71b69f2 | 109 | } |
daniQQue | 21:9b8be3dff01a | 110 | else if(onoffsignal_rightarm==-1) |
daniQQue | 8:cd0cb71b69f2 | 111 | { |
daniQQue | 21:9b8be3dff01a | 112 | richting_motor1 = 1; //motordirection (cw) |
daniQQue | 21:9b8be3dff01a | 113 | pwm_motor1 = 1; //motorspeed 1 |
daniQQue | 21:9b8be3dff01a | 114 | } |
daniQQue | 8:cd0cb71b69f2 | 115 | |
daniQQue | 21:9b8be3dff01a | 116 | else if(onoffsignal_rightarm==0) |
daniQQue | 21:9b8be3dff01a | 117 | { |
daniQQue | 21:9b8be3dff01a | 118 | richting_motor1 = -1; //motordirection (ccw) |
daniQQue | 21:9b8be3dff01a | 119 | pwm_motor1 = 0; //motorspeed 0 |
daniQQue | 21:9b8be3dff01a | 120 | |
daniQQue | 21:9b8be3dff01a | 121 | } |
daniQQue | 21:9b8be3dff01a | 122 | |
daniQQue | 8:cd0cb71b69f2 | 123 | } |
daniQQue | 0:34c739fcc3e0 | 124 | |
daniQQue | 0:34c739fcc3e0 | 125 | } |