00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <uwsim/ConfigXMLParser.h>
00014 #include <uwsim/SimulatorConfig.h>
00015 #include <osgDB/FileUtils>
00016
00017 void ConfigFile::esPi(string in, double ¶m)
00018 {
00019 in.erase(0, in.find_first_not_of("\t "));
00020 in.erase(in.find_last_not_of("\t ") + 1, -1);
00021
00022 if (in == "M_PI")
00023 param = M_PI;
00024 else if (in == "M_PI_2")
00025 param = M_PI_2;
00026 else if (in == "M_PI_4")
00027 param = M_PI_4;
00028 else if (in == "-M_PI")
00029 param = -M_PI;
00030 else if (in == "-M_PI_2")
00031 param = -M_PI_2;
00032 else if (in == "-M_PI_4")
00033 param = -M_PI_4;
00034 else
00035 param = boost::lexical_cast<double>(in.c_str());
00036 }
00037
00038 void ConfigFile::extractFloatChar(const xmlpp::Node* node, double ¶m)
00039 {
00040 xmlpp::Node::NodeList list = node->get_children();
00041
00042 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00043 {
00044 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(*iter);
00045 if (nodeText)
00046 esPi(nodeText->get_content(), param);
00047
00048 }
00049 }
00050
00051 void ConfigFile::extractIntChar(const xmlpp::Node* node, int ¶m)
00052 {
00053 xmlpp::Node::NodeList list = node->get_children();
00054
00055 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00056 {
00057 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(*iter);
00058 if (nodeText)
00059 param = atoi(nodeText->get_content().c_str());
00060 }
00061 }
00062
00063 void ConfigFile::extractUIntChar(const xmlpp::Node* node, unsigned int ¶m)
00064 {
00065 xmlpp::Node::NodeList list = node->get_children();
00066
00067 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00068 {
00069 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(*iter);
00070 if (nodeText)
00071 param = (unsigned int)atoi(nodeText->get_content().c_str());
00072 }
00073 }
00074
00075 void ConfigFile::extractStringChar(const xmlpp::Node* node, string ¶m)
00076 {
00077 xmlpp::Node::NodeList list = node->get_children();
00078
00079 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00080 {
00081 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(*iter);
00082 if (nodeText)
00083 param = nodeText->get_content();
00084 param.erase(0, param.find_first_not_of("\t "));
00085 param.erase(param.find_last_not_of("\t ") + 1, -1);
00086 }
00087 }
00088
00089 void ConfigFile::extractPositionOrColor(const xmlpp::Node* node, double param[3])
00090 {
00091 xmlpp::Node::NodeList list = node->get_children();
00092 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00093 {
00094 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00095
00096 if (child->get_name() == "x" || child->get_name() == "r")
00097 extractFloatChar(child, param[0]);
00098 else if (child->get_name() == "y" || child->get_name() == "g")
00099 extractFloatChar(child, param[1]);
00100 else if (child->get_name() == "z" || child->get_name() == "b")
00101 extractFloatChar(child, param[2]);
00102 }
00103 }
00104
00105 void ConfigFile::extractOrientation(const xmlpp::Node* node, double param[3])
00106 {
00107 xmlpp::Node::NodeList list = node->get_children();
00108 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00109 {
00110 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00111
00112 if (child->get_name() == "r")
00113 extractFloatChar(child, param[0]);
00114 else if (child->get_name() == "p")
00115 extractFloatChar(child, param[1]);
00116 else if (child->get_name() == "y")
00117 extractFloatChar(child, param[2]);
00118 }
00119 }
00120
00121 void ConfigFile::processFog(const xmlpp::Node* node)
00122 {
00123
00124 xmlpp::Node::NodeList list = node->get_children();
00125 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00126 {
00127 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00128 if (child->get_name() == "density")
00129 extractFloatChar(child, fogDensity);
00130 else if (child->get_name() == "color")
00131 extractPositionOrColor(child, fogColor);
00132 }
00133 }
00134
00135 void ConfigFile::processOceanState(const xmlpp::Node* node)
00136 {
00137
00138 xmlpp::Node::NodeList list = node->get_children();
00139 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00140 {
00141 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00142
00143 if (child->get_name() == "windx")
00144 extractFloatChar(child, windx);
00145 else if (child->get_name() == "windy")
00146 extractFloatChar(child, windy);
00147 else if (child->get_name() == "windSpeed")
00148 extractFloatChar(child, windSpeed);
00149 else if (child->get_name() == "depth")
00150 extractFloatChar(child, depth);
00151 else if (child->get_name() == "reflectionDamping")
00152 extractFloatChar(child, reflectionDamping);
00153 else if (child->get_name() == "waveScale")
00154 extractFloatChar(child, waveScale);
00155 else if (child->get_name() == "isNotChoppy")
00156 {
00157 extractIntChar(child, isNotChoppy);
00158 if (isNotChoppy != 0 && isNotChoppy != 1)
00159 {
00160 osg::notify(osg::ALWAYS) << "ConfigFile::processOceanState: isNotChoppy is not a binary value ( 0 1), using default value (1)"
00161 << std::endl;
00162 isNotChoppy = 1;
00163 }
00164 }
00165 else if (child->get_name() == "choppyFactor")
00166 extractFloatChar(child, choppyFactor);
00167 else if (child->get_name() == "crestFoamHeight")
00168 extractFloatChar(child, crestFoamHeight);
00169 else if (child->get_name() == "oceanSurfaceHeight")
00170 extractFloatChar(child, oceanSurfaceHeight);
00171 else if (child->get_name() == "fog")
00172 processFog(child);
00173 else if (child->get_name() == "color")
00174 extractPositionOrColor(child, color);
00175 else if (child->get_name() == "attenuation")
00176 extractPositionOrColor(child, attenuation);
00177 }
00178
00179 }
00180
00181 void ConfigFile::processSimParams(const xmlpp::Node* node)
00182 {
00183 xmlpp::Node::NodeList list = node->get_children();
00184 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00185 {
00186 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00187
00188 if (child->get_name() == "disableShaders")
00189 {
00190 extractIntChar(child, disableShaders);
00191 if (disableShaders != 0 && disableShaders != 1)
00192 {
00193 osg::notify(osg::ALWAYS) << "ConfigFile::processSimParams: disableShaders is not a binary value ( 0 1), using default value (0)"
00194 << std::endl;
00195 disableShaders = 0;
00196 }
00197 }
00198 else if (child->get_name() == "eye_in_hand")
00199 {
00200 extractIntChar(child, eye_in_hand);
00201 if (eye_in_hand != 0 && eye_in_hand != 1)
00202 {
00203 osg::notify(osg::ALWAYS) << "ConfigFile::processSimParams: eye_in_hand is not a binary value ( 0 1), using default value (0)"
00204 << std::endl;
00205 eye_in_hand = 0;
00206 }
00207 }
00208 else if (child->get_name() == "resw")
00209 extractIntChar(child, resw);
00210 else if (child->get_name() == "resh")
00211 extractIntChar(child, resh);
00212 else if (child->get_name() == "offsetp")
00213 extractPositionOrColor(child, offsetp);
00214 else if (child->get_name() == "offsetr")
00215 extractPositionOrColor(child, offsetr);
00216 else if (child->get_name() == "gravity")
00217 extractPositionOrColor(child, physicsConfig.gravity);
00218 else if (child->get_name() == "enablePhysics")
00219 {
00220 extractIntChar(child, enablePhysics);
00221 if (enablePhysics != 0 && enablePhysics != 1)
00222 {
00223 osg::notify(osg::ALWAYS) << "ConfigFile::processSimParams: enablePhysics is not a binary value ( 0 1), using default value (0)"
00224 << std::endl;
00225 enablePhysics = 0;
00226 }
00227 }
00228 else if (child->get_name() == "physicsFrequency")
00229 extractFloatChar(child, physicsConfig.frequency);
00230 else if (child->get_name() == "physicsSubSteps")
00231 extractIntChar(child, physicsConfig.subSteps);
00232 else if (child->get_name() == "physicsSolver"){
00233 std::string aux;
00234 extractStringChar(child, aux);
00235 if(aux=="Dantzig" || aux=="dantzig")
00236 physicsConfig.solver=PhysicsConfig::Dantzig;
00237 else if(aux=="SolveProjectedGauss" || aux=="solveProjectedGauss")
00238 physicsConfig.solver=PhysicsConfig::SolveProjectedGauss;
00239 else if( aux=="SequentialImpulse" || aux=="sequentialImpulse")
00240 physicsConfig.solver=PhysicsConfig::SequentialImpulse;
00241 else
00242 {
00243 osg::notify(osg::ALWAYS) << "ConfigFile::processSimParams: unknown physicsSolver, available solvers are Dantzig"<<
00244 ", SolveProjectedGauss and SequentialImpulse. Using default Dantzig."<< std::endl;
00245 physicsConfig.solver=PhysicsConfig::Dantzig;
00246 }
00247 }
00248 else if (child->get_name() == "showTrajectory")
00249 {
00250 ShowTrajectory aux;
00251 aux.init();
00252 processShowTrajectory(child, aux);
00253 trajectories.push_back(aux);
00254 }
00255 else if (child->get_name() == "lightRate")
00256 extractFloatChar(child, lightRate);
00257 }
00258 }
00259
00260 void ConfigFile::processShowTrajectory(const xmlpp::Node* node, ShowTrajectory & trajectory)
00261 {
00262 xmlpp::Node::NodeList list = node->get_children();
00263 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00264 {
00265 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00266 if (child->get_name() == "target")
00267 extractStringChar(child, trajectory.target);
00268 else if (child->get_name() == "color")
00269 extractPositionOrColor(child, trajectory.color);
00270 else if (child->get_name() == "lineStyle")
00271 extractIntChar(child, trajectory.lineStyle);
00272 else if (child->get_name() == "timeWindow"){
00273 extractFloatChar(child, trajectory.timeWindow);
00274 }
00275 }
00276 }
00277
00278 void ConfigFile::processParameters(const xmlpp::Node* node, Parameters *params)
00279 {
00280 xmlpp::Node::NodeList list = node->get_children();
00281 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00282 {
00283 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00284
00285 if (child->get_name() == "fx")
00286 extractFloatChar(child, params->fx);
00287 else if (child->get_name() == "fy")
00288 extractFloatChar(child, params->fy);
00289 else if (child->get_name() == "x0")
00290 extractFloatChar(child, params->x0);
00291 else if (child->get_name() == "y0")
00292 extractFloatChar(child, params->y0);
00293 else if (child->get_name() == "f")
00294 extractFloatChar(child, params->f);
00295 else if (child->get_name() == "n")
00296 extractFloatChar(child, params->n);
00297 else if (child->get_name() == "k")
00298 extractFloatChar(child, params->k);
00299
00300 }
00301 }
00302
00303 void ConfigFile::processVcam(const xmlpp::Node* node, Vcam &vcam)
00304 {
00305 xmlpp::Node::NodeList list = node->get_children();
00306 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00307 {
00308 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00309
00310 if (child->get_name() == "resw")
00311 extractIntChar(child, vcam.resw);
00312 else if (child->get_name() == "resh")
00313 extractIntChar(child, vcam.resh);
00314 else if (child->get_name() == "position")
00315 extractPositionOrColor(child, vcam.position);
00316 else if (child->get_name() == "relativeTo")
00317 extractStringChar(child, vcam.linkName);
00318 else if (child->get_name() == "orientation")
00319 extractOrientation(child, vcam.orientation);
00320 else if (child->get_name() == "name")
00321 extractStringChar(child, vcam.name);
00322 else if (child->get_name() == "baseline")
00323 {
00324 extractFloatChar(child, vcam.baseLine);
00325 }
00326 else if (child->get_name() == "frameId")
00327 {
00328 extractStringChar(child, vcam.frameId);
00329 }
00330 else if (child->get_name() == "fovy")
00331 extractFloatChar(child, vcam.fov);
00332 else if (child->get_name() == "parameters")
00333 {
00334 vcam.parameters.reset(new Parameters());
00335 processParameters(child, vcam.parameters.get());
00336 }
00337 else if (child->get_name() == "showpath")
00338 extractFloatChar(child, vcam.showpath);
00339 else if (child->get_name() == "grayscale")
00340 {
00341 extractIntChar(child, vcam.bw);
00342 if (vcam.bw != 0 && vcam.bw != 1)
00343 {
00344 osg::notify(osg::ALWAYS) << "ConfigFile::processVcam: grayscale is not a binary value ( 0 1), using default value (0)"
00345 << std::endl;
00346 vcam.bw = 0;
00347 }
00348 }
00349 else if (child->get_name() == "std")
00350 extractFloatChar(child, vcam.std);
00351 }
00352
00353 }
00354
00355 void ConfigFile::processSLProjector(const xmlpp::Node* node, slProjector &slp)
00356 {
00357 xmlpp::Node::NodeList list = node->get_children();
00358 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00359 {
00360 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00361 if (child->get_name() == "position")
00362 extractPositionOrColor(child, slp.position);
00363 else if (child->get_name() == "relativeTo")
00364 extractStringChar(child, slp.linkName);
00365 else if (child->get_name() == "orientation")
00366 extractOrientation(child, slp.orientation);
00367 else if (child->get_name() == "name")
00368 extractStringChar(child, slp.name);
00369 else if (child->get_name() == "fov")
00370 extractFloatChar(child, slp.fov);
00371 else if (child->get_name() == "laser")
00372 extractIntChar(child, slp.laser);
00373 else if (child->get_name() == "image_name")
00374 extractStringChar(child, slp.image_name);
00375 }
00376 }
00377
00378 void ConfigFile::processRangeSensor(const xmlpp::Node* node, rangeSensor &rs)
00379 {
00380 xmlpp::Node::NodeList list = node->get_children();
00381 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00382 {
00383 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00384
00385 if (child->get_name() == "position")
00386 extractPositionOrColor(child, rs.position);
00387 else if (child->get_name() == "relativeTo")
00388 extractStringChar(child, rs.linkName);
00389 else if (child->get_name() == "orientation")
00390 extractOrientation(child, rs.orientation);
00391 else if (child->get_name() == "name")
00392 extractStringChar(child, rs.name);
00393 else if (child->get_name() == "range")
00394 extractFloatChar(child, rs.range);
00395 else if (child->get_name() == "visible")
00396 extractIntChar(child, rs.visible);
00397 }
00398 }
00399
00400 void ConfigFile::processImu(const xmlpp::Node* node, Imu &imu)
00401 {
00402 xmlpp::Node::NodeList list = node->get_children();
00403 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00404 {
00405 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00406
00407 if (child->get_name() == "position")
00408 extractPositionOrColor(child, imu.position);
00409 else if (child->get_name() == "relativeTo")
00410 extractStringChar(child, imu.linkName);
00411 else if (child->get_name() == "orientation")
00412 extractOrientation(child, imu.orientation);
00413 else if (child->get_name() == "name")
00414 extractStringChar(child, imu.name);
00415 else if (child->get_name() == "std")
00416 extractFloatChar(child, imu.std);
00417 }
00418 }
00419
00420 void ConfigFile::processPressureSensor(const xmlpp::Node* node, XMLPressureSensor &ps)
00421 {
00422 xmlpp::Node::NodeList list = node->get_children();
00423 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00424 {
00425 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00426
00427 if (child->get_name() == "position")
00428 extractPositionOrColor(child, ps.position);
00429 else if (child->get_name() == "relativeTo")
00430 extractStringChar(child, ps.linkName);
00431 else if (child->get_name() == "orientation")
00432 extractOrientation(child, ps.orientation);
00433 else if (child->get_name() == "name")
00434 extractStringChar(child, ps.name);
00435 else if (child->get_name() == "std")
00436 extractFloatChar(child, ps.std);
00437 }
00438 }
00439
00440 void ConfigFile::processGPSSensor(const xmlpp::Node* node, XMLGPSSensor &s)
00441 {
00442 xmlpp::Node::NodeList list = node->get_children();
00443 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00444 {
00445 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00446
00447 if (child->get_name() == "position")
00448 extractPositionOrColor(child, s.position);
00449 else if (child->get_name() == "relativeTo")
00450 extractStringChar(child, s.linkName);
00451 else if (child->get_name() == "orientation")
00452 extractOrientation(child, s.orientation);
00453 else if (child->get_name() == "name")
00454 extractStringChar(child, s.name);
00455 else if (child->get_name() == "std")
00456 extractFloatChar(child, s.std);
00457 }
00458 }
00459
00460 void ConfigFile::processDVLSensor(const xmlpp::Node* node, XMLDVLSensor &s)
00461 {
00462 xmlpp::Node::NodeList list = node->get_children();
00463 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00464 {
00465 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00466
00467 if (child->get_name() == "position")
00468 extractPositionOrColor(child, s.position);
00469 else if (child->get_name() == "relativeTo")
00470 extractStringChar(child, s.linkName);
00471 else if (child->get_name() == "orientation")
00472 extractOrientation(child, s.orientation);
00473 else if (child->get_name() == "name")
00474 extractStringChar(child, s.name);
00475 else if (child->get_name() == "std")
00476 extractFloatChar(child, s.std);
00477 }
00478 }
00479
00480 void ConfigFile::processCamera(const xmlpp::Node* node)
00481 {
00482 xmlpp::Node::NodeList list = node->get_children();
00483 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00484 {
00485 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00486
00487 if (child->get_name() == "freeMotion")
00488 {
00489 extractIntChar(child, freeMotion);
00490 if (freeMotion != 0 && freeMotion != 1)
00491 {
00492 osg::notify(osg::ALWAYS) << "ConfigFile::processCamera: freeMotion is not a binary value ( 0 1), using default value (1)"
00493 << std::endl;
00494 freeMotion = 1;
00495 }
00496 }
00497 else if (child->get_name() == "fov")
00498 extractFloatChar(child, camFov);
00499 else if (child->get_name() == "aspectRatio")
00500 extractFloatChar(child, camAspectRatio);
00501 else if (child->get_name() == "near")
00502 extractFloatChar(child, camNear);
00503 else if (child->get_name() == "far")
00504 extractFloatChar(child, camFar);
00505 else if (child->get_name() == "position")
00506 extractPositionOrColor(child, camPosition);
00507 else if (child->get_name() == "lookAt")
00508 extractPositionOrColor(child, camLookAt);
00509 if (child->get_name() == "objectToTrack")
00510 extractStringChar(child, vehicleToTrack);
00511 }
00512 }
00513
00514 void ConfigFile::processMultibeamSensor(const xmlpp::Node* node, XMLMultibeamSensor &MB)
00515 {
00516 xmlpp::Node::NodeList list = node->get_children();
00517 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00518 {
00519 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00520
00521 if (child->get_name() == "position")
00522 extractPositionOrColor(child, MB.position);
00523 else if (child->get_name() == "relativeTo")
00524 extractStringChar(child, MB.linkName);
00525 else if (child->get_name() == "orientation")
00526 extractOrientation(child, MB.orientation);
00527 else if (child->get_name() == "name")
00528 extractStringChar(child, MB.name);
00529 else if (child->get_name() == "initAngle")
00530 extractFloatChar(child, MB.initAngle);
00531 else if (child->get_name() == "finalAngle")
00532 extractFloatChar(child, MB.finalAngle);
00533 else if (child->get_name() == "angleIncr")
00534 extractFloatChar(child, MB.angleIncr);
00535 else if (child->get_name() == "range")
00536 extractFloatChar(child, MB.range);
00537 else if (child->get_name() == "visible")
00538 extractIntChar(child, MB.visible);
00539 }
00540 }
00541
00542 void ConfigFile::processPose(urdf::Pose pose, double position[3], double rpy[3], double quat[4])
00543 {
00544 position[0] = pose.position.x;
00545 position[1] = pose.position.y;
00546 position[2] = pose.position.z;
00547 pose.rotation.getRPY(rpy[0], rpy[1], rpy[2]);
00548 pose.rotation.getQuaternion(quat[0], quat[1], quat[2], quat[3]);
00549 }
00550
00551 void ConfigFile::processGeometry(urdf::Geometry * geometry, Geometry * geom)
00552 {
00553
00554 geom->scale[0] = 1;
00555 geom->scale[1] = 1;
00556 geom->scale[2] = 1;
00557 if (geometry->type == urdf::Geometry::MESH)
00558 {
00559 urdf::Mesh *mesh = dynamic_cast<urdf::Mesh*>(geometry);
00560 geom->file = mesh->filename;
00561 geom->type = 0;
00562 geom->scale[0] = mesh->scale.x;
00563 geom->scale[1] = mesh->scale.y;
00564 geom->scale[2] = mesh->scale.z;
00565 }
00566 else if (geometry->type == urdf::Geometry::BOX)
00567 {
00568 urdf::Box *box = dynamic_cast<urdf::Box*>(geometry);
00569 geom->type = 1;
00570 geom->boxSize[0] = box->dim.x;
00571 geom->boxSize[1] = box->dim.y;
00572 geom->boxSize[2] = box->dim.z;
00573 }
00574 else if (geometry->type == urdf::Geometry::CYLINDER)
00575 {
00576 urdf::Cylinder *cylinder = dynamic_cast<urdf::Cylinder*>(geometry);
00577 geom->type = 2;
00578 geom->length = cylinder->length;
00579 geom->radius = cylinder->radius;
00580 }
00581 else if (geometry->type == urdf::Geometry::SPHERE)
00582 {
00583 urdf::Sphere *sphere = dynamic_cast<urdf::Sphere*>(geometry);
00584 geom->type = 3;
00585 geom->radius = sphere->radius;
00586 }
00587 }
00588
00589 void ConfigFile::processVisual(boost::shared_ptr<const urdf::Visual> visual, Link &link,
00590 std::map<std::string, Material> &materials)
00591 {
00592 processGeometry(visual->geometry.get(), link.geom.get());
00593 processPose(visual->origin, link.position, link.rpy, link.quat);
00594
00595
00596 link.material = visual->material_name;
00597 if (visual->material && materials.find(visual->material_name) == materials.end())
00598 {
00599 Material m;
00600 m.name = visual->material_name;
00601 m.r = visual->material->color.r;
00602 m.g = visual->material->color.g;
00603 m.b = visual->material->color.b;
00604 m.a = visual->material->color.a;
00605 materials[m.name] = m;
00606 }
00607 }
00608
00609 void ConfigFile::processJoint(boost::shared_ptr<const urdf::Joint> joint, Joint &jointVehicle, int parentLink,
00610 int childLink)
00611 {
00612 jointVehicle.name = joint->name;
00613 jointVehicle.axis[0] = joint->axis.x;
00614 jointVehicle.axis[1] = joint->axis.y;
00615 jointVehicle.axis[2] = joint->axis.z;
00616 processPose(joint->parent_to_joint_origin_transform, jointVehicle.position, jointVehicle.rpy, jointVehicle.quat);
00617 jointVehicle.child = childLink;
00618 jointVehicle.parent = parentLink;
00619
00620 if (joint->type == 6)
00621 jointVehicle.type = 0;
00622 else if (joint->type == 1 || joint->type == 2)
00623 jointVehicle.type = 1;
00624 else if (joint->type == 3)
00625 jointVehicle.type = 2;
00626 else
00627 {
00628 osg::notify(osg::ALWAYS) << "Unsupported type of joint in " << joint->name << ", fixed joint will be used." << std::endl;
00629 jointVehicle.type = 0;
00630 }
00631
00632
00633 if (joint->mimic != NULL)
00634 {
00635 jointVehicle.mimic.reset(new Mimic);
00636 jointVehicle.mimic->jointName = joint->mimic->joint_name;
00637 jointVehicle.mimic->offset = joint->mimic->offset;
00638 jointVehicle.mimic->multiplier = joint->mimic->multiplier;
00639 }
00640 else
00641 jointVehicle.mimic.reset();
00642
00643
00644 if (joint->limits != NULL)
00645 {
00646 jointVehicle.lowLimit = joint->limits->lower;
00647 jointVehicle.upLimit = joint->limits->upper;
00648 }
00649 else
00650 {
00651 jointVehicle.lowLimit = -M_PI;
00652 jointVehicle.upLimit = M_PI;
00653 }
00654 }
00655
00656 int ConfigFile::processLink(boost::shared_ptr<const urdf::Link> link, Vehicle &vehicle, int nlink, int njoint,
00657 std::map<std::string, Material> &materials)
00658 {
00659 vehicle.links[nlink].name = link->name;
00660 vehicle.links[nlink].geom.reset(new Geometry);
00661 if (link->visual)
00662 processVisual(link->visual, vehicle.links[nlink], materials);
00663 else
00664 {
00665 vehicle.links[nlink].geom->scale[0] = 1;
00666 vehicle.links[nlink].geom->scale[1] = 1;
00667 vehicle.links[nlink].geom->scale[2] = 1;
00668 vehicle.links[nlink].geom->type = 4;
00669 vehicle.links[nlink].material = std::string();
00670 memset(vehicle.links[nlink].position, 0, 3 * sizeof(double));
00671 memset(vehicle.links[nlink].rpy, 0, 3 * sizeof(double));
00672 memset(vehicle.links[nlink].quat, 0, 4 * sizeof(double));
00673 vehicle.links[nlink].quat[3] = 1;
00674 }
00675
00676 if (link->collision)
00677 {
00678 vehicle.links[nlink].cs.reset(new Geometry);
00679 processGeometry(link->collision->geometry.get(), vehicle.links[nlink].cs.get());
00680 }
00681 else
00682 vehicle.links[nlink].cs.reset();
00683
00684 if(link->inertial)
00685 vehicle.links[nlink].mass=link->inertial->mass;
00686 else
00687 vehicle.links[nlink].mass=-1;
00688
00689 int linkNumber = nlink;
00690 for (uint i = 0; i < link->child_joints.size(); i++)
00691 {
00692 processJoint(link->child_joints[i], vehicle.joints[linkNumber], nlink, linkNumber + 1);
00693 linkNumber = processLink(link->child_links[i], vehicle, linkNumber + 1, linkNumber + 1, materials);
00694 }
00695 return linkNumber;
00696 }
00697
00698 int ConfigFile::processURDFFile(string file, Vehicle &vehicle)
00699 {
00700 urdf::Model model;
00701
00702 std::string file_fullpath = osgDB::findDataFile(file);
00703 if (file_fullpath == std::string("") || !model.initFile(file_fullpath))
00704 {
00705 osg::notify(osg::ALWAYS) << "Failed to parse urdf file " << file << std::endl;
00706 exit(0);
00707 }
00708 vehicle.URDFFile=file_fullpath;
00709 OSG_INFO << "Successfully parsed urdf file " << file << std::endl;
00710
00711 vehicle.nlinks = model.links_.size();
00712 vehicle.links.resize(vehicle.nlinks);
00713 vehicle.njoints = model.joints_.size();
00714 vehicle.joints.resize(vehicle.njoints);
00715 boost::shared_ptr<const urdf::Link> root = model.getRoot();
00716 processLink(root, vehicle, 0, 0, vehicle.materials);
00717 return 0;
00718 }
00719
00720 void ConfigFile::processJointValues(const xmlpp::Node* node, std::vector<double> &jointValues, int &ninitJoints)
00721 {
00722 xmlpp::Node::NodeList list = node->get_children();
00723 ninitJoints = (list.size() - 1) / 2;
00724 jointValues.resize((list.size() - 1) / 2);
00725 int pos = 0;
00726 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00727 {
00728 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00729 if (child->get_name() == "joint")
00730 {
00731 extractFloatChar(child, jointValues[pos++]);
00732 }
00733 }
00734 }
00735
00736 void ConfigFile::postprocessVehicle(Vehicle &vehicle)
00737 {
00738
00739 int found;
00740 for (int i = 0; i < vehicle.njoints; i++)
00741 {
00742 if (vehicle.joints[i].mimic != NULL)
00743 {
00744 found = 0;
00745 for (int j = 0; j < vehicle.njoints && !found; j++)
00746 {
00747 if (vehicle.joints[j].name == vehicle.joints[i].mimic->jointName)
00748 {
00749 vehicle.joints[i].mimicp = j;
00750 found = 1;
00751 }
00752 }
00753 if (found == 0)
00754 {
00755 vehicle.joints[i].mimicp = -1;
00756 }
00757 }
00758 else
00759 {
00760 vehicle.joints[i].mimicp = -1;
00761 }
00762 }
00763
00764
00765 Vcam aux;
00766 for (unsigned int i = 0; i < vehicle.Vcams.size(); i++)
00767 {
00768 found = 0;
00769 aux = vehicle.Vcams.front();
00770 vehicle.Vcams.pop_front();
00771 for (int j = 0; j < vehicle.nlinks && !found; j++)
00772 {
00773 if (vehicle.links[j].name == aux.linkName)
00774 {
00775 aux.link = j;
00776 found = 1;
00777 }
00778 }
00779 if (found == 0)
00780 {
00781 osg::notify(osg::ALWAYS) << "ConfigFile::postProcessVehicle: Camera attached to unknown link: " << aux.linkName
00782 << " camera will be ignored" << std::endl;
00783 }
00784 else
00785 vehicle.Vcams.push_back(aux);
00786 }
00787
00788
00789 Vcam aux2;
00790 for (unsigned int i = 0; i < vehicle.VRangecams.size(); i++)
00791 {
00792 found = 0;
00793 aux2 = vehicle.VRangecams.front();
00794 vehicle.VRangecams.pop_front();
00795 for (int j = 0; j < vehicle.nlinks && !found; j++)
00796 {
00797 if (vehicle.links[j].name == aux2.linkName)
00798 {
00799 aux2.link = j;
00800 found = 1;
00801 }
00802 }
00803 if (found == 0)
00804 {
00805 osg::notify(osg::ALWAYS) << "ConfigFile::postProcessVehicle: Range Camera attached to unknown link: " << aux.linkName
00806 << " camera will be ignored" << std::endl;
00807 }
00808 else
00809 vehicle.VRangecams.push_back(aux2);
00810 }
00811
00812
00813 slProjector slp;
00814 for (unsigned int i = 0; i < vehicle.sls_projectors.size(); i++)
00815 {
00816 found = 0;
00817 slp = vehicle.sls_projectors.front();
00818 vehicle.sls_projectors.pop_front();
00819 for (int j = 0; j < vehicle.nlinks && !found; j++)
00820 {
00821 if (vehicle.links[j].name == slp.linkName)
00822 {
00823 slp.link = j;
00824 found = 1;
00825 }
00826 }
00827 if (found == 0)
00828 {
00829 osg::notify(osg::ALWAYS) << "ConfigFile::postProcessVehicle: Structured Light Projector attached to unknown link: "
00830 << slp.linkName << ". Will be ignored" << std::endl;
00831 }
00832 else
00833 {
00834 vehicle.sls_projectors.push_back(slp);
00835 }
00836 }
00837
00838
00839 rangeSensor rs;
00840 for (unsigned int i = 0; i < vehicle.range_sensors.size(); i++)
00841 {
00842 found = 0;
00843 rs = vehicle.range_sensors.front();
00844 vehicle.range_sensors.pop_front();
00845 for (int j = 0; j < vehicle.nlinks && !found; j++)
00846 {
00847 if (vehicle.links[j].name == rs.linkName)
00848 {
00849 rs.link = j;
00850 found = 1;
00851 }
00852 }
00853 if (found == 0)
00854 {
00855 osg::notify(osg::ALWAYS) << "ConfigFile::postProcessVehicle: RangeSensor attached to unknown link: " << rs.linkName
00856 << ". Will be ignored" << std::endl;
00857 }
00858 else
00859 vehicle.range_sensors.push_back(rs);
00860 }
00861
00862
00863 Imu imu;
00864 for (unsigned int i = 0; i < vehicle.imus.size(); i++)
00865 {
00866 found = 0;
00867 imu = vehicle.imus.front();
00868 vehicle.imus.pop_front();
00869 for (int j = 0; j < vehicle.nlinks && !found; j++)
00870 {
00871 if (vehicle.links[j].name == imu.linkName)
00872 {
00873 imu.link = j;
00874 found = 1;
00875 }
00876 }
00877 if (found == 0)
00878 {
00879 osg::notify(osg::ALWAYS) << "ConfigFile::postProcessVehicle: IMU attached to unknown link: " << imu.linkName
00880 << ". Will be ignored" << std::endl;
00881 }
00882 else
00883 vehicle.imus.push_back(imu);
00884 }
00885
00886
00887 XMLPressureSensor ps;
00888 for (unsigned int i = 0; i < vehicle.pressure_sensors.size(); i++)
00889 {
00890 found = 0;
00891 ps = vehicle.pressure_sensors.front();
00892 vehicle.pressure_sensors.pop_front();
00893 for (int j = 0; j < vehicle.nlinks && !found; j++)
00894 {
00895 if (vehicle.links[j].name == ps.linkName)
00896 {
00897 ps.link = j;
00898 found = 1;
00899 }
00900 }
00901 if (found == 0)
00902 {
00903 osg::notify(osg::ALWAYS) << "ConfigFile::postProcessVehicle: PressureSensor attached to unknown link: " << ps.linkName
00904 << ". Will be ignored" << std::endl;
00905 }
00906 else
00907 vehicle.pressure_sensors.push_back(ps);
00908 }
00909
00910
00911 {
00912 XMLGPSSensor s;
00913 for (unsigned int i = 0; i < vehicle.gps_sensors.size(); i++)
00914 {
00915 found = 0;
00916 s = vehicle.gps_sensors.front();
00917 vehicle.gps_sensors.pop_front();
00918 for (int j = 0; j < vehicle.nlinks && !found; j++)
00919 {
00920 if (vehicle.links[j].name == s.linkName)
00921 {
00922 s.link = j;
00923 found = 1;
00924 }
00925 }
00926 if (found == 0)
00927 {
00928 osg::notify(osg::ALWAYS) << "ConfigFile::postProcessVehicle: GPSSensor attached to unknown link: " << s.linkName
00929 << ". Will be ignored" << std::endl;
00930 }
00931 else
00932 vehicle.gps_sensors.push_back(s);
00933 }
00934 }
00935
00936
00937 {
00938 XMLDVLSensor s;
00939 for (unsigned int i = 0; i < vehicle.dvl_sensors.size(); i++)
00940 {
00941 found = 0;
00942 s = vehicle.dvl_sensors.front();
00943 vehicle.dvl_sensors.pop_front();
00944 for (int j = 0; j < vehicle.nlinks && !found; j++)
00945 {
00946 if (vehicle.links[j].name == s.linkName)
00947 {
00948 s.link = j;
00949 found = 1;
00950 }
00951 }
00952 if (found == 0)
00953 {
00954 osg::notify(osg::ALWAYS) << "ConfigFile::postProcessVehicle: DVLSensor attached to unknown link: " << s.linkName
00955 << ". Will be ignored" << std::endl;
00956 }
00957 else
00958 vehicle.dvl_sensors.push_back(s);
00959 }
00960 }
00961
00962
00963 {
00964 XMLMultibeamSensor MB;
00965 for (unsigned int i = 0; i < vehicle.multibeam_sensors.size(); i++)
00966 {
00967 found = 0;
00968 MB = vehicle.multibeam_sensors.front();
00969 vehicle.multibeam_sensors.pop_front();
00970 for (int j = 0; j < vehicle.nlinks && !found; j++)
00971 {
00972 if (vehicle.links[j].name == MB.linkName)
00973 {
00974 MB.link = j;
00975 found = 1;
00976 }
00977 }
00978 if (found == 0)
00979 {
00980 osg::notify(osg::ALWAYS) << "ConfigFile::postProcessVehicle: multibeamSensor attached to unknown link: " << MB.linkName
00981 << ". Will be ignored" << std::endl;
00982 }
00983 else
00984 vehicle.multibeam_sensors.push_back(MB);
00985 }
00986 }
00987
00988
00989 for (unsigned int i = 0; i < vehicle.object_pickers.size(); i++)
00990 {
00991 found = 0;
00992 rs = vehicle.object_pickers.front();
00993 vehicle.object_pickers.pop_front();
00994 for (int j = 0; j < vehicle.nlinks && !found; j++)
00995 {
00996 if (vehicle.links[j].name == rs.linkName)
00997 {
00998 rs.link = j;
00999 found = 1;
01000 }
01001 }
01002 if (found == 0)
01003 {
01004 osg::notify(osg::ALWAYS) << "ObjectPicker attached to unknown link: " << rs.linkName << ". Will be ignored" << std::endl;
01005 }
01006 else
01007 vehicle.object_pickers.push_back(rs);
01008 }
01009 }
01010
01011 void ConfigFile::processVehicle(const xmlpp::Node* node, Vehicle &vehicle)
01012 {
01013 xmlpp::Node::NodeList list = node->get_children();
01014 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
01015 {
01016 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
01017
01018 if (child->get_name() == "name")
01019 extractStringChar(child, vehicle.name);
01020 else if (child->get_name() == "file")
01021 {
01022 string aux;
01023 extractStringChar(child, aux);
01024 processURDFFile(aux, vehicle);
01025 }
01026 else if (child->get_name() == "position")
01027 extractPositionOrColor(child, vehicle.position);
01028 else if (child->get_name() == "orientation")
01029 extractOrientation(child, vehicle.orientation);
01030 else if (child->get_name() == "scaleFactor")
01031 extractPositionOrColor(child, vehicle.scale);
01032 else if (child->get_name() == "jointValues")
01033 processJointValues(child, vehicle.jointValues, vehicle.ninitJoints);
01034 else if (child->get_name() == "virtualCamera")
01035 {
01036 Vcam aux;
01037 aux.init();
01038 processVcam(child, aux);
01039 vehicle.Vcams.push_back(aux);
01040 }
01041 else if (child->get_name() == "virtualRangeImage")
01042 {
01043 Vcam aux;
01044 aux.init();
01045 aux.range = 1;
01046 processVcam(child, aux);
01047 vehicle.VRangecams.push_back(aux);
01048 }
01049 else if (child->get_name() == "structuredLightProjector")
01050 {
01051 slProjector aux;
01052 aux.init();
01053 processSLProjector(child, aux);
01054 vehicle.sls_projectors.push_back(aux);
01055 }
01056 else if (child->get_name() == "rangeSensor")
01057 {
01058 rangeSensor aux;
01059 aux.init();
01060 processRangeSensor(child, aux);
01061 vehicle.range_sensors.push_back(aux);
01062 }
01063 else if (child->get_name() == "objectPicker")
01064 {
01065 rangeSensor aux;
01066 aux.init();
01067 processRangeSensor(child, aux);
01068 vehicle.object_pickers.push_back(aux);
01069 }
01070 else if (child->get_name() == "imu")
01071 {
01072 Imu aux;
01073 aux.init();
01074 processImu(child, aux);
01075 vehicle.imus.push_back(aux);
01076 }
01077 else if (child->get_name() == "pressureSensor")
01078 {
01079 XMLPressureSensor aux;
01080 aux.init();
01081 processPressureSensor(child, aux);
01082 vehicle.pressure_sensors.push_back(aux);
01083 }
01084 else if (child->get_name() == "gpsSensor")
01085 {
01086 XMLGPSSensor aux;
01087 aux.init();
01088 processGPSSensor(child, aux);
01089 vehicle.gps_sensors.push_back(aux);
01090 }
01091 else if (child->get_name() == "dvlSensor")
01092 {
01093 XMLDVLSensor aux;
01094 aux.init();
01095 processDVLSensor(child, aux);
01096 vehicle.dvl_sensors.push_back(aux);
01097 }
01098 else if (child->get_name() == "multibeamSensor")
01099 {
01100 XMLMultibeamSensor aux;
01101 aux.init();
01102 const xmlpp::Attribute * atrib = dynamic_cast<const xmlpp::Element*>(child)->get_attribute("underwaterParticles");
01103 if(atrib and atrib->get_value()=="true")
01104 {
01105 aux.underwaterParticles=true;
01106 }
01107 processMultibeamSensor(child, aux);
01108 vehicle.multibeam_sensors.push_back(aux);
01109 }
01110 else
01111 {
01112 std::vector < uwsim::SimulatedDeviceConfig::Ptr > auxs = SimulatedDevices::processConfig(
01113 child, this, child->get_name() != "simulatedDevices");
01114 for (size_t i = 0; i < auxs.size(); ++i)
01115 if (auxs[i])
01116 vehicle.simulated_devices.push_back(auxs[i]);
01117 }
01118 }
01119 }
01120
01121 void ConfigFile::processPhysicProperties(const xmlpp::Node* node, PhysicProperties &pp)
01122 {
01123 xmlpp::Node::NodeList list = node->get_children();
01124 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
01125 {
01126 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
01127
01128 if (child->get_name() == "mass")
01129 extractFloatChar(child, pp.mass);
01130 else if (child->get_name() == "inertia")
01131 extractPositionOrColor(child, pp.inertia);
01132 else if (child->get_name() == "collisionShapeType")
01133 extractStringChar(child, pp.csType);
01134 else if (child->get_name() == "collisionShape")
01135 extractStringChar(child, pp.cs);
01136 else if (child->get_name() == "linearDamping")
01137 {
01138 extractFloatChar(child, pp.linearDamping);
01139 if (pp.linearDamping > 1.0)
01140 osg::notify(osg::ALWAYS) << "ConfigFile::PhysicProperties: linearDamping is higher than 1.0." << std::endl;
01141 }
01142 else if (child->get_name() == "angularDamping")
01143 {
01144 extractFloatChar(child, pp.angularDamping);
01145 if (pp.linearDamping > 1.0)
01146 osg::notify(osg::ALWAYS) << "ConfigFile::PhysicProperties: angularDamping is higher than 1.0." << std::endl;
01147 }
01148 else if (child->get_name() == "minLinearLimit")
01149 extractPositionOrColor(child, pp.minLinearLimit);
01150 else if (child->get_name() == "maxLinearLimit")
01151 extractPositionOrColor(child, pp.maxLinearLimit);
01152 else if (child->get_name() == "isKinematic")
01153 {
01154 extractIntChar(child, pp.isKinematic);
01155 if (pp.isKinematic != 0 && pp.isKinematic != 1)
01156 {
01157 osg::notify(osg::ALWAYS) << "ConfigFile::PhysicProperties: isKinematic is not a binary value ( 0 1), using default value (0)"
01158 << std::endl;
01159 freeMotion = 0;
01160 }
01161 }
01162 else if (child->get_name() == "minAngularLimit")
01163 extractPositionOrColor(child, pp.minAngularLimit);
01164 else if (child->get_name() == "maxAngularLimit")
01165 extractPositionOrColor(child, pp.maxAngularLimit);
01166
01167 }
01168 }
01169
01170 void ConfigFile::processObject(const xmlpp::Node* node, Object &object)
01171 {
01172 xmlpp::Node::NodeList list = node->get_children();
01173 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
01174 {
01175 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
01176
01177 if (child->get_name() == "name")
01178 extractStringChar(child, object.name);
01179 else if (child->get_name() == "file")
01180 extractStringChar(child, object.file);
01181 else if (child->get_name() == "position")
01182 extractPositionOrColor(child, object.position);
01183 else if (child->get_name() == "orientation")
01184 extractOrientation(child, object.orientation);
01185 else if (child->get_name() == "scaleFactor")
01186 extractPositionOrColor(child, object.scale);
01187 else if (child->get_name() == "offsetp")
01188 extractPositionOrColor(child, object.offsetp);
01189 else if (child->get_name() == "offsetr")
01190 extractPositionOrColor(child, object.offsetr);
01191 else if (child->get_name() == "physics")
01192 {
01193 object.physicProperties.reset(new PhysicProperties);
01194 object.physicProperties->init();
01195 processPhysicProperties(child, *object.physicProperties);
01196
01197 }
01198
01199 }
01200 }
01201
01202 void ConfigFile::processROSInterface(const xmlpp::Node* node, ROSInterfaceInfo &rosInterface)
01203 {
01204 xmlpp::Node::NodeList list = node->get_children();
01205 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
01206 {
01207 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
01208 if (child->get_name() != "text")
01209 {
01210 std::string text;
01211 extractStringChar(child, text);
01212 rosInterface.values[child->get_name()] = text;
01213 }
01214
01215 if (child->get_name() == "topic" || child->get_name() == "imageTopic")
01216 extractStringChar(child, rosInterface.topic);
01217 else if (child->get_name() == "vehicleName" || child->get_name() == "cameraName" || child->get_name() == "name")
01218 extractStringChar(child, rosInterface.targetName);
01219 else if (child->get_name() == "rate")
01220 extractIntChar(child, rosInterface.rate);
01221 else if (child->get_name() == "rootName")
01222 extractStringChar(child, rosInterface.rootName);
01223 else if (child->get_name() == "enableObjects")
01224 extractUIntChar(child, rosInterface.enableObjects);
01225 else if (child->get_name() == "infoTopic")
01226 extractStringChar(child, rosInterface.infoTopic);
01227 else if (child->get_name() == "width")
01228 extractUIntChar(child, rosInterface.w);
01229 else if (child->get_name() == "height")
01230 extractUIntChar(child, rosInterface.h);
01231 else if (child->get_name() == "posx")
01232 extractUIntChar(child, rosInterface.posx);
01233 else if (child->get_name() == "posy")
01234 extractUIntChar(child, rosInterface.posy);
01235 else if (child->get_name() == "blackWhite")
01236 {
01237 extractUIntChar(child, rosInterface.blackWhite);
01238 if (rosInterface.blackWhite != 0 && rosInterface.blackWhite != 1)
01239 {
01240 osg::notify(osg::ALWAYS) << "ConfigFile::processROSInterface: blackWhite is not a binary value ( 0 1), using default value (0)"
01241 << std::endl;
01242 rosInterface.blackWhite = 0;
01243 }
01244 }
01245 else if (child->get_name() == "depth")
01246 {
01247 extractUIntChar(child, rosInterface.depth);
01248 if (rosInterface.depth != 0 && rosInterface.depth != 1)
01249 {
01250 osg::notify(osg::ALWAYS)
01251 << "ConfigFile::processROSInterface: depth is not a binary value ( 0 1), using default value (0)"
01252 << std::endl;
01253 rosInterface.depth = 0;
01254 }
01255 }
01256 else if (child->get_name() == "scale")
01257 extractFloatChar(child, rosInterface.scale);
01258 else if (child->get_name() == "text")
01259 {
01260 }
01261 else
01262 osg::notify(osg::ALWAYS) << "processROSInterface:: unexpected child: " << child->get_name() << std::endl;
01263 }
01264 }
01265
01266 void ConfigFile::processROSInterfaces(const xmlpp::Node* node)
01267 {
01268 xmlpp::Node::NodeList list = node->get_children();
01269 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
01270 {
01271 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
01272 const xmlpp::Node* subchild = NULL;
01273
01274 ROSInterfaceInfo rosInterface;
01275 rosInterface.rate = 10;
01276 rosInterface.posx = rosInterface.posy = 0;
01277 rosInterface.scale = 1;
01278 rosInterface.type = ROSInterfaceInfo::Unknown;
01279 rosInterface.depth = 0;
01280 if (child->get_name() == "ROSOdomToPAT")
01281 {
01282 rosInterface.type = ROSInterfaceInfo::ROSOdomToPAT;
01283 }
01284 else if (child->get_name() == "PATToROSOdom")
01285 {
01286 rosInterface.type = ROSInterfaceInfo::PATToROSOdom;
01287 }
01288 else if (child->get_name() == "WorldToROSTF")
01289 {
01290 rosInterface.type = ROSInterfaceInfo::WorldToROSTF;
01291 }
01292 else if (child->get_name() == "ArmToROSJointState")
01293 {
01294 rosInterface.type = ROSInterfaceInfo::ArmToROSJointState;
01295 }
01296 else if (child->get_name() == "ROSJointStateToArm")
01297 {
01298 rosInterface.type = ROSInterfaceInfo::ROSJointStateToArm;
01299 }
01300 else if (child->get_name() == "VirtualCameraToROSImage")
01301 {
01302 rosInterface.type = ROSInterfaceInfo::VirtualCameraToROSImage;
01303 }
01304 else if (child->get_name() == "RangeImageSensorToROSImage")
01305 {
01306 rosInterface.type = ROSInterfaceInfo::RangeImageSensorToROSImage;
01307 }
01308 else if (child->get_name() == "RangeSensorToROSRange")
01309 {
01310 rosInterface.type = ROSInterfaceInfo::RangeSensorToROSRange;
01311 }
01312 else if (child->get_name() == "ROSImageToHUD")
01313 {
01314 rosInterface.type = ROSInterfaceInfo::ROSImageToHUD;
01315 }
01316 else if (child->get_name() == "ROSTwistToPAT")
01317 {
01318 rosInterface.type = ROSInterfaceInfo::ROSTwistToPAT;
01319 }
01320 else if (child->get_name() == "ROSPoseToPAT")
01321 {
01322 rosInterface.type = ROSInterfaceInfo::ROSPoseToPAT;
01323 }
01324 else if (child->get_name() == "ImuToROSImu")
01325 {
01326 rosInterface.type = ROSInterfaceInfo::ImuToROSImu;
01327 }
01328 else if (child->get_name() == "PressureSensorToROS")
01329 {
01330 rosInterface.type = ROSInterfaceInfo::PressureSensorToROS;
01331 }
01332 else if (child->get_name() == "GPSSensorToROS")
01333 {
01334 rosInterface.type = ROSInterfaceInfo::GPSSensorToROS;
01335 }
01336 else if (child->get_name() == "DVLSensorToROS")
01337 {
01338 rosInterface.type = ROSInterfaceInfo::DVLSensorToROS;
01339 }
01340 else if (child->get_name() == "multibeamSensorToLaserScan")
01341 {
01342 rosInterface.type = ROSInterfaceInfo::multibeamSensorToLaserScan;
01343 }
01344 else if (child->get_name() == "contactSensorToROS")
01345 {
01346 rosInterface.type = ROSInterfaceInfo::contactSensorToROS;
01347 }
01348 else if (child->get_name() == "ROSPointCloudLoader")
01349 {
01350 rosInterface.type = ROSInterfaceInfo::ROSPointCloudLoader;
01351 const xmlpp::Attribute * atrib = dynamic_cast<const xmlpp::Element*>(child)->get_attribute("delLastPCD");
01352 if(atrib and atrib->get_value()=="false")
01353 {
01354 rosInterface.del=false;
01355 }
01356 else
01357 rosInterface.del=true;
01358 }
01359 else if (child->get_name() == "SimulatedDeviceROS")
01360 {
01361 xmlpp::Node::NodeList sublist = child->get_children();
01362 for (xmlpp::Node::NodeList::iterator subiter = sublist.begin(); subiter != sublist.end(); ++subiter)
01363 {
01364 const xmlpp::Node* tempchild = dynamic_cast<const xmlpp::Node*>(*subiter);
01365 if (tempchild != NULL && tempchild->get_name() != "text")
01366 {
01367 std::string subtype = tempchild->get_name();
01368 if (subtype.length() > 3 && subtype.substr(subtype.length() - 3, 3) == "ROS")
01369 {
01370 rosInterface.type = ROSInterfaceInfo::SimulatedDevice;
01371 rosInterface.subtype = subtype.substr(0, subtype.length() - 3);
01372 subchild = tempchild;
01373 }
01374 }
01375 }
01376 }
01377 else
01378 {
01379 std::string subtype = child->get_name();
01380 if (subtype.length() > 3 && subtype.substr(subtype.length() - 3, 3) == "ROS")
01381 {
01382 rosInterface.type = ROSInterfaceInfo::SimulatedDevice;
01383 rosInterface.subtype = subtype.substr(0, subtype.length() - 3);
01384 }
01385 }
01386
01387 if (rosInterface.type != ROSInterfaceInfo::Unknown)
01388 {
01389 processROSInterface(subchild != NULL ? subchild : child, rosInterface);
01390 if (rosInterface.type != ROSInterfaceInfo::contactSensorToROS)
01391 ROSInterfaces.push_back(rosInterface);
01392 else
01393 ROSPhysInterfaces.push_back(rosInterface);
01394 }
01395 }
01396 }
01397
01398 void ConfigFile::processXML(const xmlpp::Node* node)
01399 {
01400 if (node->get_name() != "UWSimScene")
01401 {
01402 osg::notify(osg::ALWAYS) << "ConfigFile::processXML: XML file is not an UWSimScene file." << std::endl;
01403 }
01404 else
01405 {
01406 xmlpp::Node::NodeList list = node->get_children();
01407 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
01408 {
01409 const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
01410 if (child->get_name() == "oceanState")
01411 processOceanState(child);
01412 else if (child->get_name() == "simParams")
01413 processSimParams(child);
01414 else if (child->get_name() == "camera")
01415 processCamera(child);
01416 else if (child->get_name() == "vehicle")
01417 {
01418 Vehicle vehicle;
01419 vehicle.scale[0]=1;vehicle.scale[1]=1;vehicle.scale[2]=1;
01420 processVehicle(child, vehicle);
01421 postprocessVehicle(vehicle);
01422 vehicles.push_back(vehicle);
01423 }
01424 else if (child->get_name() == "object")
01425 {
01426 Object object;
01427 object.scale[0]=1;object.scale[1]=1;object.scale[2]=1;
01428 object.buried=0;
01429 memset(object.offsetp, 0, 3 * sizeof(double));
01430 memset(object.offsetr, 0, 3 * sizeof(double));
01431 object.physicProperties.reset();
01432 const xmlpp::Attribute * atrib = dynamic_cast<const xmlpp::Element*>(child)->get_attribute("buried");
01433 if(atrib)
01434 {
01435 object.buried=boost::lexical_cast<double>(atrib->get_value().c_str());
01436 }
01437 processObject(child, object);
01438 objects.push_back(object);
01439 }
01440 else if (child->get_name() == "rosInterfaces")
01441 processROSInterfaces(child);
01442 }
01443 }
01444
01445 }
01446
01447 ConfigFile::ConfigFile(const std::string &fName)
01448 {
01449 memset(offsetr, 0, 3 * sizeof(double));
01450 memset(offsetp, 0, 3 * sizeof(double));
01451 camNear = camFar = -1;
01452 enablePhysics = 0;
01453 lightRate=1.0;
01454 physicsConfig.init();
01455 try
01456 {
01457 xmlpp::DomParser parser;
01458 parser.set_validate();
01459 parser.set_substitute_entities();
01460 std::string fName_fullpath = osgDB::findDataFile(fName);
01461 if (fName_fullpath != std::string(""))
01462 {
01463 parser.parse_file(fName_fullpath);
01464 if (parser)
01465 {
01466
01467 const xmlpp::Node* pNode = parser.get_document()->get_root_node();
01468 processXML(pNode);
01469 }
01470 }
01471 else
01472 {
01473 osg::notify(osg::ALWAYS) << "Cannot locate file " << fName << std::endl;
01474 exit(0);
01475 }
01476 }
01477 catch (const std::exception& ex)
01478 {
01479 osg::notify(osg::ALWAYS) << "Exception caught: " << ex.what() << std::endl;
01480 osg::notify(osg::ALWAYS) << "Please check your XML file" << std::endl;
01481 exit(0);
01482 }
01483
01484 }