Motor_Control_EMG

Dependencies:   HIDScope MODSERIAL QEI Servo biquadFilter mbed

Fork of Motor_Control_buttons by Janet Huisman

Committer:
huismaja
Date:
Mon Oct 31 11:17:49 2016 +0000
Revision:
15:c43f0dfe7cdf
Parent:
14:63f2a5165ffd
Child:
16:196abf318ea4
Working for 2 EMG signals

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 15:c43f0dfe7cdf 9 //HIDScope scope(4); //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 15:c43f0dfe7cdf 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 15:c43f0dfe7cdf 55 double maximum_calibration_value_3=0;
huismaja 15:c43f0dfe7cdf 56 double maximum_calibration_value_4=0;
huismaja 14:63f2a5165ffd 57 bool calibration_done=0;
huismaja 14:63f2a5165ffd 58
huismaja 14:63f2a5165ffd 59 volatile double emg_1_threshold = 0.2; //Set the threshold for emg 1
huismaja 14:63f2a5165ffd 60 volatile double emg_2_threshold = 0.2; //Set the threshold for emg 2
huismaja 14:63f2a5165ffd 61 volatile double emg_3_threshold = 0.2; //Set the threshold for emg 3
huismaja 14:63f2a5165ffd 62 volatile double emg_4_threshold = 0.2; //Set the threshold for emg 4
huismaja 13:746240466172 63
huismaja 13:746240466172 64 Ticker filter_EMG_ticker; //Create a ticker for the filtering of all emg signals
huismaja 14:63f2a5165ffd 65 Ticker calibration_ticker;
huismaja 13:746240466172 66 Ticker check_threshold_crossing_ticker; //Create a ticker for checking if the threshold is crossed
huismaja 13:746240466172 67 Ticker check_goflags_ticker; //Create a ticker for checking if the go-flags are set true
huismaja 13:746240466172 68
huismaja 15:c43f0dfe7cdf 69 BiQuad highpass1(0.9565, -1.9131, 0.9565, -1.9112, 0.9150);
huismaja 14:63f2a5165ffd 70 BiQuad notch_low1(1.0000, -1.9023, 1.0000, -1.8795, 0.9819);
huismaja 14:63f2a5165ffd 71 BiQuad notch_high1(1.0000, -1.9023, 1.0000, -1.8913, 0.9829);
huismaja 15:c43f0dfe7cdf 72 BiQuad lowpass1(0.00003913, 0.00007826, 0.00003913, -1.9822, 0.9824);
huismaja 15:c43f0dfe7cdf 73
huismaja 15:c43f0dfe7cdf 74 BiQuad highpass2(0.9565, -1.9131, 0.9565, -1.9112, 0.9150);
huismaja 15:c43f0dfe7cdf 75 BiQuad notch_low2(1.0000, -1.9023, 1.0000, -1.8795, 0.9819);
huismaja 15:c43f0dfe7cdf 76 BiQuad notch_high2(1.0000, -1.9023, 1.0000, -1.8913, 0.9829);
huismaja 15:c43f0dfe7cdf 77 BiQuad lowpass2(0.00003913, 0.00007826, 0.00003913, -1.9822, 0.9824);
huismaja 14:63f2a5165ffd 78
huismaja 15:c43f0dfe7cdf 79 //BiQuad highpass3(0.9565, -1.9131, 0.9565, -1.9112, 0.9150);
huismaja 15:c43f0dfe7cdf 80 //BiQuad notch_low3(1.0000, -1.9023, 1.0000, -1.8795, 0.9819);
huismaja 15:c43f0dfe7cdf 81 //BiQuad notch_high3(1.0000, -1.9023, 1.0000, -1.8913, 0.9829);
huismaja 15:c43f0dfe7cdf 82 //BiQuad lowpass3(0.00003913, 0.00007826, 0.00003913, -1.9822, 0.9824);
huismaja 15:c43f0dfe7cdf 83
huismaja 15:c43f0dfe7cdf 84 //BiQuad highpass4(0.9565, -1.9131, 0.9565, -1.9112, 0.9150);
huismaja 15:c43f0dfe7cdf 85 //BiQuad notch_low4(1.0000, -1.9023, 1.0000, -1.8795, 0.9819);
huismaja 15:c43f0dfe7cdf 86 //BiQuad notch_high4(1.0000, -1.9023, 1.0000, -1.8913, 0.9829);
huismaja 15:c43f0dfe7cdf 87 //BiQuad lowpass4(0.00003913, 0.00007826, 0.00003913, -1.9822, 0.9824);
huismaja 2:b20570f160c6 88
huismaja 5:9b5edadc023b 89 int counter_rotation_left=0; //To count the number of times the rotation_left switch (switch_1) has been pushed
huismaja 5:9b5edadc023b 90 int counter_rotation_right=0; //To count the number of times the rotation_right switch (switch_2) has been pushed
huismaja 5:9b5edadc023b 91 int counter_translation=0; //To count the number of times the translation switch (switch_3) has been pushed
huismaja 5:9b5edadc023b 92 int counter_gripper=0; //To count the number of times the gripper switch (switch_4) has been pushed
huismaja 5:9b5edadc023b 93
huismaja 13:746240466172 94 bool emg_1_activated = false; //Initially the emg_1 has not crossed the threshold
huismaja 13:746240466172 95 bool emg_2_activated = false; //Initially the emg_2 has not crossed the threshold
huismaja 13:746240466172 96 bool emg_3_activated = false; //Initially the emg_3 has not crossed the threshold
huismaja 13:746240466172 97 bool emg_4_activated = false; //Initially the emg_4 has not crossed the threshold
huismaja 13:746240466172 98
huismaja 11:b1ad5267a6bd 99 volatile bool rotation_left_go = false; //Create a go-flag for the rotation_left and set it to false
huismaja 11:b1ad5267a6bd 100 volatile bool rotation_right_go = false; //Create a go-flag for the rotation_right and set it to false
huismaja 11:b1ad5267a6bd 101 volatile bool translation_go = false; //Create a go-flag for the translation and set it to false
huismaja 11:b1ad5267a6bd 102 volatile bool gripper_go = false; //Create a go-flag for the gripper and set it to false
huismaja 0:6c8444d06e97 103
huismaja 13:746240466172 104 float angle_M1=0; //The measured angle of motor 1 is initially zero
huismaja 13:746240466172 105 float angle_M2=0; //The measured angle of motor 2 is initially zero
huismaja 9:cca4d4084775 106
huismaja 13:746240466172 107 void read_position_M1 (){ //Function to read the position of motor 1
huismaja 13:746240466172 108 int pulses_M1 = -encoder_M1.getPulses(); //Read the encoder data and store it in pulses_M1
huismaja 13:746240466172 109 angle_M1 = float(pulses_M1)/4200*2.0*pi; //Calculate the angle that corresponds with the measured encoder pulses
huismaja 13:746240466172 110 // pc.printf("%i \t%f \t", pulses_M1, angle_M1);
huismaja 13:746240466172 111 }
huismaja 13:746240466172 112
huismaja 13:746240466172 113 void read_position_M2 (){ //Function to read the position of motor 2
huismaja 12:35a81d6c6505 114 int pulses_M2 = -encoder_M2.getPulses(); //Read the encoder data and store it in pulses_M2
huismaja 12:35a81d6c6505 115 angle_M2 = float(pulses_M2)/4200*2.0*pi; //Calculate the angle that corresponds with the measured encoder pulses
huismaja 13:746240466172 116 // pc.printf("%i \t%f \n", pulses_M2, angle_M2);
huismaja 9:cca4d4084775 117 }
huismaja 9:cca4d4084775 118
huismaja 11:b1ad5267a6bd 119 void activate_rotation_left (){ //To activate the rotation_left
huismaja 11:b1ad5267a6bd 120 counter_rotation_left++; //Increase the counter_rotation_left that counts the number of time switch 1 has been pressed
huismaja 11:b1ad5267a6bd 121 if (counter_rotation_left > 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1 etc.
huismaja 11:b1ad5267a6bd 122 counter_rotation_left=1;
huismaja 11:b1ad5267a6bd 123 }
huismaja 11:b1ad5267a6bd 124 rotation_left_go = true; //After increasing the counter, set the rotation_left go-flag to true
huismaja 11:b1ad5267a6bd 125 }
huismaja 11:b1ad5267a6bd 126
huismaja 10:cf579c3eaf01 127 void rotation_left (){ //Function to control the rotation to the left
huismaja 10:cf579c3eaf01 128 switch (counter_rotation_left){ //Create a switch statement
huismaja 10:cf579c3eaf01 129 case 1: //For activating the rotation to the left
huismaja 13:746240466172 130 Direction_M1 = 1; //The arm will rotate to the left
huismaja 10:cf579c3eaf01 131 Speed_M1 = speedM1; //The motor is turned on at speed_rotation rad/sec
huismaja 8:9c58ca13076e 132 pc.printf("The arm will now rotate to the left with %f rad/sec \n", speedM1);
huismaja 12:35a81d6c6505 133 wait(0.1f);
huismaja 3:0a4bfcb3f339 134 break;
huismaja 10:cf579c3eaf01 135 case 2: //For stopping the rotation to the left
huismaja 13:746240466172 136 Direction_M1 = 1; //The arm will rotate to the left
huismaja 10:cf579c3eaf01 137 Speed_M1 = 0; //The motor is turned off
huismaja 5:9b5edadc023b 138 pc.printf("The arm will now stop rotating to the left \n");
huismaja 12:35a81d6c6505 139 wait(0.1f);
huismaja 3:0a4bfcb3f339 140 break;
huismaja 3:0a4bfcb3f339 141 }
huismaja 11:b1ad5267a6bd 142 }
huismaja 3:0a4bfcb3f339 143
huismaja 11:b1ad5267a6bd 144 void activate_rotation_right (){ //To activate the rotation_right
huismaja 11:b1ad5267a6bd 145 counter_rotation_right++; //Increase the counter_rotation_right that counts the number of time switch 2 has been pressed
huismaja 11:b1ad5267a6bd 146 if (counter_rotation_right> 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1
huismaja 11:b1ad5267a6bd 147 counter_rotation_right=1;
huismaja 3:0a4bfcb3f339 148 }
huismaja 11:b1ad5267a6bd 149 rotation_right_go = true; //After increasing the counter, set the rotation_right go-flag to true
huismaja 3:0a4bfcb3f339 150 }
huismaja 3:0a4bfcb3f339 151
huismaja 10:cf579c3eaf01 152 void rotation_right (){ //Function to control the rotation to the left
huismaja 10:cf579c3eaf01 153 switch (counter_rotation_right){ //Create a switch statement
huismaja 10:cf579c3eaf01 154 case 1: //For activation the rotation to the right
huismaja 13:746240466172 155 Direction_M1 = 0; //The arm will rotate to the right
huismaja 8:9c58ca13076e 156 Speed_M1 = speedM1; //The motor is turned on at speed_rotation rad/sec
huismaja 8:9c58ca13076e 157 pc.printf("The arm will now rotate to the right with %f rad/sec \n", speedM1);
huismaja 12:35a81d6c6505 158 wait(0.1f);
huismaja 3:0a4bfcb3f339 159 break;
huismaja 5:9b5edadc023b 160 case 2: //For stopping the rotation to the right
huismaja 13:746240466172 161 Direction_M1 = 0; //The arm will rotate to the right
huismaja 5:9b5edadc023b 162 Speed_M1 = 0; //The motor is turned off
huismaja 5:9b5edadc023b 163 pc.printf("The arm will now stop rotating to the right \n");
huismaja 12:35a81d6c6505 164 wait(0.1f);
huismaja 3:0a4bfcb3f339 165 break;
huismaja 3:0a4bfcb3f339 166 }
huismaja 3:0a4bfcb3f339 167 }
huismaja 3:0a4bfcb3f339 168
huismaja 11:b1ad5267a6bd 169 void activate_translation (){ //To activate the translation
huismaja 11:b1ad5267a6bd 170 counter_translation++; //Increase the counter_translation that counts the number of time switch 3 has been pressed
huismaja 11:b1ad5267a6bd 171 if (counter_translation > 4){ //Because there are 4 cases in the switch statement, case 5 = case 1
huismaja 11:b1ad5267a6bd 172 counter_translation=1;
huismaja 3:0a4bfcb3f339 173 }
huismaja 11:b1ad5267a6bd 174 translation_go = true; //After increasing the counter, set the translation go-flag to true
huismaja 3:0a4bfcb3f339 175 }
huismaja 3:0a4bfcb3f339 176
huismaja 10:cf579c3eaf01 177 void translation (){ //Function to control the translation
huismaja 10:cf579c3eaf01 178 switch (counter_translation){ //Create a switch statement
huismaja 8:9c58ca13076e 179 case 1: //For activating the elongation of the arm
huismaja 8:9c58ca13076e 180 Direction_M2 = 1; //The arm will get longer
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 longer \n");
huismaja 12:35a81d6c6505 183 wait(0.1f);
huismaja 5:9b5edadc023b 184 break;
huismaja 8:9c58ca13076e 185 case 2: //For stopping the elongation of the arm
huismaja 8:9c58ca13076e 186 Direction_M2 = 1; //The arm will get longer
huismaja 8:9c58ca13076e 187 Speed_M2 = 0; //The motor is turned off
huismaja 5:9b5edadc023b 188 pc.printf("The arm will now stop getting longer \n");
huismaja 12:35a81d6c6505 189 wait(0.1f);
huismaja 5:9b5edadc023b 190 break;
huismaja 8:9c58ca13076e 191 case 3: //For activating the shortening of the arm
huismaja 8:9c58ca13076e 192 Direction_M2 = 0; //The arm will get shorter
huismaja 10:cf579c3eaf01 193 Speed_M2 = speedM2; //The motor is turned on at speed_translation rad/sec
huismaja 5:9b5edadc023b 194 pc.printf("The arm will now get shorter \n");
huismaja 12:35a81d6c6505 195 wait(0.1f);
huismaja 5:9b5edadc023b 196 break;
huismaja 8:9c58ca13076e 197 case 4: //For stopping the shortening of the arm
huismaja 8:9c58ca13076e 198 Direction_M2 = 0; //The arm will get shorter
huismaja 8:9c58ca13076e 199 Speed_M2 = 0; //The motor is turned off
huismaja 5:9b5edadc023b 200 pc.printf("The arm will now stop getting shorter \n");
huismaja 12:35a81d6c6505 201 wait(0.1f);
huismaja 5:9b5edadc023b 202 break;
huismaja 5:9b5edadc023b 203 }
huismaja 11:b1ad5267a6bd 204 }
huismaja 5:9b5edadc023b 205
huismaja 11:b1ad5267a6bd 206 void activate_gripper (){ //To activate the gripper
huismaja 11:b1ad5267a6bd 207 counter_gripper++; //Increase the couter_gripper that counts the number of time switch 4 has been pressed
huismaja 11:b1ad5267a6bd 208 if (counter_gripper> 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1
huismaja 11:b1ad5267a6bd 209 counter_gripper=1;
huismaja 5:9b5edadc023b 210 }
huismaja 11:b1ad5267a6bd 211 gripper_go = true; //After increasing the counter, set the gripper go-flag to true
huismaja 5:9b5edadc023b 212 }
huismaja 5:9b5edadc023b 213
huismaja 10:cf579c3eaf01 214 void gripper (){ //Function to control the gripper
huismaja 10:cf579c3eaf01 215 switch (counter_gripper){ //Create a switch statement
huismaja 10:cf579c3eaf01 216 case 1: //For closing the gripper
huismaja 10:cf579c3eaf01 217 gripper_servo = 0; //The gripper is now closed
huismaja 5:9b5edadc023b 218 pc.printf("The gripper will now close \n");
huismaja 12:35a81d6c6505 219 wait(0.1f);
huismaja 4:84bd5ead83f9 220 break;
huismaja 10:cf579c3eaf01 221 case 2: //For opening the gripper
huismaja 13:746240466172 222 gripper_servo = 0.3; //The gripper is now open
huismaja 5:9b5edadc023b 223 pc.printf("The gripper will now open \n");
huismaja 12:35a81d6c6505 224 wait(0.1f);
huismaja 4:84bd5ead83f9 225 break;
huismaja 4:84bd5ead83f9 226 }
huismaja 4:84bd5ead83f9 227 }
huismaja 14:63f2a5165ffd 228 void calibration(){
huismaja 14:63f2a5165ffd 229 if(Switch_1.read()== false) {
huismaja 14:63f2a5165ffd 230 for(int n=0; n<5000; n++){
huismaja 15:c43f0dfe7cdf 231 signalpart1 = highpass1.step(emg_1.read());
huismaja 14:63f2a5165ffd 232 signalpart2 = notch_low1.step(signalpart1);
huismaja 14:63f2a5165ffd 233 signalpart3 = notch_high1.step(signalpart2);
huismaja 14:63f2a5165ffd 234 signalpart4 = fabs(signalpart3);
huismaja 15:c43f0dfe7cdf 235 emg_1_filtered = lowpass1.step(signalpart4);
huismaja 14:63f2a5165ffd 236 if (emg_1_filtered > maximum_calibration_value_1) {
huismaja 14:63f2a5165ffd 237 maximum_calibration_value_1 = emg_1_filtered;
huismaja 14:63f2a5165ffd 238 }
huismaja 15:c43f0dfe7cdf 239 emg_1_threshold = maximum_calibration_value_1*0.7; //Set the threshold for emg 1
huismaja 15:c43f0dfe7cdf 240
huismaja 15:c43f0dfe7cdf 241 signalpart1 = highpass2.step(emg_2.read());
huismaja 15:c43f0dfe7cdf 242 signalpart2 = notch_low2.step(signalpart1);
huismaja 15:c43f0dfe7cdf 243 signalpart3 = notch_high2.step(signalpart2);
huismaja 15:c43f0dfe7cdf 244 signalpart4 = fabs(signalpart3);
huismaja 15:c43f0dfe7cdf 245 emg_2_filtered = lowpass2.step(signalpart4);
huismaja 15:c43f0dfe7cdf 246 if (emg_2_filtered > maximum_calibration_value_2) {
huismaja 15:c43f0dfe7cdf 247 maximum_calibration_value_2 = emg_2_filtered;
huismaja 15:c43f0dfe7cdf 248 }
huismaja 15:c43f0dfe7cdf 249 emg_2_threshold = maximum_calibration_value_2*0.7; //Set the threshold for emg 2
huismaja 14:63f2a5165ffd 250 }
huismaja 15:c43f0dfe7cdf 251
huismaja 15:c43f0dfe7cdf 252 printf("%f \t %f \n",maximum_calibration_value_1, maximum_calibration_value_2);
huismaja 14:63f2a5165ffd 253 calibration_done=1;
huismaja 14:63f2a5165ffd 254 }
huismaja 14:63f2a5165ffd 255 }
huismaja 14:63f2a5165ffd 256
huismaja 14:63f2a5165ffd 257 void filter_emg(){
huismaja 14:63f2a5165ffd 258 if(calibration_done==1) {
huismaja 15:c43f0dfe7cdf 259 signalpart1 = highpass1.step(emg_1.read());
huismaja 14:63f2a5165ffd 260 signalpart2 = notch_low1.step(signalpart1);
huismaja 14:63f2a5165ffd 261 signalpart3 = notch_high1.step(signalpart2);
huismaja 14:63f2a5165ffd 262 signalpart4 = fabs(signalpart3);
huismaja 15:c43f0dfe7cdf 263 emg_1_filtered = lowpass1.step(signalpart4);
huismaja 14:63f2a5165ffd 264 // pc.printf("%f \n", emg_1_filtered);
huismaja 14:63f2a5165ffd 265
huismaja 15:c43f0dfe7cdf 266 signalpart1 = highpass2.step(emg_2.read());
huismaja 15:c43f0dfe7cdf 267 signalpart2 = notch_low2.step(signalpart1);
huismaja 15:c43f0dfe7cdf 268 signalpart3 = notch_high2.step(signalpart2);
huismaja 15:c43f0dfe7cdf 269 signalpart4 = fabs(signalpart3);
huismaja 15:c43f0dfe7cdf 270 emg_2_filtered = lowpass2.step(signalpart4);
huismaja 14:63f2a5165ffd 271 // pc.printf("%f \t %f \n", emg_1_filtered, emg_2_filtered);
huismaja 14:63f2a5165ffd 272
huismaja 14:63f2a5165ffd 273 // scope.set(0,emg_1_filtered);
huismaja 14:63f2a5165ffd 274 // scope.set(1,emg_2_filtered);
huismaja 14:63f2a5165ffd 275 // scope.send();
huismaja 14:63f2a5165ffd 276
huismaja 15:c43f0dfe7cdf 277 // 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 15:c43f0dfe7cdf 278 // emg_1_activated = true;
huismaja 15:c43f0dfe7cdf 279 // activate_rotation_left(); //Execute the activate_rotation_left function
huismaja 15:c43f0dfe7cdf 280 // } else if (emg_1_filtered <= emg_1_threshold){
huismaja 15:c43f0dfe7cdf 281 // emg_1_activated = false;
huismaja 15:c43f0dfe7cdf 282 // }
huismaja 15:c43f0dfe7cdf 283 //
huismaja 15:c43f0dfe7cdf 284 // 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 15:c43f0dfe7cdf 285 // activate_rotation_right(); //Execute the activate_rotation_right function
huismaja 15:c43f0dfe7cdf 286 // emg_2_activated = true; //Declare that the activate_rotation_right function is now activated
huismaja 15:c43f0dfe7cdf 287 // } else if (emg_2_filtered <= emg_2_threshold){ //If the filtered emg 2 signal gets below the threshold value
huismaja 15:c43f0dfe7cdf 288 // emg_2_activated = false; //The activate_rotation_right function is now deactivated and can be activated again
huismaja 15:c43f0dfe7cdf 289 // }
huismaja 15:c43f0dfe7cdf 290
huismaja 14:63f2a5165ffd 291 }
huismaja 14:63f2a5165ffd 292 }
huismaja 4:84bd5ead83f9 293
huismaja 13:746240466172 294 void check_threshold_crossing (){ //Function to check if the emg thresholds are crossed
huismaja 15:c43f0dfe7cdf 295 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 15:c43f0dfe7cdf 296 emg_1_activated = true;
huismaja 15:c43f0dfe7cdf 297 activate_rotation_left(); //Execute the activate_rotation_left function
huismaja 15:c43f0dfe7cdf 298 } else if (emg_1_filtered <= emg_1_threshold){
huismaja 15:c43f0dfe7cdf 299 emg_1_activated = false;
huismaja 15:c43f0dfe7cdf 300 }
huismaja 15:c43f0dfe7cdf 301 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 15:c43f0dfe7cdf 302 activate_rotation_right(); //Execute the activate_rotation_right function
huismaja 15:c43f0dfe7cdf 303 emg_2_activated = true; //Declare that the activate_rotation_right function is now activated
huismaja 15:c43f0dfe7cdf 304 } else if (emg_2_filtered <= emg_2_threshold){ //If the filtered emg 2 signal gets below the threshold value
huismaja 15:c43f0dfe7cdf 305 emg_2_activated = false; //The activate_rotation_right function is now deactivated and can be activated again
huismaja 15:c43f0dfe7cdf 306 }
huismaja 13:746240466172 307 // 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 308 // activate_translation(); //Execute the activate_translation function
huismaja 13:746240466172 309 // emg_3_activated = true; //Declare that the activate_translation function is now activated
huismaja 13:746240466172 310 // } else if (emg_3_filtered <= emg_3_threshold){ //If the filtered emg 3 signal gets below the threshold value
huismaja 13:746240466172 311 // emg_3_activated = false; //The activate_translation function is now deactivated and can be activated again
huismaja 13:746240466172 312 // }
huismaja 13:746240466172 313 // 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 314 // activate_gripper(); //Execute the activate_gripper function
huismaja 13:746240466172 315 // emg_4_activated = true; //Declare that the activate_gripper function is now activated
huismaja 13:746240466172 316 // } else if (emg_4_filtered <= emg_4_threshold){ //If the filtered emg 4 signal gets below the threshold value
huismaja 13:746240466172 317 // emg_4_activated = false; //The activate_gripper function is now deactivated and can be activated again
huismaja 13:746240466172 318 // }
huismaja 13:746240466172 319 }
huismaja 13:746240466172 320
huismaja 13:746240466172 321 void check_goflags (){ //Function to check if the go-flags are activated
huismaja 13:746240466172 322 if (rotation_left_go == true) { //If the rotation_left go-flag is true
huismaja 13:746240466172 323 rotation_left_go = false; //Set the rotation_left go-flag to false
huismaja 13:746240466172 324 rotation_left(); //Execute the rotation_left function
huismaja 13:746240466172 325 }
huismaja 13:746240466172 326 if (rotation_right_go == true) { //If the rotation_right go-flag is true
huismaja 13:746240466172 327 rotation_right_go = false; //Set the rotation_right go-flag to false
huismaja 13:746240466172 328 rotation_right(); //Execute the rotation_right function
huismaja 13:746240466172 329 }
huismaja 13:746240466172 330 if (translation_go == true) { //If the translation go-flag is true
huismaja 13:746240466172 331 translation_go = false; //Set the translation go-flag to false
huismaja 13:746240466172 332 translation(); //Execute the translation function
huismaja 13:746240466172 333 }
huismaja 13:746240466172 334 if (gripper_go == true) { //If the gripper go-flag is true
huismaja 13:746240466172 335 gripper_go = false; //Set the gripper go-flag to false
huismaja 13:746240466172 336 gripper(); //Execute the gripper function
huismaja 13:746240466172 337 }
huismaja 13:746240466172 338 }
huismaja 13:746240466172 339
huismaja 13:746240466172 340 int main (){
huismaja 10:cf579c3eaf01 341 pc.baud(115200); //Set the boud rate for serial communication
huismaja 10:cf579c3eaf01 342 pc.printf("RESET \n"); //Print "RESET"
huismaja 1:0d55a4bf2269 343
huismaja 5:9b5edadc023b 344 Direction_M1 = 1; //The arm will initially get longer
huismaja 5:9b5edadc023b 345 Speed_M1 = 0; //The first motor is initially turned off
huismaja 13:746240466172 346 Direction_M2 = 1; //The arm will initially turn left
huismaja 6:98121d2d76a6 347 Speed_M2 = 0; //The second motor is initially turned off
huismaja 13:746240466172 348 gripper_servo = 0.3; //The gripper is initially open
huismaja 10:cf579c3eaf01 349 encoder_M1.reset(); //Reset the encoder for motor 1
huismaja 10:cf579c3eaf01 350 encoder_M2.reset(); //Reset the encoder for motor 2
huismaja 1:0d55a4bf2269 351
huismaja 10:cf579c3eaf01 352 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 353 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 354
huismaja 13:746240466172 355 // Switch_1.rise(&activate_rotation_left); //Use switch_1 to activate the counter_rotation_left go-flag
huismaja 15:c43f0dfe7cdf 356 // Switch_2.rise(&activate_rotation_right); //Use switch_2 to activate the counter_rotation_right go-flag
huismaja 11:b1ad5267a6bd 357 Switch_3.rise(&activate_translation); //Use switch_3 to activate the counter_translation go-flag
huismaja 11:b1ad5267a6bd 358 Switch_4.rise(&activate_gripper); //Use switch_4 to activate the counter_gripper go-flag
huismaja 5:9b5edadc023b 359
huismaja 14:63f2a5165ffd 360 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 361 calibration_ticker.attach(&calibration, 0.001);
huismaja 14:63f2a5165ffd 362 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 363 check_goflags_ticker.attach(&check_goflags, 0.01); //Connect the check_goflags_ticker to the check_goflags
huismaja 13:746240466172 364
huismaja 13:746240466172 365 while (true){} //Create a while loop to let the main loop run indefinitly
huismaja 0:6c8444d06e97 366 }