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

Dependencies:   MODSERIAL QEI Servo mbed

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