beautypg.com

Pioneer 2 User Manual

Page 43

background image

Pioneer Mobile Robots

37

/* Send command and retrieve response via P2OS AUX serial port */
#define SERAUXpac 0xB0
int serbytes, inbufptr;
char inbuf[200];
...
/* Replace Saphira's default packet processor with
our own that can detect and parse SERAUXpac packets */
void
myStartupFn(void)
{
sfInitProcess(myPackets, "GetAuxPkt");
sfRemoveTask("packets"); /* default packet processing */
...
}

/* SERAUXpac packet processor */
void
myPackets(void)
{
char packet_type;
if (sfIsConnected)
{

while(sfHaveClientPacket())
{

packet_type = sfReadClientByte();
if (packet_type == SERAUXpac)
{

/* get aux serbytes into inbuf */
while (inbufptr++ < serbytes)

inbuf[inbufptr] = sfReadClientByte();

}
/* If not SERAUXpac, send along to default */

else sfProcessClientPacket(packet_type);

}

}

void
main(int argc, char **argv)

{

/* Initialize client and connect with robot */
...

sfRobotComInt(43,0);

/* flush the aux buffer */

sfRobotComStrn(42,"fictitious command",18); /* send command to AUX device */
inbufptr = 0;
serbytes = 10;

/* number of response bytes to retrieve */

sfRobotComInt(43,serbytes); /* Expect a 10 byte response */

...

}

}

P2OS maintains a circular buffer for incoming serial data from the AUX port and returns successive
portions depending on the number of bytes you request via the GETAUX command, up to 200 total at a
time. P2OS waits to collect that number of incoming AUX-port serial bytes before sending the packet to
the client. Use the GETAUX command with a zero argument to flush the P2OS circular buffer and reset its
pointers.

For example, the following Saphira client code fragment sends a fictitious command to a serial device
attached to the Pioneer 2 microcontroller's AUX port and retrieves a 10-byte response from that device
(note that ellipses mark the many portions of a complete client that have been deleted for brevity):