Script for controlling 2 DC-motors and a gripper-servo using buttons

Dependencies:   MODSERIAL QEI Servo mbed

Committer:
huismaja
Date:
Fri Oct 14 18:00:27 2016 +0000
Revision:
10:cf579c3eaf01
Parent:
9:cca4d4084775
Child:
11:b1ad5267a6bd
Comments added (working script for controlling 2 DC-motor and 1 gripper with buttons)

Who changed what in which revision?

UserRevisionLine numberNew 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 5:9b5edadc023b 12 DigitalOut Direction_M1(D4); //To control the rotation direction of the arm
huismaja 6:98121d2d76a6 13 PwmOut Speed_M1(D5); //To control the rotation speed of the arm
huismaja 6:98121d2d76a6 14 PwmOut Speed_M2(D6); //To control the translation direction of the arm
huismaja 5:9b5edadc023b 15 DigitalOut Direction_M2(D7); //To control the translation speed of the arm
huismaja 1:0d55a4bf2269 16
huismaja 10:cf579c3eaf01 17 Servo gripper_servo(D13); //To control the gripper
huismaja 0:6c8444d06e97 18
huismaja 10:cf579c3eaf01 19 InterruptIn Switch_1(SW2); //Switch 1 to control the rotation to the left
huismaja 10:cf579c3eaf01 20 InterruptIn Switch_2(SW3); //Switch 2 to control the rotation to the right
huismaja 10:cf579c3eaf01 21 InterruptIn Switch_3(D2); //Switch 3 to control the translation of the arm
huismaja 10:cf579c3eaf01 22 InterruptIn Switch_4(D3); //Switch 4 to control the gripper
huismaja 2:b20570f160c6 23
huismaja 5:9b5edadc023b 24 int counter_rotation_left=0; //To count the number of times the rotation_left switch (switch_1) has been pushed
huismaja 5:9b5edadc023b 25 int counter_rotation_right=0; //To count the number of times the rotation_right switch (switch_2) has been pushed
huismaja 5:9b5edadc023b 26 int counter_translation=0; //To count the number of times the translation switch (switch_3) has been pushed
huismaja 5:9b5edadc023b 27 int counter_gripper=0; //To count the number of times the gripper switch (switch_4) has been pushed
huismaja 5:9b5edadc023b 28
huismaja 5:9b5edadc023b 29 MODSERIAL pc(USBTX, USBRX); //To make connection with the PC
huismaja 0:6c8444d06e97 30
huismaja 10:cf579c3eaf01 31 const double pi = 3.1415926535897; //Declare the value of pi
huismaja 9:cca4d4084775 32
huismaja 10:cf579c3eaf01 33 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 34 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 35 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 36 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 37
huismaja 10:cf579c3eaf01 38 void read_position_M1(){ //Function to read the position of motor 1
huismaja 10:cf579c3eaf01 39 int pulses_M1 = encoder_M1.getPulses(); //Read the encoder data and store it in pulses_M1
huismaja 10:cf579c3eaf01 40 float angle_M1 = float(pulses_M1)/4200*2.0*pi; //Calculate the angle that corresponds with the measured encoder pulses
huismaja 10:cf579c3eaf01 41 // pc.printf("%i \t%f \t", pulses_M1, angle_M1);
huismaja 9:cca4d4084775 42 }
huismaja 9:cca4d4084775 43
huismaja 10:cf579c3eaf01 44 void read_position_M2(){ //Function to read the position of motor 2
huismaja 10:cf579c3eaf01 45 int pulses_M2 = encoder_M2.getPulses(); //Read the encoder data and store it in pulses_M2
huismaja 10:cf579c3eaf01 46 float angle_M2 = float(pulses_M2)/4200*2.0*pi; //Calculate the angle that corresponds with the measured encoder pulses
huismaja 10:cf579c3eaf01 47 // pc.printf("%i \t%f \n", pulses_M2, angle_M2);
huismaja 9:cca4d4084775 48 }
huismaja 9:cca4d4084775 49
huismaja 10:cf579c3eaf01 50 void rotation_left (){ //Function to control the rotation to the left
huismaja 10:cf579c3eaf01 51 switch (counter_rotation_left){ //Create a switch statement
huismaja 10:cf579c3eaf01 52 case 1: //For activating the rotation to the left
huismaja 10:cf579c3eaf01 53 Direction_M1 = 1; //The arm will rotate to the left
huismaja 10:cf579c3eaf01 54 Speed_M1 = speedM1; //The motor is turned on at speed_rotation rad/sec
huismaja 8:9c58ca13076e 55 pc.printf("The arm will now rotate to the left with %f rad/sec \n", speedM1);
huismaja 3:0a4bfcb3f339 56 wait(0.5f);
huismaja 3:0a4bfcb3f339 57 break;
huismaja 10:cf579c3eaf01 58 case 2: //For stopping the rotation to the left
huismaja 10:cf579c3eaf01 59 Direction_M1 = 1; //The arm will rotate to the left
huismaja 10:cf579c3eaf01 60 Speed_M1 = 0; //The motor is turned off
huismaja 5:9b5edadc023b 61 pc.printf("The arm will now stop rotating to the left \n");
huismaja 3:0a4bfcb3f339 62 wait(0.5f);
huismaja 3:0a4bfcb3f339 63 break;
huismaja 3:0a4bfcb3f339 64 }
huismaja 3:0a4bfcb3f339 65 }
huismaja 3:0a4bfcb3f339 66
huismaja 10:cf579c3eaf01 67 void switch_counter_rotation_left (){ //To count the number of times the rotation_left switch (switch_1) has been pushed
huismaja 10:cf579c3eaf01 68 counter_rotation_left++; //Increase the counter_rotation_left
huismaja 10:cf579c3eaf01 69 if (counter_rotation_left > 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1 etc.
huismaja 3:0a4bfcb3f339 70 counter_rotation_left=1;
huismaja 3:0a4bfcb3f339 71 }
huismaja 10:cf579c3eaf01 72 rotation_left(); //After increasing the counter, execute the rotation_left function
huismaja 3:0a4bfcb3f339 73 }
huismaja 3:0a4bfcb3f339 74
huismaja 10:cf579c3eaf01 75 void rotation_right (){ //Function to control the rotation to the left
huismaja 10:cf579c3eaf01 76 switch (counter_rotation_right){ //Create a switch statement
huismaja 10:cf579c3eaf01 77 case 1: //For activation the rotation to the right
huismaja 10:cf579c3eaf01 78 Direction_M1 = 0; //The arm will rotate to the right
huismaja 8:9c58ca13076e 79 Speed_M1 = speedM1; //The motor is turned on at speed_rotation rad/sec
huismaja 8:9c58ca13076e 80 pc.printf("The arm will now rotate to the right with %f rad/sec \n", speedM1);
huismaja 3:0a4bfcb3f339 81 wait(0.5f);
huismaja 3:0a4bfcb3f339 82 break;
huismaja 5:9b5edadc023b 83 case 2: //For stopping the rotation to the right
huismaja 5:9b5edadc023b 84 Direction_M1 = 0; //The arm will rotate to the right
huismaja 5:9b5edadc023b 85 Speed_M1 = 0; //The motor is turned off
huismaja 5:9b5edadc023b 86 pc.printf("The arm will now stop rotating to the right \n");
huismaja 3:0a4bfcb3f339 87 wait(0.5f);
huismaja 3:0a4bfcb3f339 88 break;
huismaja 3:0a4bfcb3f339 89 }
huismaja 3:0a4bfcb3f339 90 }
huismaja 3:0a4bfcb3f339 91
huismaja 5:9b5edadc023b 92 void switch_counter_rotation_right (){ //To count the number of times the rotation_right switch (switch_2) has been pushed
huismaja 10:cf579c3eaf01 93 counter_rotation_right++; //Increase the counter_rotation_right
huismaja 10:cf579c3eaf01 94 if (counter_rotation_right> 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1
huismaja 3:0a4bfcb3f339 95 counter_rotation_right=1;
huismaja 3:0a4bfcb3f339 96 }
huismaja 10:cf579c3eaf01 97 rotation_right(); //After increasing the counter, execute the rotation_right function
huismaja 3:0a4bfcb3f339 98 }
huismaja 3:0a4bfcb3f339 99
huismaja 10:cf579c3eaf01 100 void translation (){ //Function to control the translation
huismaja 10:cf579c3eaf01 101 switch (counter_translation){ //Create a switch statement
huismaja 8:9c58ca13076e 102 case 1: //For activating the elongation of the arm
huismaja 8:9c58ca13076e 103 Direction_M2 = 1; //The arm will get longer
huismaja 10:cf579c3eaf01 104 Speed_M2 = speedM2; //The motor is turned on at speed_translation rad/sec
huismaja 5:9b5edadc023b 105 pc.printf("The arm will now get longer \n");
huismaja 5:9b5edadc023b 106 wait(0.5f);
huismaja 5:9b5edadc023b 107 break;
huismaja 8:9c58ca13076e 108 case 2: //For stopping the elongation of the arm
huismaja 8:9c58ca13076e 109 Direction_M2 = 1; //The arm will get longer
huismaja 8:9c58ca13076e 110 Speed_M2 = 0; //The motor is turned off
huismaja 5:9b5edadc023b 111 pc.printf("The arm will now stop getting longer \n");
huismaja 5:9b5edadc023b 112 wait(0.5f);
huismaja 5:9b5edadc023b 113 break;
huismaja 8:9c58ca13076e 114 case 3: //For activating the shortening of the arm
huismaja 8:9c58ca13076e 115 Direction_M2 = 0; //The arm will get shorter
huismaja 10:cf579c3eaf01 116 Speed_M2 = speedM2; //The motor is turned on at speed_translation rad/sec
huismaja 5:9b5edadc023b 117 pc.printf("The arm will now get shorter \n");
huismaja 5:9b5edadc023b 118 wait(0.5f);
huismaja 5:9b5edadc023b 119 break;
huismaja 8:9c58ca13076e 120 case 4: //For stopping the shortening of the arm
huismaja 8:9c58ca13076e 121 Direction_M2 = 0; //The arm will get shorter
huismaja 8:9c58ca13076e 122 Speed_M2 = 0; //The motor is turned off
huismaja 5:9b5edadc023b 123 pc.printf("The arm will now stop getting shorter \n");
huismaja 5:9b5edadc023b 124 wait(0.5f);
huismaja 5:9b5edadc023b 125 break;
huismaja 5:9b5edadc023b 126 }
huismaja 5:9b5edadc023b 127 }
huismaja 5:9b5edadc023b 128
huismaja 5:9b5edadc023b 129 void switch_counter_translation (){ //To count the number of times the translation switch (switch_3) has been pushed
huismaja 10:cf579c3eaf01 130 counter_translation++; //Increase the counter_translation
huismaja 10:cf579c3eaf01 131 if (counter_translation > 4){ //Because there are 4 cases in the switch statement, case 5 = case 1
huismaja 5:9b5edadc023b 132 counter_translation=1;
huismaja 5:9b5edadc023b 133 }
huismaja 10:cf579c3eaf01 134 translation(); //After increasing the counter, execute the translation function
huismaja 5:9b5edadc023b 135 }
huismaja 5:9b5edadc023b 136
huismaja 10:cf579c3eaf01 137 void gripper (){ //Function to control the gripper
huismaja 10:cf579c3eaf01 138 switch (counter_gripper){ //Create a switch statement
huismaja 10:cf579c3eaf01 139 case 1: //For closing the gripper
huismaja 10:cf579c3eaf01 140 gripper_servo = 0; //The gripper is now closed
huismaja 5:9b5edadc023b 141 pc.printf("The gripper will now close \n");
huismaja 4:84bd5ead83f9 142 wait(0.5f);
huismaja 4:84bd5ead83f9 143 break;
huismaja 10:cf579c3eaf01 144 case 2: //For opening the gripper
huismaja 10:cf579c3eaf01 145 gripper_servo = 1; //The gripper is now open
huismaja 5:9b5edadc023b 146 pc.printf("The gripper will now open \n");
huismaja 4:84bd5ead83f9 147 wait(0.5f);
huismaja 4:84bd5ead83f9 148 break;
huismaja 4:84bd5ead83f9 149 }
huismaja 4:84bd5ead83f9 150 }
huismaja 4:84bd5ead83f9 151
huismaja 5:9b5edadc023b 152 void switch_counter_gripper (){ //To count the number of times the gripper switch (switch_4) has been pushed
huismaja 10:cf579c3eaf01 153 counter_gripper++; //Increase the couter_gripper
huismaja 10:cf579c3eaf01 154 if (counter_gripper> 2){ //Because there are only 2 cases in the switch statement, case 3 = case 1
huismaja 4:84bd5ead83f9 155 counter_gripper=1;
huismaja 4:84bd5ead83f9 156 }
huismaja 10:cf579c3eaf01 157 gripper(); //After increasing the counter, execute the gripper function
huismaja 4:84bd5ead83f9 158 }
huismaja 4:84bd5ead83f9 159
huismaja 0:6c8444d06e97 160 int main(){
huismaja 10:cf579c3eaf01 161 pc.baud(115200); //Set the boud rate for serial communication
huismaja 10:cf579c3eaf01 162 pc.printf("RESET \n"); //Print "RESET"
huismaja 1:0d55a4bf2269 163
huismaja 5:9b5edadc023b 164 Direction_M1 = 1; //The arm will initially get longer
huismaja 5:9b5edadc023b 165 Speed_M1 = 0; //The first motor is initially turned off
huismaja 5:9b5edadc023b 166 Direction_M2 = 255; //The arm will initially turn left
huismaja 6:98121d2d76a6 167 Speed_M2 = 0; //The second motor is initially turned off
huismaja 5:9b5edadc023b 168 gripper_servo = 1; //The gripper is initially open
huismaja 10:cf579c3eaf01 169 encoder_M1.reset(); //Reset the encoder for motor 1
huismaja 10:cf579c3eaf01 170 encoder_M2.reset(); //Reset the encoder for motor 2
huismaja 1:0d55a4bf2269 171
huismaja 10:cf579c3eaf01 172 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 173 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 174
huismaja 10:cf579c3eaf01 175 Switch_1.rise(&switch_counter_rotation_left); //Connect switch_1 to the counter_rotation_left
huismaja 10:cf579c3eaf01 176 Switch_2.rise(&switch_counter_rotation_right); //Connect switch_2 to the counter_rotation_right
huismaja 10:cf579c3eaf01 177 Switch_3.rise(&switch_counter_translation); //Connect switch_3 to the counter_translation
huismaja 10:cf579c3eaf01 178 Switch_4.rise(&switch_counter_gripper); //Connect switch_4 to the counter_gripper
huismaja 5:9b5edadc023b 179
huismaja 5:9b5edadc023b 180 while (true);
huismaja 0:6c8444d06e97 181 }