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