Society of Robots - Robot Forum

Software => Software => Topic started by: idee17 on February 06, 2013, 08:16:01 PM

Title: USB2SERIAL Converter and Processing Troubleshooting
Post by: idee17 on February 06, 2013, 08:16:01 PM
Hello, I have been having some trouble getting Processing code to send data though the USB2SERIAL to the Arduino. The SoftwareSerial library is what i'm using to take the incoming information and then mySerial.print(val) the value back to the computer. This is where I run into problems: the Arduino can print things to the computer through the USB2SERIAL converter, but when information is sent to the Arduino from a PC and the Arduino printing them back to the PC is when things stop working correctly. The serial  monitor shows a mess of information.

Here is the output:

I Received: -1
I Received: -1
I Received: -1
I Rec255
I Recei
I ReceivedReceived: -1
Ieived: -1
I Reed: -1
I Recei -1
I ReceivedReceived: -1
Ieived: -1
I Reced: -1
I Recei -1
I Received2
I Received: I Received: -1
eceived: -1
I Rece: -1
I Receiv15
I Received:

I'm am rather confused on where the -1 and 255 is coming from, because the number sent to the Arduino from Processing is a 15, which only shows up once and a while. The couple of things I thought that it could be is: packet collisions and Processing is saying: RXTX Warning:  Removing stale lock file. /var/lock/LK.059.033.126   


Any help would be appreciated

This is being ran on:
OS X 10.8.2 (12C60)
2.2 GHz Intel Core i7
8 GB 1333 MHz DDR3

If this is the wrong place to post this please forgive me 

~Thanks Idan

Here is the code that I am using

Processing code:

Code: [Select]
import processing.serial.*;
int i;
Serial port;

void setup()
{
println(Serial.list());
port = new Serial(this, Serial.list()[4], 9600);

}

void draw()
{

  port.write(15);
  delay(100);
  println("testing");
 
}


Arduino code

Code: [Select]
#include <SoftwareSerial.h>

int i;

SoftwareSerial mySerial(9,8); //set tx and rx pins

void setup(){
 
//set up serial comunication
mySerial.begin(9600);  //speed of serial port used by NewSoftSerial

}

void loop()
{
 
                // read the incoming byte:
                i = mySerial.read();
                //then print it
                mySerial.print("I Received: ");
                mySerial.println(i);
               
 
}
Title: Re: USB2SERIAL Converter and Processing Troubleshooting
Post by: jwatte on February 07, 2013, 10:59:31 AM
read() will return -1 when there is no data available. It will not wait for data to come in.
The cut-off "I received" messages seem to indicate you are sending data faster than the 9600 baud connection can transmit it.
Wrap your loop() in something like if(Serial.available()) { ... }
Also, 255 is the unsigned char version of -1 as signed char. I don't know if that is relevant in this case, but it's useful to know.
Title: Re: USB2SERIAL Converter and Processing Troubleshooting
Post by: idee17 on February 07, 2013, 11:16:53 AM
That seemed to help, but the output is still a little cut-off. Is a delay (I know not good coding) the correct way to slow down the sending of the data?

Here is the output:

Ic
 eved: 2
Ieed: 2
IRe
Ic
IReivd
 ReIReed
I d2Ie
 RecIeed2Ic
 ee
Iieved: 2
Ie
Iieved: 2
Iecie d
Rce
RecIRc
IRv:
Ie
 RecIe:
Iieved: 2
Ie2I ieved: 2
 c
Rd2
 Reeci Re

I'm assuming that how i'm slowing down the rate to 9600 baud is incorrect, because the output seems cut-off still. I didn't think that the baud rate can be slowed down on the computer (4800 baud) and still be at 9600 on the Arduino board, right? How would you slow down how fast the data is sent?

~Thanks Idan

Code: [Select]
import processing.serial.*;
int i;
Serial port;

void setup()
{
println(Serial.list());
port = new Serial(this, Serial.list()[4], 9600);

}

void draw()
{

  port.write(2);
  delay(1000);
  println("testing");
 
}

Code: [Select]
int incomingByte = 0;   // for incoming serial data
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9,8);

void setup() {
        mySerial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

        // send data only when you receive data:
        if (mySerial.available() > 0) {
                // read the incoming byte:
                incomingByte = mySerial.read();

                // say what you got:
                mySerial.print("I Recieved: ");
                mySerial.println(incomingByte, DEC);
               
        }
}
Title: Re: USB2SERIAL Converter and Processing Troubleshooting
Post by: jwatte on February 07, 2013, 01:40:22 PM
I don't know how Processing works exactly. However, if you're trying to keep both a port monitor program and the Processing program using the same serial port at the same time, that may lead to trouble.

Because the Arduino program only responds when there is data available, pacing the data sent from Processing should be enough. The delay you have should reduce the data rate to one line a second, which is plenty slow for a 9600 baud connection.
Title: Re: USB2SERIAL Converter and Processing Troubleshooting
Post by: idee17 on February 08, 2013, 12:38:48 PM
Well, I'm still running into problems  :'( When I set some servos to move to a postion coming from the PC they move to the a different place then what is being sent. On these particular servos a 0 corresponds to all the way one way and 1023 all the way the other direction, so if a 511 would be about center. When the PC writes 511 to the board the servo doesn't move to that postion... But when the if a 2 is written to the board it moves to a different postion then a 511, but still the wrong place. Any ideas on what is happening here would be helpful, I'm kind of in the dark on this one.

~Thanks Idan

Here is the Arduino code
Code: [Select]
int incomingByte = 0;   // for incoming serial data
#include <SoftwareSerial.h>

#include <DynamixelSerial.h>

SoftwareSerial mySerial(9,8);

void setup() {
        mySerial.begin(9600);     // opens serial port, sets data rate to 9600 bps

        Dynamixel.begin(1000000,2);  // Inicialize the servo at 1Mbps and Pin Control 2

}

void loop() {

        // send data only when you receive data:
        if (mySerial.available() > 0) {
                // read the incoming byte:
                incomingByte = mySerial.read();

                Dynamixel.moveSpeed(1,incomingByte,300);  // Move the Servo 1, 2, and to what is read from the serial port at a move speed
                Dynamixel.moveSpeed(2,incomingByte,300);  //of 300
                Dynamixel.moveSpeed(3,incomingByte,300); 
               
        }
}

Here is the Processing code

Code: [Select]
import processing.serial.*;

Serial port;

void setup()
{
println(Serial.list());
port = new Serial(this, Serial.list()[4], 9600);

}

void draw()
{
  port.write(511);
  delay(1000);
  println("testing");
 
}
Title: Re: USB2SERIAL Converter and Processing Troubleshooting
Post by: jwatte on February 08, 2013, 02:36:10 PM
The maximum value of a byte is 255. If you write a larger value, the Serial library will do one of two things:
1) Truncate it to 8 bits.
2) Send more than one byte.
3) Convert it to text format and send it as text rather than binary.

To send exactly the data you want, you should send it like so:

Code: [Select]
unsigned short value = 511;

mySerial.write((void *)&value, sizeof(value));

and receive it something like:

Code: [Select]
if (mySerial.available() >= 2) {
  unsigned short value = mySerial.read();
  value = value | ((unsigned short)mySerial.read() << 8u);
  .. use 'value' for the dynamixel ..
}