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):
/* Copyright (c) 2005, Brad Kratochvil, Toby Collett, Brian Gerkey, Andrew Howard, ... All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the Player Project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #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)