Script for controlling 2 DC-motors and a gripper-servo using buttons
Dependencies: MODSERIAL QEI Servo mbed
Diff: main.cpp
- Revision:
- 9:cca4d4084775
- Parent:
- 8:9c58ca13076e
- Child:
- 10:cf579c3eaf01
diff -r 9c58ca13076e -r cca4d4084775 main.cpp --- a/main.cpp Mon Oct 10 12:09:49 2016 +0000 +++ b/main.cpp Tue Oct 11 11:00:18 2016 +0000 @@ -1,11 +1,13 @@ #include "mbed.h" #include "MODSERIAL.h" #include "Servo.h" +#include "QEI.h" -DigitalIn Encoder_M1A(D9); -DigitalIn Encoder_M1B(D10); -DigitalIn Encoder_M2A(D11); -DigitalIn Encoder_M2B(D12); +QEI encoder_M1 (D9, D10, NC, 8400); +QEI encoder_M2 (D11, D12, NC, 8400); + +Ticker encoder_M1_ticker; +Ticker encoder_M2_ticker; DigitalOut Direction_M1(D4); //To control the rotation direction of the arm PwmOut Speed_M1(D5); //To control the rotation speed of the arm @@ -14,8 +16,8 @@ Servo gripper_servo(D13); //To control the gripper (Note: D8=PTC12) -InterruptIn Switch_1(SW2); //To control the rotation to the left -InterruptIn Switch_2(SW3); //To control the rotation to the right +InterruptIn Switch_1(SW2); //To control the rotation to the left +InterruptIn Switch_2(SW3); //To control the rotation to the right InterruptIn Switch_3(D2); //To control the translation of the arm InterruptIn Switch_4(D3); //To control the gripper @@ -27,11 +29,24 @@ MODSERIAL pc(USBTX, USBRX); //To make connection with the PC const double pi = 3.1415926535897; + double speed_rotation=pi/5; //in rad/sec double speed_translation=pi/5; //in rad/sec double speedM1=speed_rotation/8.4; double speedM2=speed_translation/8.4; +void read_position_M1(){ + int pulses_M1 = encoder_M1.getPulses(); + float angle_M1 = float(pulses_M1)/4200*2.0*pi; + pc.printf("%i \t%f \t", pulses_M1, angle_M1); +} + +void read_position_M2(){ + int pulses_M2 = encoder_M2.getPulses(); + float angle_M2 = float(pulses_M2)/4200*2.0*pi; + pc.printf("%i \t%f \n", pulses_M2, angle_M2); +} + void rotation_left (){ switch (counter_rotation_left){ case 1: //For activating the rotation to the left @@ -151,6 +166,10 @@ Direction_M2 = 255; //The arm will initially turn left Speed_M2 = 0; //The second motor is initially turned off gripper_servo = 1; //The gripper is initially open + encoder_M1.reset(); + encoder_M2.reset(); + encoder_M1_ticker.attach(&read_position_M1,0.5); + encoder_M2_ticker.attach(&read_position_M2,0.5); Switch_1.rise(&switch_counter_rotation_left); Switch_2.rise(&switch_counter_rotation_right);