Motor_Control_EMG
Dependencies: HIDScope MODSERIAL QEI Servo biquadFilter mbed
Fork of Motor_Control_buttons by
main.cpp@5:9b5edadc023b, 2016-10-08 (annotated)
- Committer:
- huismaja
- Date:
- Sat Oct 08 15:34:37 2016 +0000
- Revision:
- 5:9b5edadc023b
- Parent:
- 4:84bd5ead83f9
- Child:
- 6:98121d2d76a6
Robot control with 2-DC motors and a working gripper (using Servo-library)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
huismaja | 0:6c8444d06e97 | 1 | #include "mbed.h" |
huismaja | 4:84bd5ead83f9 | 2 | #include "MODSERIAL.h" |
huismaja | 4:84bd5ead83f9 | 3 | #include "Servo.h" |
huismaja | 0:6c8444d06e97 | 4 | |
huismaja | 5:9b5edadc023b | 5 | DigitalOut Direction_M1(D4); //To control the rotation direction of the arm |
huismaja | 5:9b5edadc023b | 6 | DigitalOut Speed_M1(D5); //To control the rotation speed of the arm |
huismaja | 5:9b5edadc023b | 7 | DigitalOut Speed_M2(D6); //To control the translation direction of the arm |
huismaja | 5:9b5edadc023b | 8 | DigitalOut Direction_M2(D7); //To control the translation speed of the arm |
huismaja | 1:0d55a4bf2269 | 9 | |
huismaja | 5:9b5edadc023b | 10 | Servo gripper_servo(D3); //To control the gripper (Note: doesn't work on D8, unknown why) |
huismaja | 0:6c8444d06e97 | 11 | |
huismaja | 5:9b5edadc023b | 12 | InterruptIn Switch_1(D9); //To control the rotation to the left |
huismaja | 5:9b5edadc023b | 13 | InterruptIn Switch_2(D10); //To control the rotation to the right |
huismaja | 5:9b5edadc023b | 14 | InterruptIn Switch_3(D11); //To control the translation of the arm |
huismaja | 5:9b5edadc023b | 15 | InterruptIn Switch_4(D12); //To control the gripper |
huismaja | 2:b20570f160c6 | 16 | |
huismaja | 5:9b5edadc023b | 17 | int counter_rotation_left=0; //To count the number of times the rotation_left switch (switch_1) has been pushed |
huismaja | 5:9b5edadc023b | 18 | int counter_rotation_right=0; //To count the number of times the rotation_right switch (switch_2) has been pushed |
huismaja | 5:9b5edadc023b | 19 | int counter_translation=0; //To count the number of times the translation switch (switch_3) has been pushed |
huismaja | 5:9b5edadc023b | 20 | int counter_gripper=0; //To count the number of times the gripper switch (switch_4) has been pushed |
huismaja | 5:9b5edadc023b | 21 | |
huismaja | 5:9b5edadc023b | 22 | MODSERIAL pc(USBTX, USBRX); //To make connection with the PC |
huismaja | 0:6c8444d06e97 | 23 | |
huismaja | 3:0a4bfcb3f339 | 24 | void rotation_left (){ |
huismaja | 3:0a4bfcb3f339 | 25 | switch (counter_rotation_left){ |
huismaja | 5:9b5edadc023b | 26 | case 1: //For activating the rotation to the left |
huismaja | 5:9b5edadc023b | 27 | Direction_M1 = 1; //The arm will rotate to the left |
huismaja | 5:9b5edadc023b | 28 | Speed_M1 = 1; //The motor is turned on |
huismaja | 5:9b5edadc023b | 29 | pc.printf("The arm will now rotate to the left \n"); |
huismaja | 3:0a4bfcb3f339 | 30 | wait(0.5f); |
huismaja | 3:0a4bfcb3f339 | 31 | break; |
huismaja | 5:9b5edadc023b | 32 | case 2: //For stopping the rotation to the left |
huismaja | 5:9b5edadc023b | 33 | Direction_M1 = 1; //The arm will rotate to the left |
huismaja | 5:9b5edadc023b | 34 | Speed_M1 = 0; //The motor is turned off |
huismaja | 5:9b5edadc023b | 35 | pc.printf("The arm will now stop rotating to the left \n"); |
huismaja | 3:0a4bfcb3f339 | 36 | wait(0.5f); |
huismaja | 3:0a4bfcb3f339 | 37 | break; |
huismaja | 3:0a4bfcb3f339 | 38 | } |
huismaja | 3:0a4bfcb3f339 | 39 | } |
huismaja | 3:0a4bfcb3f339 | 40 | |
huismaja | 5:9b5edadc023b | 41 | void switch_counter_rotation_left (){ //To count the number of times the rotation_left switch (switch_1) has been pushed |
huismaja | 3:0a4bfcb3f339 | 42 | counter_rotation_left++; |
huismaja | 3:0a4bfcb3f339 | 43 | if (counter_rotation_left > 2){ |
huismaja | 3:0a4bfcb3f339 | 44 | counter_rotation_left=1; |
huismaja | 3:0a4bfcb3f339 | 45 | } |
huismaja | 3:0a4bfcb3f339 | 46 | rotation_left(); |
huismaja | 3:0a4bfcb3f339 | 47 | } |
huismaja | 3:0a4bfcb3f339 | 48 | |
huismaja | 3:0a4bfcb3f339 | 49 | void rotation_right (){ |
huismaja | 3:0a4bfcb3f339 | 50 | switch (counter_rotation_right){ |
huismaja | 5:9b5edadc023b | 51 | case 1: //For activation the rotation to the right |
huismaja | 5:9b5edadc023b | 52 | Direction_M1 = 0; //The arm will rotate to the right |
huismaja | 5:9b5edadc023b | 53 | Speed_M1 = 1; //The motor is turned on |
huismaja | 5:9b5edadc023b | 54 | pc.printf("The arm will now rotate to the right \n"); |
huismaja | 3:0a4bfcb3f339 | 55 | wait(0.5f); |
huismaja | 3:0a4bfcb3f339 | 56 | break; |
huismaja | 5:9b5edadc023b | 57 | case 2: //For stopping the rotation to the right |
huismaja | 5:9b5edadc023b | 58 | Direction_M1 = 0; //The arm will rotate to the right |
huismaja | 5:9b5edadc023b | 59 | Speed_M1 = 0; //The motor is turned off |
huismaja | 5:9b5edadc023b | 60 | pc.printf("The arm will now stop rotating to the right \n"); |
huismaja | 3:0a4bfcb3f339 | 61 | wait(0.5f); |
huismaja | 3:0a4bfcb3f339 | 62 | break; |
huismaja | 3:0a4bfcb3f339 | 63 | } |
huismaja | 3:0a4bfcb3f339 | 64 | } |
huismaja | 3:0a4bfcb3f339 | 65 | |
huismaja | 5:9b5edadc023b | 66 | void switch_counter_rotation_right (){ //To count the number of times the rotation_right switch (switch_2) has been pushed |
huismaja | 3:0a4bfcb3f339 | 67 | counter_rotation_right++; |
huismaja | 3:0a4bfcb3f339 | 68 | if (counter_rotation_right> 2){ |
huismaja | 3:0a4bfcb3f339 | 69 | counter_rotation_right=1; |
huismaja | 3:0a4bfcb3f339 | 70 | } |
huismaja | 3:0a4bfcb3f339 | 71 | rotation_right(); |
huismaja | 3:0a4bfcb3f339 | 72 | } |
huismaja | 3:0a4bfcb3f339 | 73 | |
huismaja | 5:9b5edadc023b | 74 | void translation (){ |
huismaja | 5:9b5edadc023b | 75 | switch (counter_translation){ |
huismaja | 5:9b5edadc023b | 76 | case 1: //For activating the elongation of the arm |
huismaja | 5:9b5edadc023b | 77 | Direction_M2 = 1; //The arm will get longer |
huismaja | 5:9b5edadc023b | 78 | Speed_M2 = 1; //The motor is turned on |
huismaja | 5:9b5edadc023b | 79 | pc.printf("The arm will now get longer \n"); |
huismaja | 5:9b5edadc023b | 80 | wait(0.5f); |
huismaja | 5:9b5edadc023b | 81 | break; |
huismaja | 5:9b5edadc023b | 82 | case 2: //For stopping the elongation of the arm |
huismaja | 5:9b5edadc023b | 83 | Direction_M2 = 1; //The arm will get longer |
huismaja | 5:9b5edadc023b | 84 | Speed_M2 = 0; //The motor is turned off |
huismaja | 5:9b5edadc023b | 85 | pc.printf("The arm will now stop getting longer \n"); |
huismaja | 5:9b5edadc023b | 86 | wait(0.5f); |
huismaja | 5:9b5edadc023b | 87 | break; |
huismaja | 5:9b5edadc023b | 88 | case 3: //For activating the shortening of the arm |
huismaja | 5:9b5edadc023b | 89 | Direction_M2 = 0; //The arm will get shorter |
huismaja | 5:9b5edadc023b | 90 | Speed_M2 = 1; //The motor is turned off |
huismaja | 5:9b5edadc023b | 91 | pc.printf("The arm will now get shorter \n"); |
huismaja | 5:9b5edadc023b | 92 | wait(0.5f); |
huismaja | 5:9b5edadc023b | 93 | break; |
huismaja | 5:9b5edadc023b | 94 | case 4: //For stopping the shortening of the arm |
huismaja | 5:9b5edadc023b | 95 | Direction_M2 = 0; //The arm will get shorter |
huismaja | 5:9b5edadc023b | 96 | Speed_M2 = 0; //The motor is turned off |
huismaja | 5:9b5edadc023b | 97 | pc.printf("The arm will now stop getting shorter \n"); |
huismaja | 5:9b5edadc023b | 98 | wait(0.5f); |
huismaja | 5:9b5edadc023b | 99 | break; |
huismaja | 5:9b5edadc023b | 100 | } |
huismaja | 5:9b5edadc023b | 101 | } |
huismaja | 5:9b5edadc023b | 102 | |
huismaja | 5:9b5edadc023b | 103 | void switch_counter_translation (){ //To count the number of times the translation switch (switch_3) has been pushed |
huismaja | 5:9b5edadc023b | 104 | counter_translation++; |
huismaja | 5:9b5edadc023b | 105 | if (counter_translation > 4){ |
huismaja | 5:9b5edadc023b | 106 | counter_translation=1; |
huismaja | 5:9b5edadc023b | 107 | } |
huismaja | 5:9b5edadc023b | 108 | translation(); |
huismaja | 5:9b5edadc023b | 109 | } |
huismaja | 5:9b5edadc023b | 110 | |
huismaja | 4:84bd5ead83f9 | 111 | void gripper (){ |
huismaja | 4:84bd5ead83f9 | 112 | switch (counter_gripper){ |
huismaja | 4:84bd5ead83f9 | 113 | case 1: |
huismaja | 5:9b5edadc023b | 114 | gripper_servo = 0; //The gripper is now closed |
huismaja | 5:9b5edadc023b | 115 | pc.printf("The gripper will now close \n"); |
huismaja | 4:84bd5ead83f9 | 116 | wait(0.5f); |
huismaja | 4:84bd5ead83f9 | 117 | break; |
huismaja | 4:84bd5ead83f9 | 118 | case 2: |
huismaja | 5:9b5edadc023b | 119 | gripper_servo = 1; //The gripper is now open |
huismaja | 5:9b5edadc023b | 120 | pc.printf("The gripper will now open \n"); |
huismaja | 4:84bd5ead83f9 | 121 | wait(0.5f); |
huismaja | 4:84bd5ead83f9 | 122 | break; |
huismaja | 4:84bd5ead83f9 | 123 | } |
huismaja | 4:84bd5ead83f9 | 124 | } |
huismaja | 4:84bd5ead83f9 | 125 | |
huismaja | 5:9b5edadc023b | 126 | void switch_counter_gripper (){ //To count the number of times the gripper switch (switch_4) has been pushed |
huismaja | 4:84bd5ead83f9 | 127 | counter_gripper++; |
huismaja | 4:84bd5ead83f9 | 128 | if (counter_gripper> 2){ |
huismaja | 4:84bd5ead83f9 | 129 | counter_gripper=1; |
huismaja | 4:84bd5ead83f9 | 130 | } |
huismaja | 4:84bd5ead83f9 | 131 | gripper(); |
huismaja | 4:84bd5ead83f9 | 132 | } |
huismaja | 4:84bd5ead83f9 | 133 | |
huismaja | 0:6c8444d06e97 | 134 | int main(){ |
huismaja | 0:6c8444d06e97 | 135 | pc.baud(115200); |
huismaja | 0:6c8444d06e97 | 136 | pc.printf("RESET \n"); |
huismaja | 1:0d55a4bf2269 | 137 | |
huismaja | 5:9b5edadc023b | 138 | Direction_M1 = 1; //The arm will initially get longer |
huismaja | 5:9b5edadc023b | 139 | Speed_M1 = 0; //The first motor is initially turned off |
huismaja | 5:9b5edadc023b | 140 | Direction_M2 = 255; //The arm will initially turn left |
huismaja | 5:9b5edadc023b | 141 | Speed_M1 = 0; //The second motor is initially turned off |
huismaja | 5:9b5edadc023b | 142 | gripper_servo = 1; //The gripper is initially open |
huismaja | 1:0d55a4bf2269 | 143 | |
huismaja | 5:9b5edadc023b | 144 | Switch_1.rise(&switch_counter_rotation_left); |
huismaja | 5:9b5edadc023b | 145 | Switch_2.rise(&switch_counter_rotation_right); |
huismaja | 5:9b5edadc023b | 146 | Switch_3.rise(&switch_counter_translation); |
huismaja | 5:9b5edadc023b | 147 | Switch_4.rise(&switch_counter_gripper); |
huismaja | 5:9b5edadc023b | 148 | |
huismaja | 5:9b5edadc023b | 149 | while (true); |
huismaja | 0:6c8444d06e97 | 150 | } |