Go Down

Topic: Serial Port Delay issues Using LeapMotion and Processing for Lobot Robot (Read 637 times) previous topic - next topic

Hawkman1996

I am having an issue with transmitting the serial data from LeapMotion to another Arduino board. While the data is written from LeapMotion serially to my Arduino Uno board it seems to wait until it is no longer receiving a signal from LeapMotion to transmit the data from the Arduino Uno board onto the other Ardunio board connected to the robotic arm. What I am trying to do is take 4 values from Leap Motion and through processing send them serially to the robotic arm in a continuous manner. Essentially what I am trying to do is create a real time motion controlled robotic arm.

Here is my code.

Processing from LeapMotion:

Code: [Select]
import processing.serial.*;
import de.voidplus.leapmotion.*;

LeapMotion leap;
Serial myPort;

void setup(){
  size(600, 600);
  leap = new LeapMotion(this);
  println((Object[])Serial.list());
  myPort = new Serial(this, "COM7", 9600);
}

int position[] = new int [4];
void draw() {
 
  background(255);
  for (Hand hand : leap.getHands()) {
   
    hand.draw();
      pushMatrix();
      float myhand = hand.getSphereRadius();
      float wristRot = hand.getRoll();
      float wrist = 0.0;
      if(hand.getPitch() > 0.0){wrist = hand.getPitch();}
      float fullRotation = hand.getYaw();
      int fullhand = int(myhand);
      int wristRotation = int(wristRot);
      int pwrist = int(wrist);
      int pFullRotation = int(fullRotation);
      position[0] = fullhand;
      position[1] = wristRotation;
      position[2] = pwrist;
      //position[3] = elbow;
      //position[4] = shoulder;
      position[3] = pFullRotation; // put back to postion 5 when finished
      //pitch is servo 3
      //roll is servo 2
      //yaw is servo 6
      //hand.palmpostion() is servo 5
       
        popMatrix(); 
        String str= "";
        for(int i = 0; i < 4; i++){
          if(i > 0){
            str += ",";//We add a comma before each value, except the first value
          }
          str += position[i];//We concatenate each number in the string.
          }
          myPort.write(str);
          println(str, "\n");
        }   
    }


Arduino code:

Code: [Select]
int fullhand = 100;
int wristRotation = 500;
int pwrist = 500;
int pFullRotation = 500;
int data[4];
// the setup routine runs once when you press reset:

void setup()  {
Serial.begin(9600);
SerialxArm.begin(9600);
}

// the loop routine runs over and over again forever:
void loop(){   
  if (Serial.available() > 0) {
    String content = Serial.readString();
    for(int i = 0; i < 4; i++){
      int index = content.indexOf(","); //We find the next comma
      data[i] = atol(content.substring(0,index).c_str()); //Extract the number
      content = content.substring(index+1); //Remove the number from the string
    }

    //gets the correct value to put into the robot
    int fullhand = 620-((59.0/9.0)*(data[0]-30));
    int wristRotation = ((37.0/9.0)*data[1])+480;
    int pwrist = (-(37.0/9.0)*data[2])+870;
    int pFullRotation = ((-38.0/9.0)*data[3])+500;
   
    LobotSerialServoMove(SerialxArm, ID1, fullhand, 50, ID2, wristRotation, ID3, pwrist, ID4, pFullRotation);
  }
}


LobotSerialServoMove() :

Code: [Select]
void LobotSerialServoMove(SoftwareSerial &SerialX, uint8_t id1, int16_t position1, uint16_t time, uint8_t id2, int16_t position2, uint8_t id3, int16_t position3, uint8_t id4, int16_t position4)
{
  byte buf[19];
  if(position1 < 0)
    position1 = 0;
  if(position1 > 1000)
    position1 = 1000;
   if(position2 < 0)
    position2 = 0;
  if(position2 > 1000)
    position2 = 1000;
  if(position3 < 0)
    position3 = 0;
  if(position3 > 1000)
    position3 = 1000; 
  if(position4 < 0)
    position4 = 0;
  if(position4 > 1000)
    position4 = 1000;   
  buf[0] = buf[1] = LOBOT_SERVO_FRAME_HEADER;
  buf[2] = 17;
  buf[3] = 3;
  buf[4] = 4; //amount of servos running
  buf[5] = GET_LOW_BYTE(time);
  buf[6] = GET_HIGH_BYTE(time);
  buf[7] = id1;
  buf[8] = GET_LOW_BYTE(position1);
  buf[9] = GET_HIGH_BYTE(position1);
  buf[10] = id2;
  buf[11] = GET_LOW_BYTE(position2);
  buf[12] = GET_HIGH_BYTE(position2);
  buf[13] = id3;
  buf[14] = GET_LOW_BYTE(position3);
  buf[15] = GET_HIGH_BYTE(position3);
  buf[16] = id4;
  buf[17] = GET_LOW_BYTE(position4);
  buf[18] = GET_HIGH_BYTE(position4);
  SerialX.write(buf, 19);
}




Any help would be greatly appreciated.   

groundFungus

Code: [Select]
String content = Serial.readString();
The readString() function blocks until the line ending is received or the timeout is reached (1 second by default, I believe).  That may slow things down.

The use of String objects in the Arduino code* can cause memory fragmentation and lead to weird problems and resets.  See the Evils of Strings page. for why and some information on using strings (null terminated character arrays) in their place.

The serial input basics tutorial uses non blocking code to read serial data into strings.   Example #5 shows how to receive and parse the data.

Here are some of the string functions.

*OK to use Strings in Processing code as the PC does memory housekeeping and has much much more memory.

Go Up