Script for controlling 2 DC-motors and a gripper-servo using buttons
Dependencies: MODSERIAL QEI Servo mbed
main.cpp@12:35a81d6c6505, 2016-10-19 (annotated)
- Committer:
- huismaja
- Date:
- Wed Oct 19 11:54:55 2016 +0000
- Revision:
- 12:35a81d6c6505
- Parent:
- 11:b1ad5267a6bd
- Child:
- 13:ea065d364277
Working script without limitations for the range of the motors
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 | 9:cca4d4084775 | 4 | #include "QEI.h" |
huismaja | 0:6c8444d06e97 | 5 | |
huismaja | 10:cf579c3eaf01 | 6 | QEI encoder_M1 (D9, D10, NC, 8400); //Define an encoder for motor 1 called encoder_M1 |
huismaja | 10:cf579c3eaf01 | 7 | QEI encoder_M2 (D11, D12, NC, 8400); //Define an encoder for motor 2 called encoder_M2 |
huismaja | 9:cca4d4084775 | 8 | |
huismaja | 10:cf579c3eaf01 | 9 | Ticker encoder_M1_ticker; //Create a ticker for reading the encoder data of encoder_M1 |
huismaja | 10:cf579c3eaf01 | 10 | Ticker encoder_M2_ticker; //Create a ticker for reading the encoder data of encoder_M2 |
huismaja | 6:98121d2d76a6 | 11 | |
huismaja | 12:35a81d6c6505 | 12 | DigitalOut Direction_M2(D4); //To control the rotation direction of the arm |
huismaja | 12:35a81d6c6505 | 13 | PwmOut Speed_M2(D5); //To control the rotation speed of the arm |
huismaja | 12:35a81d6c6505 | 14 | PwmOut Speed_M1(D6); //To control the translation direction of the arm |
huismaja | 12:35a81d6c6505 | 15 | DigitalOut Direction_M1(D7); //To control the translation speed of the arm |
huismaja | 10:cf579c3eaf01 | 16 | Servo gripper_servo(D13); //To control the gripper |
huismaja | 0:6c8444d06e97 | 17 | |
huismaja | 10:cf579c3eaf01 | 18 | InterruptIn Switch_1(SW2); //Switch 1 to control the rotation to the left |
huismaja | 10:cf579c3eaf01 | 19 | InterruptIn Switch_2(SW3); //Switch 2 to control the rotation to the right |
huismaja | 10:cf579c3eaf01 | 20 | InterruptIn Switch_3(D2); //Switch 3 to control the translation of the arm |
huismaja | 10:cf579c3eaf01 | 21 | InterruptIn Switch_4(D3); //Switch 4 to control the gripper |
huismaja | 2:b20570f160c6 | 22 | |
huismaja | 5:9b5edadc023b | 23 | int counter_rotation_left=0; //To count the number of times the rotation_left switch (switch_1) has been pushed |
huismaja | 5:9b5edadc023b | 24 | int counter_rotation_right=0; //To count the number of times the rotation_right switch (switch_2) has been pushed |
huismaja | 5:9b5edadc023b | 25 | int counter_translation=0; //To count the number of times the translation switch (switch_3) has been pushed |
huismaja | 5:9b5edadc023b | 26 | int counter_gripper=0; //To count the number of times the gripper switch (switch_4) has been pushed |
huismaja | 5:9b5edadc023b | 27 | |
huismaja | 11:b1ad5267a6bd | 28 | volatile bool rotation_left_go = false; //Create a go-flag for the rotation_left and set it to false |
huismaja | 11:b1ad5267a6bd | 29 | volatile bool rotation_right_go = false; //Create a go-flag for the rotation_right and set it to false |
huismaja | 11:b1ad5267a6bd | 30 | volatile bool translation_go = false; //Create a go-flag for the translation and set it to false |
huismaja | 11:b1ad5267a6bd | 31 | volatile bool gripper_go = false; //Create a go-flag for the gripper and set it to false |
huismaja | 0:6c8444d06e97 | 32 | |
huismaja | 11:b1ad5267a6bd | 33 | MODSERIAL pc(USBTX, USBRX); //Make a connection with the PC |
huismaja | 11:b1ad5267a6bd | 34 | |
huismaja | 11:b1ad5267a6bd | 35 | const double pi = 3.1415926535897; //Declare the value of pi |
huismaja | 9:cca4d4084775 | 36 | |
huismaja | 10:cf579c3eaf01 | 37 | double speed_rotation=pi/5; //Set the rotation speed in rad/sec -> NOTE: this has to be below 8.4 rad/sec |
huismaja | 10:cf579c3eaf01 | 38 | double speed_translation=pi/5; //Set the translation speed in rad/sec -> NOTE: this has to be below 8.4 rad/sec |
huismaja | 10:cf579c3eaf01 | 39 | double speedM1=speed_rotation/8.4; //Map the rotation speed from (0-8.4) to (0-1) by dividing by 8.4 |
huismaja | 10:cf579c3eaf01 | 40 | double speedM2=speed_translation/8.4; //Map the translation speed from (0-8.4) to (0-1) by dividing by 8.4 |
huismaja | 6:98121d2d76a6 | 41 | |
huismaja | 12:35a81d6c6505 | 42 | float angle_M1=0; |
huismaja | 12:35a81d6c6505 | 43 | float angle_M2=0; |
huismaja | 12:35a81d6c6505 | 44 | |
huismaja | 10:cf579c3eaf01 | 45 | void read_position_M1(){ //Function to read the position of motor 1 |
huismaja | 12:35a81d6c6505 | 46 | int pulses_M1 = -encoder_M1.getPulses(); //Read the encoder data and store it in pulses_M1 |
huismaja | 12:35a81d6c6505 | 47 | angle_M1 = float(pulses_M1)/4200*2.0*pi; //Calculate the angle that corresponds with the measured encoder pulses |
huismaja | 12:35a81d6c6505 | 48 | pc.printf("%i \t%f \t", pulses_M1, angle_M1); |
huismaja | 9:cca4d4084775 | 49 | } |
huismaja | 9:cca4d4084775 | 50 | |
huismaja | 10:cf579c3eaf01 | 51 | void read_position_M2(){ //Function to read the position of motor 2 |
huismaja | 12:35a81d6c6505 | 52 | int pulses_M2 = -encoder_M2.getPulses(); //Read the encoder data and store it in pulses_M2 |
huismaja | 12:35a81d6c6505 | 53 | angle_M2 = float(pulses_M2)/4200*2.0*pi; //Calculate the angle that corresponds with the measured encoder pulses |
huismaja | 12:35a81d6c6505 | 54 | pc.printf("%i \t%f \n", pulses_M2, angle_M2); |
huismaja | 9:cca4d4084775 | 55 | } |
huismaja | 9:cca4d4084775 | 56 | |
huismaja | 11:b1ad5267a6bd | 57 | void activate_rotation_left (){ //To activate the rotation_left |
huismaja | 11:b1ad5267a6bd | 58 | counter_rotation_left++; //Increase the counter_rotation_left that counts the number of time switch 1 has been pressed |
huismaja | 11:b1ad5267a6bd | 59 | if (counter_rotation_left > 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1 etc. |
huismaja | 11:b1ad5267a6bd | 60 | counter_rotation_left=1; |
huismaja | 11:b1ad5267a6bd | 61 | } |
huismaja | 11:b1ad5267a6bd | 62 | rotation_left_go = true; //After increasing the counter, set the rotation_left go-flag to true |
huismaja | 11:b1ad5267a6bd | 63 | } |
huismaja | 11:b1ad5267a6bd | 64 | |
huismaja | 10:cf579c3eaf01 | 65 | void rotation_left (){ //Function to control the rotation to the left |
huismaja | 10:cf579c3eaf01 | 66 | switch (counter_rotation_left){ //Create a switch statement |
huismaja | 10:cf579c3eaf01 | 67 | case 1: //For activating the rotation to the left |
huismaja | 12:35a81d6c6505 | 68 | Direction_M1 = 0; //The arm will rotate to the left |
huismaja | 10:cf579c3eaf01 | 69 | Speed_M1 = speedM1; //The motor is turned on at speed_rotation rad/sec |
huismaja | 8:9c58ca13076e | 70 | pc.printf("The arm will now rotate to the left with %f rad/sec \n", speedM1); |
huismaja | 12:35a81d6c6505 | 71 | wait(0.1f); |
huismaja | 3:0a4bfcb3f339 | 72 | break; |
huismaja | 10:cf579c3eaf01 | 73 | case 2: //For stopping the rotation to the left |
huismaja | 12:35a81d6c6505 | 74 | Direction_M1 = 0; //The arm will rotate to the left |
huismaja | 10:cf579c3eaf01 | 75 | Speed_M1 = 0; //The motor is turned off |
huismaja | 5:9b5edadc023b | 76 | pc.printf("The arm will now stop rotating to the left \n"); |
huismaja | 12:35a81d6c6505 | 77 | wait(0.1f); |
huismaja | 3:0a4bfcb3f339 | 78 | break; |
huismaja | 3:0a4bfcb3f339 | 79 | } |
huismaja | 11:b1ad5267a6bd | 80 | } |
huismaja | 3:0a4bfcb3f339 | 81 | |
huismaja | 11:b1ad5267a6bd | 82 | void activate_rotation_right (){ //To activate the rotation_right |
huismaja | 11:b1ad5267a6bd | 83 | counter_rotation_right++; //Increase the counter_rotation_right that counts the number of time switch 2 has been pressed |
huismaja | 11:b1ad5267a6bd | 84 | if (counter_rotation_right> 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1 |
huismaja | 11:b1ad5267a6bd | 85 | counter_rotation_right=1; |
huismaja | 3:0a4bfcb3f339 | 86 | } |
huismaja | 11:b1ad5267a6bd | 87 | rotation_right_go = true; //After increasing the counter, set the rotation_right go-flag to true |
huismaja | 3:0a4bfcb3f339 | 88 | } |
huismaja | 3:0a4bfcb3f339 | 89 | |
huismaja | 10:cf579c3eaf01 | 90 | void rotation_right (){ //Function to control the rotation to the left |
huismaja | 10:cf579c3eaf01 | 91 | switch (counter_rotation_right){ //Create a switch statement |
huismaja | 10:cf579c3eaf01 | 92 | case 1: //For activation the rotation to the right |
huismaja | 12:35a81d6c6505 | 93 | Direction_M1 = 1; //The arm will rotate to the right |
huismaja | 8:9c58ca13076e | 94 | Speed_M1 = speedM1; //The motor is turned on at speed_rotation rad/sec |
huismaja | 8:9c58ca13076e | 95 | pc.printf("The arm will now rotate to the right with %f rad/sec \n", speedM1); |
huismaja | 12:35a81d6c6505 | 96 | wait(0.1f); |
huismaja | 3:0a4bfcb3f339 | 97 | break; |
huismaja | 5:9b5edadc023b | 98 | case 2: //For stopping the rotation to the right |
huismaja | 12:35a81d6c6505 | 99 | Direction_M1 = 1; //The arm will rotate to the right |
huismaja | 5:9b5edadc023b | 100 | Speed_M1 = 0; //The motor is turned off |
huismaja | 5:9b5edadc023b | 101 | pc.printf("The arm will now stop rotating to the right \n"); |
huismaja | 12:35a81d6c6505 | 102 | wait(0.1f); |
huismaja | 3:0a4bfcb3f339 | 103 | break; |
huismaja | 3:0a4bfcb3f339 | 104 | } |
huismaja | 3:0a4bfcb3f339 | 105 | } |
huismaja | 3:0a4bfcb3f339 | 106 | |
huismaja | 11:b1ad5267a6bd | 107 | void activate_translation (){ //To activate the translation |
huismaja | 11:b1ad5267a6bd | 108 | counter_translation++; //Increase the counter_translation that counts the number of time switch 3 has been pressed |
huismaja | 11:b1ad5267a6bd | 109 | if (counter_translation > 4){ //Because there are 4 cases in the switch statement, case 5 = case 1 |
huismaja | 11:b1ad5267a6bd | 110 | counter_translation=1; |
huismaja | 3:0a4bfcb3f339 | 111 | } |
huismaja | 11:b1ad5267a6bd | 112 | translation_go = true; //After increasing the counter, set the translation go-flag to true |
huismaja | 3:0a4bfcb3f339 | 113 | } |
huismaja | 3:0a4bfcb3f339 | 114 | |
huismaja | 10:cf579c3eaf01 | 115 | void translation (){ //Function to control the translation |
huismaja | 10:cf579c3eaf01 | 116 | switch (counter_translation){ //Create a switch statement |
huismaja | 8:9c58ca13076e | 117 | case 1: //For activating the elongation of the arm |
huismaja | 8:9c58ca13076e | 118 | Direction_M2 = 1; //The arm will get longer |
huismaja | 10:cf579c3eaf01 | 119 | Speed_M2 = speedM2; //The motor is turned on at speed_translation rad/sec |
huismaja | 5:9b5edadc023b | 120 | pc.printf("The arm will now get longer \n"); |
huismaja | 12:35a81d6c6505 | 121 | wait(0.1f); |
huismaja | 5:9b5edadc023b | 122 | break; |
huismaja | 8:9c58ca13076e | 123 | case 2: //For stopping the elongation of the arm |
huismaja | 8:9c58ca13076e | 124 | Direction_M2 = 1; //The arm will get longer |
huismaja | 8:9c58ca13076e | 125 | Speed_M2 = 0; //The motor is turned off |
huismaja | 5:9b5edadc023b | 126 | pc.printf("The arm will now stop getting longer \n"); |
huismaja | 12:35a81d6c6505 | 127 | wait(0.1f); |
huismaja | 5:9b5edadc023b | 128 | break; |
huismaja | 8:9c58ca13076e | 129 | case 3: //For activating the shortening of the arm |
huismaja | 8:9c58ca13076e | 130 | Direction_M2 = 0; //The arm will get shorter |
huismaja | 10:cf579c3eaf01 | 131 | Speed_M2 = speedM2; //The motor is turned on at speed_translation rad/sec |
huismaja | 5:9b5edadc023b | 132 | pc.printf("The arm will now get shorter \n"); |
huismaja | 12:35a81d6c6505 | 133 | wait(0.1f); |
huismaja | 5:9b5edadc023b | 134 | break; |
huismaja | 8:9c58ca13076e | 135 | case 4: //For stopping the shortening of the arm |
huismaja | 8:9c58ca13076e | 136 | Direction_M2 = 0; //The arm will get shorter |
huismaja | 8:9c58ca13076e | 137 | Speed_M2 = 0; //The motor is turned off |
huismaja | 5:9b5edadc023b | 138 | pc.printf("The arm will now stop getting shorter \n"); |
huismaja | 12:35a81d6c6505 | 139 | wait(0.1f); |
huismaja | 5:9b5edadc023b | 140 | break; |
huismaja | 5:9b5edadc023b | 141 | } |
huismaja | 11:b1ad5267a6bd | 142 | } |
huismaja | 5:9b5edadc023b | 143 | |
huismaja | 11:b1ad5267a6bd | 144 | void activate_gripper (){ //To activate the gripper |
huismaja | 11:b1ad5267a6bd | 145 | counter_gripper++; //Increase the couter_gripper that counts the number of time switch 4 has been pressed |
huismaja | 11:b1ad5267a6bd | 146 | if (counter_gripper> 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1 |
huismaja | 11:b1ad5267a6bd | 147 | counter_gripper=1; |
huismaja | 5:9b5edadc023b | 148 | } |
huismaja | 11:b1ad5267a6bd | 149 | gripper_go = true; //After increasing the counter, set the gripper go-flag to true |
huismaja | 5:9b5edadc023b | 150 | } |
huismaja | 5:9b5edadc023b | 151 | |
huismaja | 10:cf579c3eaf01 | 152 | void gripper (){ //Function to control the gripper |
huismaja | 10:cf579c3eaf01 | 153 | switch (counter_gripper){ //Create a switch statement |
huismaja | 10:cf579c3eaf01 | 154 | case 1: //For closing the gripper |
huismaja | 10:cf579c3eaf01 | 155 | gripper_servo = 0; //The gripper is now closed |
huismaja | 5:9b5edadc023b | 156 | pc.printf("The gripper will now close \n"); |
huismaja | 12:35a81d6c6505 | 157 | wait(0.1f); |
huismaja | 4:84bd5ead83f9 | 158 | break; |
huismaja | 10:cf579c3eaf01 | 159 | case 2: //For opening the gripper |
huismaja | 10:cf579c3eaf01 | 160 | gripper_servo = 1; //The gripper is now open |
huismaja | 5:9b5edadc023b | 161 | pc.printf("The gripper will now open \n"); |
huismaja | 12:35a81d6c6505 | 162 | wait(0.1f); |
huismaja | 4:84bd5ead83f9 | 163 | break; |
huismaja | 4:84bd5ead83f9 | 164 | } |
huismaja | 4:84bd5ead83f9 | 165 | } |
huismaja | 4:84bd5ead83f9 | 166 | |
huismaja | 0:6c8444d06e97 | 167 | int main(){ |
huismaja | 10:cf579c3eaf01 | 168 | pc.baud(115200); //Set the boud rate for serial communication |
huismaja | 10:cf579c3eaf01 | 169 | pc.printf("RESET \n"); //Print "RESET" |
huismaja | 1:0d55a4bf2269 | 170 | |
huismaja | 5:9b5edadc023b | 171 | Direction_M1 = 1; //The arm will initially get longer |
huismaja | 5:9b5edadc023b | 172 | Speed_M1 = 0; //The first motor is initially turned off |
huismaja | 5:9b5edadc023b | 173 | Direction_M2 = 255; //The arm will initially turn left |
huismaja | 6:98121d2d76a6 | 174 | Speed_M2 = 0; //The second motor is initially turned off |
huismaja | 5:9b5edadc023b | 175 | gripper_servo = 1; //The gripper is initially open |
huismaja | 10:cf579c3eaf01 | 176 | encoder_M1.reset(); //Reset the encoder for motor 1 |
huismaja | 10:cf579c3eaf01 | 177 | encoder_M2.reset(); //Reset the encoder for motor 2 |
huismaja | 1:0d55a4bf2269 | 178 | |
huismaja | 10:cf579c3eaf01 | 179 | 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 | 180 | 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 | 181 | |
huismaja | 11:b1ad5267a6bd | 182 | Switch_1.rise(&activate_rotation_left); //Use switch_1 to activate the rotation_left go-flag |
huismaja | 11:b1ad5267a6bd | 183 | Switch_2.rise(&activate_rotation_right); //Use switch_2 to activate the counter_rotation_right go-flag |
huismaja | 11:b1ad5267a6bd | 184 | Switch_3.rise(&activate_translation); //Use switch_3 to activate the counter_translation go-flag |
huismaja | 11:b1ad5267a6bd | 185 | Switch_4.rise(&activate_gripper); //Use switch_4 to activate the counter_gripper go-flag |
huismaja | 5:9b5edadc023b | 186 | |
huismaja | 11:b1ad5267a6bd | 187 | while (true){ |
huismaja | 12:35a81d6c6505 | 188 | if (rotation_left_go == true) { //If the rotation_left go-flag is true |
huismaja | 12:35a81d6c6505 | 189 | rotation_left_go = false; //Set the rotation_left go-flag to false |
huismaja | 12:35a81d6c6505 | 190 | rotation_left(); //Execute the rotation_left function |
huismaja | 11:b1ad5267a6bd | 191 | } |
huismaja | 12:35a81d6c6505 | 192 | if (rotation_right_go == true) { //If the rotation_right go-flag is true |
huismaja | 12:35a81d6c6505 | 193 | rotation_right_go = false; //Set the rotation_right go-flag to false |
huismaja | 12:35a81d6c6505 | 194 | rotation_right(); //Execute the rotation_right function |
huismaja | 11:b1ad5267a6bd | 195 | } |
huismaja | 12:35a81d6c6505 | 196 | if (translation_go == true) { //If the translation go-flag is true |
huismaja | 12:35a81d6c6505 | 197 | translation_go = false; //Set the translation go-flag to false |
huismaja | 12:35a81d6c6505 | 198 | translation(); //Execute the translation function |
huismaja | 11:b1ad5267a6bd | 199 | } |
huismaja | 12:35a81d6c6505 | 200 | if (gripper_go == true) { //If the gripper go-flag is true |
huismaja | 12:35a81d6c6505 | 201 | gripper_go = false; //Set the gripper go-flag to false |
huismaja | 12:35a81d6c6505 | 202 | gripper(); //Execute the gripper function |
huismaja | 11:b1ad5267a6bd | 203 | } |
huismaja | 11:b1ad5267a6bd | 204 | } |
huismaja | 0:6c8444d06e97 | 205 | } |