yarpobject.cpp
1 /********************************************************************************
2  * WorldSim -- library for robot simulations *
3  * Copyright (C) 2008-2011 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #ifdef FARSA_USE_YARP_AND_ICUB
21 
22 #include "yarpobject.h"
23 
24 #ifdef __GNUC__
25 #pragma GCC diagnostic ignored "-Wunused-parameter"
26 #endif
27  #include <yarp/dev/all.h>
28  #include <yarp/os/all.h>
29 #ifdef __GNUC__
30 #pragma GCC diagnostic warning "-Wunused-parameter"
31 #endif
32 
33 using namespace yarp::os;
34 using namespace yarp::dev;
35 
36 namespace farsa {
37 
45 class FakeDriverCreator : public DriverCreator {
46 public:
47  FakeDriverCreator( DeviceDriver* instance, const char *name, const char *wrap, const char *code)
48  : desc(name), wrap(wrap), code(code) {
49  device = instance;
50  }
51  virtual yarp::os::ConstString toString() {
52  return desc;
53  }
54  virtual yarp::os::ConstString getName() {
55  return desc;
56  }
57  virtual yarp::os::ConstString getWrapper() {
58  return wrap;
59  }
60  virtual yarp::os::ConstString getCode() {
61  return code;
62  }
63  virtual DeviceDriver *create() {
64  return device;
65  }
66 private:
67  ConstString desc, wrap, code;
68  DeviceDriver* device;
69 };
70 
71 YarpObject::YarpObject( World* world, QString name, const wMatrix& tm )
72  : WObject( world, name, tm, false ), controls() {
73  //--- nothing to do here
74 }
75 
77  // it's responsability of subclass to proper closing any PolyDriver opened
78 }
79 
80 void YarpObject::registerServerControlBoard( yarp::dev::DeviceDriver* device, QString devicename ) {
81  QString nameDev = QString( "%1-%2-%3" ).arg( world()->name() ).arg( name() ).arg( devicename );
82  //--- add to the factory a FakeDriverCreator for allow creation of ServerControlBoard
83  Drivers::factory().add( new FakeDriverCreator( device, nameDev.toAscii().data(), "controlboard", nameDev.toAscii().data() ) );
84  //--- create the ServerControlBoard
85  QString namePort = QString( "/%1/%2/%3" ).arg( world()->name() ).arg( name() ).arg( devicename );
86  QString settings = QString( "(device controlboard) (subdevice %1) (name %2)" ).arg( nameDev ).arg(namePort);
87  controls[devicename] = new PolyDriver( settings.toAscii().data() );
88  return;
89 }
90 
91 void YarpObject::removeServerControlBoard( QString devicename ) {
92  if ( !controls.contains(devicename) ) return;
93  QString nameDev = QString( "%1-%2-%3" ).arg( world()->name() ).arg( name() ).arg( devicename );
94  PolyDriver* drv = controls[devicename];
95  drv->close();
96  controls.remove( devicename );
97  delete drv;
98  Drivers::factory().remove( nameDev.toAscii().data() );
99 }
100 
101 void YarpObject::registerServerFrameGrabber( yarp::dev::DeviceDriver* device, QString devicename ) {
102  QString nameDev = QString( "%1-%2-%3" ).arg( world()->name() ).arg( name() ).arg( devicename );
103  if ( nameDev.endsWith("/") ) {
104  nameDev.chop(1);
105  }
106  //--- add to the factory a FakeDriverCreator for allow creation of ServerControlBoard
107  Drivers::factory().add( new FakeDriverCreator( device, nameDev.toAscii().data(), "framegrabber", nameDev.toAscii().data() ) );
108  //--- create the ServerControlBoard
109  QString namePort = QString( "/%1/%2/%3" ).arg( world()->name() ).arg( name() ).arg( devicename );
110  if ( namePort.endsWith("/") ) {
111  namePort.chop(1);
112  }
113  QString settings = QString( "(device grabber) (subdevice %1) (name %2)" ).arg( nameDev ).arg(namePort);
114  controls[devicename] = new PolyDriver( settings.toAscii().data() );
115  return;
116 }
117 
118 } // end namespace farsa
119 
120 #endif // FARSA_USE_YARP_AND_ICUB