worldsim/src/yarpobject.cpp

00001 /********************************************************************************
00002  *  WorldSim -- library for robot simulations                                   *
00003  *  Copyright (C) 2008-2011 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #ifdef FARSA_USE_YARP_AND_ICUB
00021 
00022 #include "yarpobject.h"
00023 
00024 #ifdef __GNUC__
00025 #pragma GCC diagnostic ignored "-Wunused-parameter"
00026 #endif
00027     #include <yarp/dev/all.h>
00028     #include <yarp/os/all.h>
00029 #ifdef __GNUC__
00030 #pragma GCC diagnostic warning "-Wunused-parameter"
00031 #endif
00032 
00033 using namespace yarp::os;
00034 using namespace yarp::dev;
00035 
00036 namespace farsa {
00037 
00045 class FakeDriverCreator : public DriverCreator {
00046 public:
00047     FakeDriverCreator( DeviceDriver* instance, const char *name, const char *wrap, const char *code)
00048         : desc(name), wrap(wrap), code(code) {
00049         device = instance;
00050     }
00051     virtual yarp::os::ConstString toString() {
00052         return desc;
00053     }
00054     virtual yarp::os::ConstString getName() {
00055         return desc;
00056     }
00057     virtual yarp::os::ConstString getWrapper() {
00058         return wrap;
00059     }
00060     virtual yarp::os::ConstString getCode() {
00061         return code;
00062     }
00063     virtual DeviceDriver *create() {
00064         return device;
00065     }
00066 private:
00067     ConstString desc, wrap, code;
00068     DeviceDriver* device;
00069 };
00070 
00071 YarpObject::YarpObject( World* world, QString name, const wMatrix& tm )
00072     : WObject( world, name, tm, false ), controls() {
00073     //--- nothing to do here
00074 }
00075 
00076 YarpObject::~YarpObject() {
00077     // it's responsability of subclass to proper closing any PolyDriver opened
00078 }
00079 
00080 void YarpObject::registerServerControlBoard( yarp::dev::DeviceDriver* device, QString devicename ) {
00081     QString nameDev = QString( "%1-%2-%3" ).arg( world()->name() ).arg( name() ).arg( devicename );
00082     //--- add to the factory a FakeDriverCreator for allow creation of ServerControlBoard
00083     Drivers::factory().add( new FakeDriverCreator( device, nameDev.toAscii().data(), "controlboard", nameDev.toAscii().data() ) );
00084     //--- create the ServerControlBoard
00085     QString namePort = QString( "/%1/%2/%3" ).arg( world()->name() ).arg( name() ).arg( devicename );
00086     QString settings = QString( "(device controlboard) (subdevice %1) (name %2)" ).arg( nameDev ).arg(namePort);
00087     controls[devicename] = new PolyDriver( settings.toAscii().data() );
00088     return;
00089 }
00090 
00091 void YarpObject::removeServerControlBoard( QString devicename ) {
00092     if ( !controls.contains(devicename) ) return;
00093     QString nameDev = QString( "%1-%2-%3" ).arg( world()->name() ).arg( name() ).arg( devicename );
00094     PolyDriver* drv = controls[devicename];
00095     drv->close();
00096     controls.remove( devicename );
00097     delete drv;
00098     Drivers::factory().remove( nameDev.toAscii().data() );
00099 }
00100 
00101 void YarpObject::registerServerFrameGrabber( yarp::dev::DeviceDriver* device, QString devicename ) {
00102     QString nameDev = QString( "%1-%2-%3" ).arg( world()->name() ).arg( name() ).arg( devicename );
00103     if ( nameDev.endsWith("/") ) {
00104         nameDev.chop(1);
00105     }
00106     //--- add to the factory a FakeDriverCreator for allow creation of ServerControlBoard
00107     Drivers::factory().add( new FakeDriverCreator( device, nameDev.toAscii().data(), "framegrabber", nameDev.toAscii().data() ) );
00108     //--- create the ServerControlBoard
00109     QString namePort = QString( "/%1/%2/%3" ).arg( world()->name() ).arg( name() ).arg( devicename );
00110     if ( namePort.endsWith("/") ) {
00111         namePort.chop(1);
00112     }
00113     QString settings = QString( "(device grabber) (subdevice %1) (name %2)" ).arg( nameDev ).arg(namePort);
00114     controls[devicename] = new PolyDriver( settings.toAscii().data() );
00115     return;
00116 }
00117 
00118 } // end namespace farsa
00119 
00120 #endif // FARSA_USE_YARP_AND_ICUB