change at hidscope

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed

Fork of emg_import by Daniqe Kottelenberg

Committer:
daniQQue
Date:
Tue Nov 01 16:43:18 2016 +0000
Revision:
47:9b1f09b4d15c
Parent:
46:f3c205dfb749
Child:
48:d6894723d9bf
encoder nummers omgewisseld

Who changed what in which revision?

UserRevisionLine numberNew 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 46:f3c205dfb749 60
daniQQue 46:f3c205dfb749 61 //encoders
daniQQue 46:f3c205dfb749 62 double rev_counts_motor1=0;
daniQQue 46:f3c205dfb749 63 double rev_counts_motor2=0;
daniQQue 46:f3c205dfb749 64 double counts_encoder1;
daniQQue 46:f3c205dfb749 65 double counts_encoder2;
daniQQue 45:d0e9f586cd03 66
daniQQue 45:d0e9f586cd03 67 //variables and constants for calibration
daniQQue 45:d0e9f586cd03 68 const float percentage_max_triceps=0.3;
daniQQue 45:d0e9f586cd03 69 const float percentage_max_biceps =0.3;
daniQQue 45:d0e9f586cd03 70 double max_biceps; //calibration maximum biceps
daniQQue 45:d0e9f586cd03 71 double max_triceps; //calibration maximum triceps
daniQQue 45:d0e9f586cd03 72
daniQQue 45:d0e9f586cd03 73 //biceps arm 1, right arm
daniQQue 45:d0e9f586cd03 74 double emg_biceps_right;
daniQQue 45:d0e9f586cd03 75 double emg_filtered_high_biceps_right;
daniQQue 45:d0e9f586cd03 76 double emg_abs_biceps_right;
daniQQue 45:d0e9f586cd03 77 double emg_filtered_biceps_right;
daniQQue 45:d0e9f586cd03 78 double emg_filtered_high_notch_1_biceps_right;
daniQQue 45:d0e9f586cd03 79 //double emg_filtered_high_notch_1_2_biceps_right;
daniQQue 45:d0e9f586cd03 80
daniQQue 45:d0e9f586cd03 81 //triceps arm 1, right arm
daniQQue 45:d0e9f586cd03 82 double emg_triceps_right;
daniQQue 45:d0e9f586cd03 83 double emg_filtered_high_triceps_right;
daniQQue 45:d0e9f586cd03 84 double emg_abs_triceps_right;
daniQQue 45:d0e9f586cd03 85 double emg_filtered_triceps_right;
daniQQue 45:d0e9f586cd03 86 double emg_filtered_high_notch_1_triceps_right;
daniQQue 45:d0e9f586cd03 87
daniQQue 45:d0e9f586cd03 88 //biceps arm 1, left arm
daniQQue 45:d0e9f586cd03 89 double emg_biceps_left;
daniQQue 45:d0e9f586cd03 90 double emg_filtered_high_biceps_left;
daniQQue 45:d0e9f586cd03 91 double emg_abs_biceps_left;
daniQQue 45:d0e9f586cd03 92 double emg_filtered_biceps_left;
daniQQue 45:d0e9f586cd03 93 double emg_filtered_high_notch_1_biceps_left;
daniQQue 45:d0e9f586cd03 94
daniQQue 45:d0e9f586cd03 95 //before abs filtering
daniQQue 45:d0e9f586cd03 96
daniQQue 45:d0e9f586cd03 97 //b1 = biceps right arm
daniQQue 45:d0e9f586cd03 98 BiQuad filterhigh_b1(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01);
daniQQue 45:d0e9f586cd03 99 BiQuad filternotch1_b1 (9.9376e-01 , -1.8902e-00, 9.9376e-01 , -1.8902e-00 , 9.875e-01);
daniQQue 45:d0e9f586cd03 100
daniQQue 45:d0e9f586cd03 101 //t1= triceps right arm
daniQQue 45:d0e9f586cd03 102 BiQuad filterhigh_t1(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01);
daniQQue 45:d0e9f586cd03 103 BiQuad filternotch1_t1 (9.9376e-01 , -1.8902e-00, 9.9376e-01 , -1.8902e-00 , 9.875e-01);
daniQQue 45:d0e9f586cd03 104
daniQQue 45:d0e9f586cd03 105 //b2= biceps left arm
daniQQue 45:d0e9f586cd03 106 BiQuad filterhigh_b2(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01);
daniQQue 45:d0e9f586cd03 107 BiQuad filternotch1_b2 (9.9376e-01 , -1.8902e-00, 9.9376e-01 , -1.8902e-00 , 9.875e-01);
daniQQue 45:d0e9f586cd03 108
daniQQue 45:d0e9f586cd03 109 //after abs filtering
daniQQue 45:d0e9f586cd03 110 BiQuad filterlow_b1 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01);
daniQQue 45:d0e9f586cd03 111 BiQuad filterlow_t1 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01);
daniQQue 45:d0e9f586cd03 112 BiQuad filterlow_b2 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01);
daniQQue 10:7255b59224cc 113
daniQQue 40:187ef29de53d 114 //function teller
daniQQue 40:187ef29de53d 115 void SwitchN() { // maakt simpele functie die 1 bij n optelt
daniQQue 41:9ea3d5921f07 116 if(switch_signal_triceps==1)
daniQQue 40:187ef29de53d 117 {
daniQQue 42:7164ccd2aa14 118 motorswitch++;
daniQQue 42:7164ccd2aa14 119
daniQQue 42:7164ccd2aa14 120 if (motorswitch%2==0)
daniQQue 42:7164ccd2aa14 121 {pc.printf("If you contract the right arm, the robot will go right \r\n");
daniQQue 42:7164ccd2aa14 122 pc.printf("If you contract biceps of the left arm, the robot will go left \r\n");
daniQQue 42:7164ccd2aa14 123 pc.printf("\r\n");
daniQQue 42:7164ccd2aa14 124 green=0;
daniQQue 42:7164ccd2aa14 125 red=1;
daniQQue 42:7164ccd2aa14 126 }
daniQQue 42:7164ccd2aa14 127
daniQQue 42:7164ccd2aa14 128 else
daniQQue 42:7164ccd2aa14 129 {pc.printf("If you contract the biceps of right arm, the robot will go up \r\n");
daniQQue 41:9ea3d5921f07 130 pc.printf("If you contract the biceps of left arm, the robot will go down \r\n");
daniQQue 41:9ea3d5921f07 131 pc.printf("\r\n");
daniQQue 42:7164ccd2aa14 132 green=1;
daniQQue 42:7164ccd2aa14 133 red=0;
daniQQue 42:7164ccd2aa14 134
daniQQue 40:187ef29de53d 135 }
daniQQue 42:7164ccd2aa14 136
daniQQue 40:187ef29de53d 137 }
daniQQue 41:9ea3d5921f07 138 }
daniQQue 40:187ef29de53d 139
daniQQue 40:187ef29de53d 140 //functions which are called in ticker to sample the analog signal
daniQQue 40:187ef29de53d 141
daniQQue 42:7164ccd2aa14 142 //callibration
daniQQue 42:7164ccd2aa14 143 void calibration_biceps(){
daniQQue 44:969348be74a5 144 if (button_calibration_biceps==0){
daniQQue 42:7164ccd2aa14 145 pc.printf("start of calibration biceps, contract maximal \n");
daniQQue 42:7164ccd2aa14 146 red=1;
daniQQue 42:7164ccd2aa14 147 green=1;
daniQQue 42:7164ccd2aa14 148 blue=0;
daniQQue 42:7164ccd2aa14 149
daniQQue 44:969348be74a5 150 for(int n =0; n<1500;n++) //read for 2000 samples as calibration
daniQQue 42:7164ccd2aa14 151 {
daniQQue 42:7164ccd2aa14 152 emg_biceps_right=emg_biceps_right_in.read(); //read the emg value from the elektrodes
daniQQue 42:7164ccd2aa14 153 emg_filtered_high_biceps_right= filterhigh_b1.step(emg_biceps_right);
daniQQue 42:7164ccd2aa14 154 emg_filtered_high_notch_1_biceps_right=filternotch1_b1.step(emg_filtered_high_biceps_right);
daniQQue 42:7164ccd2aa14 155 emg_abs_biceps_right=fabs(emg_filtered_high_notch_1_biceps_right); //fabs because float
daniQQue 42:7164ccd2aa14 156 emg_filtered_biceps_right=filterlow_b1.step(emg_abs_biceps_right);
daniQQue 42:7164ccd2aa14 157
daniQQue 42:7164ccd2aa14 158 if (emg_filtered_biceps_right > max_biceps) //determine what the highest reachable emg signal is
daniQQue 42:7164ccd2aa14 159 {
daniQQue 42:7164ccd2aa14 160 max_biceps = emg_filtered_biceps_right;
daniQQue 42:7164ccd2aa14 161
daniQQue 42:7164ccd2aa14 162 }
daniQQue 42:7164ccd2aa14 163 wait(0.001f); //to sample at same freq; 1000Hz
daniQQue 42:7164ccd2aa14 164 }
daniQQue 42:7164ccd2aa14 165 cut_off_value_biceps_right=percentage_max_biceps*max_biceps;
daniQQue 42:7164ccd2aa14 166 cut_off_value_biceps_left=-cut_off_value_biceps_right;
daniQQue 42:7164ccd2aa14 167 //toggle lights
daniQQue 42:7164ccd2aa14 168 blue=!blue;
daniQQue 42:7164ccd2aa14 169
daniQQue 42:7164ccd2aa14 170 pc.printf(" end of calibration\r\n",cut_off_value_biceps_right );
daniQQue 42:7164ccd2aa14 171 pc.printf(" change of cv biceps: %f ",cut_off_value_biceps_right );
daniQQue 42:7164ccd2aa14 172
daniQQue 44:969348be74a5 173 wait(0.2f);
daniQQue 42:7164ccd2aa14 174
daniQQue 42:7164ccd2aa14 175 if (motorswitch%2==0)
daniQQue 42:7164ccd2aa14 176 {green=0;
daniQQue 42:7164ccd2aa14 177 red=1;}
daniQQue 42:7164ccd2aa14 178
daniQQue 42:7164ccd2aa14 179 else {green=1;
daniQQue 42:7164ccd2aa14 180 red=0;}
daniQQue 44:969348be74a5 181 }
daniQQue 42:7164ccd2aa14 182 }
daniQQue 42:7164ccd2aa14 183
daniQQue 42:7164ccd2aa14 184 void calibration_triceps(){
daniQQue 44:969348be74a5 185 if(button_calibration_triceps==0){
daniQQue 42:7164ccd2aa14 186 red=1;
daniQQue 42:7164ccd2aa14 187 green=1;
daniQQue 42:7164ccd2aa14 188 blue=0;
daniQQue 42:7164ccd2aa14 189
daniQQue 42:7164ccd2aa14 190 pc.printf("start of calibration triceps\r\n");
daniQQue 42:7164ccd2aa14 191
daniQQue 44:969348be74a5 192 for(int n =0; n<1500;n++) //read for 2000 samples as calibration
daniQQue 42:7164ccd2aa14 193 {
daniQQue 42:7164ccd2aa14 194 emg_triceps_right=emg_triceps_right_in.read(); //read the emg value from the elektrodes
daniQQue 42:7164ccd2aa14 195 emg_filtered_high_triceps_right= filterhigh_t1.step(emg_triceps_right);
daniQQue 42:7164ccd2aa14 196 emg_filtered_high_notch_1_triceps_right=filternotch1_t1.step(emg_filtered_high_triceps_right);
daniQQue 42:7164ccd2aa14 197 emg_abs_triceps_right=fabs(emg_filtered_high_notch_1_triceps_right); //fabs because float
daniQQue 42:7164ccd2aa14 198 emg_filtered_triceps_right=filterlow_t1.step(emg_abs_triceps_right);
daniQQue 42:7164ccd2aa14 199
daniQQue 42:7164ccd2aa14 200 if (emg_filtered_triceps_right > max_triceps) //determine what the highest reachable emg signal is
daniQQue 42:7164ccd2aa14 201 {
daniQQue 42:7164ccd2aa14 202 max_triceps = emg_filtered_triceps_right;
daniQQue 42:7164ccd2aa14 203
daniQQue 42:7164ccd2aa14 204 }
daniQQue 42:7164ccd2aa14 205 wait(0.001f); //to sample at same freq; 1000Hz
daniQQue 42:7164ccd2aa14 206 }
daniQQue 44:969348be74a5 207 cut_off_value_triceps=-percentage_max_triceps*max_triceps;
daniQQue 42:7164ccd2aa14 208 pc.printf(" end of calibration\r\n");
daniQQue 42:7164ccd2aa14 209 pc.printf(" change of cv triceps: %f ",cut_off_value_triceps );
daniQQue 42:7164ccd2aa14 210 blue=!blue;
daniQQue 44:969348be74a5 211 wait(0.2f);
daniQQue 42:7164ccd2aa14 212 if (motorswitch%2==0)
daniQQue 42:7164ccd2aa14 213 {green=0;
daniQQue 42:7164ccd2aa14 214 red=1;}
daniQQue 42:7164ccd2aa14 215
daniQQue 42:7164ccd2aa14 216 else {green=1;
daniQQue 42:7164ccd2aa14 217 red=0;}
daniQQue 44:969348be74a5 218 }
daniQQue 42:7164ccd2aa14 219 }
daniQQue 42:7164ccd2aa14 220
daniQQue 0:34c739fcc3e0 221 void filter(){
daniQQue 40:187ef29de53d 222 //biceps right arm read+filtering
daniQQue 15:bb4a6c7836d8 223 emg_biceps_right=emg_biceps_right_in.read(); //read the emg value from the elektrodes
daniQQue 40:187ef29de53d 224 emg_filtered_high_biceps_right= filterhigh_b1.step(emg_biceps_right);
daniQQue 40:187ef29de53d 225 emg_filtered_high_notch_1_biceps_right=filternotch1_b1.step(emg_filtered_high_biceps_right);
daniQQue 44:969348be74a5 226 emg_abs_biceps_right=fabs(emg_filtered_high_notch_1_biceps_right); //fabs because float
daniQQue 40:187ef29de53d 227 emg_filtered_biceps_right=filterlow_b1.step(emg_abs_biceps_right);
daniQQue 40:187ef29de53d 228
daniQQue 40:187ef29de53d 229 //triceps right arm read+filtering
daniQQue 40:187ef29de53d 230 emg_triceps_right=emg_triceps_right_in.read(); //read the emg value from the elektrodes
daniQQue 40:187ef29de53d 231 emg_filtered_high_triceps_right= filterhigh_t1.step(emg_triceps_right);
daniQQue 40:187ef29de53d 232 emg_filtered_high_notch_1_triceps_right=filternotch1_t1.step(emg_filtered_high_triceps_right);
daniQQue 40:187ef29de53d 233 emg_abs_triceps_right=fabs(emg_filtered_high_notch_1_triceps_right); //fabs because float
daniQQue 40:187ef29de53d 234 emg_filtered_triceps_right=filterlow_t1.step(emg_abs_triceps_right);
daniQQue 7:42d0e38196f1 235
daniQQue 40:187ef29de53d 236 //biceps left arm read+filtering
daniQQue 40:187ef29de53d 237 emg_biceps_left=emg_biceps_left_in.read(); //read the emg value from the elektrodes
daniQQue 40:187ef29de53d 238 emg_filtered_high_biceps_left= filterhigh_b2.step(emg_biceps_left);
daniQQue 40:187ef29de53d 239 emg_filtered_high_notch_1_biceps_left=filternotch1_b2.step(emg_filtered_high_biceps_left);
daniQQue 40:187ef29de53d 240 emg_abs_biceps_left=fabs(emg_filtered_high_notch_1_biceps_left); //fabs because float
daniQQue 40:187ef29de53d 241 emg_filtered_biceps_left=filterlow_b2.step(emg_abs_biceps_left);
daniQQue 40:187ef29de53d 242
daniQQue 41:9ea3d5921f07 243 //signal substraction of filter biceps and triceps. right Biceps + left biceps -
daniQQue 41:9ea3d5921f07 244 signal_biceps_sum=emg_filtered_biceps_right-emg_filtered_biceps_left;
daniQQue 44:969348be74a5 245 bicepstriceps_rightarm=emg_filtered_biceps_right-emg_filtered_triceps_right;
daniQQue 40:187ef29de53d 246 //creating of on/off signal with the created on/off signals, with if statement for right arm!
daniQQue 41:9ea3d5921f07 247 if (signal_biceps_sum>cut_off_value_biceps_right)
daniQQue 41:9ea3d5921f07 248 {onoffsignal_biceps=1;}
daniQQue 7:42d0e38196f1 249
daniQQue 41:9ea3d5921f07 250 else if (signal_biceps_sum<cut_off_value_biceps_left)
daniQQue 40:187ef29de53d 251 {
daniQQue 41:9ea3d5921f07 252 onoffsignal_biceps=-1;
daniQQue 40:187ef29de53d 253 }
daniQQue 40:187ef29de53d 254
daniQQue 40:187ef29de53d 255 else
daniQQue 41:9ea3d5921f07 256 {onoffsignal_biceps=0;}
daniQQue 7:42d0e38196f1 257
daniQQue 40:187ef29de53d 258 //creating on/off signal for switch (left arm)
daniQQue 40:187ef29de53d 259
daniQQue 44:969348be74a5 260 if (bicepstriceps_rightarm<cut_off_value_triceps)
daniQQue 40:187ef29de53d 261 {
daniQQue 41:9ea3d5921f07 262 switch_signal_triceps=1;
daniQQue 40:187ef29de53d 263 }
daniQQue 40:187ef29de53d 264
daniQQue 40:187ef29de53d 265 else
daniQQue 40:187ef29de53d 266 {
daniQQue 41:9ea3d5921f07 267 switch_signal_triceps=0;
daniQQue 40:187ef29de53d 268 }
daniQQue 40:187ef29de53d 269
daniQQue 0:34c739fcc3e0 270 //send signals to scope
daniQQue 40:187ef29de53d 271 scope.set(0, emg_filtered_biceps_right); //set emg signal to scope in channel 0
daniQQue 40:187ef29de53d 272 scope.set(1, emg_filtered_triceps_right); // set emg signal to scope in channel 1
daniQQue 40:187ef29de53d 273 scope.set(2, emg_filtered_biceps_left); // set emg signal to scope in channel 2
daniQQue 44:969348be74a5 274 scope.set(3, onoffsignal_biceps);
daniQQue 40:187ef29de53d 275
daniQQue 40:187ef29de53d 276 scope.send(); //send all the signals to the scope
daniQQue 0:34c739fcc3e0 277 }
daniQQue 0:34c739fcc3e0 278
daniQQue 0:34c739fcc3e0 279 //program
daniQQue 0:34c739fcc3e0 280
daniQQue 0:34c739fcc3e0 281 int main()
daniQQue 0:34c739fcc3e0 282 {
daniQQue 42:7164ccd2aa14 283 pc.baud(115200); //connect with pc with baudrate 115200
daniQQue 47:9b1f09b4d15c 284 QEI Encoder1(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 47:9b1f09b4d15c 285 QEI Encoder2(D10,D11, NC, rev_rond,QEI::X4_ENCODING);
daniQQue 42:7164ccd2aa14 286
daniQQue 42:7164ccd2aa14 287 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 288 switch_function.attach(&SwitchN,1.0); //switch is every second available
daniQQue 44:969348be74a5 289 ticker_calibration_biceps.attach (&calibration_biceps,2.0); //to call calibration biceps, stop everything else
daniQQue 44:969348be74a5 290 ticker_calibration_triceps.attach(&calibration_triceps,2.0); //to call calibration triceps, stop everything else
daniQQue 40:187ef29de53d 291
daniQQue 44:969348be74a5 292 if (motorswitch%2==0) {
daniQQue 44:969348be74a5 293 pc.printf("If you contract the right arm, the robot will go right \r\n");
daniQQue 42:7164ccd2aa14 294 pc.printf("If you contract biceps of the left arm, the robot will go left \r\n");
daniQQue 42:7164ccd2aa14 295 pc.printf("\r\n");
daniQQue 44:969348be74a5 296 green=0;
daniQQue 44:969348be74a5 297 red=1;
daniQQue 44:969348be74a5 298 blue=1;
daniQQue 42:7164ccd2aa14 299 }
daniQQue 42:7164ccd2aa14 300
daniQQue 42:7164ccd2aa14 301 else
daniQQue 42:7164ccd2aa14 302 {pc.printf("If you contract the biceps of right arm, the robot will go up \r\n");
daniQQue 42:7164ccd2aa14 303 pc.printf("If you contract the biceps of left arm, the robot will go down \r\n");
daniQQue 42:7164ccd2aa14 304 pc.printf("\r\n");
daniQQue 42:7164ccd2aa14 305 green=1;
daniQQue 42:7164ccd2aa14 306 red=0;
daniQQue 42:7164ccd2aa14 307 blue=1;
daniQQue 42:7164ccd2aa14 308
daniQQue 42:7164ccd2aa14 309 }
daniQQue 0:34c739fcc3e0 310 //endless loop
daniQQue 0:34c739fcc3e0 311
daniQQue 40:187ef29de53d 312
daniQQue 40:187ef29de53d 313 while (true) { // zorgt er voor dat de code oneindig doorgelopen wordt
daniQQue 40:187ef29de53d 314
daniQQue 46:f3c205dfb749 315 //encoder aan
daniQQue 46:f3c205dfb749 316 counts_encoder1 = Encoder1.getPulses();
daniQQue 46:f3c205dfb749 317 rev_counts_motor1=counts_encoder1/(gearboxratio*rev_rond);
daniQQue 46:f3c205dfb749 318 counts_encoder2 = Encoder2.getPulses();
daniQQue 46:f3c205dfb749 319 rev_counts_motor2=counts_encoder2/(gearboxratio*rev_rond);
daniQQue 46:f3c205dfb749 320
daniQQue 45:d0e9f586cd03 321 //motor control with muscles.
daniQQue 41:9ea3d5921f07 322 if (onoffsignal_biceps==-1) // als s ingedrukt wordt gebeurd het volgende
daniQQue 40:187ef29de53d 323 {
daniQQue 42:7164ccd2aa14 324 if (motorswitch%2==0) // als s ingedrukt wordt en het getal is even gebeurd het onderstaande
daniQQue 40:187ef29de53d 325 {
daniQQue 45:d0e9f586cd03 326 richting_motor1 = ccw;
daniQQue 44:969348be74a5 327 pwm_motor1 = 0.5;
daniQQue 44:969348be74a5 328 pc.printf("ccw m1\r\n");
daniQQue 42:7164ccd2aa14 329
daniQQue 40:187ef29de53d 330 }
daniQQue 40:187ef29de53d 331
daniQQue 40:187ef29de53d 332 else // als s is ingedrukt maar het getal is niet even (dus oneven) gebeurdt het onderstaande
daniQQue 40:187ef29de53d 333 {
daniQQue 45:d0e9f586cd03 334 richting_motor2 = ccw;
daniQQue 40:187ef29de53d 335 pwm_motor2 = 1;
daniQQue 42:7164ccd2aa14 336
daniQQue 40:187ef29de53d 337 }
daniQQue 40:187ef29de53d 338
daniQQue 8:cd0cb71b69f2 339 }
daniQQue 41:9ea3d5921f07 340 else if (onoffsignal_biceps==1) // als d ingedrukt wordt gebeurd het volgende
daniQQue 40:187ef29de53d 341 {
daniQQue 42:7164ccd2aa14 342 if (motorswitch%2==0) // als d is ingedrukt en n is even dan gebeurd het volgende
daniQQue 40:187ef29de53d 343 {
daniQQue 45:d0e9f586cd03 344 richting_motor1 = cw;
daniQQue 44:969348be74a5 345 pwm_motor1 = 0.5;
daniQQue 44:969348be74a5 346 pc.printf("cw 1 aan\r\n");
daniQQue 40:187ef29de53d 347
daniQQue 46:f3c205dfb749 348
daniQQue 42:7164ccd2aa14 349
daniQQue 40:187ef29de53d 350 }
daniQQue 40:187ef29de53d 351 else // als d is ingedrukt maar het getal is niet even (dus oneven) gebeurt het onderstaande
daniQQue 40:187ef29de53d 352 {
daniQQue 45:d0e9f586cd03 353 richting_motor2 = cw;
daniQQue 40:187ef29de53d 354 pwm_motor2 = 1;
daniQQue 46:f3c205dfb749 355
daniQQue 40:187ef29de53d 356 }
daniQQue 40:187ef29de53d 357 }
daniQQue 40:187ef29de53d 358 else{
daniQQue 40:187ef29de53d 359
daniQQue 40:187ef29de53d 360 pwm_motor2=0;
daniQQue 40:187ef29de53d 361 pwm_motor1=0;
daniQQue 46:f3c205dfb749 362
daniQQue 40:187ef29de53d 363 }
daniQQue 45:d0e9f586cd03 364 pc.printf("rev_counts_motor1= %f \r\n",rev_counts_motor1);
daniQQue 45:d0e9f586cd03 365 pc.printf("counts_encoder 1= %f \r\n",counts_encoder1);
daniQQue 40:187ef29de53d 366 }
daniQQue 44:969348be74a5 367 }