An example. More...
libplayerc is based on a device proxy model, in which the client maintains a local proxy for each of the devices on the remote server. Thus, for example, one can create local proxies for the position2d and laser devices. There is also a special
client proxy, used to control the Player server itself.
Programs using libplayerc will generally have the following structure:
This example can be built using the command:
$ gcc -o simpleclient `pkg-config --cflags playerc` simpleclient.c `pkg-config --libs playerc`
Make sure that
libplayerc is installed somewhere that pkg-config can find it.
The above program can be broken into six steps, as follows.
Create and connect a client proxy.
client = playerc_client_create(NULL, "localhost", 6665); playerc_client_connect(client);
createfunction creates a new client proxy and returns a pointer to be used in future function calls (
localhostshould be replaced with the network host name of the robot). The
connectfunction notifies the Player server that a new client wishes to recieve data.
Create and subscribe a device proxy.
position2d = playerc_position2d_create(client, 0); playerc_position2d_subscribe(position2d, PLAYERC_OPEN_MODE);
createfunction creates a new position2d device proxy and returns a pointer to be used in future function calls. The
subscribefunction notifies the Player server that the client is using the position2d device, and that the client expects to both send commands and recieve data (
Configure the device, send commands.
playerc_position2d_enable(position2d, 1); playerc_position2d_set_speed(position2d, 0, 0, 0.1);
enablefunction sends a configuration request to the server, changing the robot's motor state from
on, thereby allowing the robot to move. The
setspeedfunction sends a new motor speed, in this case commanding the robot to turn on the spot.
Note that most Player devices will accept both asynchronous command and synchronous configuration requests. Sending commands is analogous using the standard Unix
writedevice interface, while sending configuration requests is analogous to using the
ioctlinterface. For the most part,
libplayerchides the distinction between these two interfaces. Users should be aware, however, that while commands are always handled promptly by the server, configuration requests may take significant time to complete. If possible, configuration requests should therefore be restricted to the initialization phase of the program.
Read data from the device.
playerc_client_read(client); printf("position : %f %f %f\n", position2d->px, ... );
readfunction blocks until new data arrives from the Player server. This data may be from one of the subscribed devices, or it may be from the server itself (which sends regular synchronization messages to all of its clients). The
readfunction inspects the incoming data and automatically updates the elements in the appropriate device proxy. This function also returns a pointer to the proxy that was updated, so that user programs may, if desired, trigger appropriate events on the arrival of different kinds of data.
In pull mode read will process a full set of messages from the server, in push mode read will process a single message.
Unsubscribe and destroy the device proxy.
unsubscribefunction tells the Player server that the client is no longer using this device. The
destroyfunction then frees the memory associated with the device proxy; the
devicepointer is now invalid and should be not be re-used.
Disconnect and destroy the client proxy.
disconnectfunction tells the server that the client is shutting down. The
destroyfunction then frees the memory associated with the client proxy; the
clientpointer is now invalid and should be not be re-used.