PROJECT Nº 6: Control the Arm Robot with 2 joysticks

Once you have built the Arm Robot and done the exercises 1 to 5, do this final project to control its movements using 2 joysticks. With the J1 you will move the forearm (S3) and the gripper (S4), and with the J2 you will move the arm (S2) and the base (S1).

DIFFICULTY LEVEL: Advanced.

DURATION OF THE EXERCISE:  60 min.

MATERIALS:

  • Arm Robot
  • 1 USB – USB type B cable
  • Computer

CONNECTIONS:

To do this project, the Arm Robot must be aseembled according to the instructions manual (link to the manual). Follow the steps and do the corresponding connections.

PROGRAMMING CODE

You can do this project using the Arduino program or a visual programming software by blocks compatible. Below you will find the necessary code.

Arduino Code

  1. Download and install the Arduino IDE program. It is available for Windows, Mac OS and Linux.
  2. Open the Arduino program and copy the following code in it:
    #include <Servo.h>
    
    int valueX = 0; // J2 X-AXIS READING
    int valueY = 0; // J2 Y-AXIS READING
    int valueX2 = 0; // J1 X-AXIS READING
    int valueY2 = 0; // J1 Y-AXIS READING
    /*-----------------------------------------------------------------------------------------*/
    int pinJX = A1; // A1 X-AXIS ANALOG PIN
    int pinJY = A0; // A0 Y-AXIS ANALOG PIN
    int pinJX2 = A2; // A2 X2-AXIS ANALOG PIN
    int pinJY2 = A3; // A3 Y2-AXIS ANALOG PIN
    /*-----------------------------------------------------------------------------------------*/
    Servo motor1; // DECLARE S1 // SERVO MOTOR 1
    Servo motor2; // DECLARE S2 // SERVO MOTOR 2
    Servo motor3; // DECLARE S3 // SERVO MOTOR 3
    Servo motor4; // DECLARE S4 // SERVO MOTOR 4
    /*-----------------------------------------------------------------------------------------*/
    int degree1 = 0; // S1 DEGREES
    int degree2 = 0; // S2 DEGREES
    int degree3 = 0; // S3 DEGREES
    int degree4 = 0; // S4 DEGREES
    /*-----------------------------------------------------------------------------------------*/
    float tiempo;
    
    void setup() {
      // put your setup code here, to run once: 
    motor1.attach (6); // DIGITAL PIN PWM 6 WHERE S1 IS CONNECTED
    motor2.attach (9); // DIGITAL PIN PWM 9 WHERE S2 IS CONNECTED
    motor3.attach (10); // DIGITAL PIN PWM 10 WHERE S3 IS CONNECTED 
    motor4.attach (11); // DIGITAL PIN PWM 11 WHERE S4 IS CONNECTED
    /*-----------------------------------------------------------------------------------------*/
    // ARM ROBOT INITIAL POSITION
    degree1 = 20;  //S1
    degree2 = 70;  //S2
    degree3 = 80;  //S3
    degree4 = 90;  //S4
    /*-----------------------------------------------------------------------------------------*/
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    Motor1();
    Motor2();
    Motor3();
    Motor4();
    
    for (tiempo = 0; tiempo < 2500; tiempo++)
    {
      if (tiempo >= 2500)
      {
        tiempo == 0;
      }
    }
    }
    
    void Motor1 ()
    {
      valueX = analogRead ( pinJX); // STORES THE READINGS OF THE Y-AXIS OF THE ANALOG PORT A1
      // INCREASE MOTOR 1 DEGREES 
      if ( (valueX > 520) && (valueX <= 1023) && (degree1 < 170)  ) // INCREASE THE DEGREES WHEN THE X-AXIS READING IS HIGHER THAN THE CALIBRATION OR LOWER/EQUAL TO 1023
      {
        degree1 = degree1 + 5; // +5 degree
      }
      
      // DECREASE MOTOR 1 DEGREES 
      else if ( (valueX >= 0 ) && (valueX < 480)&& (degree1 > 20) ) // DECREASE DEGREES WHEN THE X-AXIS READING IS LOWER THAN THE CALIBRATION OR HIGHER / EQUAL TO 0
      {
        degree1 = degree1 - 5;  // -5 degree
      }
      motor1.write (degree1); // SEND DEGREES TO SERVO 1
    }
    
    void Motor2 ()
    {
      valueY = analogRead ( pinJY); // STORES THE READINGS OF THE Y-AXIS OF THE ANALOG PORT AO
        // INCREASE MOTOR2 DEGREES
      if ( (valueY > 530 ) && (valueY <= 1023) && (degree2 < 140)  )// INCREASE THE DEGREES WHEN THE Y-AXIS READING IS HIGHER THAN THE CALIBRATION O LOWER / EQUAL TO 1023
      {
        degree2 = degree2 + 5; // +5 degree
      }
        
      // MOTRO2 DECREASE DEGREES
      else if ( (valueY >= 0 ) && (valueY < 480) && (degree2 > 70) ) // DECREASE DEGREES WHEN THE Y-AXIS LECTURE IS LOWER THAN THE CALIBRATION OR HIGHER/EQUAL TO 0
      {
        degree2 = degree2 - 5;  // -5 degree
      }
      motor2.write (degree2); // SEND DEGREES TO SERVO 2 
    }
    
    void Motor3 ()
    {
      valueX2 = analogRead ( pinJX2); // STORES THE READINGS OF THE X2-AXIS OF THE ANALOG PORT A2
        // MOTOR3 DEGREES INCREASE
      if ( (valueX2 >= 0 ) && (valueX2 < 480) && (degree3 < 155)  ) // INCREASE DEGREES WHEN THE X2-AXIS READING IS HIGHER THAN THE CALIBRATION OR LOWER/EQUAL TO 1023
      {
        degree3 = degree3 + 5; // +5 degree
      }
      
      // MOTOR3 DEGREE DECREASE
      if ( (valueX2 > 530 ) && (valueX2 <= 1023) && (degree3 > 80)  ) // DECREASE DEGREES WHEN THE X2-AXIS READING IS LOWER THAN THE CALIBRATION OR HIGHER/EQUAL TO 0
      {
        degree3 = degree3 - 5;  // -5 degree
      }
      motor3.write (degree3); // SEND DEGREES TO SERVO 3 
    }
    
    void Motor4 ()
    {
      valueY2 = analogRead ( pinJY2); // STORES THE READINGS OF THE Y-AXIS OF THE ANALOG PORT A3
        // MOTOR4 INCREASE DEGREES
      if ( (valueY2 >= 0 ) && (valueY2 < 480) && (degree4 < 125) ) // INCREASE DEGREES WHEN Y2-AXIS READING IS HIGHER THAN THE CALIBRATION OR LOWER/EQUAL TO 1023
      { 
        degree4 = degree4 + 5; // +5 degree
      }
      
      // MOTOR4 DEGREE DECREASE
      else if ( (valueY2 > 530 ) && (valueY2 <= 1023) && (degree4 > 90) ) // DECREASE DEGREES WHEN THE Y2-AXIS READING IS LOWER THAN THE CALIBRATION OR HIGHER/EQUAL TO 0
      {
        degree4 = degree4 - 5;  // -5 degree
      }
      motor4.write (degree4); // SEND DEGREES TO SERVO 4 
    }
    
  3. Configure and upload the code, following the instructions indicated in the Arm Robot First Steps guide

Code for the visual programming software by blocks compatible

  1. Download and install the program.
  2. Open the program and copy the following code:
  3. Configure the program and upload it to the Arm Robot. You will find the instructions in the Arm Robot First Steps guide.

RESULT OF THE EXERCISE

Once you have created the program, you will be able to control the Arm Robot with two joysticks. With the J1 you will move the forearm (S3) and the gripper (S4), and with the J2 you will move the arm (S2) and the base (S1).

1 0

0