Script for controlling 2 DC-motors and a gripper-servo using buttons
Dependencies: MODSERIAL QEI Servo mbed
main.cpp
00001 #include "mbed.h" 00002 #include "MODSERIAL.h" 00003 #include "Servo.h" 00004 #include "QEI.h" 00005 00006 QEI encoder_M1 (D9, D10, NC, 8400); //Define an encoder for motor 1 called encoder_M1 00007 QEI encoder_M2 (D11, D12, NC, 8400); //Define an encoder for motor 2 called encoder_M2 00008 00009 Ticker encoder_M1_ticker; //Create a ticker for reading the encoder data of encoder_M1 00010 Ticker encoder_M2_ticker; //Create a ticker for reading the encoder data of encoder_M2 00011 00012 DigitalOut Direction_M2(D4); //To control the rotation direction of the arm 00013 PwmOut Speed_M2(D5); //To control the rotation speed of the arm 00014 PwmOut Speed_M1(D6); //To control the translation direction of the arm 00015 DigitalOut Direction_M1(D7); //To control the translation speed of the arm 00016 Servo gripper_servo(D13); //To control the gripper 00017 00018 InterruptIn Switch_1(SW3); //Switch 1 to control the rotation to the left 00019 InterruptIn Switch_2(SW2); //Switch 2 to control the rotation to the right 00020 InterruptIn Switch_3(D2); //Switch 3 to control the translation of the arm 00021 InterruptIn Switch_4(D3); //Switch 4 to control the gripper 00022 00023 Ticker check_goflags_ticker; //Create a ticker for checking if the go-flags are set true 00024 00025 int counter_rotation_left=0; //To count the number of times the rotation_left switch (switch_1) has been pushed 00026 int counter_rotation_right=0; //To count the number of times the rotation_right switch (switch_2) has been pushed 00027 int counter_translation=0; //To count the number of times the translation switch (switch_3) has been pushed 00028 int counter_gripper=0; //To count the number of times the gripper switch (switch_4) has been pushed 00029 00030 volatile bool rotation_left_go = false; //Create a go-flag for the rotation_left and set it to false 00031 volatile bool rotation_right_go = false; //Create a go-flag for the rotation_right and set it to false 00032 volatile bool translation_go = false; //Create a go-flag for the translation and set it to false 00033 volatile bool gripper_go = false; //Create a go-flag for the gripper and set it to false 00034 00035 MODSERIAL pc(USBTX, USBRX); //Make a connection with the PC 00036 00037 const double pi = 3.1415926535897; //Declare the value of pi 00038 00039 double speed_rotation=pi/5; //Set the rotation speed in rad/sec -> NOTE: this has to be below 8.4 rad/sec 00040 double speed_translation=pi/5; //Set the translation speed in rad/sec -> NOTE: this has to be below 8.4 rad/sec 00041 double speedM1=speed_rotation/8.4; //Map the rotation speed from (0-8.4) to (0-1) by dividing by 8.4 00042 double speedM2=speed_translation/8.4; //Map the translation speed from (0-8.4) to (0-1) by dividing by 8.4 00043 00044 float angle_M1=0; //The measured angle of motor 1 is initially zero 00045 float angle_M2=0; //The measured angle of motor 2 is initially zero 00046 00047 void read_position_M1(){ //Function to read the position of motor 1 00048 int pulses_M1 = -encoder_M1.getPulses(); //Read the encoder data and store it in pulses_M1 00049 angle_M1 = float(pulses_M1)/4200*2.0*pi; //Calculate the angle that corresponds with the measured encoder pulses 00050 // pc.printf("%i \t%f \t", pulses_M1, angle_M1); 00051 } 00052 00053 void read_position_M2(){ //Function to read the position of motor 2 00054 int pulses_M2 = -encoder_M2.getPulses(); //Read the encoder data and store it in pulses_M2 00055 angle_M2 = float(pulses_M2)/4200*2.0*pi; //Calculate the angle that corresponds with the measured encoder pulses 00056 // pc.printf("%i \t%f \n", pulses_M2, angle_M2); 00057 } 00058 00059 void activate_rotation_left (){ //To activate the rotation_left 00060 counter_rotation_left++; //Increase the counter_rotation_left that counts the number of time switch 1 has been pressed 00061 if (counter_rotation_left > 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1 etc. 00062 counter_rotation_left=1; 00063 } 00064 rotation_left_go = true; //After increasing the counter, set the rotation_left go-flag to true 00065 } 00066 00067 void rotation_left (){ //Function to control the rotation to the left 00068 switch (counter_rotation_left){ //Create a switch statement 00069 case 1: //For activating the rotation to the left 00070 Direction_M1 = 1; //The arm will rotate to the left 00071 Speed_M1 = speedM1; //The motor is turned on at speed_rotation rad/sec 00072 pc.printf("The arm will now rotate to the left with %f rad/sec \n", speedM1); 00073 wait(0.1f); 00074 break; 00075 case 2: //For stopping the rotation to the left 00076 Direction_M1 = 1; //The arm will rotate to the left 00077 Speed_M1 = 0; //The motor is turned off 00078 pc.printf("The arm will now stop rotating to the left \n"); 00079 wait(0.1f); 00080 break; 00081 } 00082 } 00083 00084 void activate_rotation_right (){ //To activate the rotation_right 00085 counter_rotation_right++; //Increase the counter_rotation_right that counts the number of time switch 2 has been pressed 00086 if (counter_rotation_right> 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1 00087 counter_rotation_right=1; 00088 } 00089 rotation_right_go = true; //After increasing the counter, set the rotation_right go-flag to true 00090 } 00091 00092 void rotation_right (){ //Function to control the rotation to the left 00093 switch (counter_rotation_right){ //Create a switch statement 00094 case 1: //For activation the rotation to the right 00095 Direction_M1 = 0; //The arm will rotate to the right 00096 Speed_M1 = speedM1; //The motor is turned on at speed_rotation rad/sec 00097 pc.printf("The arm will now rotate to the right with %f rad/sec \n", speedM1); 00098 wait(0.1f); 00099 break; 00100 case 2: //For stopping the rotation to the right 00101 Direction_M1 = 0; //The arm will rotate to the right 00102 Speed_M1 = 0; //The motor is turned off 00103 pc.printf("The arm will now stop rotating to the right \n"); 00104 wait(0.1f); 00105 break; 00106 } 00107 } 00108 00109 void activate_translation (){ //To activate the translation 00110 counter_translation++; //Increase the counter_translation that counts the number of time switch 3 has been pressed 00111 if (counter_translation > 4){ //Because there are 4 cases in the switch statement, case 5 = case 1 00112 counter_translation=1; 00113 } 00114 translation_go = true; //After increasing the counter, set the translation go-flag to true 00115 } 00116 00117 void translation (){ //Function to control the translation 00118 switch (counter_translation){ //Create a switch statement 00119 case 1: //For activating the elongation of the arm 00120 Direction_M2 = 1; //The arm will get longer 00121 Speed_M2 = speedM2; //The motor is turned on at speed_translation rad/sec 00122 pc.printf("The arm will now get longer \n"); 00123 wait(0.1f); 00124 break; 00125 case 2: //For stopping the elongation of the arm 00126 Direction_M2 = 1; //The arm will get longer 00127 Speed_M2 = 0; //The motor is turned off 00128 pc.printf("The arm will now stop getting longer \n"); 00129 wait(0.1f); 00130 break; 00131 case 3: //For activating the shortening of the arm 00132 Direction_M2 = 0; //The arm will get shorter 00133 Speed_M2 = speedM2; //The motor is turned on at speed_translation rad/sec 00134 pc.printf("The arm will now get shorter \n"); 00135 wait(0.1f); 00136 break; 00137 case 4: //For stopping the shortening of the arm 00138 Direction_M2 = 0; //The arm will get shorter 00139 Speed_M2 = 0; //The motor is turned off 00140 pc.printf("The arm will now stop getting shorter \n"); 00141 wait(0.1f); 00142 break; 00143 } 00144 } 00145 00146 void activate_gripper (){ //To activate the gripper 00147 counter_gripper++; //Increase the couter_gripper that counts the number of time switch 4 has been pressed 00148 if (counter_gripper> 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1 00149 counter_gripper=1; 00150 } 00151 gripper_go = true; //After increasing the counter, set the gripper go-flag to true 00152 } 00153 00154 void gripper (){ //Function to control the gripper 00155 switch (counter_gripper){ //Create a switch statement 00156 case 1: //For closing the gripper 00157 gripper_servo = 0; //The gripper is now closed 00158 pc.printf("The gripper will now close \n"); 00159 wait(0.1f); 00160 break; 00161 case 2: //For opening the gripper 00162 gripper_servo = 0.3; //The gripper is now open 00163 pc.printf("The gripper will now open \n"); 00164 wait(0.1f); 00165 break; 00166 } 00167 } 00168 00169 void check_goflags (){ //Function to check if the go-flags are activated 00170 if (rotation_left_go == true) { //If the rotation_left go-flag is true 00171 rotation_left_go = false; //Set the rotation_left go-flag to false 00172 rotation_left(); //Execute the rotation_left function 00173 } 00174 if (rotation_right_go == true) { //If the rotation_right go-flag is true 00175 rotation_right_go = false; //Set the rotation_right go-flag to false 00176 rotation_right(); //Execute the rotation_right function 00177 } 00178 if (translation_go == true) { //If the translation go-flag is true 00179 translation_go = false; //Set the translation go-flag to false 00180 translation(); //Execute the translation function 00181 } 00182 if (gripper_go == true) { //If the gripper go-flag is true 00183 gripper_go = false; //Set the gripper go-flag to false 00184 gripper(); //Execute the gripper function 00185 } 00186 } 00187 00188 int main(){ 00189 pc.baud(115200); //Set the boud rate for serial communication 00190 pc.printf("RESET \n"); //Print "RESET" 00191 00192 Direction_M1 = 1; //The arm will initially get longer 00193 Speed_M1 = 0; //The first motor is initially turned off 00194 Direction_M2 = 255; //The arm will initially turn left 00195 Speed_M2 = 0; //The second motor is initially turned off 00196 gripper_servo = 0.3; //The gripper is initially open 00197 encoder_M1.reset(); //Reset the encoder for motor 1 00198 encoder_M2.reset(); //Reset the encoder for motor 2 00199 00200 encoder_M1_ticker.attach(&read_position_M1,0.01); //Connect the encoder_M1_ticker to the read_position_M1 function and execute at 100Hz 00201 encoder_M2_ticker.attach(&read_position_M2,0.01); //Connect the encoder_M2_ticker to the read_position_M2 function and execute at 100Hz 00202 00203 Switch_1.rise(&activate_rotation_left); //Use switch_1 to activate the counter_rotation_left go-flag 00204 Switch_2.rise(&activate_rotation_right); //Use switch_2 to activate the counter_rotation_right go-flag 00205 Switch_3.rise(&activate_translation); //Use switch_3 to activate the counter_translation go-flag 00206 Switch_4.rise(&activate_gripper); //Use switch_4 to activate the counter_gripper go-flag 00207 00208 check_goflags_ticker.attach(&check_goflags, 0.01); //Connect the check_goflags_ticker to the check_goflags 00209 00210 while (true){} 00211 }
Generated on Sun Jul 24 2022 01:14:08 by 1.7.2