Motor_Control_EMG

Dependencies:   HIDScope MODSERIAL QEI Servo biquadFilter mbed

Fork of Motor_Control_buttons by Janet Huisman

Committer:
huismaja
Date:
Fri Oct 28 14:18:04 2016 +0000
Revision:
14:63f2a5165ffd
Parent:
13:746240466172
Child:
15:c43f0dfe7cdf
working for 1 emg signal, but sometimes it counts multiple actions in stead of 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
huismaja 13:746240466172 1 #include "mbed.h" //Include the mbed library
huismaja 13:746240466172 2 #include "MODSERIAL.h" //Include the MODSERIAL library for communication with the pc
huismaja 13:746240466172 3 #include "Servo.h" //Include the Servo library for controlling the gripper
huismaja 13:746240466172 4 #include "QEI.h" //Include the QEI library for reading the encoder data of the DC-motors
huismaja 13:746240466172 5 //#include "HIDScope.h" //Include the HIDScope library for plotting the emg data
huismaja 13:746240466172 6 #include "BiQuad.h" //Include the BiQuad library for filtering the emg signal
huismaja 13:746240466172 7
huismaja 14:63f2a5165ffd 8 MODSERIAL pc(USBTX, USBRX); //Make a connection with the PC
huismaja 14:63f2a5165ffd 9 //HIDScope scope(2); //Create a 4-channel HIDScope object
huismaja 13:746240466172 10
huismaja 13:746240466172 11 const double pi = 3.1415926535897; //Declare the value of pi
huismaja 13:746240466172 12
huismaja 13:746240466172 13 double speed_rotation=pi/5; //Set the rotation speed in rad/sec -> NOTE: this has to be below 8.4 rad/sec
huismaja 13:746240466172 14 double speed_translation=pi/5; //Set the translation speed in rad/sec -> NOTE: this has to be below 8.4 rad/sec
huismaja 13:746240466172 15 double speedM1=speed_rotation/8.4; //Map the rotation speed from (0-8.4) to (0-1) by dividing by 8.4
huismaja 13:746240466172 16 double speedM2=speed_translation/8.4; //Map the translation speed from (0-8.4) to (0-1) by dividing by 8.4
huismaja 0:6c8444d06e97 17
huismaja 10:cf579c3eaf01 18 QEI encoder_M1 (D9, D10, NC, 8400); //Define an encoder for motor 1 called encoder_M1
huismaja 10:cf579c3eaf01 19 QEI encoder_M2 (D11, D12, NC, 8400); //Define an encoder for motor 2 called encoder_M2
huismaja 9:cca4d4084775 20
huismaja 10:cf579c3eaf01 21 Ticker encoder_M1_ticker; //Create a ticker for reading the encoder data of encoder_M1
huismaja 10:cf579c3eaf01 22 Ticker encoder_M2_ticker; //Create a ticker for reading the encoder data of encoder_M2
huismaja 6:98121d2d76a6 23
huismaja 12:35a81d6c6505 24 DigitalOut Direction_M2(D4); //To control the rotation direction of the arm
huismaja 12:35a81d6c6505 25 PwmOut Speed_M2(D5); //To control the rotation speed of the arm
huismaja 12:35a81d6c6505 26 PwmOut Speed_M1(D6); //To control the translation direction of the arm
huismaja 12:35a81d6c6505 27 DigitalOut Direction_M1(D7); //To control the translation speed of the arm
huismaja 10:cf579c3eaf01 28 Servo gripper_servo(D13); //To control the gripper
huismaja 0:6c8444d06e97 29
huismaja 13:746240466172 30 InterruptIn Switch_1(SW3); //Switch 1 to control the rotation to the left
huismaja 13:746240466172 31 InterruptIn Switch_2(SW2); //Switch 2 to control the rotation to the right
huismaja 13:746240466172 32 InterruptIn Switch_3(D2); //Switch 3 to control the translation of the arm
huismaja 13:746240466172 33 InterruptIn Switch_4(D3); //Switch 4 to control the gripper
huismaja 13:746240466172 34
huismaja 13:746240466172 35 AnalogIn emg_1(A0); //Analog of EMG 1
huismaja 14:63f2a5165ffd 36 //AnalogIn emg_2(A1); //Analog of EMG 2
huismaja 14:63f2a5165ffd 37 //AnalogIn emg_3(A2); //Analog of EMG 3
huismaja 14:63f2a5165ffd 38 //AnalogIn emg_4(A3); //Analog of EMG 4
huismaja 13:746240466172 39
huismaja 13:746240466172 40 double emg_1_value = 0; //Initially the emg_1 value is zero
huismaja 13:746240466172 41 double emg_2_value = 0; //Initially the emg_2 value is zero
huismaja 13:746240466172 42 double emg_3_value = 0; //Initially the emg_3 value is zero
huismaja 13:746240466172 43 double emg_4_value = 0; //Initially the emg_4 value is zero
huismaja 13:746240466172 44
huismaja 14:63f2a5165ffd 45 double signalpart1=0;
huismaja 14:63f2a5165ffd 46 double signalpart2=0;
huismaja 14:63f2a5165ffd 47 double signalpart3=0;
huismaja 14:63f2a5165ffd 48 double signalpart4=0;
huismaja 13:746240466172 49 double emg_1_filtered = 0; //Initially the emg_1_filtered signal is zero
huismaja 13:746240466172 50 double emg_2_filtered = 0; //Initially the emg_2_filtered signal is zero
huismaja 13:746240466172 51 double emg_3_filtered = 0; //Initially the emg_3_filtered signal is zero
huismaja 13:746240466172 52 double emg_4_filtered = 0; //Initially the emg_4_filtered signal is zero
huismaja 14:63f2a5165ffd 53 double maximum_calibration_value_1=0;
huismaja 14:63f2a5165ffd 54 double maximum_calibration_value_2=0;
huismaja 14:63f2a5165ffd 55 bool calibration_done=0;
huismaja 14:63f2a5165ffd 56
huismaja 14:63f2a5165ffd 57 volatile double emg_1_threshold = 0.2; //Set the threshold for emg 1
huismaja 14:63f2a5165ffd 58 volatile double emg_2_threshold = 0.2; //Set the threshold for emg 2
huismaja 14:63f2a5165ffd 59 volatile double emg_3_threshold = 0.2; //Set the threshold for emg 3
huismaja 14:63f2a5165ffd 60 volatile double emg_4_threshold = 0.2; //Set the threshold for emg 4
huismaja 13:746240466172 61
huismaja 13:746240466172 62 Ticker filter_EMG_ticker; //Create a ticker for the filtering of all emg signals
huismaja 14:63f2a5165ffd 63 Ticker calibration_ticker;
huismaja 13:746240466172 64 Ticker check_threshold_crossing_ticker; //Create a ticker for checking if the threshold is crossed
huismaja 13:746240466172 65 Ticker check_goflags_ticker; //Create a ticker for checking if the go-flags are set true
huismaja 13:746240466172 66
huismaja 14:63f2a5165ffd 67 BiQuad filterhigh1(0.9565, -1.9131, 0.9565, -1.9112, 0.9150);
huismaja 14:63f2a5165ffd 68 BiQuad notch_low1(1.0000, -1.9023, 1.0000, -1.8795, 0.9819);
huismaja 14:63f2a5165ffd 69 BiQuad notch_high1(1.0000, -1.9023, 1.0000, -1.8913, 0.9829);
huismaja 14:63f2a5165ffd 70 BiQuad filterlow1(0.00003913, 0.00007826, 0.00003913, -1.9822, 0.9824);
huismaja 14:63f2a5165ffd 71
huismaja 14:63f2a5165ffd 72 //BiQuad filterhigh2(0.9565, -1.9131, 0.9565, -1.9112, 0.9150);
huismaja 14:63f2a5165ffd 73 //BiQuad notch_low2(1.0000, -1.9023, 1.0000, -1.8795, 0.9819);
huismaja 14:63f2a5165ffd 74 //BiQuad notch_high2(1.0000, -1.9023, 1.0000, -1.8913, 0.9829);
huismaja 14:63f2a5165ffd 75 //BiQuad filterlow2(0.00003913, 0.00007826, 0.00003913, -1.9822, 0.9824);
huismaja 2:b20570f160c6 76
huismaja 5:9b5edadc023b 77 int counter_rotation_left=0; //To count the number of times the rotation_left switch (switch_1) has been pushed
huismaja 5:9b5edadc023b 78 int counter_rotation_right=0; //To count the number of times the rotation_right switch (switch_2) has been pushed
huismaja 5:9b5edadc023b 79 int counter_translation=0; //To count the number of times the translation switch (switch_3) has been pushed
huismaja 5:9b5edadc023b 80 int counter_gripper=0; //To count the number of times the gripper switch (switch_4) has been pushed
huismaja 5:9b5edadc023b 81
huismaja 13:746240466172 82 bool emg_1_activated = false; //Initially the emg_1 has not crossed the threshold
huismaja 13:746240466172 83 bool emg_2_activated = false; //Initially the emg_2 has not crossed the threshold
huismaja 13:746240466172 84 bool emg_3_activated = false; //Initially the emg_3 has not crossed the threshold
huismaja 13:746240466172 85 bool emg_4_activated = false; //Initially the emg_4 has not crossed the threshold
huismaja 13:746240466172 86
huismaja 11:b1ad5267a6bd 87 volatile bool rotation_left_go = false; //Create a go-flag for the rotation_left and set it to false
huismaja 11:b1ad5267a6bd 88 volatile bool rotation_right_go = false; //Create a go-flag for the rotation_right and set it to false
huismaja 11:b1ad5267a6bd 89 volatile bool translation_go = false; //Create a go-flag for the translation and set it to false
huismaja 11:b1ad5267a6bd 90 volatile bool gripper_go = false; //Create a go-flag for the gripper and set it to false
huismaja 0:6c8444d06e97 91
huismaja 13:746240466172 92 float angle_M1=0; //The measured angle of motor 1 is initially zero
huismaja 13:746240466172 93 float angle_M2=0; //The measured angle of motor 2 is initially zero
huismaja 9:cca4d4084775 94
huismaja 13:746240466172 95 void read_position_M1 (){ //Function to read the position of motor 1
huismaja 13:746240466172 96 int pulses_M1 = -encoder_M1.getPulses(); //Read the encoder data and store it in pulses_M1
huismaja 13:746240466172 97 angle_M1 = float(pulses_M1)/4200*2.0*pi; //Calculate the angle that corresponds with the measured encoder pulses
huismaja 13:746240466172 98 // pc.printf("%i \t%f \t", pulses_M1, angle_M1);
huismaja 13:746240466172 99 }
huismaja 13:746240466172 100
huismaja 13:746240466172 101 void read_position_M2 (){ //Function to read the position of motor 2
huismaja 12:35a81d6c6505 102 int pulses_M2 = -encoder_M2.getPulses(); //Read the encoder data and store it in pulses_M2
huismaja 12:35a81d6c6505 103 angle_M2 = float(pulses_M2)/4200*2.0*pi; //Calculate the angle that corresponds with the measured encoder pulses
huismaja 13:746240466172 104 // pc.printf("%i \t%f \n", pulses_M2, angle_M2);
huismaja 9:cca4d4084775 105 }
huismaja 9:cca4d4084775 106
huismaja 11:b1ad5267a6bd 107 void activate_rotation_left (){ //To activate the rotation_left
huismaja 11:b1ad5267a6bd 108 counter_rotation_left++; //Increase the counter_rotation_left that counts the number of time switch 1 has been pressed
huismaja 11:b1ad5267a6bd 109 if (counter_rotation_left > 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1 etc.
huismaja 11:b1ad5267a6bd 110 counter_rotation_left=1;
huismaja 11:b1ad5267a6bd 111 }
huismaja 11:b1ad5267a6bd 112 rotation_left_go = true; //After increasing the counter, set the rotation_left go-flag to true
huismaja 11:b1ad5267a6bd 113 }
huismaja 11:b1ad5267a6bd 114
huismaja 10:cf579c3eaf01 115 void rotation_left (){ //Function to control the rotation to the left
huismaja 10:cf579c3eaf01 116 switch (counter_rotation_left){ //Create a switch statement
huismaja 10:cf579c3eaf01 117 case 1: //For activating the rotation to the left
huismaja 13:746240466172 118 Direction_M1 = 1; //The arm will rotate to the left
huismaja 10:cf579c3eaf01 119 Speed_M1 = speedM1; //The motor is turned on at speed_rotation rad/sec
huismaja 8:9c58ca13076e 120 pc.printf("The arm will now rotate to the left with %f rad/sec \n", speedM1);
huismaja 12:35a81d6c6505 121 wait(0.1f);
huismaja 3:0a4bfcb3f339 122 break;
huismaja 10:cf579c3eaf01 123 case 2: //For stopping the rotation to the left
huismaja 13:746240466172 124 Direction_M1 = 1; //The arm will rotate to the left
huismaja 10:cf579c3eaf01 125 Speed_M1 = 0; //The motor is turned off
huismaja 5:9b5edadc023b 126 pc.printf("The arm will now stop rotating to the left \n");
huismaja 12:35a81d6c6505 127 wait(0.1f);
huismaja 3:0a4bfcb3f339 128 break;
huismaja 3:0a4bfcb3f339 129 }
huismaja 11:b1ad5267a6bd 130 }
huismaja 3:0a4bfcb3f339 131
huismaja 11:b1ad5267a6bd 132 void activate_rotation_right (){ //To activate the rotation_right
huismaja 11:b1ad5267a6bd 133 counter_rotation_right++; //Increase the counter_rotation_right that counts the number of time switch 2 has been pressed
huismaja 11:b1ad5267a6bd 134 if (counter_rotation_right> 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1
huismaja 11:b1ad5267a6bd 135 counter_rotation_right=1;
huismaja 3:0a4bfcb3f339 136 }
huismaja 11:b1ad5267a6bd 137 rotation_right_go = true; //After increasing the counter, set the rotation_right go-flag to true
huismaja 3:0a4bfcb3f339 138 }
huismaja 3:0a4bfcb3f339 139
huismaja 10:cf579c3eaf01 140 void rotation_right (){ //Function to control the rotation to the left
huismaja 10:cf579c3eaf01 141 switch (counter_rotation_right){ //Create a switch statement
huismaja 10:cf579c3eaf01 142 case 1: //For activation the rotation to the right
huismaja 13:746240466172 143 Direction_M1 = 0; //The arm will rotate to the right
huismaja 8:9c58ca13076e 144 Speed_M1 = speedM1; //The motor is turned on at speed_rotation rad/sec
huismaja 8:9c58ca13076e 145 pc.printf("The arm will now rotate to the right with %f rad/sec \n", speedM1);
huismaja 12:35a81d6c6505 146 wait(0.1f);
huismaja 3:0a4bfcb3f339 147 break;
huismaja 5:9b5edadc023b 148 case 2: //For stopping the rotation to the right
huismaja 13:746240466172 149 Direction_M1 = 0; //The arm will rotate to the right
huismaja 5:9b5edadc023b 150 Speed_M1 = 0; //The motor is turned off
huismaja 5:9b5edadc023b 151 pc.printf("The arm will now stop rotating to the right \n");
huismaja 12:35a81d6c6505 152 wait(0.1f);
huismaja 3:0a4bfcb3f339 153 break;
huismaja 3:0a4bfcb3f339 154 }
huismaja 3:0a4bfcb3f339 155 }
huismaja 3:0a4bfcb3f339 156
huismaja 11:b1ad5267a6bd 157 void activate_translation (){ //To activate the translation
huismaja 11:b1ad5267a6bd 158 counter_translation++; //Increase the counter_translation that counts the number of time switch 3 has been pressed
huismaja 11:b1ad5267a6bd 159 if (counter_translation > 4){ //Because there are 4 cases in the switch statement, case 5 = case 1
huismaja 11:b1ad5267a6bd 160 counter_translation=1;
huismaja 3:0a4bfcb3f339 161 }
huismaja 11:b1ad5267a6bd 162 translation_go = true; //After increasing the counter, set the translation go-flag to true
huismaja 3:0a4bfcb3f339 163 }
huismaja 3:0a4bfcb3f339 164
huismaja 10:cf579c3eaf01 165 void translation (){ //Function to control the translation
huismaja 10:cf579c3eaf01 166 switch (counter_translation){ //Create a switch statement
huismaja 8:9c58ca13076e 167 case 1: //For activating the elongation of the arm
huismaja 8:9c58ca13076e 168 Direction_M2 = 1; //The arm will get longer
huismaja 10:cf579c3eaf01 169 Speed_M2 = speedM2; //The motor is turned on at speed_translation rad/sec
huismaja 5:9b5edadc023b 170 pc.printf("The arm will now get longer \n");
huismaja 12:35a81d6c6505 171 wait(0.1f);
huismaja 5:9b5edadc023b 172 break;
huismaja 8:9c58ca13076e 173 case 2: //For stopping the elongation of the arm
huismaja 8:9c58ca13076e 174 Direction_M2 = 1; //The arm will get longer
huismaja 8:9c58ca13076e 175 Speed_M2 = 0; //The motor is turned off
huismaja 5:9b5edadc023b 176 pc.printf("The arm will now stop getting longer \n");
huismaja 12:35a81d6c6505 177 wait(0.1f);
huismaja 5:9b5edadc023b 178 break;
huismaja 8:9c58ca13076e 179 case 3: //For activating the shortening of the arm
huismaja 8:9c58ca13076e 180 Direction_M2 = 0; //The arm will get shorter
huismaja 10:cf579c3eaf01 181 Speed_M2 = speedM2; //The motor is turned on at speed_translation rad/sec
huismaja 5:9b5edadc023b 182 pc.printf("The arm will now get shorter \n");
huismaja 12:35a81d6c6505 183 wait(0.1f);
huismaja 5:9b5edadc023b 184 break;
huismaja 8:9c58ca13076e 185 case 4: //For stopping the shortening of the arm
huismaja 8:9c58ca13076e 186 Direction_M2 = 0; //The arm will get shorter
huismaja 8:9c58ca13076e 187 Speed_M2 = 0; //The motor is turned off
huismaja 5:9b5edadc023b 188 pc.printf("The arm will now stop getting shorter \n");
huismaja 12:35a81d6c6505 189 wait(0.1f);
huismaja 5:9b5edadc023b 190 break;
huismaja 5:9b5edadc023b 191 }
huismaja 11:b1ad5267a6bd 192 }
huismaja 5:9b5edadc023b 193
huismaja 11:b1ad5267a6bd 194 void activate_gripper (){ //To activate the gripper
huismaja 11:b1ad5267a6bd 195 counter_gripper++; //Increase the couter_gripper that counts the number of time switch 4 has been pressed
huismaja 11:b1ad5267a6bd 196 if (counter_gripper> 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1
huismaja 11:b1ad5267a6bd 197 counter_gripper=1;
huismaja 5:9b5edadc023b 198 }
huismaja 11:b1ad5267a6bd 199 gripper_go = true; //After increasing the counter, set the gripper go-flag to true
huismaja 5:9b5edadc023b 200 }
huismaja 5:9b5edadc023b 201
huismaja 10:cf579c3eaf01 202 void gripper (){ //Function to control the gripper
huismaja 10:cf579c3eaf01 203 switch (counter_gripper){ //Create a switch statement
huismaja 10:cf579c3eaf01 204 case 1: //For closing the gripper
huismaja 10:cf579c3eaf01 205 gripper_servo = 0; //The gripper is now closed
huismaja 5:9b5edadc023b 206 pc.printf("The gripper will now close \n");
huismaja 12:35a81d6c6505 207 wait(0.1f);
huismaja 4:84bd5ead83f9 208 break;
huismaja 10:cf579c3eaf01 209 case 2: //For opening the gripper
huismaja 13:746240466172 210 gripper_servo = 0.3; //The gripper is now open
huismaja 5:9b5edadc023b 211 pc.printf("The gripper will now open \n");
huismaja 12:35a81d6c6505 212 wait(0.1f);
huismaja 4:84bd5ead83f9 213 break;
huismaja 4:84bd5ead83f9 214 }
huismaja 4:84bd5ead83f9 215 }
huismaja 14:63f2a5165ffd 216 void calibration(){
huismaja 14:63f2a5165ffd 217 if(Switch_1.read()== false) {
huismaja 14:63f2a5165ffd 218 for(int n=0; n<5000; n++){
huismaja 14:63f2a5165ffd 219 signalpart1 = filterhigh1.step(emg_1.read());
huismaja 14:63f2a5165ffd 220 signalpart2 = notch_low1.step(signalpart1);
huismaja 14:63f2a5165ffd 221 signalpart3 = notch_high1.step(signalpart2);
huismaja 14:63f2a5165ffd 222 signalpart4 = fabs(signalpart3);
huismaja 14:63f2a5165ffd 223 emg_1_filtered = filterlow1.step(signalpart4);
huismaja 14:63f2a5165ffd 224 if (emg_1_filtered > maximum_calibration_value_1) {
huismaja 14:63f2a5165ffd 225 maximum_calibration_value_1 = emg_1_filtered;
huismaja 14:63f2a5165ffd 226 }
huismaja 14:63f2a5165ffd 227 emg_1_threshold = maximum_calibration_value_1*0.5; //Set the threshold for emg 1
huismaja 14:63f2a5165ffd 228 //signalpart1 = filterhigh2.step(emg_2.read());
huismaja 14:63f2a5165ffd 229 // signalpart2 = notch_low2.step(signalpart1);
huismaja 14:63f2a5165ffd 230 // signalpart3 = notch_high2.step(signalpart2);
huismaja 14:63f2a5165ffd 231 // signalpart4 = fabs(signalpart3);
huismaja 14:63f2a5165ffd 232 // emg_2_filtered = filterlow2.step(signalpart4);
huismaja 14:63f2a5165ffd 233 // if (emg_2_filtered > maximum_calibration_value_2) {
huismaja 14:63f2a5165ffd 234 // maximum_calibration_value_2 = emg_2_filtered;
huismaja 14:63f2a5165ffd 235 // }
huismaja 14:63f2a5165ffd 236 }
huismaja 14:63f2a5165ffd 237 printf("%f \n",maximum_calibration_value_1);
huismaja 14:63f2a5165ffd 238 calibration_done=1;
huismaja 14:63f2a5165ffd 239 }
huismaja 14:63f2a5165ffd 240 }
huismaja 14:63f2a5165ffd 241
huismaja 14:63f2a5165ffd 242 void filter_emg(){
huismaja 14:63f2a5165ffd 243 if(calibration_done==1) {
huismaja 14:63f2a5165ffd 244 signalpart1 = filterhigh1.step(emg_1.read());
huismaja 14:63f2a5165ffd 245 signalpart2 = notch_low1.step(signalpart1);
huismaja 14:63f2a5165ffd 246 signalpart3 = notch_high1.step(signalpart2);
huismaja 14:63f2a5165ffd 247 signalpart4 = fabs(signalpart3);
huismaja 14:63f2a5165ffd 248 emg_1_filtered = filterlow1.step(signalpart4);
huismaja 14:63f2a5165ffd 249 // pc.printf("%f \n", emg_1_filtered);
huismaja 14:63f2a5165ffd 250
huismaja 14:63f2a5165ffd 251 // signalpart1 = filterhigh2.step(emg_2.read());
huismaja 14:63f2a5165ffd 252 // signalpart2 = notch_low2.step(signalpart1);
huismaja 14:63f2a5165ffd 253 // signalpart3 = notch_high2.step(signalpart2);
huismaja 14:63f2a5165ffd 254 // signalpart4 = fabs(signalpart3);
huismaja 14:63f2a5165ffd 255 // emg_2_filtered = filterlow2.step(signalpart4);
huismaja 14:63f2a5165ffd 256 // pc.printf("%f \t %f \n", emg_1_filtered, emg_2_filtered);
huismaja 14:63f2a5165ffd 257
huismaja 14:63f2a5165ffd 258 // scope.set(0,emg_1_filtered);
huismaja 14:63f2a5165ffd 259 // scope.set(1,emg_2_filtered);
huismaja 14:63f2a5165ffd 260 // scope.send();
huismaja 14:63f2a5165ffd 261
huismaja 14:63f2a5165ffd 262 if(emg_1_filtered >= emg_1_threshold && emg_1_activated == false){ //If the filtered emg 1 signal is above the threshold value and if the activate_rotation_left function is not activated yet
huismaja 14:63f2a5165ffd 263 emg_1_activated = true;
huismaja 14:63f2a5165ffd 264 activate_rotation_left(); //Execute the activate_rotation_left function
huismaja 14:63f2a5165ffd 265 } else if (emg_1_filtered <= emg_1_threshold){
huismaja 14:63f2a5165ffd 266 emg_1_activated = false;
huismaja 14:63f2a5165ffd 267 }
huismaja 14:63f2a5165ffd 268 }
huismaja 14:63f2a5165ffd 269 }
huismaja 4:84bd5ead83f9 270
huismaja 13:746240466172 271 void check_threshold_crossing (){ //Function to check if the emg thresholds are crossed
huismaja 14:63f2a5165ffd 272 // if(emg_1_filtered >= emg_1_threshold && calibration_done==1 && emg_1_activated == false){ //If the filtered emg 1 signal is above the threshold value and if the activate_rotation_left function is not activated yet
huismaja 14:63f2a5165ffd 273 // emg_1_activated = true;
huismaja 14:63f2a5165ffd 274 // activate_rotation_left(); //Execute the activate_rotation_left function
huismaja 14:63f2a5165ffd 275 // } else if (emg_1_filtered <= emg_1_threshold){
huismaja 14:63f2a5165ffd 276 // emg_1_activated = false;
huismaja 14:63f2a5165ffd 277 // }
huismaja 13:746240466172 278 // if(emg_2_filtered >= emg_2_threshold && emg_2_activated == false){ //If the filtered emg 2 signal is above the threshold value and if the activate_rotation_right function is not activated yet
huismaja 13:746240466172 279 // activate_rotation_right(); //Execute the activate_rotation_right function
huismaja 13:746240466172 280 // emg_2_activated = true; //Declare that the activate_rotation_right function is now activated
huismaja 13:746240466172 281 // } else if (emg_2_filtered <= emg_2_threshold){ //If the filtered emg 2 signal gets below the threshold value
huismaja 13:746240466172 282 // emg_2_activated = false; //The activate_rotation_right function is now deactivated and can be activated again
huismaja 13:746240466172 283 // }
huismaja 13:746240466172 284 // if(emg_3_filtered >= emg_3_threshold && emg_3_activated == false){ //If the filtered emg 3 signal is above the threshold value and if the activate_translation function is not activated yet
huismaja 13:746240466172 285 // activate_translation(); //Execute the activate_translation function
huismaja 13:746240466172 286 // emg_3_activated = true; //Declare that the activate_translation function is now activated
huismaja 13:746240466172 287 // } else if (emg_3_filtered <= emg_3_threshold){ //If the filtered emg 3 signal gets below the threshold value
huismaja 13:746240466172 288 // emg_3_activated = false; //The activate_translation function is now deactivated and can be activated again
huismaja 13:746240466172 289 // }
huismaja 13:746240466172 290 // if(emg_4_filtered >= emg_4_threshold && emg_4_activated == false){ //If the filtered emg 4 signal is above the threshold value and if the activate_gripper function is not activated yet
huismaja 13:746240466172 291 // activate_gripper(); //Execute the activate_gripper function
huismaja 13:746240466172 292 // emg_4_activated = true; //Declare that the activate_gripper function is now activated
huismaja 13:746240466172 293 // } else if (emg_4_filtered <= emg_4_threshold){ //If the filtered emg 4 signal gets below the threshold value
huismaja 13:746240466172 294 // emg_4_activated = false; //The activate_gripper function is now deactivated and can be activated again
huismaja 13:746240466172 295 // }
huismaja 13:746240466172 296 }
huismaja 13:746240466172 297
huismaja 13:746240466172 298 void check_goflags (){ //Function to check if the go-flags are activated
huismaja 13:746240466172 299 if (rotation_left_go == true) { //If the rotation_left go-flag is true
huismaja 13:746240466172 300 rotation_left_go = false; //Set the rotation_left go-flag to false
huismaja 13:746240466172 301 rotation_left(); //Execute the rotation_left function
huismaja 13:746240466172 302 }
huismaja 13:746240466172 303 if (rotation_right_go == true) { //If the rotation_right go-flag is true
huismaja 13:746240466172 304 rotation_right_go = false; //Set the rotation_right go-flag to false
huismaja 13:746240466172 305 rotation_right(); //Execute the rotation_right function
huismaja 13:746240466172 306 }
huismaja 13:746240466172 307 if (translation_go == true) { //If the translation go-flag is true
huismaja 13:746240466172 308 translation_go = false; //Set the translation go-flag to false
huismaja 13:746240466172 309 translation(); //Execute the translation function
huismaja 13:746240466172 310 }
huismaja 13:746240466172 311 if (gripper_go == true) { //If the gripper go-flag is true
huismaja 13:746240466172 312 gripper_go = false; //Set the gripper go-flag to false
huismaja 13:746240466172 313 gripper(); //Execute the gripper function
huismaja 13:746240466172 314 }
huismaja 13:746240466172 315 }
huismaja 13:746240466172 316
huismaja 13:746240466172 317 int main (){
huismaja 10:cf579c3eaf01 318 pc.baud(115200); //Set the boud rate for serial communication
huismaja 10:cf579c3eaf01 319 pc.printf("RESET \n"); //Print "RESET"
huismaja 1:0d55a4bf2269 320
huismaja 5:9b5edadc023b 321 Direction_M1 = 1; //The arm will initially get longer
huismaja 5:9b5edadc023b 322 Speed_M1 = 0; //The first motor is initially turned off
huismaja 13:746240466172 323 Direction_M2 = 1; //The arm will initially turn left
huismaja 6:98121d2d76a6 324 Speed_M2 = 0; //The second motor is initially turned off
huismaja 13:746240466172 325 gripper_servo = 0.3; //The gripper is initially open
huismaja 10:cf579c3eaf01 326 encoder_M1.reset(); //Reset the encoder for motor 1
huismaja 10:cf579c3eaf01 327 encoder_M2.reset(); //Reset the encoder for motor 2
huismaja 1:0d55a4bf2269 328
huismaja 10:cf579c3eaf01 329 encoder_M1_ticker.attach(&read_position_M1,0.01); //Connect the encoder_M1_ticker to the read_position_M1 function and execute at 100Hz
huismaja 10:cf579c3eaf01 330 encoder_M2_ticker.attach(&read_position_M2,0.01); //Connect the encoder_M2_ticker to the read_position_M2 function and execute at 100Hz
huismaja 10:cf579c3eaf01 331
huismaja 13:746240466172 332 // Switch_1.rise(&activate_rotation_left); //Use switch_1 to activate the counter_rotation_left go-flag
huismaja 11:b1ad5267a6bd 333 Switch_2.rise(&activate_rotation_right); //Use switch_2 to activate the counter_rotation_right go-flag
huismaja 11:b1ad5267a6bd 334 Switch_3.rise(&activate_translation); //Use switch_3 to activate the counter_translation go-flag
huismaja 11:b1ad5267a6bd 335 Switch_4.rise(&activate_gripper); //Use switch_4 to activate the counter_gripper go-flag
huismaja 5:9b5edadc023b 336
huismaja 14:63f2a5165ffd 337 filter_EMG_ticker.attach(&filter_emg, 0.001); //Connect the filter_EMG_ticker to the filter_EMG funtion and execute at 1000Hz
huismaja 14:63f2a5165ffd 338 calibration_ticker.attach(&calibration, 0.001);
huismaja 14:63f2a5165ffd 339 check_threshold_crossing_ticker.attach(&check_threshold_crossing, 0.01); //Connect the check_threshold_crossing_ticker to the check_threshold_crossing function at 100Hz
huismaja 13:746240466172 340 check_goflags_ticker.attach(&check_goflags, 0.01); //Connect the check_goflags_ticker to the check_goflags
huismaja 13:746240466172 341
huismaja 13:746240466172 342 while (true){} //Create a while loop to let the main loop run indefinitly
huismaja 0:6c8444d06e97 343 }