Script for controlling 2 DC-motors and a gripper-servo using buttons
Dependencies: MODSERIAL QEI Servo mbed
Diff: main.cpp
- Revision:
- 11:b1ad5267a6bd
- Parent:
- 10:cf579c3eaf01
- Child:
- 12:35a81d6c6505
--- a/main.cpp Fri Oct 14 18:00:27 2016 +0000 +++ b/main.cpp Mon Oct 17 13:01:00 2016 +0000 @@ -13,7 +13,6 @@ PwmOut Speed_M1(D5); //To control the rotation speed of the arm PwmOut Speed_M2(D6); //To control the translation direction of the arm DigitalOut Direction_M2(D7); //To control the translation speed of the arm - Servo gripper_servo(D13); //To control the gripper InterruptIn Switch_1(SW2); //Switch 1 to control the rotation to the left @@ -26,9 +25,14 @@ int counter_translation=0; //To count the number of times the translation switch (switch_3) has been pushed int counter_gripper=0; //To count the number of times the gripper switch (switch_4) has been pushed -MODSERIAL pc(USBTX, USBRX); //To make connection with the PC +volatile bool rotation_left_go = false; //Create a go-flag for the rotation_left and set it to false +volatile bool rotation_right_go = false; //Create a go-flag for the rotation_right and set it to false +volatile bool translation_go = false; //Create a go-flag for the translation and set it to false +volatile bool gripper_go = false; //Create a go-flag for the gripper and set it to false -const double pi = 3.1415926535897; //Declare the value of pi +MODSERIAL pc(USBTX, USBRX); //Make a connection with the PC + +const double pi = 3.1415926535897; //Declare the value of pi double speed_rotation=pi/5; //Set the rotation speed in rad/sec -> NOTE: this has to be below 8.4 rad/sec double speed_translation=pi/5; //Set the translation speed in rad/sec -> NOTE: this has to be below 8.4 rad/sec @@ -47,6 +51,14 @@ // pc.printf("%i \t%f \n", pulses_M2, angle_M2); } +void activate_rotation_left (){ //To activate the rotation_left + counter_rotation_left++; //Increase the counter_rotation_left that counts the number of time switch 1 has been pressed + if (counter_rotation_left > 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1 etc. + counter_rotation_left=1; + } + rotation_left_go = true; //After increasing the counter, set the rotation_left go-flag to true +} + void rotation_left (){ //Function to control the rotation to the left switch (counter_rotation_left){ //Create a switch statement case 1: //For activating the rotation to the left @@ -62,14 +74,14 @@ wait(0.5f); break; } -} +} -void switch_counter_rotation_left (){ //To count the number of times the rotation_left switch (switch_1) has been pushed - counter_rotation_left++; //Increase the counter_rotation_left - if (counter_rotation_left > 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1 etc. - counter_rotation_left=1; +void activate_rotation_right (){ //To activate the rotation_right + counter_rotation_right++; //Increase the counter_rotation_right that counts the number of time switch 2 has been pressed + if (counter_rotation_right> 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1 + counter_rotation_right=1; } - rotation_left(); //After increasing the counter, execute the rotation_left function + rotation_right_go = true; //After increasing the counter, set the rotation_right go-flag to true } void rotation_right (){ //Function to control the rotation to the left @@ -89,12 +101,12 @@ } } -void switch_counter_rotation_right (){ //To count the number of times the rotation_right switch (switch_2) has been pushed - counter_rotation_right++; //Increase the counter_rotation_right - if (counter_rotation_right> 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1 - counter_rotation_right=1; +void activate_translation (){ //To activate the translation + counter_translation++; //Increase the counter_translation that counts the number of time switch 3 has been pressed + if (counter_translation > 4){ //Because there are 4 cases in the switch statement, case 5 = case 1 + counter_translation=1; } - rotation_right(); //After increasing the counter, execute the rotation_right function + translation_go = true; //After increasing the counter, set the translation go-flag to true } void translation (){ //Function to control the translation @@ -124,14 +136,14 @@ wait(0.5f); break; } -} +} -void switch_counter_translation (){ //To count the number of times the translation switch (switch_3) has been pushed - counter_translation++; //Increase the counter_translation - if (counter_translation > 4){ //Because there are 4 cases in the switch statement, case 5 = case 1 - counter_translation=1; +void activate_gripper (){ //To activate the gripper + counter_gripper++; //Increase the couter_gripper that counts the number of time switch 4 has been pressed + if (counter_gripper> 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1 + counter_gripper=1; } - translation(); //After increasing the counter, execute the translation function + gripper_go = true; //After increasing the counter, set the gripper go-flag to true } void gripper (){ //Function to control the gripper @@ -149,14 +161,6 @@ } } -void switch_counter_gripper (){ //To count the number of times the gripper switch (switch_4) has been pushed - counter_gripper++; //Increase the couter_gripper - if (counter_gripper> 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1 - counter_gripper=1; - } - gripper(); //After increasing the counter, execute the gripper function -} - int main(){ pc.baud(115200); //Set the boud rate for serial communication pc.printf("RESET \n"); //Print "RESET" @@ -172,10 +176,27 @@ encoder_M1_ticker.attach(&read_position_M1,0.01); //Connect the encoder_M1_ticker to the read_position_M1 function and execute at 100Hz encoder_M2_ticker.attach(&read_position_M2,0.01); //Connect the encoder_M2_ticker to the read_position_M2 function and execute at 100Hz - Switch_1.rise(&switch_counter_rotation_left); //Connect switch_1 to the counter_rotation_left - Switch_2.rise(&switch_counter_rotation_right); //Connect switch_2 to the counter_rotation_right - Switch_3.rise(&switch_counter_translation); //Connect switch_3 to the counter_translation - Switch_4.rise(&switch_counter_gripper); //Connect switch_4 to the counter_gripper + Switch_1.rise(&activate_rotation_left); //Use switch_1 to activate the rotation_left go-flag + Switch_2.rise(&activate_rotation_right); //Use switch_2 to activate the counter_rotation_right go-flag + Switch_3.rise(&activate_translation); //Use switch_3 to activate the counter_translation go-flag + Switch_4.rise(&activate_gripper); //Use switch_4 to activate the counter_gripper go-flag - while (true); + while (true){ + if (rotation_left_go == true){ //If the rotation_left go-flag is true + rotation_left_go = false; //Set the rotation_left go-flag to false + rotation_left(); //Execute the rotation_left function + } + if (rotation_right_go == true){ //If the rotation_right go-flag is true + rotation_right_go = false; //Set the rotation_right go-flag to false + rotation_right(); //Execute the rotation_right function + } + if (translation_go == true){ //If the translation go-flag is true + translation_go = false; //Set the translation go-flag to false + translation(); //Execute the translation function + } + if (gripper_go == true){ //If the gripper go-flag is true + gripper_go = false; //Set the gripper go-flag to false + gripper(); //Execute the gripper function + } + } } \ No newline at end of file