PDA

View Full Version : [Question(s)] Arbotix FTDI command problem



sarendt
03-14-2011, 03:46 AM
I have been having a strange issue with my Arbotix while running my current tests. I used a slightly modified code from Darkback2's code from here:

http://forums.trossenrobotics.com/showthread.php?t=4434

I basically just cut out the python stuff and was using the serial monitor to send commands, seems pretty simple. So here is the problem: I have to send something twice to get the arbotix to respond, both in the serial monitor and for the servo to move. Basiclly it is ignoring every other command. I am using a FTDI to communicate with the arbotix and I don't have a Xbee installed.

Here is the code:


#include <ax12.h>
#include <BioloidController.h>

BioloidController bioloid = BioloidController(1000000);

void setup()
{
// need to start the serial port
Serial.begin(38400);

// recommended pause
delay(100);

}

void loop()
{
char cmd[8];
int startByte = 0;

// if there are bytes waiting on the serial port
if(Serial.available() > 0) {
// read the incoming byte
startByte = Serial.read();
} // end Serial.available()
if(startByte == 'p') {
// get the command from the serial port

// START FERGY EDITS HERE
while(Serial.available() < 8); // wait for data to arrive, do nothing
for(int i=0;i<8;i++){
cmd[i] = Serial.read();
} //this creates an array that takes in the data from the serial port
// END THE FERGY EDITS

// convert command to two numbers
int servoNum = 0;
int servoPos = 0;

servoNum = (cmd[0]-48)*10 + (cmd[1]-48);
servoPos = (cmd[3]-48)*1000 + (cmd[4]-48)*100 + (cmd[5]-48)*10 + (cmd[6]-48);

//sets servo position using 2 numbers one for servo, other for position. currently not working.
SetPosition(servoNum,servoPos);

// debug
Serial.print("cmd array:");
for(int i=0;i<8;i++){
Serial.print(cmd[i]);
}
Serial.println("----");
Serial.print("servoNum:");
Serial.println(servoNum);
Serial.print("servoPos:");
Serial.println(servoPos);

}

} // end loop()
After uploading the code to the arbotix, I open the serial monitor and type a command. Usually something like: "p01,0500" - the command shows up in the lower serial monitor box, but nothing else happens. Then I enter the exact same line and I receive a response from the arbotix over the serial monitor and the servo jumps to that position. I can continue this for a while, entering two commands for every 'received' command.

Any suggestions would be greatly appreciated :*)

Cheers,
Scott

lnxfergy
03-14-2011, 08:45 AM
That code assumes a newline (or any character really) at the end of each command. What you are seeing is that it blocks on the line "while(Serial.available() < 8);" until the first character of the second command arrives (at which point it consumes the "p" character and so the second command becomes garbage that is ignored).

-Fergs

sarendt
03-14-2011, 01:19 PM
Would it be better to add a extra characater to my command (EX: p01,0500x) or change the code

"while(Serial.available() < 8);"

to


"while(Serial.available() <= 8);"

and use the normal command? Or does it not matter much?

Thanks again for your help Fergs!

Cheers,
Scott