22 #include <yarp/os/Bottle.h>
23 #include <yarp/os/BufferedPort.h>
24 #include <yarp/os/Property.h>
25 #include <yarp/os/Network.h>
26 #include <yarp/os/Os.h>
27 #include <yarp/os/Time.h>
28 #include <yarp/sig/Vector.h>
29 #include <yarp/sig/Image.h>
30 #include <yarp/dev/Drivers.h>
31 #include <yarp/dev/PolyDriver.h>
32 #include <yarp/os/LogStream.h>
42 using namespace yarp::os;
43 using namespace yarp::sig;
45 using namespace iCub::logpolar;
58 moduleName(config.getModuleName()),
61 robot_flags(config.getFlags()),
62 finder(config.getFinder()),
67 extractImages =
false;
78 failureToLaunch =
false;
85 tactileLeftHandPort.prepare() = report;
86 generalStamp.update(Time::now());
87 tactileLeftHandPort.setEnvelope(generalStamp);
88 tactileLeftHandPort.write();
92 tactileRightHandPort.prepare() = report;
93 generalStamp.update(Time::now());
94 tactileRightHandPort.setEnvelope(generalStamp);
95 tactileRightHandPort.write();
99 return tactileLeftHandPort.getOutputCount()>0;
103 return tactileRightHandPort.getOutputCount()>0;
111 skinEvents.insert(skinEvents.end(), skinContactListReport.begin(), skinContactListReport.end());
112 generalStamp.update(Time::now());
113 skinEventsPort.setEnvelope(generalStamp);
114 skinEventsPort.write();
118 return skinEventsPort.getOutputCount()>0;
122 tactileLeftArmPort.prepare() = report;
123 generalStamp.update(Time::now());
124 tactileLeftArmPort.setEnvelope(generalStamp);
125 tactileLeftArmPort.write();
129 tactileRightArmPort.prepare() = report;
130 generalStamp.update(Time::now());
131 tactileRightArmPort.setEnvelope(generalStamp);
132 tactileRightArmPort.write();
136 return tactileLeftArmPort.getOutputCount()>0;
140 return tactileRightArmPort.getOutputCount()>0;
144 tactileLeftForearmPort.prepare() = report;
145 generalStamp.update(Time::now());
146 tactileLeftForearmPort.setEnvelope(generalStamp);
147 tactileLeftForearmPort.write();
151 tactileRightForearmPort.prepare() = report;
152 generalStamp.update(Time::now());
153 tactileRightForearmPort.setEnvelope(generalStamp);
154 tactileRightForearmPort.write();
158 return tactileLeftForearmPort.getOutputCount()>0;
162 return tactileRightForearmPort.getOutputCount()>0;
166 tactileTorsoPort.prepare() = report;
167 generalStamp.update(Time::now());
168 tactileTorsoPort.setEnvelope(generalStamp);
169 tactileTorsoPort.write();
173 return tactileTorsoPort.getOutputCount()>0;
178 inertialPort.prepare() = report;
179 generalStamp.update(Time::now());
180 inertialPort.setEnvelope(generalStamp);
181 inertialPort.write();
185 return inertialPort.getOutputCount()>0;
193 yInfo(
"Closing module...\n");
200 portLeft.interrupt();
201 portRight.interrupt();
202 #ifndef OMIT_LOGPOLAR
203 portLeftFov.interrupt();
204 portLeftLog.interrupt();
205 portRightFov.interrupt();
206 portRightLog.interrupt();
208 portWide.interrupt();
214 #ifndef OMIT_LOGPOLAR
217 portRightFov.close();
218 portRightLog.close();
223 tactileLeftHandPort.close();
224 tactileRightHandPort.close();
225 tactileLeftArmPort.close();
226 tactileRightArmPort.close();
227 tactileLeftForearmPort.close();
228 tactileRightForearmPort.close();
229 tactileTorsoPort.close();
231 inertialPort.close();
234 trqLeftLegPort.close();
235 trqRightLegPort.close();
236 trqTorsoPort.close();
237 trqLeftArmPort.close();
238 trqRightArmPort.close();
242 dd_descClnt->close();
254 yInfo(
"Successfully terminated...bye...\n");
262 if (iCubLArm!=NULL) {
266 if (iCubRArm!=NULL) {
270 if (iCubHead!=NULL) {
274 if (iCubLLeg!=NULL) {
278 if (iCubRLeg!=NULL) {
282 if (iCubTorso!=NULL) {
289 world_manager.
clear();
297 cmd.read(connection);
299 if (connection.getWriter()!=NULL){
300 reply.write(*connection.getWriter());
306 string cmd = command.get(0).asString();
310 yInfo(
"Commands available:\n");
315 reply.fromString(
"world etc");
317 }
else if (
cmd==
"left") {
320 reply.fromString(
"ok");
322 }
else if (
cmd==
"right") {
325 reply.fromString(
"ok");
327 }
else if (
cmd==
"wide") {
331 reply.fromString(
"ok");
333 }
else if (
cmd==
"world"){
334 return world_manager.
respond(command,reply);
339 yarp::dev::PolyDriver *SimulatorModule::createPart(
const char *name) {
340 yDebug(
"Creating interface for body part %s\n", name);
342 string part_file = finder.findFile(name);
343 options.fromConfigFile(part_file.c_str());
344 string part_port = options.check(
"name",Value(1),
"what did the user select?").asString();
345 string full_name = moduleName + part_port.c_str();
346 options.put(
"name", full_name.c_str() );
347 options.put(
"joint_device",
"robot_joints");
348 yarp::dev::PolyDriver *driver =
new yarp::dev::PolyDriver(options);
350 if (!driver->isValid()){
351 yError(
"Device not available. Here are the known devices:\n");
352 yError(
"%s", yarp::dev::Drivers::factory().
toString().c_str());
353 failureToLaunch =
true;
359 bool SimulatorModule::initSimulatorModule()
361 if (!robot_flags.
valid)
363 yDebug(
"Robot flags are not set when creating SimulatorModule\n");
364 failureToLaunch =
true;
369 bool b_client =
false;
371 clnt_opt.put(
"device",
"robotDescriptionClient");
372 clnt_opt.put(
"local",
"/icubSim/robotDescriptionClient");
373 clnt_opt.put(
"remote",
"/robotDescription");
374 dd_descClnt =
new yarp::dev::PolyDriver;
375 if (yarp::os::Network::exists(
"/robotDescription/rpc"))
377 if (dd_descClnt->open(clnt_opt) && dd_descClnt->isValid())
379 yInfo() <<
"External robotDescriptionServer was found. Connection succesful.";
381 dd_descClnt->view(idesc);
385 yInfo() <<
"External robotDescriptionServer was not found. Opening a new RobotDescriptionServer.";
390 yInfo() <<
"External robotDescriptionServer was not found. Opening a new RobotDescriptionServer.";
394 if (b_client ==
false)
396 dd_descSrv =
new yarp::dev::PolyDriver;
398 srv_opt.put(
"device",
"robotDescriptionServer");
399 srv_opt.put(
"local",
"/robotDescription");
401 bool b_server =
false;
402 if (dd_descSrv->open(srv_opt) && dd_descSrv->isValid())
411 yError() <<
"Unable to open robotDescriptionServer";
412 failureToLaunch =
true;
417 if (dd_descClnt->open(clnt_opt) && dd_descClnt->isValid())
420 dd_descClnt->view(idesc);
427 yError() <<
"Unable to open robotDescriptionClient";
428 failureToLaunch =
true;
434 yarp::dev::DeviceDescription desc;
437 iCubLArm = createPart(
"left_arm");
438 desc.device_name = moduleName+
"/left_arm";
439 desc.device_type =
"controlBoard_nws_yarp";
440 if (idesc) idesc->registerDevice(desc);
445 iCubRArm = createPart(
"right_arm");
446 desc.device_name = moduleName +
"/right_arm";
447 desc.device_type =
"controlBoard_nws_yarp";
448 if (idesc) idesc->registerDevice(desc);
453 iCubHead = createPart(
"head");
454 desc.device_name = moduleName +
"/head";
455 desc.device_type =
"controlBoard_nws_yarp";
456 if (idesc) idesc->registerDevice(desc);
461 iCubLLeg = createPart(
"left_leg");
462 desc.device_name = moduleName +
"/left_leg";
463 desc.device_type =
"controlBoard_nws_yarp";
464 if (idesc) idesc->registerDevice(desc);
469 iCubRLeg = createPart(
"right_leg");
470 desc.device_name = moduleName +
"/right_leg";
471 desc.device_type =
"controlBoard_nws_yarp";
472 if (idesc) idesc->registerDevice(desc);
476 iCubTorso = createPart(
"torso");
477 desc.device_name = moduleName +
"/torso";
478 desc.device_type =
"controlBoard_nws_yarp";
479 if (idesc) idesc->registerDevice(desc);
482 Property masServerConf {{
"device", Value(
"multipleanalogsensorsserver")},
483 {
"name",Value(moduleName+
"/head/inertials")},
484 {
"period",Value(10)}};
485 Property simImuConf {{
"device",Value(
"simulationIMU")}};
488 if (!simImu.open(simImuConf)) {
492 polyList.push(&simImu,
"simImu");
494 if (!masserver.open(masServerConf)){
498 if (!masserver.view(iMWrapper))
500 yError()<<
"Failed to view the IMultipleWrapper";
504 iMWrapper->attachAll(polyList);
506 dd_descClnt->close();
513 void SimulatorModule::initImagePorts() {
516 string cameras = finder.findFile(
"cameras");
517 options.fromConfigFile(cameras.c_str());
519 string nameExternal =
520 options.check(
"name_wide",
522 "Name of external view camera port").asString();
525 options.check(
"name_left",
527 "Name of left camera port").asString();
530 options.check(
"name_right",
532 "Name of right camera port").asString();
534 #ifndef OMIT_LOGPOLAR
536 options.check(
"name_leftFov",
537 Value(
"/cam/left/fov"),
538 "Name of left camera fovea port").asString();
539 string nameRightFov =
540 options.check(
"name_rightFov",
541 Value(
"/cam/right/fov"),
542 "Name of right camera fovea port ").asString();
545 options.check(
"name_leftLog",
546 Value(
"/cam/left/logpolar"),
547 "Name of left camera logpolar port").asString();
548 string nameRightLog =
549 options.check(
"name_rightLog",
550 Value(
"/cam/right/logpolar"),
551 "Name of right camera logpolar port").asString();
554 string leftCam = moduleName + nameLeft.c_str();
555 string rightCam = moduleName + nameRight.c_str();
556 string mainCam = moduleName + nameExternal.c_str();
558 #ifndef OMIT_LOGPOLAR
559 string leftFov = moduleName + nameLeftFov.c_str();
560 string rightFov = moduleName + nameRightFov.c_str();
561 string leftLog = moduleName + nameLeftLog.c_str();
562 string rightLog = moduleName + nameRightLog.c_str();
565 portLeft.open( leftCam.c_str());
566 portRight.open( rightCam.c_str() );
567 portWide.open( mainCam.c_str() );
569 #ifndef OMIT_LOGPOLAR
570 portLeftFov.open( leftFov.c_str() );
571 portRightFov.open( rightFov.c_str() );
573 portLeftLog.open( leftLog.c_str() );
574 portRightLog.open( rightLog.c_str() );
580 cmdPort.setReader(*
this);
581 string world = moduleName +
"/world";
583 string tactileLeft = moduleName +
"/skin/left_hand_comp";
584 string tactileRight = moduleName +
"/skin/right_hand_comp";
585 string tactileLeftrpc = moduleName +
"/skin/left_hand_comp/rpc:i";
586 string tactileRightrpc = moduleName +
"/skin/right_hand_comp/rpc:i";
588 string torqueLeftLeg = moduleName +
"/joint_vsens/left_leg:i";
589 string torqueRightLeg = moduleName +
"/joint_vsens/right_leg:i";
590 string torqueTorso = moduleName +
"/joint_vsens/torso:i";
591 string torqueRightArm = moduleName +
"/joint_vsens/left_arm:i";
592 string torqueLeftArm = moduleName +
"/joint_vsens/right_arm:i";
594 string inertial = moduleName +
"/inertial";
595 cmdPort.open( world.c_str() );
596 tactileLeftHandPort.open( tactileLeft.c_str() );
597 tactileLeftHandPortrpc.open( tactileLeftrpc.c_str() );
598 tactileRightHandPort.open( tactileRight.c_str() );
599 tactileRightHandPortrpc.open( tactileRightrpc.c_str() );
600 inertialPort.open( inertial.c_str() );
602 trqLeftLegPort.open( torqueLeftLeg.c_str() );
603 trqRightLegPort.open( torqueRightLeg.c_str() );
604 trqTorsoPort.open( torqueTorso.c_str() );
605 trqLeftArmPort.open( torqueLeftArm.c_str() );
606 trqRightArmPort.open( torqueRightArm.c_str() );
609 string skinEventsPortString = moduleName +
"/skinManager/skin_events:o";
610 skinEventsPort.open(skinEventsPortString.c_str());
611 string tactileLeftArmPortString = moduleName +
"/skin/left_arm_comp";
612 tactileLeftArmPort.open(tactileLeftArmPortString.c_str());
613 string tactileRightArmPortString = moduleName +
"/skin/right_arm_comp";
614 tactileRightArmPort.open(tactileRightArmPortString.c_str());
615 string tactileLeftForearmPortString = moduleName +
"/skin/left_forearm_comp";
616 tactileLeftForearmPort.open(tactileLeftForearmPortString.c_str());
617 string tactileRightForearmPortString = moduleName +
"/skin/right_forearm_comp";
618 tactileRightForearmPort.open(tactileRightForearmPortString.c_str());
619 string tactileTorsoPortString = moduleName +
"/skin/torso_comp";
620 tactileTorsoPort.open(tactileTorsoPortString.c_str());
625 initSimulatorModule();
626 if (failureToLaunch)
return false;
628 sim->
init(
this,&robot_config);
634 buffer.resize( w, h);
648 void SimulatorModule::checkTorques()
650 bool needLeftLeg = (trqLeftLegPort.getInputCount()>0);
651 bool needRightLeg = (trqRightLegPort.getInputCount()>0);
652 bool needLeftArm = (trqLeftArmPort.getInputCount()>0);
653 bool needRightArm = (trqRightArmPort.getInputCount()>0);
654 bool needTorso = (trqTorsoPort.getInputCount()>0);
657 getTorques( trqLeftLegPort );
659 getTorques( trqRightLegPort );
661 getTorques( trqLeftArmPort );
663 getTorques( trqRightArmPort );
665 getTorques( trqTorsoPort );
668 void SimulatorModule::getTorques( yarp::os::BufferedPort<yarp::os::Bottle>& buffPort )
670 lock_guard<mutex> lck(mtx);
671 Bottle *torques = NULL;
672 torques = buffPort.read(
false);
673 if ( torques !=NULL )
679 void SimulatorModule::displayStep(
int pause) {
682 bool needLeft = (portLeft.getOutputCount()>0);
683 bool needRight = (portRight.getOutputCount()>0);
684 bool needWide = (portWide.getOutputCount()>0);
685 #ifndef OMIT_LOGPOLAR
686 bool needLeftFov = (portLeftFov.getOutputCount()>0);
687 bool needLeftLog = (portLeftLog.getOutputCount()>0);
688 bool needRightFov = (portRightFov.getOutputCount()>0);
689 bool needRightLog = (portRightLog.getOutputCount()>0);
697 #ifndef WIN32_DYNAMIC_LINK
698 const char *order =
"lrw";
706 camerasStamp.update(Time::now());
708 for (
int i=0; i<3; i++) {
718 #ifndef OMIT_LOGPOLAR
722 sendImageFov(portLeftFov);
728 sendImageLog(portLeftLog);
737 sendImage(portRight);
740 #ifndef OMIT_LOGPOLAR
744 sendImageFov(portRightFov);
750 sendImageLog(portRightLog);
767 if (wrappedStep!=NULL) {
772 double now = Time::now();
773 static double first = now;
774 static double target = 0;
778 Time::delay(target-now);
784 void SimulatorModule::getImage(){
788 void SimulatorModule::sendImage(BufferedPort<ImageOf<PixelRgb> >& port) {
789 ImageOf<PixelRgb>& normal = port.prepare();
790 normal.copy( buffer );
791 port.setEnvelope(camerasStamp);
795 #ifndef OMIT_LOGPOLAR
797 void SimulatorModule::sendImageFov(BufferedPort<ImageOf<PixelRgb> >& portFov) {
798 static ImageOf<PixelRgb> fov;
799 ImageOf<PixelRgb>& targetFov = portFov.prepare();
800 subsampleFovea( fov, buffer );
801 targetFov.copy( fov );
805 void SimulatorModule::sendImageLog(BufferedPort<ImageOf<PixelRgb> >& portLog) {
806 static ImageOf<PixelRgb> lp;
807 lp.resize (252, 152);
809 ImageOf<PixelRgb>& targetLog = portLog.prepare();
810 cartToLogPolar( lp , buffer );
811 targetLog.copy( lp );
816 bool SimulatorModule::cartToLogPolar(ImageOf<PixelRgb>& lp,
const ImageOf<PixelRgb>& cart) {
819 if (!trsf.allocated())
820 trsf.allocLookupTables(C2L, lp.height(), lp.width(), cart.width(), cart.height(), 1.);
825 return trsf.cartToLogpolar(lp, cart);
828 bool SimulatorModule::subsampleFovea(yarp::sig::ImageOf<yarp::sig::PixelRgb>& dst,
829 const yarp::sig::ImageOf<yarp::sig::PixelRgb>& src) {
832 return iCub::logpolar::subsampleFovea(dst, src);
virtual bool getTrqData(yarp::os::Bottle data)=0
virtual void clearBuffer()=0
Signal that we're done with a view.
virtual void drawView(bool left, bool right, bool wide)=0
Render the requested view.
virtual void simLoop(int h, int w)=0
Run the simulation.
virtual void init(RobotStreamer *streamer, RobotConfig *config)=0
Initialization.
virtual bool getImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &img)=0
virtual bool shouldSendTouchRightHand()
virtual void sendTouchRightArm(yarp::os::Bottle &report)
virtual void sendTouchLeftArm(yarp::os::Bottle &report)
virtual bool shouldSendTouchLeftArm()
virtual void sendTouchTorso(yarp::os::Bottle &report)
virtual bool shouldSendTouchLeftHand()
virtual void sendSkinEvents(iCub::skinDynLib::skinContactList &skinContactListReport)
virtual void sendTouchLeftForearm(yarp::os::Bottle &report)
virtual void sendTouchLeftHand(yarp::os::Bottle &report)
SimulatorModule(WorldManager &world, RobotConfig &config, Simulation *sim)
virtual void sendTouchRightForearm(yarp::os::Bottle &report)
bool read(yarp::os::ConnectionReader &connection)
virtual bool shouldSendTouchTorso()
virtual bool shouldSendTouchLeftForearm()
bool respond(const yarp::os::Bottle &command, yarp::os::Bottle &reply)
virtual bool shouldSendTouchRightForearm()
virtual void sendVision()
virtual bool shouldSendInertial()
virtual bool shouldSendTouchRightArm()
virtual void sendTouchRightHand(yarp::os::Bottle &report)
virtual void sendInertial(yarp::os::Bottle &report)
virtual bool shouldSendSkinEvents()
virtual bool respond(const yarp::os::Bottle &command, yarp::os::Bottle &reply)
static RobotConfig * robot_config
std::string toString(const T &t)