libplayerc++ example

Collaboration diagram for libplayerc++ example:

The C++ library is built on a "service proxy" model in which the client maintains local objects that are proxies for remote services. There are two kinds of proxies: the special server proxy PlayerClient and the various device-specific proxies. Each kind of proxy is implemented as a separate class. The user first creates a PlayerClient proxy and uses it to establish a connection to a Player server. Next, the proxies of the appropriate device-specific types are created and initialized using the existing PlayerClient proxy. To make this process concrete, consider the following simple example (for clarity, we omit some error-checking):

#include <iostream>
#include <libplayerc++/playerc++.h>

main(int argc, char *argv[])
  using namespace PlayerCc;

  PlayerClient    robot("localhost");
  SonarProxy      sp(&robot,0);
  Position2dProxy pp(&robot,0);

    double turnrate, speed;

    // read from the proxies

    // print out sonars for fun
    std::cout << sp << std::endl;

    // do simple collision avoidance
    if((sp[0] + sp[1]) < (sp[6] + sp[7]))
      turnrate = dtor(-20); // turn 20 degrees per second
      turnrate = dtor(20);

    if(sp[3] < 0.500)
      speed = 0;
      speed = 0.100;

    // command the motors
    pp.SetSpeed(speed, turnrate);

Compile this program like so:

$ g++ -o example0 `pkg-config --cflags playerc++` `pkg-config --libs playerc++`

Be sure that libplayerc++ is installed somewhere that pkg-config can find it.

This program performs simple (and bad) sonar-based obstacle avoidance with a mobile robot . First, a PlayerClient proxy is created, using the default constructor to connect to the server listening at localhost:6665. Next, a SonarProxy is created to control the sonars and a PositionProxy to control the robot's motors. The constructors for these objects use the existing PlayerClient proxy to establish access to the 0th sonar and position2d devices, respectively. Finally, we enter a simple loop that reads the current sonar state and writes appropriate commands to the motors.


An Automake package config file is included(playerc++.pc). To use this in your automake project, simply add the following to your or

# Player C++ Library

Then, in your you can add:

programname_LDFLAGS = $(PLAYERCC_LIBS)

Last updated 12 September 2005 21:38:45