Script for controlling 2 DC-motors and a gripper-servo using buttons
Dependencies: MODSERIAL QEI Servo mbed
Diff: main.cpp
- Revision:
- 6:98121d2d76a6
- Parent:
- 5:9b5edadc023b
- Child:
- 7:9a3809a9ab3e
diff -r 9b5edadc023b -r 98121d2d76a6 main.cpp --- a/main.cpp Sat Oct 08 15:34:37 2016 +0000 +++ b/main.cpp Mon Oct 10 11:03:35 2016 +0000 @@ -2,12 +2,17 @@ #include "MODSERIAL.h" #include "Servo.h" +//DigitalIn Encoder_M1A(D9); +//DigitalIn Encoder_M1B(D10); +//DigitalIn Encoder_M2A(D11); +//DigitalIn Encoder_M2B(D12); + DigitalOut Direction_M1(D4); //To control the rotation direction of the arm -DigitalOut Speed_M1(D5); //To control the rotation speed of the arm -DigitalOut Speed_M2(D6); //To control the translation direction of the arm +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(D3); //To control the gripper (Note: doesn't work on D8, unknown why) +Servo gripper_servo(D3); //To control the gripper (Note: D8=PTC12) InterruptIn Switch_1(D9); //To control the rotation to the left InterruptIn Switch_2(D10); //To control the rotation to the right @@ -21,11 +26,14 @@ MODSERIAL pc(USBTX, USBRX); //To make connection with the PC +float speed_translation=0.1; //0.075 +float speed_rotation=0.1; //0.075 + void rotation_left (){ switch (counter_rotation_left){ case 1: //For activating the rotation to the left Direction_M1 = 1; //The arm will rotate to the left - Speed_M1 = 1; //The motor is turned on + Speed_M1 = speed_rotation; //The motor is turned on pc.printf("The arm will now rotate to the left \n"); wait(0.5f); break; @@ -50,7 +58,7 @@ switch (counter_rotation_right){ case 1: //For activation the rotation to the right Direction_M1 = 0; //The arm will rotate to the right - Speed_M1 = 1; //The motor is turned on + Speed_M1 = speed_rotation; //The motor is turned on pc.printf("The arm will now rotate to the right \n"); wait(0.5f); break; @@ -75,7 +83,7 @@ switch (counter_translation){ case 1: //For activating the elongation of the arm Direction_M2 = 1; //The arm will get longer - Speed_M2 = 1; //The motor is turned on + Speed_M2 = speed_translation; //The motor is turned on pc.printf("The arm will now get longer \n"); wait(0.5f); break; @@ -87,7 +95,7 @@ break; case 3: //For activating the shortening of the arm Direction_M2 = 0; //The arm will get shorter - Speed_M2 = 1; //The motor is turned off + Speed_M2 = speed_translation; //The motor is turned off pc.printf("The arm will now get shorter \n"); wait(0.5f); break; @@ -138,7 +146,7 @@ Direction_M1 = 1; //The arm will initially get longer Speed_M1 = 0; //The first motor is initially turned off Direction_M2 = 255; //The arm will initially turn left - Speed_M1 = 0; //The second motor is initially turned off + Speed_M2 = 0; //The second motor is initially turned off gripper_servo = 1; //The gripper is initially open Switch_1.rise(&switch_counter_rotation_left);