Society of Robots - Robot Forum

General Misc => Misc => Topic started by: idee17 on March 28, 2013, 05:13:44 PM

Title: Software Serial Errors (Arduino)
Post by: idee17 on March 28, 2013, 05:13:44 PM
I am trying to get my PC to communicate to the Arduino through an USB2SERIAL converter. Then read the value and move a AX-12 servo that postion. When I type 0,1,2,3 (hitting enter between them, no commas) into the serial monitor it behaves as normal and moves a tad counter clock wise. This works all the way up to 11 but when 12 is typed the servo swings all the way past half way. This is just one case, other sets of numbers do something similar to this. The AX-12 hijack the rx and tx pin on the Arduino thats why the USB2SERIAL converter is used, for the servos dont use PWM signals. The library for AX-12s is attached.

~Thanks Idan

http://arduino.cc/en/Main/USBSerial (http://arduino.cc/en/Main/USBSerial)

http://www.electronickits.com/robot/BioloidAX-12(english).pdf (http://www.electronickits.com/robot/BioloidAX-12(english).pdf)

http://robottini.altervista.org/dynamixel-ax-12a-and-arduino-how-to-use-the-serial-port?doing_wp_cron=1364066994.3524849414825439453125 (http://robottini.altervista.org/dynamixel-ax-12a-and-arduino-how-to-use-the-serial-port?doing_wp_cron=1364066994.3524849414825439453125)


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

SoftwareSerial mySerial(9,8);


void setup(){
 
 Dynamixel.begin(1000000,2);
 mySerial.begin(57600);
 delay(1000);
 
}

void loop(){
  if (mySerial.available() > 0) {
   
    int J0 = mySerial.read();
    Dynamixel.move(1, J0);
  }
}
   
Title: Re: Software Serial Errors (Arduino)
Post by: Metal Slug 2 on March 30, 2013, 02:18:25 AM
Hi Idan,

I think what might be causing you trouble is how you are using 'Serial.read()'.  That function only reads the very next byte available in the serial buffer (or returns -1 if there is no serial data).  When you send a decimal number such as "1" through the Arduino serial monitor, you are actually sending the ASCII character '1', which translates to the decimal equivalent of 49 *.  See ASCII Table (http://www.asciitable.com/)
When you send a number like "12", it is split up into its respective characters, '1' and '2', and sent one after the other over serial.

What might solve the problem is to change the line "Serial.read()" to "Serial.parseInt()".  Also, set the option 'newline' in the Arduino serial monitor.

Hope that helps!

* EDIT: char '1' == 49 decimal, not 50
Title: Re: Software Serial Errors (Arduino)
Post by: olivthill on March 30, 2013, 07:25:13 AM
...ASCII character '1', which translates to the decimal equivalent of 50...
49  ;)
Title: Re: Software Serial Errors (Arduino)
Post by: Metal Slug 2 on March 30, 2013, 12:29:20 PM
Oops, my bad.  Thanks for the fix, I edited my above answer.
Title: Re: Software Serial Errors (Arduino)
Post by: idee17 on March 30, 2013, 02:36:32 PM
Well... When using mySerial.parseInt() the servo won't move at all  :( Am I using parseInt() incorrectly? Here is what i've tried and what happened:

.parseInt and newline = no movement
.parseInt and no line ending = no movement
.read and newline = same problem as last post, just moved to different places
.read and no line ending = same problem as last post

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

SoftwareSerial mySerial(9,8);


void setup(){
 
 Dynamixel.begin(1000000,2); //2=data control at 1 mbps
 mySerial.begin(57600); // I've tried using 9600 baud also
 
 delay(1000);
 
}

void loop(){
  if (mySerial.available() > 0) {
   
    int incomingByte = mySerial.parseInt();
   
    Dynamixel.move(1, incomingByte);
    }
}

I've also tried putting commas after the numbers when typing into the serial monitor. Sorry if this is just a noob mistake, i'm not experienced with communication stuff...

~Thanks Idan
Title: Re: Software Serial Errors (Arduino)
Post by: idee17 on March 30, 2013, 05:35:21 PM
Could it be that I have TX and RX switch in the code. In the hardware TX goes pin 9. The code that im using (that move to the wrong spot) has:
Code: [Select]
mySerial(9,8); //9=tx, 8=rxArduino's reference page:
Code: [Select]
mySerial(10, 11); // RX, TXBut when I try switching them the servo doesn't move at all, with everything being the same, just switching the numbers.

Could this be contributing to the problem, did Arduino get RX and TX mixed up? Or does it have to do with the USB2SERIAL converter.


This code moves the servo to the wrong place:
Code: [Select]
#include <DynamixelSerial.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(9,8);


void setup(){
 
 Dynamixel.begin(1000000,2); //2=data control at 1 mbps
 mySerial.begin(57600); // I've tried using 9600 baud also
 
 delay(1000);
 
}

void loop(){
  if (mySerial.available() > 0) {
   
    int incomingByte = mySerial.read();
   
    Dynamixel.move(1, incomingByte);
    }
}

This code doesn't move the servo at all:
Code: [Select]
#include <DynamixelSerial.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(8,9);


void setup(){
 
 Dynamixel.begin(1000000,2); //2=data control at 1 mbps
 mySerial.begin(57600); // I've tried using 9600 baud also
 
 delay(1000);
 
}

void loop(){
  if (mySerial.available() > 0) {
   
    int incomingByte = mySerial.read();
   
    Dynamixel.move(1, incomingByte);
    }
}
Title: Re: Software Serial Errors (Arduino)
Post by: Metal Slug 2 on March 30, 2013, 10:43:23 PM
Hmm...  Upload this and tell me what happens:
Code: [Select]
#include <DynamixelSerial.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(9, 8);

int vel = 1, pos = 0;
unsigned long now = 0, last = 0;

void setup(){
  Dynamixel.begin(1000000,2); //2=data control at 1 mbps
  mySerial.begin(57600); // I've tried using 9600 baud also
  delay(1000);
}

void loop(){
  now = millis();
  if (now - last > 5) {
    last = now;
    pos += vel;
    Dynamixel.move(1, pos);
    if (pos <= 0 || pos >= 1023)
      vel *= -1;
  }
}

Also, how are you powering the servo and the arduino?
Title: Re: Software Serial Errors (Arduino)
Post by: 20162265 on March 31, 2013, 08:55:23 AM
Hello, I am Idan's partner in this project.

I do not have access to the Arduino right now so I cannot test the coding.

Right now for testing we are powering the Arduino through  the USB2SERIAL converter plugged into a mac by a USB cable. After testing, we may power the Arduino and USB2SERIAL converter through a regular 9 volt battery.

We are powering the Dynamixel servo by a UPG UB1250 (12 volt 5 Amp hour lead acid) battery.

Thank you for helping us, we will get back to you on the coding later today.

-Jason
Title: Re: Software Serial Errors (Arduino)
Post by: idee17 on March 31, 2013, 09:44:44 AM
Here a video of what happens with that code.
SoR_Code_Testing (http://www.youtube.com/watch?v=hirEwM_ylhg#)

~Thanks Idan
Title: Re: Software Serial Errors (Arduino)
Post by: Metal Slug 2 on March 31, 2013, 02:06:03 PM
Cool, so power isn't an issue, and the behavior of the servo in your video was as expected.
Now lets check to see if serial communication using your usb2serial converter is working the same as it would if you were using hardware serial.

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

SoftwareSerial mySerial(9, 8);

void setup(){
  mySerial.begin(57600); // I've tried using 9600 baud also
}

void loop(){
  if (Serial.available() > 1) {
    int pos = mySerial.parseInt();
    mySerial.println(pos);
  }
}
Set the arduino serial monitor for 57600 baud rate, and set the 'newline' option.  Type a few different numbers into the serial window, and check to see if the same number is printed to the screen.
Title: Re: Software Serial Errors (Arduino)
Post by: idee17 on March 31, 2013, 08:31:28 PM
Well, no numbers are being printed to the screen.  :-\ Is my wiring wrong? (I've triple checked it but I could still manage to make a mistake :( )
Title: Re: Software Serial Errors (Arduino)
Post by: Metal Slug 2 on March 31, 2013, 08:47:11 PM
Wups, I made a small mistake in the code from my previous post.

Change
Code: [Select]
if (Serial.available() > 1) { to
Code: [Select]
if (mySerial.available() > 1) {
Title: Re: Software Serial Errors (Arduino)
Post by: idee17 on March 31, 2013, 08:58:59 PM
Hmm... The numbers I type don't show up, just zeros keep appearing with or without me typing anything.

~Thanks Idan
Title: Re: Software Serial Errors (Arduino)
Post by: Metal Slug 2 on March 31, 2013, 09:07:30 PM
Hmm...  Try uploading this:
Code: [Select]
#include <SoftwareSerial.h>

SoftwareSerial mySerial(9, 8);

void setup() {
  mySerial.begin(57600); // I've tried using 9600 baud also
}

void loop() {
    mySerial.println(123);
    delay(100);
}
Does the number 123 show up on the serial monitor?
Title: Re: Software Serial Errors (Arduino)
Post by: idee17 on March 31, 2013, 09:11:57 PM
Yes it does.

Title: Re: Software Serial Errors (Arduino)
Post by: Metal Slug 2 on March 31, 2013, 09:52:59 PM
This is really strange.   ???

I guess the last thing we can check for is if the software serial + usb2serial setup properly works on the receiving line.

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

SoftwareSerial mySerial(9, 8);

void setup(){
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  mySerial.begin(57600); // I've tried using 9600 baud also
}

void loop() {
  if (mySerial.available()) {
    if (mySerial.read() == 'a') {
        digitalWrite(13, HIGH);
    }
  }
}

Type anything into the serial monitor, but make sure it has at least one 'a'.  Check to see if the LED on pin 13 lights up.
Title: Re: Software Serial Errors (Arduino)
Post by: idee17 on March 31, 2013, 10:02:44 PM
No light turns on. Hmm... So this means the receiving line of the USB2SERIAL converter is broken? ???
Title: Re: Software Serial Errors (Arduino)
Post by: Metal Slug 2 on March 31, 2013, 10:27:21 PM
I know you've already checked your wiring, but maybe the TX pin on the usb2serial device is shorted with the 5V line?
Title: Re: Software Serial Errors (Arduino)
Post by: idee17 on April 01, 2013, 09:24:27 PM
Sorry about the belated response, there are no lines shorting now, but it might have shorted at one point. Could this be the problem? Do I need a new adapter?

~Thanks for all the help thus far :D it's really appreciated, Idan
Title: Re: Software Serial Errors (Arduino)
Post by: jwatte on April 02, 2013, 03:51:19 PM
I believe SoftwareSerial usually doesn't work faster than 38400, and is more reliable with lower baud rates.

Also, make sure you tie the "-" pole of the batter to the "gnd" connector on the Arduino, else you may have a floating voltage difference problem.

Some components can take a shorting without dieing, and others will die. Don't know specifically about yours, so you will have to figure out a way to test them.
Title: Re: Software Serial Errors (Arduino)
Post by: idee17 on April 02, 2013, 06:17:22 PM
Thanks for the info. I have had the gnds tied, now there is another USB2SERIAL adapter on its way... :( (I need to stop using alligator clips, they short way to easy) when it gets here I will try 9600 baud.

~Thanks