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