20 #ifdef FARSA_USE_YARP_AND_ICUB
22 #include "worldcontroller.h"
24 #include "physphere.h"
25 #include "phycylinder.h"
28 #include <QCoreApplication>
29 #include <QTimerEvent>
31 using namespace yarp::os;
35 ServerWorldController::ServerWorldController(
WorldController* ctrl ) : QThread() {
38 asyn_cmds[
"start"] = &ServerWorldController::startCmd;
39 asyn_cmds[
"pause"] = &ServerWorldController::pauseCmd;
40 asyn_cmds[
"stop"] = &ServerWorldController::stopCmd;
41 asyn_cmds[
"advance"] = &ServerWorldController::advanceCmd;
43 syn_cmds[
"mk"] = &ServerWorldController::makeCmd;
44 syn_cmds[
"set"] = &ServerWorldController::setCmd;
45 setTerminationEnabled(
true );
54 while( !queue_cmds.isEmpty() ) {
55 Bottle cmd = queue_cmds.dequeue();
57 QString cmd0( cmd.get(0).toString().c_str() );
60 res = (this->*(syn_cmds[cmd0]))( cmd );
66 Port* inport = wctrl->
inPort();
67 Port* outport = wctrl->
outPort();
72 inport->read( cmd,
false );
74 QString cmd0( cmd.get(0).asString().c_str() );
75 if ( asyn_cmds.contains( cmd0 ) ) {
76 res = (this->*(asyn_cmds[cmd0]))( cmd );
77 }
else if ( syn_cmds.contains( cmd0 ) ) {
78 res.addString(
"Command Queued" );
79 queue_cmds.enqueue( cmd );
81 res.addString(
"Unsupported Command: " );
82 res.addString( cmd.get(0).toString() );
89 yarp::os::Bottle ServerWorldController::startCmd(
const yarp::os::Bottle& ) {
90 QCoreApplication::postEvent( wctrl->
world(),
new QEvent( (QEvent::Type)(World::E_Play) ) );
92 res.addString(
"World Running");
96 yarp::os::Bottle ServerWorldController::pauseCmd(
const yarp::os::Bottle& ) {
97 QCoreApplication::postEvent( wctrl->
world(),
new QEvent( (QEvent::Type)(World::E_Pause) ) );
99 res.addString(
"World Paused");
103 yarp::os::Bottle ServerWorldController::stopCmd(
const yarp::os::Bottle& ) {
104 QCoreApplication::postEvent( wctrl->
world(),
new QEvent( (QEvent::Type)(World::E_Stop) ) );
106 res.addString(
"World Stopped");
110 yarp::os::Bottle ServerWorldController::advanceCmd(
const yarp::os::Bottle& ) {
112 if ( wctrl->
world()->
status() == World::playingS ) {
113 res.addString(
"World is currently running, pause it before use advance command");
115 QCoreApplication::postEvent( wctrl->
world(),
new QEvent( (QEvent::Type)(World::E_Advance) ) );
116 res.addString(
"World Advanced");
121 yarp::os::Bottle ServerWorldController::makeCmd(
const yarp::os::Bottle& cmd ) {
124 QString objtype = cmd.get(1).toString().c_str();
136 if ( objtype ==
"box" ) {
142 Value v = cmd.get(2);
144 res.addString(
"You must specify a name for the object" );
147 QString name = v.toString().c_str();
148 x_dim = cmd.get(3).asDouble();
149 y_dim = cmd.get(4).asDouble();
150 z_dim = cmd.get(5).asDouble();
151 if ( x_dim == 0 || y_dim == 0 || z_dim == 0 ) {
152 res.addString(
"Dimension of the object must be greater than zero" );
155 wVector pos( cmd.get(6).asDouble(), cmd.get(7).asDouble(), cmd.get(8).asDouble() );
160 new PhyBox( x_dim, y_dim, z_dim, wctrl->
world(), name, tm );
161 }
else if ( objtype ==
"sphere" ) {
165 Value v = cmd.get(2);
167 res.addString(
"You must specify a name for the object" );
170 QString name = v.toString().c_str();
171 radius = cmd.get(3).asDouble();
173 res.addString(
"Dimension of the object must be greater than zero" );
176 wVector pos( cmd.get(4).asDouble(), cmd.get(5).asDouble(), cmd.get(6).asDouble() );
181 new PhySphere( radius, wctrl->
world(), name, tm );
182 }
else if ( objtype ==
"cylinder" ) {
187 Value v = cmd.get(2);
189 res.addString(
"You must specify a name for the object" );
192 QString name = v.toString().c_str();
193 radius = cmd.get(3).asDouble();
194 len = cmd.get(4).asDouble();
195 if ( radius == 0 || len == 0 ) {
196 res.addString(
"Dimension of the object must be greater than zero" );
199 wVector pos( cmd.get(5).asDouble(), cmd.get(6).asDouble(), cmd.get(7).asDouble() );
204 new PhyCylinder( radius, len, wctrl->
world(), name, tm );
205 }
else if ( objtype ==
"icub" ) {
208 Value v = cmd.get(2);
210 res.addString(
"You must specify a name for the object" );
213 QString name = v.toString().c_str();
214 wVector pos( cmd.get(3).asDouble(), cmd.get(4).asDouble(), cmd.get(5).asDouble() );
219 PhyiCub* icub =
new PhyiCub( wctrl->
world(), name, tm );
220 icub->blockTorso0(
true );
221 }
else if ( objtype ==
"camera" ) {
224 Value v = cmd.get(2);
226 res.addString(
"You must specify a name for the object" );
229 QString name = v.toString().c_str();
230 wVector pos( cmd.get(3).asDouble(), cmd.get(4).asDouble(), cmd.get(5).asDouble() );
235 unsigned int width = cmd.get(9).asInt();
236 unsigned int height = cmd.get(10).asInt();
237 if ( width == 0 || height == 0 ) {
238 res.addString(
"Dimension of the frame must be greater than zero" );
241 new WCamera( wctrl->
world(), name, width, height, tm );
243 res.addString(
"Make Command executed");
247 yarp::os::Bottle ServerWorldController::setCmd(
const yarp::os::Bottle& cmd ) {
259 QString objname = cmd.get(1).toString().c_str();
260 if ( objname ==
"world" ) {
261 return setWorldCmd( cmd );
265 return setObjectCmd( cmd, obj );
269 res.addString(
"Object not found" );
273 yarp::os::Bottle ServerWorldController::setWorldCmd(
const yarp::os::Bottle& cmd ) {
276 QString propname = cmd.get(2).toString().c_str();
277 if ( propname ==
"size" ) {
278 real x_dim = fabs( cmd.get(3).asDouble()/2.0 );
279 real y_dim = fabs( cmd.get(4).asDouble()/2.0 );
280 real z_dim = fabs( cmd.get(5).asDouble() );
281 if ( x_dim == 0 || y_dim == 0 || z_dim == 0 ) {
282 res.addString(
"Dimension of the world must be greater than zero" );
285 wctrl->
world()->
setSize( wVector( -x_dim, -y_dim, 0 ), wVector( +x_dim, +y_dim, +z_dim ) );
286 res.addString(
"World Re-Sized" );
287 }
else if ( propname ==
"timestep" ) {
288 real newtimestep = fabs( cmd.get(3).asDouble() );
289 if ( newtimestep == 0 ) {
290 res.addString(
"TimeStep of the world must be greater than zero" );
294 res.addString(
"New TimeStep setted" );
295 }
else if ( propname ==
"threads" ) {
296 real numThreads = fabs( (
float)(cmd.get(3).asInt()) );
297 if ( numThreads == 0 ) {
298 res.addString(
"Threads of the world must be greater than zero" );
302 res.addString(
"Multi-Thread setted" );
303 }
else if ( propname ==
"realtime" ) {
304 bool ison = ( QString(cmd.get(3).toString().c_str()) ==
"on" );
307 res.addString(
"RealTime Activated" );
309 res.addString(
"RealTime De-Activated" );
315 yarp::os::Bottle ServerWorldController::setObjectCmd(
const yarp::os::Bottle& , WObject* ) {
318 res.addString(
"Not Yet Implemented" );
325 outport =
new Port();
326 inport->open( QString(
"/%1/%2:i" ).arg(
world()->
name() ).arg( name ).toAscii().data() );
327 outport->open( QString(
"/%1/%2:o" ).arg(
world()->
name() ).arg( name ).toAscii().data() );
328 w->pushObject(
this );
349 #endif //FARSA_USE_YARP_AND_ICUB