libplayerc++ example
[libplayerc++]

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>

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

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

  for(;;)
  {
    double turnrate, speed;

    // read from the proxies
    robot.Read();

    // 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
    else
      turnrate = dtor(20);

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

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

Compile this program like so:

$ g++ -o example0 `pkg-config --cflags playerc++` example0.cc `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.

automake

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

# Player C++ Library
PKG_CHECK_MODULES(PLAYERCC, playerc++)
AC_SUBST(PLAYERCC_CFLAGS)
AC_SUBST(PLAYERCC_LIBS)

Then, in your Makefile.am you can add:

AM_CPPFLAGS += $(PLAYERCC_CFLAGS)
programname_LDFLAGS = $(PLAYERCC_LIBS)


Last updated 12 September 2005 21:38:45