SimulatedIAUV.cpp
Go to the documentation of this file.
00001 /* 
00002  * Copyright (c) 2013 University of Jaume-I.
00003  * All rights reserved. This program and the accompanying materials
00004  * are made available under the terms of the GNU Public License v3.0
00005  * which accompanies this distribution, and is available at
00006  * http://www.gnu.org/licenses/gpl.html
00007  * 
00008  * Contributors:
00009  *     Mario Prats
00010  *     Javier Perez
00011  */
00012 
00013 #include <uwsim/SceneBuilder.h>
00014 #include <uwsim/osgOceanScene.h>
00015 #include <uwsim/SimulatorConfig.h>
00016 #include <uwsim/SimulatedIAUV.h>
00017 #include <uwsim/URDFRobot.h>
00018 #include <osg/MatrixTransform>
00019 #include <osg/PositionAttitudeTransform>
00020 
00022 /*
00023  class LightUpdateCallback:public osg::NodeCallback {
00024  osg::Transform *trackNode;     ///< Node that the light must track
00025 
00026  public:
00027  LightUpdateCallback(osg::Transform *trackNode)
00028  {this->trackNode=trackNode;}
00029 
00030  void operator () (osg::Node *node, osg::NodeVisitor *nv) {
00031  //update light position to track the node
00032  osg::LightSource *ls=dynamic_cast<osg::LightSource*>(node);
00033  osg::Light *l=ls->getLight();
00034  osg::PositionAttitudeTransform *pat=trackNode->asPositionAttitudeTransform();
00035  osg::Vec3d pos=pat->getPosition();
00036  l->setPosition( osg::Vec4f(pos.x(),pos.y(),pos.z()-0.5, 1.f) );
00037 
00038  //call to standard callback
00039  osg::NodeCallback::operator()(node,nv);
00040  }
00041  };
00042  */
00043 
00044 /*
00045  SimulatedIAUV::SimulatedIAUV(osgOcean::OceanScene *oscene, arm_t armtype) {
00046  vehicle=new SimulatedVehicle(oscene, "GIRONA500/girona500.osg");
00047 
00048  if (armtype==PA10)
00049  arm=new SimulatedPA10(oscene);
00050  else if (armtype==ARM5)
00051  arm=new SimulatedArmFromURDF5(oscene);
00052 
00053  baseTransform=NULL;
00054  if(vehicle->baseTransform!=NULL && arm->baseTransform!=NULL) {
00055  baseTransform=vehicle->baseTransform;
00056  baseTransform->addChild(arm->baseTransform);
00057 
00058  //Vehicle frame to Arm base frame transform
00059  osg::Matrix m=arm->baseTransform->getMatrix();
00060  if (armtype==PA10) {
00061  m.makeRotate(M_PI,1,0,0);
00062  } else if (armtype==ARM5) {
00063  }
00064  arm->baseTransform->setMatrix(m);
00065  }
00066  camview=NULL;
00067 
00068  //Set-up a lamp attached to the vehicle
00069  osg::Light *_light=new osg::Light;
00070  _light->setLightNum(1);
00071  _light->setAmbient( osg::Vec4d(1.0f, 1.0f, 1.0f, 1.0f ));
00072  _light->setDiffuse( osg::Vec4d( 1.0, 1.0, 1.0, 1.0 ) );
00073  _light->setSpecular(osg::Vec4d( 0.1f, 0.1f, 0.1f, 1.0f ) );
00074  _light->setDirection(osg::Vec3d(0.0, 0.0, -5.0));
00075  _light->setSpotCutoff(40.0);
00076  _light->setSpotExponent(10.0);
00077 
00078  lightSource = new osg::LightSource;
00079  lightSource->setLight(_light);
00080  lightSource->setLocalStateSetModes(osg::StateAttribute::ON);
00081  lightSource->setUpdateCallback(new LightUpdateCallback(baseTransform));
00082  }
00083  */
00084 
00085 SimulatedIAUV::SimulatedIAUV(SceneBuilder *oscene, Vehicle vehicleChars) :
00086     urdf(new URDFRobot(oscene->scene->getOceanScene(), vehicleChars))
00087 {
00088   name = vehicleChars.name;
00089   baseTransform = new osg::MatrixTransform;
00090 
00091   if (urdf->baseTransform != NULL /* && arm->baseTransform!=NULL*/)
00092   {
00093     baseTransform->addChild(urdf->baseTransform);
00094     baseTransform->setName(vehicleChars.name);
00095   }
00096 
00097   //Add virtual  cameras in config file
00098   while (vehicleChars.Vcams.size() > 0)
00099   {
00100     Vcam vcam = vehicleChars.Vcams.front();
00101     OSG_INFO << "Adding a virtual camera " << vcam.name << "..." << std::endl;
00102     vehicleChars.Vcams.pop_front();
00103     //Camera frame given wrt vehicle origin frame.
00104     //Remember that in opengl/osg, the camera frame is a right-handed system with Z going backwards (opposite to the viewing direction) and Y up.
00105     osg::ref_ptr < osg::Transform > vMc = (osg::Transform*)new osg::PositionAttitudeTransform;
00106     vMc->asPositionAttitudeTransform()->setPosition(osg::Vec3d(vcam.position[0], vcam.position[1], vcam.position[2]));
00107     vMc->asPositionAttitudeTransform()->setAttitude(
00108         osg::Quat(vcam.orientation[0], osg::Vec3d(1, 0, 0), vcam.orientation[1], osg::Vec3d(0, 1, 0),
00109                   vcam.orientation[2], osg::Vec3d(0, 0, 1)));
00110     urdf->link[vcam.link]->asGroup()->addChild(vMc);
00111     camview.push_back(
00112         VirtualCamera(oscene->root, vcam.name, vMc, vcam.resw, vcam.resh, vcam.baseLine, vcam.frameId,
00113                       vcam.parameters.get(), 0, vcam.bw));
00114     if (vcam.showpath)
00115       camview[camview.size() - 1].showPath(vcam.showpath);
00116     OSG_INFO << "Done adding a virtual camera..." << std::endl;
00117   }
00118 
00119   //Add virtual range cameras in config file
00120   while (vehicleChars.VRangecams.size() > 0)
00121   {
00122     Vcam vcam = vehicleChars.VRangecams.front();
00123     OSG_INFO << "Adding a virtual camera " << vcam.name << "..." << std::endl;
00124     vehicleChars.VRangecams.pop_front();
00125     //Camera frame given wrt vehicle origin frame.
00126     //Remember that in opengl/osg, the camera frame is a right-handed system with Z going backwards (opposite to the viewing direction) and Y up.
00127     osg::ref_ptr < osg::Transform > vMc = (osg::Transform*)new osg::PositionAttitudeTransform;
00128     vMc->asPositionAttitudeTransform()->setPosition(osg::Vec3d(vcam.position[0], vcam.position[1], vcam.position[2]));
00129     vMc->asPositionAttitudeTransform()->setAttitude(
00130         osg::Quat(vcam.orientation[0], osg::Vec3d(1, 0, 0), vcam.orientation[1], osg::Vec3d(0, 1, 0),
00131                   vcam.orientation[2], osg::Vec3d(0, 0, 1)));
00132     urdf->link[vcam.link]->asGroup()->addChild(vMc);
00133     camview.push_back(
00134         VirtualCamera(oscene->root, vcam.name, vMc, vcam.resw, vcam.resh, vcam.baseLine, vcam.frameId,
00135                       vcam.parameters.get(), 1, 0));
00136     if (vcam.showpath)
00137       camview[camview.size() - 1].showPath(vcam.showpath);
00138     OSG_INFO << "Done adding a virtual camera..." << std::endl;
00139   }
00140 
00141   // Adding Structured light projector
00142   while (vehicleChars.sls_projectors.size() > 0)
00143   {
00144     OSG_INFO << "Adding a structured light projector..." << std::endl;
00145     slProjector slp;
00146     slp = vehicleChars.sls_projectors.front();
00147     vehicleChars.sls_projectors.pop_front();
00148     osg::ref_ptr < osg::Transform > vMp = (osg::Transform*)new osg::PositionAttitudeTransform;
00149     vMp->asPositionAttitudeTransform()->setPosition(osg::Vec3d(slp.position[0], slp.position[1], slp.position[2]));
00150     vMp->asPositionAttitudeTransform()->setAttitude(
00151         osg::Quat(slp.orientation[0], osg::Vec3d(1, 0, 0), slp.orientation[1], osg::Vec3d(0, 1, 0), slp.orientation[2],
00152                   osg::Vec3d(0, 0, 1)));
00153     urdf->link[slp.link]->asGroup()->addChild(vMp);
00154     //camview.push_back(VirtualCamera(oscene->root, "slp_camera", vMp, 512, 512,slp.fov,102.4));
00155     sls_projectors.push_back(VirtualSLSProjector(slp.name, oscene->root, //maybe oscene->scene->localizedWorld ?
00156                                                  vMp, slp.image_name, slp.fov, (slp.laser) ? true : false));
00157     camview.push_back(sls_projectors.back().camera);
00158     OSG_INFO << "Done adding a structured light projector..." << std::endl;
00159   }
00160 
00161   //Adding range sensors
00162   while (vehicleChars.range_sensors.size() > 0)
00163   {
00164     OSG_INFO << "Adding a virtual range sensor..." << std::endl;
00165     rangeSensor rs;
00166     rs = vehicleChars.range_sensors.front();
00167     vehicleChars.range_sensors.pop_front();
00168     osg::ref_ptr < osg::Transform > vMr = (osg::Transform*)new osg::PositionAttitudeTransform;
00169     vMr->asPositionAttitudeTransform()->setPosition(osg::Vec3d(rs.position[0], rs.position[1], rs.position[2]));
00170     vMr->asPositionAttitudeTransform()->setAttitude(
00171         osg::Quat(rs.orientation[0], osg::Vec3d(1, 0, 0), rs.orientation[1], osg::Vec3d(0, 1, 0), rs.orientation[2],
00172                   osg::Vec3d(0, 0, 1)));
00173     urdf->link[rs.link]->asGroup()->addChild(vMr);
00174     range_sensors.push_back(
00175         VirtualRangeSensor(rs.name, oscene->scene->localizedWorld, vMr, rs.range, (rs.visible) ? true : false));
00176     OSG_INFO << "Done adding a virtual range sensor..." << std::endl;
00177   }
00178 
00179   //Adding imus
00180   while (vehicleChars.imus.size() > 0)
00181   {
00182     OSG_INFO << "Adding an IMU..." << std::endl;
00183     Imu imu;
00184     imu = vehicleChars.imus.front();
00185     vehicleChars.imus.pop_front();
00186     osg::ref_ptr < osg::Transform > vMi = (osg::Transform*)new osg::PositionAttitudeTransform;
00187     vMi->asPositionAttitudeTransform()->setPosition(osg::Vec3d(imu.position[0], imu.position[1], imu.position[2]));
00188     vMi->asPositionAttitudeTransform()->setAttitude(
00189         osg::Quat(imu.orientation[0], osg::Vec3d(1, 0, 0), imu.orientation[1], osg::Vec3d(0, 1, 0), imu.orientation[2],
00190                   osg::Vec3d(0, 0, 1)));
00191     urdf->link[imu.link]->getParent(0)->getParent(0)->asGroup()->addChild(vMi);
00192     imus.push_back(InertialMeasurementUnit(imu.name, vMi, oscene->scene->localizedWorld->getMatrix(), imu.std));
00193     OSG_INFO << "Done adding an IMU..." << std::endl;
00194   }
00195 
00196   //Adding pressure sensors
00197   while (vehicleChars.pressure_sensors.size() > 0)
00198   {
00199     OSG_INFO << "Adding a pressure sensor..." << std::endl;
00200     XMLPressureSensor ps;
00201     ps = vehicleChars.pressure_sensors.front();
00202     vehicleChars.pressure_sensors.pop_front();
00203     osg::ref_ptr < osg::Transform > vMs = (osg::Transform*)new osg::PositionAttitudeTransform;
00204     vMs->asPositionAttitudeTransform()->setPosition(osg::Vec3d(ps.position[0], ps.position[1], ps.position[2]));
00205     vMs->asPositionAttitudeTransform()->setAttitude(
00206         osg::Quat(ps.orientation[0], osg::Vec3d(1, 0, 0), ps.orientation[1], osg::Vec3d(0, 1, 0), ps.orientation[2],
00207                   osg::Vec3d(0, 0, 1)));
00208     urdf->link[ps.link]->getParent(0)->getParent(0)->asGroup()->addChild(vMs);
00209     pressure_sensors.push_back(PressureSensor(ps.name, vMs, oscene->scene->localizedWorld->getMatrix(), ps.std));
00210     OSG_INFO << "Done adding an Pressure Sensor..." << std::endl;
00211   }
00212 
00213   //Adding GPS sensors
00214   while (vehicleChars.gps_sensors.size() > 0)
00215   {
00216     OSG_INFO << "Adding a gps sensor..." << std::endl;
00217     XMLGPSSensor ps;
00218     ps = vehicleChars.gps_sensors.front();
00219     vehicleChars.gps_sensors.pop_front();
00220     osg::ref_ptr < osg::Transform > vMs = (osg::Transform*)new osg::PositionAttitudeTransform;
00221     vMs->asPositionAttitudeTransform()->setPosition(osg::Vec3d(ps.position[0], ps.position[1], ps.position[2]));
00222     vMs->asPositionAttitudeTransform()->setAttitude(
00223         osg::Quat(ps.orientation[0], osg::Vec3d(1, 0, 0), ps.orientation[1], osg::Vec3d(0, 1, 0), ps.orientation[2],
00224                   osg::Vec3d(0, 0, 1)));
00225     urdf->link[ps.link]->getParent(0)->getParent(0)->asGroup()->addChild(vMs);
00226     gps_sensors.push_back(GPSSensor(oscene->scene, ps.name, vMs, oscene->scene->localizedWorld->getMatrix(), ps.std));
00227     OSG_INFO << "Done adding an GPS Sensor..." << std::endl;
00228   }
00229 
00230   //Adding dvl sensors
00231   while (vehicleChars.dvl_sensors.size() > 0)
00232   {
00233     OSG_INFO << "Adding a dvl sensor..." << std::endl;
00234     XMLDVLSensor ps;
00235     ps = vehicleChars.dvl_sensors.front();
00236     vehicleChars.dvl_sensors.pop_front();
00237     osg::ref_ptr < osg::Transform > vMs = (osg::Transform*)new osg::PositionAttitudeTransform;
00238     vMs->asPositionAttitudeTransform()->setPosition(osg::Vec3d(ps.position[0], ps.position[1], ps.position[2]));
00239     vMs->asPositionAttitudeTransform()->setAttitude(
00240         osg::Quat(ps.orientation[0], osg::Vec3d(1, 0, 0), ps.orientation[1], osg::Vec3d(0, 1, 0), ps.orientation[2],
00241                   osg::Vec3d(0, 0, 1)));
00242     urdf->link[ps.link]->getParent(0)->getParent(0)->asGroup()->addChild(vMs);
00243     dvl_sensors.push_back(DVLSensor(ps.name, vMs, oscene->scene->localizedWorld->getMatrix(), ps.std));
00244     OSG_INFO << "Done adding an DVL Sensor..." << std::endl;
00245   }
00246 
00247   //Adding Multibeam sensors
00248   while (vehicleChars.multibeam_sensors.size() > 0)
00249   {
00250     OSG_INFO << "Adding a Multibeam sensor..." << std::endl;
00251     XMLMultibeamSensor MB;
00252     MB = vehicleChars.multibeam_sensors.front();
00253     vehicleChars.multibeam_sensors.pop_front();
00254     osg::ref_ptr < osg::Transform > vMs = (osg::Transform*)new osg::PositionAttitudeTransform;
00255     vMs->asPositionAttitudeTransform()->setPosition(osg::Vec3d(MB.position[0], MB.position[1], MB.position[2]));
00256     vMs->asPositionAttitudeTransform()->setAttitude(
00257         osg::Quat(MB.orientation[0], osg::Vec3d(1, 0, 0), MB.orientation[1], osg::Vec3d(0, 1, 0), MB.orientation[2],
00258                   osg::Vec3d(0, 0, 1)));
00259     urdf->link[MB.link]->getParent(0)->getParent(0)->asGroup()->addChild(vMs);
00260     MultibeamSensor mb = MultibeamSensor(oscene->root, MB.name, vMs, MB.initAngle, MB.finalAngle, MB.angleIncr,
00261                                          MB.range);
00262     multibeam_sensors.push_back(mb);
00263     camview.push_back(mb);
00264     OSG_INFO << "Done adding a Multibeam Sensor..." << std::endl;
00265   }
00266 
00267   //Adding object pickers
00268   while (vehicleChars.object_pickers.size() > 0)
00269   {
00270     OSG_INFO << "Adding an object picker..." << std::endl;
00271     rangeSensor rs;
00272     rs = vehicleChars.object_pickers.front();
00273     vehicleChars.object_pickers.pop_front();
00274     osg::ref_ptr < osg::Transform > vMr = (osg::Transform*)new osg::PositionAttitudeTransform;
00275     vMr->asPositionAttitudeTransform()->setPosition(osg::Vec3d(rs.position[0], rs.position[1], rs.position[2]));
00276     vMr->asPositionAttitudeTransform()->setAttitude(
00277         osg::Quat(rs.orientation[0], osg::Vec3d(1, 0, 0), rs.orientation[1], osg::Vec3d(0, 1, 0), rs.orientation[2],
00278                   osg::Vec3d(0, 0, 1)));
00279     vMr->setName("ObjectPickerNode");
00280     urdf->link[rs.link]->asGroup()->addChild(vMr);
00281     object_pickers.push_back(ObjectPicker(rs.name, oscene->scene->localizedWorld, vMr, rs.range, true, urdf));
00282     OSG_INFO << "Done adding an object picker..." << std::endl;
00283   }
00284 
00285   devices.reset(new SimulatedDevices());
00286   devices->applyConfig(this, vehicleChars, oscene);
00287 
00288   //Set-up a lamp attached to the vehicle: TODO
00289   /*
00290    osg::Light *_light=new osg::Light;
00291    _light->setLightNum(1);
00292    _light->setAmbient( osg::Vec4d(1.0f, 1.0f, 1.0f, 1.0f ));
00293    _light->setDiffuse( osg::Vec4d( 1.0, 1.0, 1.0, 1.0 ) );
00294    _light->setSpecular(osg::Vec4d( 0.1f, 0.1f, 0.1f, 1.0f ) );
00295    _light->setDirection(osg::Vec3d(0.0, 0.0, -5.0));
00296    _light->setSpotCutoff(40.0);
00297    _light->setSpotExponent(10.0);
00298 
00299    lightSource = new osg::LightSource;
00300    lightSource->setLight(_light);
00301    lightSource->setLocalStateSetModes(osg::StateAttribute::ON);
00302    lightSource->setUpdateCallback(new LightUpdateCallback(baseTransform));
00303    */
00304 }
00305 
00306 /*
00307  void SimulatedIAUV::setVirtualCamera(std::string name, osg::Transform* transform, int width, int height) {
00308  //Set I-AUV virtual camera
00309 
00310  baseTransform->asGroup()->addChild(transform);
00311 
00312  if (camview==NULL)
00313  camview=new VirtualCamera(name, transform, width, height);
00314  }
00315  */
00316 
00318 void SimulatedIAUV::setVehiclePosition(double x, double y, double z, double roll, double pitch, double yaw)
00319 {
00320   osg::Matrixd S, T, Rx, Ry, Rz, transform;
00321   T.makeTranslate(x, y, z);
00322   Rx.makeRotate(roll, 1, 0, 0);
00323   Ry.makeRotate(pitch, 0, 1, 0);
00324   Rz.makeRotate(yaw, 0, 0, 1);
00325   S.makeScale(2, 5, 8);
00326   transform = Rz * Ry * Rx * T;
00327   setVehiclePosition(transform);
00328 }
00329 
00330 void SimulatedIAUV::setVehiclePosition(osg::Matrixd m)
00331 {
00332   baseTransform->setMatrix(m);
00333 }
00334 


uwsim
Author(s): Mario Prats
autogenerated on Mon Oct 6 2014 08:24:07