An example of using libplayerc++. More...
An example of using 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):
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)