change at hidscope
Dependencies: HIDScope MODSERIAL QEI biquadFilter mbed
Fork of emg_import by
main.cpp@45:d0e9f586cd03, 2016-11-01 (annotated)
- Committer:
- daniQQue
- Date:
- Tue Nov 01 16:24:09 2016 +0000
- Revision:
- 45:d0e9f586cd03
- Parent:
- 44:969348be74a5
- Child:
- 46:f3c205dfb749
pc.printf("rev_counts_motor1= %f \r\n",rev_counts_motor1);; pc.printf("counts_encoder 1= %f \r\n",counts_encoder1);
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 | 40:187ef29de53d | 5 | #include "MODSERIAL.h" |
daniQQue | 45:d0e9f586cd03 | 6 | #include "QEI.h" |
daniQQue | 0:34c739fcc3e0 | 7 | |
daniQQue | 0:34c739fcc3e0 | 8 | //Define objects |
daniQQue | 45:d0e9f586cd03 | 9 | //EMG |
daniQQue | 45:d0e9f586cd03 | 10 | AnalogIn emg_biceps_right_in( A0); //analog in to get EMG biceps (r) in to c++ |
daniQQue | 45:d0e9f586cd03 | 11 | AnalogIn emg_triceps_right_in(A1); //analog in to get EMG triceps (r) in to c++ |
daniQQue | 45:d0e9f586cd03 | 12 | AnalogIn emg_biceps_left_in (A2); //analog in to get EMG biceps (l) in to c++ |
daniQQue | 42:7164ccd2aa14 | 13 | |
daniQQue | 45:d0e9f586cd03 | 14 | //Encoder |
daniQQue | 45:d0e9f586cd03 | 15 | DigitalIn encoder1A(D13); |
daniQQue | 45:d0e9f586cd03 | 16 | DigitalIn encoder1B(D12); |
daniQQue | 45:d0e9f586cd03 | 17 | DigitalIn encoder2A(D11); |
daniQQue | 45:d0e9f586cd03 | 18 | DigitalIn encoder2B(D10); |
daniQQue | 45:d0e9f586cd03 | 19 | |
daniQQue | 45:d0e9f586cd03 | 20 | //callibration buttons |
daniQQue | 45:d0e9f586cd03 | 21 | DigitalIn button_calibration_biceps (SW3); //button to start calibration biceps |
daniQQue | 45:d0e9f586cd03 | 22 | DigitalIn button_calibration_triceps (SW2); // button to start calibration tricps |
daniQQue | 42:7164ccd2aa14 | 23 | |
daniQQue | 45:d0e9f586cd03 | 24 | //tickers |
daniQQue | 45:d0e9f586cd03 | 25 | Ticker sample_timer; //ticker |
daniQQue | 45:d0e9f586cd03 | 26 | Ticker switch_function; //ticker |
daniQQue | 45:d0e9f586cd03 | 27 | Ticker ticker_calibration_biceps; |
daniQQue | 45:d0e9f586cd03 | 28 | Ticker ticker_calibration_triceps; |
daniQQue | 45:d0e9f586cd03 | 29 | |
daniQQue | 44:969348be74a5 | 30 | |
daniQQue | 45:d0e9f586cd03 | 31 | //everything for monitoring |
daniQQue | 45:d0e9f586cd03 | 32 | HIDScope scope(5); //open 5 channels in hidscope |
daniQQue | 45:d0e9f586cd03 | 33 | MODSERIAL pc(USBTX, USBRX); //pc connection |
daniQQue | 45:d0e9f586cd03 | 34 | DigitalOut red(LED_RED); |
daniQQue | 45:d0e9f586cd03 | 35 | DigitalOut green(LED_GREEN); |
daniQQue | 45:d0e9f586cd03 | 36 | DigitalOut blue(LED_BLUE); |
daniQQue | 45:d0e9f586cd03 | 37 | |
daniQQue | 40:187ef29de53d | 38 | //motors |
daniQQue | 45:d0e9f586cd03 | 39 | DigitalOut richting_motor1(D4); |
daniQQue | 45:d0e9f586cd03 | 40 | PwmOut pwm_motor1(D5); |
daniQQue | 45:d0e9f586cd03 | 41 | DigitalOut richting_motor2(D7); |
daniQQue | 45:d0e9f586cd03 | 42 | PwmOut pwm_motor2(D6); |
daniQQue | 0:34c739fcc3e0 | 43 | |
daniQQue | 0:34c739fcc3e0 | 44 | //define variables |
daniQQue | 42:7164ccd2aa14 | 45 | |
daniQQue | 45:d0e9f586cd03 | 46 | //for motorcontrol |
daniQQue | 45:d0e9f586cd03 | 47 | const int cw = 0; // motor should turn clockwise |
daniQQue | 45:d0e9f586cd03 | 48 | const int ccw =1; // motor should turn counterclockwise |
daniQQue | 45:d0e9f586cd03 | 49 | const float gearboxratio=131.25; // gearboxratio from encoder to motor |
daniQQue | 45:d0e9f586cd03 | 50 | const float rev_rond=64.0; // revolutions per round of encoder |
daniQQue | 45:d0e9f586cd03 | 51 | int onoffsignal_biceps=0; // on/off signal: 1; biceps activation, 0: nothing, -1, triceps activation |
daniQQue | 45:d0e9f586cd03 | 52 | int switch_signal_triceps=0; // switching between motors. |
daniQQue | 45:d0e9f586cd03 | 53 | |
daniQQue | 45:d0e9f586cd03 | 54 | volatile double cut_off_value_biceps_right = 0.04; //tested, normal values. Can be changed by calibration |
daniQQue | 45:d0e9f586cd03 | 55 | volatile double cut_off_value_biceps_left = -0.04; //volatiles becaused changen in interrupt |
daniQQue | 45:d0e9f586cd03 | 56 | volatile double cut_off_value_triceps=-0.03; |
daniQQue | 45:d0e9f586cd03 | 57 | double signal_biceps_sum; |
daniQQue | 45:d0e9f586cd03 | 58 | double bicepstriceps_rightarm; |
daniQQue | 45:d0e9f586cd03 | 59 | int motorswitch=0; |
daniQQue | 45:d0e9f586cd03 | 60 | volatile double rev_counts_motor1=0; |
daniQQue | 45:d0e9f586cd03 | 61 | volatile double rev_counts_motor2=0; |
daniQQue | 45:d0e9f586cd03 | 62 | volatile double counts_encoder1; |
daniQQue | 45:d0e9f586cd03 | 63 | volatile double counts_encoder2; |
daniQQue | 45:d0e9f586cd03 | 64 | |
daniQQue | 45:d0e9f586cd03 | 65 | //variables and constants for calibration |
daniQQue | 45:d0e9f586cd03 | 66 | const float percentage_max_triceps=0.3; |
daniQQue | 45:d0e9f586cd03 | 67 | const float percentage_max_biceps =0.3; |
daniQQue | 45:d0e9f586cd03 | 68 | double max_biceps; //calibration maximum biceps |
daniQQue | 45:d0e9f586cd03 | 69 | double max_triceps; //calibration maximum triceps |
daniQQue | 45:d0e9f586cd03 | 70 | |
daniQQue | 45:d0e9f586cd03 | 71 | //biceps arm 1, right arm |
daniQQue | 45:d0e9f586cd03 | 72 | double emg_biceps_right; |
daniQQue | 45:d0e9f586cd03 | 73 | double emg_filtered_high_biceps_right; |
daniQQue | 45:d0e9f586cd03 | 74 | double emg_abs_biceps_right; |
daniQQue | 45:d0e9f586cd03 | 75 | double emg_filtered_biceps_right; |
daniQQue | 45:d0e9f586cd03 | 76 | double emg_filtered_high_notch_1_biceps_right; |
daniQQue | 45:d0e9f586cd03 | 77 | //double emg_filtered_high_notch_1_2_biceps_right; |
daniQQue | 45:d0e9f586cd03 | 78 | |
daniQQue | 45:d0e9f586cd03 | 79 | //triceps arm 1, right arm |
daniQQue | 45:d0e9f586cd03 | 80 | double emg_triceps_right; |
daniQQue | 45:d0e9f586cd03 | 81 | double emg_filtered_high_triceps_right; |
daniQQue | 45:d0e9f586cd03 | 82 | double emg_abs_triceps_right; |
daniQQue | 45:d0e9f586cd03 | 83 | double emg_filtered_triceps_right; |
daniQQue | 45:d0e9f586cd03 | 84 | double emg_filtered_high_notch_1_triceps_right; |
daniQQue | 45:d0e9f586cd03 | 85 | |
daniQQue | 45:d0e9f586cd03 | 86 | //biceps arm 1, left arm |
daniQQue | 45:d0e9f586cd03 | 87 | double emg_biceps_left; |
daniQQue | 45:d0e9f586cd03 | 88 | double emg_filtered_high_biceps_left; |
daniQQue | 45:d0e9f586cd03 | 89 | double emg_abs_biceps_left; |
daniQQue | 45:d0e9f586cd03 | 90 | double emg_filtered_biceps_left; |
daniQQue | 45:d0e9f586cd03 | 91 | double emg_filtered_high_notch_1_biceps_left; |
daniQQue | 45:d0e9f586cd03 | 92 | |
daniQQue | 45:d0e9f586cd03 | 93 | //before abs filtering |
daniQQue | 45:d0e9f586cd03 | 94 | |
daniQQue | 45:d0e9f586cd03 | 95 | //b1 = biceps right arm |
daniQQue | 45:d0e9f586cd03 | 96 | BiQuad filterhigh_b1(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01); |
daniQQue | 45:d0e9f586cd03 | 97 | BiQuad filternotch1_b1 (9.9376e-01 , -1.8902e-00, 9.9376e-01 , -1.8902e-00 , 9.875e-01); |
daniQQue | 45:d0e9f586cd03 | 98 | |
daniQQue | 45:d0e9f586cd03 | 99 | //t1= triceps right arm |
daniQQue | 45:d0e9f586cd03 | 100 | BiQuad filterhigh_t1(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01); |
daniQQue | 45:d0e9f586cd03 | 101 | BiQuad filternotch1_t1 (9.9376e-01 , -1.8902e-00, 9.9376e-01 , -1.8902e-00 , 9.875e-01); |
daniQQue | 45:d0e9f586cd03 | 102 | |
daniQQue | 45:d0e9f586cd03 | 103 | //b2= biceps left arm |
daniQQue | 45:d0e9f586cd03 | 104 | BiQuad filterhigh_b2(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01); |
daniQQue | 45:d0e9f586cd03 | 105 | BiQuad filternotch1_b2 (9.9376e-01 , -1.8902e-00, 9.9376e-01 , -1.8902e-00 , 9.875e-01); |
daniQQue | 45:d0e9f586cd03 | 106 | |
daniQQue | 45:d0e9f586cd03 | 107 | //after abs filtering |
daniQQue | 45:d0e9f586cd03 | 108 | BiQuad filterlow_b1 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01); |
daniQQue | 45:d0e9f586cd03 | 109 | BiQuad filterlow_t1 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01); |
daniQQue | 45:d0e9f586cd03 | 110 | BiQuad filterlow_b2 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01); |
daniQQue | 10:7255b59224cc | 111 | |
daniQQue | 40:187ef29de53d | 112 | //function teller |
daniQQue | 40:187ef29de53d | 113 | void SwitchN() { // maakt simpele functie die 1 bij n optelt |
daniQQue | 41:9ea3d5921f07 | 114 | if(switch_signal_triceps==1) |
daniQQue | 40:187ef29de53d | 115 | { |
daniQQue | 42:7164ccd2aa14 | 116 | motorswitch++; |
daniQQue | 42:7164ccd2aa14 | 117 | |
daniQQue | 42:7164ccd2aa14 | 118 | if (motorswitch%2==0) |
daniQQue | 42:7164ccd2aa14 | 119 | {pc.printf("If you contract the right arm, the robot will go right \r\n"); |
daniQQue | 42:7164ccd2aa14 | 120 | pc.printf("If you contract biceps of the left arm, the robot will go left \r\n"); |
daniQQue | 42:7164ccd2aa14 | 121 | pc.printf("\r\n"); |
daniQQue | 42:7164ccd2aa14 | 122 | green=0; |
daniQQue | 42:7164ccd2aa14 | 123 | red=1; |
daniQQue | 42:7164ccd2aa14 | 124 | } |
daniQQue | 42:7164ccd2aa14 | 125 | |
daniQQue | 42:7164ccd2aa14 | 126 | else |
daniQQue | 42:7164ccd2aa14 | 127 | {pc.printf("If you contract the biceps of right arm, the robot will go up \r\n"); |
daniQQue | 41:9ea3d5921f07 | 128 | pc.printf("If you contract the biceps of left arm, the robot will go down \r\n"); |
daniQQue | 41:9ea3d5921f07 | 129 | pc.printf("\r\n"); |
daniQQue | 42:7164ccd2aa14 | 130 | green=1; |
daniQQue | 42:7164ccd2aa14 | 131 | red=0; |
daniQQue | 42:7164ccd2aa14 | 132 | |
daniQQue | 40:187ef29de53d | 133 | } |
daniQQue | 42:7164ccd2aa14 | 134 | |
daniQQue | 40:187ef29de53d | 135 | } |
daniQQue | 41:9ea3d5921f07 | 136 | } |
daniQQue | 40:187ef29de53d | 137 | |
daniQQue | 40:187ef29de53d | 138 | //functions which are called in ticker to sample the analog signal |
daniQQue | 40:187ef29de53d | 139 | |
daniQQue | 42:7164ccd2aa14 | 140 | //callibration |
daniQQue | 42:7164ccd2aa14 | 141 | void calibration_biceps(){ |
daniQQue | 44:969348be74a5 | 142 | if (button_calibration_biceps==0){ |
daniQQue | 42:7164ccd2aa14 | 143 | pc.printf("start of calibration biceps, contract maximal \n"); |
daniQQue | 42:7164ccd2aa14 | 144 | red=1; |
daniQQue | 42:7164ccd2aa14 | 145 | green=1; |
daniQQue | 42:7164ccd2aa14 | 146 | blue=0; |
daniQQue | 42:7164ccd2aa14 | 147 | |
daniQQue | 44:969348be74a5 | 148 | for(int n =0; n<1500;n++) //read for 2000 samples as calibration |
daniQQue | 42:7164ccd2aa14 | 149 | { |
daniQQue | 42:7164ccd2aa14 | 150 | emg_biceps_right=emg_biceps_right_in.read(); //read the emg value from the elektrodes |
daniQQue | 42:7164ccd2aa14 | 151 | emg_filtered_high_biceps_right= filterhigh_b1.step(emg_biceps_right); |
daniQQue | 42:7164ccd2aa14 | 152 | emg_filtered_high_notch_1_biceps_right=filternotch1_b1.step(emg_filtered_high_biceps_right); |
daniQQue | 42:7164ccd2aa14 | 153 | emg_abs_biceps_right=fabs(emg_filtered_high_notch_1_biceps_right); //fabs because float |
daniQQue | 42:7164ccd2aa14 | 154 | emg_filtered_biceps_right=filterlow_b1.step(emg_abs_biceps_right); |
daniQQue | 42:7164ccd2aa14 | 155 | |
daniQQue | 42:7164ccd2aa14 | 156 | if (emg_filtered_biceps_right > max_biceps) //determine what the highest reachable emg signal is |
daniQQue | 42:7164ccd2aa14 | 157 | { |
daniQQue | 42:7164ccd2aa14 | 158 | max_biceps = emg_filtered_biceps_right; |
daniQQue | 42:7164ccd2aa14 | 159 | |
daniQQue | 42:7164ccd2aa14 | 160 | } |
daniQQue | 42:7164ccd2aa14 | 161 | wait(0.001f); //to sample at same freq; 1000Hz |
daniQQue | 42:7164ccd2aa14 | 162 | } |
daniQQue | 42:7164ccd2aa14 | 163 | cut_off_value_biceps_right=percentage_max_biceps*max_biceps; |
daniQQue | 42:7164ccd2aa14 | 164 | cut_off_value_biceps_left=-cut_off_value_biceps_right; |
daniQQue | 42:7164ccd2aa14 | 165 | //toggle lights |
daniQQue | 42:7164ccd2aa14 | 166 | blue=!blue; |
daniQQue | 42:7164ccd2aa14 | 167 | |
daniQQue | 42:7164ccd2aa14 | 168 | pc.printf(" end of calibration\r\n",cut_off_value_biceps_right ); |
daniQQue | 42:7164ccd2aa14 | 169 | pc.printf(" change of cv biceps: %f ",cut_off_value_biceps_right ); |
daniQQue | 42:7164ccd2aa14 | 170 | |
daniQQue | 44:969348be74a5 | 171 | wait(0.2f); |
daniQQue | 42:7164ccd2aa14 | 172 | |
daniQQue | 42:7164ccd2aa14 | 173 | if (motorswitch%2==0) |
daniQQue | 42:7164ccd2aa14 | 174 | {green=0; |
daniQQue | 42:7164ccd2aa14 | 175 | red=1;} |
daniQQue | 42:7164ccd2aa14 | 176 | |
daniQQue | 42:7164ccd2aa14 | 177 | else {green=1; |
daniQQue | 42:7164ccd2aa14 | 178 | red=0;} |
daniQQue | 44:969348be74a5 | 179 | } |
daniQQue | 42:7164ccd2aa14 | 180 | } |
daniQQue | 42:7164ccd2aa14 | 181 | |
daniQQue | 42:7164ccd2aa14 | 182 | void calibration_triceps(){ |
daniQQue | 44:969348be74a5 | 183 | if(button_calibration_triceps==0){ |
daniQQue | 42:7164ccd2aa14 | 184 | red=1; |
daniQQue | 42:7164ccd2aa14 | 185 | green=1; |
daniQQue | 42:7164ccd2aa14 | 186 | blue=0; |
daniQQue | 42:7164ccd2aa14 | 187 | |
daniQQue | 42:7164ccd2aa14 | 188 | pc.printf("start of calibration triceps\r\n"); |
daniQQue | 42:7164ccd2aa14 | 189 | |
daniQQue | 44:969348be74a5 | 190 | for(int n =0; n<1500;n++) //read for 2000 samples as calibration |
daniQQue | 42:7164ccd2aa14 | 191 | { |
daniQQue | 42:7164ccd2aa14 | 192 | emg_triceps_right=emg_triceps_right_in.read(); //read the emg value from the elektrodes |
daniQQue | 42:7164ccd2aa14 | 193 | emg_filtered_high_triceps_right= filterhigh_t1.step(emg_triceps_right); |
daniQQue | 42:7164ccd2aa14 | 194 | emg_filtered_high_notch_1_triceps_right=filternotch1_t1.step(emg_filtered_high_triceps_right); |
daniQQue | 42:7164ccd2aa14 | 195 | emg_abs_triceps_right=fabs(emg_filtered_high_notch_1_triceps_right); //fabs because float |
daniQQue | 42:7164ccd2aa14 | 196 | emg_filtered_triceps_right=filterlow_t1.step(emg_abs_triceps_right); |
daniQQue | 42:7164ccd2aa14 | 197 | |
daniQQue | 42:7164ccd2aa14 | 198 | if (emg_filtered_triceps_right > max_triceps) //determine what the highest reachable emg signal is |
daniQQue | 42:7164ccd2aa14 | 199 | { |
daniQQue | 42:7164ccd2aa14 | 200 | max_triceps = emg_filtered_triceps_right; |
daniQQue | 42:7164ccd2aa14 | 201 | |
daniQQue | 42:7164ccd2aa14 | 202 | } |
daniQQue | 42:7164ccd2aa14 | 203 | wait(0.001f); //to sample at same freq; 1000Hz |
daniQQue | 42:7164ccd2aa14 | 204 | } |
daniQQue | 44:969348be74a5 | 205 | cut_off_value_triceps=-percentage_max_triceps*max_triceps; |
daniQQue | 42:7164ccd2aa14 | 206 | pc.printf(" end of calibration\r\n"); |
daniQQue | 42:7164ccd2aa14 | 207 | pc.printf(" change of cv triceps: %f ",cut_off_value_triceps ); |
daniQQue | 42:7164ccd2aa14 | 208 | blue=!blue; |
daniQQue | 44:969348be74a5 | 209 | wait(0.2f); |
daniQQue | 42:7164ccd2aa14 | 210 | if (motorswitch%2==0) |
daniQQue | 42:7164ccd2aa14 | 211 | {green=0; |
daniQQue | 42:7164ccd2aa14 | 212 | red=1;} |
daniQQue | 42:7164ccd2aa14 | 213 | |
daniQQue | 42:7164ccd2aa14 | 214 | else {green=1; |
daniQQue | 42:7164ccd2aa14 | 215 | red=0;} |
daniQQue | 44:969348be74a5 | 216 | } |
daniQQue | 42:7164ccd2aa14 | 217 | } |
daniQQue | 42:7164ccd2aa14 | 218 | |
daniQQue | 0:34c739fcc3e0 | 219 | void filter(){ |
daniQQue | 40:187ef29de53d | 220 | //biceps right arm read+filtering |
daniQQue | 15:bb4a6c7836d8 | 221 | emg_biceps_right=emg_biceps_right_in.read(); //read the emg value from the elektrodes |
daniQQue | 40:187ef29de53d | 222 | emg_filtered_high_biceps_right= filterhigh_b1.step(emg_biceps_right); |
daniQQue | 40:187ef29de53d | 223 | emg_filtered_high_notch_1_biceps_right=filternotch1_b1.step(emg_filtered_high_biceps_right); |
daniQQue | 44:969348be74a5 | 224 | emg_abs_biceps_right=fabs(emg_filtered_high_notch_1_biceps_right); //fabs because float |
daniQQue | 40:187ef29de53d | 225 | emg_filtered_biceps_right=filterlow_b1.step(emg_abs_biceps_right); |
daniQQue | 40:187ef29de53d | 226 | |
daniQQue | 40:187ef29de53d | 227 | //triceps right arm read+filtering |
daniQQue | 40:187ef29de53d | 228 | emg_triceps_right=emg_triceps_right_in.read(); //read the emg value from the elektrodes |
daniQQue | 40:187ef29de53d | 229 | emg_filtered_high_triceps_right= filterhigh_t1.step(emg_triceps_right); |
daniQQue | 40:187ef29de53d | 230 | emg_filtered_high_notch_1_triceps_right=filternotch1_t1.step(emg_filtered_high_triceps_right); |
daniQQue | 40:187ef29de53d | 231 | emg_abs_triceps_right=fabs(emg_filtered_high_notch_1_triceps_right); //fabs because float |
daniQQue | 40:187ef29de53d | 232 | emg_filtered_triceps_right=filterlow_t1.step(emg_abs_triceps_right); |
daniQQue | 7:42d0e38196f1 | 233 | |
daniQQue | 40:187ef29de53d | 234 | //biceps left arm read+filtering |
daniQQue | 40:187ef29de53d | 235 | emg_biceps_left=emg_biceps_left_in.read(); //read the emg value from the elektrodes |
daniQQue | 40:187ef29de53d | 236 | emg_filtered_high_biceps_left= filterhigh_b2.step(emg_biceps_left); |
daniQQue | 40:187ef29de53d | 237 | emg_filtered_high_notch_1_biceps_left=filternotch1_b2.step(emg_filtered_high_biceps_left); |
daniQQue | 40:187ef29de53d | 238 | emg_abs_biceps_left=fabs(emg_filtered_high_notch_1_biceps_left); //fabs because float |
daniQQue | 40:187ef29de53d | 239 | emg_filtered_biceps_left=filterlow_b2.step(emg_abs_biceps_left); |
daniQQue | 40:187ef29de53d | 240 | |
daniQQue | 41:9ea3d5921f07 | 241 | //signal substraction of filter biceps and triceps. right Biceps + left biceps - |
daniQQue | 41:9ea3d5921f07 | 242 | signal_biceps_sum=emg_filtered_biceps_right-emg_filtered_biceps_left; |
daniQQue | 44:969348be74a5 | 243 | bicepstriceps_rightarm=emg_filtered_biceps_right-emg_filtered_triceps_right; |
daniQQue | 40:187ef29de53d | 244 | //creating of on/off signal with the created on/off signals, with if statement for right arm! |
daniQQue | 41:9ea3d5921f07 | 245 | if (signal_biceps_sum>cut_off_value_biceps_right) |
daniQQue | 41:9ea3d5921f07 | 246 | {onoffsignal_biceps=1;} |
daniQQue | 7:42d0e38196f1 | 247 | |
daniQQue | 41:9ea3d5921f07 | 248 | else if (signal_biceps_sum<cut_off_value_biceps_left) |
daniQQue | 40:187ef29de53d | 249 | { |
daniQQue | 41:9ea3d5921f07 | 250 | onoffsignal_biceps=-1; |
daniQQue | 40:187ef29de53d | 251 | } |
daniQQue | 40:187ef29de53d | 252 | |
daniQQue | 40:187ef29de53d | 253 | else |
daniQQue | 41:9ea3d5921f07 | 254 | {onoffsignal_biceps=0;} |
daniQQue | 7:42d0e38196f1 | 255 | |
daniQQue | 40:187ef29de53d | 256 | //creating on/off signal for switch (left arm) |
daniQQue | 40:187ef29de53d | 257 | |
daniQQue | 44:969348be74a5 | 258 | if (bicepstriceps_rightarm<cut_off_value_triceps) |
daniQQue | 40:187ef29de53d | 259 | { |
daniQQue | 41:9ea3d5921f07 | 260 | switch_signal_triceps=1; |
daniQQue | 40:187ef29de53d | 261 | } |
daniQQue | 40:187ef29de53d | 262 | |
daniQQue | 40:187ef29de53d | 263 | else |
daniQQue | 40:187ef29de53d | 264 | { |
daniQQue | 41:9ea3d5921f07 | 265 | switch_signal_triceps=0; |
daniQQue | 40:187ef29de53d | 266 | } |
daniQQue | 40:187ef29de53d | 267 | |
daniQQue | 0:34c739fcc3e0 | 268 | //send signals to scope |
daniQQue | 40:187ef29de53d | 269 | scope.set(0, emg_filtered_biceps_right); //set emg signal to scope in channel 0 |
daniQQue | 40:187ef29de53d | 270 | scope.set(1, emg_filtered_triceps_right); // set emg signal to scope in channel 1 |
daniQQue | 40:187ef29de53d | 271 | scope.set(2, emg_filtered_biceps_left); // set emg signal to scope in channel 2 |
daniQQue | 44:969348be74a5 | 272 | scope.set(3, onoffsignal_biceps); |
daniQQue | 40:187ef29de53d | 273 | |
daniQQue | 40:187ef29de53d | 274 | scope.send(); //send all the signals to the scope |
daniQQue | 0:34c739fcc3e0 | 275 | } |
daniQQue | 0:34c739fcc3e0 | 276 | |
daniQQue | 0:34c739fcc3e0 | 277 | //program |
daniQQue | 0:34c739fcc3e0 | 278 | |
daniQQue | 0:34c739fcc3e0 | 279 | int main() |
daniQQue | 0:34c739fcc3e0 | 280 | { |
daniQQue | 42:7164ccd2aa14 | 281 | pc.baud(115200); //connect with pc with baudrate 115200 |
daniQQue | 45:d0e9f586cd03 | 282 | QEI Encoder2(D12,D13, NC, rev_rond,QEI::X4_ENCODING); // maakt een encoder aan! D12/D13 ingangen, rev_rond zijn aantal pulsen per revolutie! Bovenaan in te stellen. |
daniQQue | 45:d0e9f586cd03 | 283 | QEI Encoder1(D10,D11, NC, rev_rond,QEI::X4_ENCODING); |
daniQQue | 42:7164ccd2aa14 | 284 | |
daniQQue | 42:7164ccd2aa14 | 285 | 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 | 42:7164ccd2aa14 | 286 | switch_function.attach(&SwitchN,1.0); //switch is every second available |
daniQQue | 44:969348be74a5 | 287 | ticker_calibration_biceps.attach (&calibration_biceps,2.0); //to call calibration biceps, stop everything else |
daniQQue | 44:969348be74a5 | 288 | ticker_calibration_triceps.attach(&calibration_triceps,2.0); //to call calibration triceps, stop everything else |
daniQQue | 40:187ef29de53d | 289 | |
daniQQue | 44:969348be74a5 | 290 | if (motorswitch%2==0) { |
daniQQue | 44:969348be74a5 | 291 | pc.printf("If you contract the right arm, the robot will go right \r\n"); |
daniQQue | 42:7164ccd2aa14 | 292 | pc.printf("If you contract biceps of the left arm, the robot will go left \r\n"); |
daniQQue | 42:7164ccd2aa14 | 293 | pc.printf("\r\n"); |
daniQQue | 44:969348be74a5 | 294 | green=0; |
daniQQue | 44:969348be74a5 | 295 | red=1; |
daniQQue | 44:969348be74a5 | 296 | blue=1; |
daniQQue | 42:7164ccd2aa14 | 297 | } |
daniQQue | 42:7164ccd2aa14 | 298 | |
daniQQue | 42:7164ccd2aa14 | 299 | else |
daniQQue | 42:7164ccd2aa14 | 300 | {pc.printf("If you contract the biceps of right arm, the robot will go up \r\n"); |
daniQQue | 42:7164ccd2aa14 | 301 | pc.printf("If you contract the biceps of left arm, the robot will go down \r\n"); |
daniQQue | 42:7164ccd2aa14 | 302 | pc.printf("\r\n"); |
daniQQue | 42:7164ccd2aa14 | 303 | green=1; |
daniQQue | 42:7164ccd2aa14 | 304 | red=0; |
daniQQue | 42:7164ccd2aa14 | 305 | blue=1; |
daniQQue | 42:7164ccd2aa14 | 306 | |
daniQQue | 42:7164ccd2aa14 | 307 | } |
daniQQue | 0:34c739fcc3e0 | 308 | //endless loop |
daniQQue | 0:34c739fcc3e0 | 309 | |
daniQQue | 40:187ef29de53d | 310 | |
daniQQue | 40:187ef29de53d | 311 | while (true) { // zorgt er voor dat de code oneindig doorgelopen wordt |
daniQQue | 40:187ef29de53d | 312 | |
daniQQue | 45:d0e9f586cd03 | 313 | //motor control with muscles. |
daniQQue | 41:9ea3d5921f07 | 314 | if (onoffsignal_biceps==-1) // als s ingedrukt wordt gebeurd het volgende |
daniQQue | 40:187ef29de53d | 315 | { |
daniQQue | 42:7164ccd2aa14 | 316 | if (motorswitch%2==0) // als s ingedrukt wordt en het getal is even gebeurd het onderstaande |
daniQQue | 40:187ef29de53d | 317 | { |
daniQQue | 45:d0e9f586cd03 | 318 | richting_motor1 = ccw; |
daniQQue | 44:969348be74a5 | 319 | pwm_motor1 = 0.5; |
daniQQue | 44:969348be74a5 | 320 | pc.printf("ccw m1\r\n"); |
daniQQue | 42:7164ccd2aa14 | 321 | |
daniQQue | 45:d0e9f586cd03 | 322 | //encoder aan |
daniQQue | 45:d0e9f586cd03 | 323 | counts_encoder1 = Encoder1.getPulses(); |
daniQQue | 45:d0e9f586cd03 | 324 | rev_counts_motor1=counts_encoder1/(gearboxratio*rev_rond); |
daniQQue | 45:d0e9f586cd03 | 325 | counts_encoder2 = Encoder2.getPulses(); |
daniQQue | 45:d0e9f586cd03 | 326 | rev_counts_motor2=counts_encoder2/(gearboxratio*rev_rond); |
daniQQue | 40:187ef29de53d | 327 | |
daniQQue | 40:187ef29de53d | 328 | } |
daniQQue | 40:187ef29de53d | 329 | |
daniQQue | 40:187ef29de53d | 330 | else // als s is ingedrukt maar het getal is niet even (dus oneven) gebeurdt het onderstaande |
daniQQue | 40:187ef29de53d | 331 | { |
daniQQue | 45:d0e9f586cd03 | 332 | richting_motor2 = ccw; |
daniQQue | 40:187ef29de53d | 333 | pwm_motor2 = 1; |
daniQQue | 45:d0e9f586cd03 | 334 | |
daniQQue | 45:d0e9f586cd03 | 335 | //encoder aan |
daniQQue | 45:d0e9f586cd03 | 336 | counts_encoder1 = Encoder1.getPulses(); |
daniQQue | 45:d0e9f586cd03 | 337 | rev_counts_motor1=counts_encoder1/(gearboxratio*rev_rond); |
daniQQue | 45:d0e9f586cd03 | 338 | counts_encoder2 = Encoder2.getPulses(); |
daniQQue | 45:d0e9f586cd03 | 339 | rev_counts_motor2=counts_encoder2/(gearboxratio*rev_rond); |
daniQQue | 42:7164ccd2aa14 | 340 | |
daniQQue | 40:187ef29de53d | 341 | } |
daniQQue | 40:187ef29de53d | 342 | |
daniQQue | 8:cd0cb71b69f2 | 343 | } |
daniQQue | 41:9ea3d5921f07 | 344 | else if (onoffsignal_biceps==1) // als d ingedrukt wordt gebeurd het volgende |
daniQQue | 40:187ef29de53d | 345 | { |
daniQQue | 42:7164ccd2aa14 | 346 | if (motorswitch%2==0) // als d is ingedrukt en n is even dan gebeurd het volgende |
daniQQue | 40:187ef29de53d | 347 | { |
daniQQue | 45:d0e9f586cd03 | 348 | richting_motor1 = cw; |
daniQQue | 44:969348be74a5 | 349 | pwm_motor1 = 0.5; |
daniQQue | 44:969348be74a5 | 350 | pc.printf("cw 1 aan\r\n"); |
daniQQue | 40:187ef29de53d | 351 | |
daniQQue | 45:d0e9f586cd03 | 352 | //encoder aan |
daniQQue | 45:d0e9f586cd03 | 353 | counts_encoder1 = Encoder1.getPulses(); |
daniQQue | 45:d0e9f586cd03 | 354 | rev_counts_motor1=counts_encoder1/(gearboxratio*rev_rond); |
daniQQue | 45:d0e9f586cd03 | 355 | counts_encoder2 = Encoder2.getPulses(); |
daniQQue | 45:d0e9f586cd03 | 356 | rev_counts_motor2=counts_encoder2/(gearboxratio*rev_rond); |
daniQQue | 42:7164ccd2aa14 | 357 | |
daniQQue | 40:187ef29de53d | 358 | } |
daniQQue | 40:187ef29de53d | 359 | else // als d is ingedrukt maar het getal is niet even (dus oneven) gebeurt het onderstaande |
daniQQue | 40:187ef29de53d | 360 | { |
daniQQue | 45:d0e9f586cd03 | 361 | richting_motor2 = cw; |
daniQQue | 40:187ef29de53d | 362 | pwm_motor2 = 1; |
daniQQue | 42:7164ccd2aa14 | 363 | |
daniQQue | 45:d0e9f586cd03 | 364 | //encoder aan |
daniQQue | 45:d0e9f586cd03 | 365 | counts_encoder1 = Encoder1.getPulses(); |
daniQQue | 45:d0e9f586cd03 | 366 | rev_counts_motor1=counts_encoder1/(gearboxratio*rev_rond); |
daniQQue | 45:d0e9f586cd03 | 367 | counts_encoder2 = Encoder2.getPulses(); |
daniQQue | 45:d0e9f586cd03 | 368 | rev_counts_motor2=counts_encoder2/(gearboxratio*rev_rond); |
daniQQue | 40:187ef29de53d | 369 | } |
daniQQue | 40:187ef29de53d | 370 | } |
daniQQue | 40:187ef29de53d | 371 | else{ |
daniQQue | 40:187ef29de53d | 372 | |
daniQQue | 40:187ef29de53d | 373 | pwm_motor2=0; |
daniQQue | 40:187ef29de53d | 374 | pwm_motor1=0; |
daniQQue | 45:d0e9f586cd03 | 375 | //encoder aan |
daniQQue | 45:d0e9f586cd03 | 376 | counts_encoder1 = Encoder1.getPulses(); |
daniQQue | 45:d0e9f586cd03 | 377 | rev_counts_motor1=counts_encoder1/(gearboxratio*rev_rond); |
daniQQue | 45:d0e9f586cd03 | 378 | counts_encoder2 = Encoder2.getPulses(); |
daniQQue | 45:d0e9f586cd03 | 379 | rev_counts_motor2=counts_encoder2/(gearboxratio*rev_rond); |
daniQQue | 40:187ef29de53d | 380 | } |
daniQQue | 45:d0e9f586cd03 | 381 | pc.printf("rev_counts_motor1= %f \r\n",rev_counts_motor1); |
daniQQue | 45:d0e9f586cd03 | 382 | pc.printf("counts_encoder 1= %f \r\n",counts_encoder1); |
daniQQue | 40:187ef29de53d | 383 | } |
daniQQue | 44:969348be74a5 | 384 | } |