Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Tutorial: Training the Robot Arm using the Python Gui

We have had the capability to train the robot for a while now. Here is the code used in this tutorial.

The interface is already pretty intuitive, but some of the application may not be clear. Soon we will make a video of how to launch the GUI and get it working on windows.

In the meantime have fun training the robot. And please share what you make it do on the forum

There are two tutorials, the long version and the short. Use the one you need.


Long Version

Short Version


Parallel Execution of Joint Motions

Normally when you make a robot, you don't want it to move one joint at a time. Usually you want something more fluid where several joints are moving in tandem.

While it is impossible to have parallel execution on the arduino, the illusion of parallel execution can be achieved.

It is done by moving each joint only a few degrees and then moving the next joint a few degrees. Since the delays between the movements are milliseconds it appears as if all the joints are moving at once.

This code was tested using the Training Program created in the last post.  The user control the arm to some position they want it to be at and then records that position. Then when there are several positions the arm will move directly to each point.

For those familiar with engineering, what we have created is a very basic PD controller.

What is great about this parallel movement is that while before when we did training we may have had to record single joint movements to ensure that the arm didn't collide with the table. Now that is not as much of a worry since all the joints can move to their desired positions simultaneously.

Code for this post for Parallel Joint Movement

//Primary Function for Simultaneous Servo Motion
int servoParallelControl (int thePos, Servo theServo ){
 
    int startPos = theServo.read();        //read the current pos
    int newPos = startPos;
    int theSpeed = 9;
   
    //define where the pos is with respect to the command
    // if the current position is less that the actual move up
    if (startPos < (thePos-5)){
         
       newPos = newPos + 1;
       theServo.write(newPos);
       delay(theSpeed);
       return 0;
         
    }
 
   else if (newPos > (thePos + 5)){
 
      newPos = newPos - 1;
      theServo.write(newPos);
      delay(theSpeed);
      return 0;
         
    }
   
    else {
        return 1;
    }
   
 
}

Training the Robot: Record a Motion Sequence and Play It Back

Record Sequence and Playback Code

A robot is not very useful unless a person can work with it easily. While it is not difficult to put in commands for where the servos should be positioned, it is much easier if you can direct a robot to where it needs to be and have it remember that action.

That is what this program is for. Using the GUI one can move the 3D printed robot arm through a series of motions, recording each step, and then have the robot repeat the seqence. It is a great piece of software for just creating quick demos. Or for kids to learn how programming works sequentially.

The next step will be to modify to the arduino code so that is changes all of the servo positions seemingly simultaneously. Right now it moved the shoulder then the elbow. This is a little slow and un-elegant. The next iteration will have them all moving at once.

See the video below to watch how to train the robot.


Demo: Pick and Place

Here is a very simple example of using the servo control function that was created early just to have the robot perform a set of preprogrammed tasks.

In order to get the positions I used the gui program to take it through the sequence. I recorded the positions and then entered the sequence into the arduino code.

In the future I will add a recording feature to the python code so that the user can use the gui to move the arm through positions and then just have it repeat the motions automatically.

Here is the Pick and Place code

For any other resources visit the Project Links Page










If you don't want the file you can copy and paste the code below

#include <Servo.h>  //arduino library
#include <math.h>   //standard c library

#define PI 3.141


Python Gui V2: Real Time Control of the Arm

The code below gives real time control of the arm using scale bars in a python gui. There is a slight lag in control due to the speed of the USB connection but it has proven to reliable.

Arduino Code

#include <Servo.h>  //arduino library
#include <math.h>   //standard c library

#define PI 3.141

Servo baseServo;
Servo shoulderServo;
Servo elbowServo;
Servo gripperServo;

int command;

struct jointAngle{
  int base;
  int shoulder;
  int elbow;

Python + Arduino: GUI Control through Serial

I have created a basic gui program that allows me to move the arm to particular positions by controlling the angle of the servos. Scale bars created in python using Tkinter define the angle of the servo. Those angles are then sent to the arduino which uses the servo control function created earlier in the project to move the servos to the new desired position. For the moment the "move arm" button must be pressed to send a new value. It is very simple to have the arm follow the trackbar position live and that will come next, but for the moment this piecemeal layout makes the operations clearer.

Python Gui created in Tkinter




Below is the arduino code. 

#include <Servo.h>  //arduino library
#include <math.h>   //standard c library

#define PI 3.141

Servo baseServo;
Servo shoulderServo;
Servo wristServo;

int command;

struct jointAngle{
  int base;
  int shoulder;
  int elbow;
};

struct jointAngle desiredAngle; //desired angles of the servos

//+++++++++++++++FUNCTION DECLARATIONS+++++++++++++++++++++++++++

void servoControl (int thePos, int theSpeed, Servo theServo);

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Arduino Code: Kinematic Function for Servo Arm

Here is the arduino function that determines the location of the tip of the robot arm in 3D cartesian space using kinematics.

Note: While this function was designed to be used with the 3D printed microservo arm being built in this project. You may change the physical parameters of the arm to match those of any servo arm and the code will work perfectly.

The code below has the structure definition needed to setup the function and then the function itself. Soon an example program will be posted with this function implemented.


struct xyzPos{                //a struct to hold multple returned arguments for the 3D position
  float x;
  float y;
  float z;
};

struct xyzPos currentPos;     //current location of tip of arm
struct xyzPos desiredPos;     //desired location of tip of arm

//+++++++++++++++FUNCTION DECLARATIONS+++++++++++++++++++++++++++

void servoControl (int thePos, int theSpeed, Servo theServo);
struct xyzPos kinematics(float baseAng, float shoulderAng, float wristAng);

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

IN THIS AREA IS WHERE THE OTHER ARDUINO LOOP CODE WOULD BE
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


struct xyzPos kinematics(float baseAng, float shoulderAng, float wristAng){
  
    // this function takes the servo angles as input and computes the locaiton of the tip of the arm in 3D space
    Serial.println("in kinematics");
    
    struct xyzPos POS;

    // the physical parameters of the arm.
    float baseHeight   = 3;
    float upperArm     = 5;
    float foreArm      = 0;       // left at 0 since current arm does not have wriist
    double gripper     = .5;

    //convert servo angles to radians for trig functions
    baseAng = (baseAng * PI /180);
    shoulderAng = (shoulderAng * PI /180);
    wristAng = (wristAng * PI /180);

    // kinematic equations of the robot arm. These equations describe where it is based on the angle of the arm. 
    POS.x = (upperArm*cos(baseAng)*cos(shoulderAng))   +    (cos(baseAng)*(foreArm*cos(shoulderAng+wristAng))   );
    POS.y = baseHeight + (upperArm*sin(shoulderAng)) + (foreArm*sin(shoulderAng+wristAng)) ;
    POS.z = (upperArm *(-sin(baseAng))*(cos(shoulderAng))) - (sin(baseAng) * (      (foreArm*cos(shoulderAng+wristAng))          ));

    //Prints used for error checking if necessary
    /* 
    Serial.print("xpos = ");
    Serial.println(POS.x);
    Serial.print("ypos = ");
    Serial.println(POS.y);
    Serial.print("zpos = ");
    Serial.println(POS.z); */
    
    Serial.println ("done with function");
    
    return POS; //return the #D position of the arm
  
}

If you would like to learn more about using structures in c here are some explanations.

Arduino Code: Basic Servo Control Function

Here is a first iteration of a function that can be used to control the position of a servo. The user may user the arduino serial interface or some other serial program to send a position to the arduino and have the servo move to that position.

The code is below

#include <Servo.h> 

Servo baseServo;  
Servo shoulderServo;  
Servo wristServo; 

int command;
int pos = 0;    // variable to store the servo position 

//+++++++++++++++FUNCTION DECLARATIONS++++++++++++++++++++++

void servoControl (int thePos, int theSpeed, Servo theServo);

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

void setup() 
{ 
  Serial.begin(9600);
  baseServo.attach(9);  // attaches the servo on pin 9 to the servo object 
  shoulderServo.attach(10);
  wristServo.attach(11);
  Serial.setTimeout(50);
} 

void loop() 
{ 
  if (Serial.available()){
    command = Serial.parseInt();  
    Serial.println(command); 
    servoControl(command, 15, baseServo);
    servoControl(command, 15, shoulderServo);
    servoControl(command, 15, wristServo);    

  }
}

//++++++++++++FUNCTION DEFINITIONS+++++++++++++++++++++++
void servoControl (int thePos, int theSpeed, Servo theServo){
  Serial.println("in function");
  //read the current pos
  int startPos = theServo.read();
  int newPos = startPos;
  
  //define where the pos is with respect to the command
  // if the current position is less that the actual move up
  if (startPos < thePos){
        while (newPos < (thePos - 5)){
          newPos = newPos + 5;
          theServo.write(newPos);
          delay(theSpeed);
        }    
  }

  else{
       while (newPos > (thePos + 5)){
         newPos = newPos - 5;
         theServo.write(newPos);
         delay(theSpeed);
       }     
  }
}

Arm Demo: Prototype Videos

An initial prototype for the arm was completed recently. Here are a couple of videos.

Operated from Keyboard


Servo Sequence