libplayerc example
[libplayerc]

Collaboration diagram for libplayerc example:

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:

#include <stdio.h>

#include <libplayerc/playerc.h>

int main(int argc, const char **argv)
{
  int i;
  playerc_client_t *client;
  playerc_position2d_t *position2d;

  // Create a client object and connect to the server; the server must
  // be running on "localhost" at port 6665
  client = playerc_client_create(NULL, "localhost", 6665);
  if (playerc_client_connect(client) != 0)
  {
    fprintf(stderr, "error: %s\n", playerc_error_str());
    return -1;
  }

  // Create a position2d proxy (device id "position2d:0") and susbscribe
  // in read/write mode
  position2d = playerc_position2d_create(client, 0);
  if (playerc_position2d_subscribe(position2d, PLAYERC_OPEN_MODE) != 0)
  {
    fprintf(stderr, "error: %s\n", playerc_error_str());
    return -1;
  }

  // Enable the robots motors
  playerc_position2d_enable(position2d, 1);

  // Start the robot turning slowing
  playerc_position2d_set_cmd_vel(position2d, 0, 0, 0.1, 1);

  for (i = 0; i < 200; i++)
  {
    // Read data from the server and display current robot position
    playerc_client_read(client);
    printf("position : %f %f %f\n",
           position2d->px, position2d->py, position2d->pa);
  } 

  // Shutdown and tidy up
  playerc_position2d_unsubscribe(position2d);
  playerc_position2d_destroy(position2d);
  playerc_client_disconnect(client);
  playerc_client_destroy(client);

  return 0;
}

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.


Last updated 12 September 2005 21:38:45