DredgeTool.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 
00014 #include <pluginlib/class_list_macros.h>
00015 #include <uwsim/DredgeTool.h>
00016 
00017 //PARTICLE SYSTEM FUNCTIONS
00018 
00019 inline void AttractOperator::operate( osgParticle::Particle* P, double dt )
00020 {
00021   osg::Vec3 dir = - P->getPosition();
00022   if (dir.length()>0.02 )
00023   {
00024     //Probable something more realistic can be done, but I think it's not worthy
00025     P->setVelocity(dir*(1/dir.length())*_magnitude);
00026   }
00027   else
00028   {
00029     if (_killSink)
00030     {
00031       P->kill();
00032     }
00033     else
00034     {
00035       P->setPosition(osg::Vec3(0,0,0));
00036       P->setVelocity(osg::Vec3(0,0,0));
00037     }
00038   }
00039 }
00040 
00041 
00042 
00043 osgParticle::ParticleSystem* createSmokeParticles( osg::Group* parent, osgParticle::RandomRateCounter * rrc)
00044 {
00045 
00046     //Set particle properties (TODO: take them from config file)
00047     osg::ref_ptr<osgParticle::ParticleSystem> ps = new osgParticle::ParticleSystem;
00048     ps->getDefaultParticleTemplate().setLifeTime( 15.0f );
00049     ps->getDefaultParticleTemplate().setShape( osgParticle::Particle::QUAD );
00050     ps->getDefaultParticleTemplate().setSizeRange( osgParticle::rangef(0.05f, 0.1f) );
00051     ps->getDefaultParticleTemplate().setAlphaRange( osgParticle::rangef(1.0f, 0.0f) );
00052     ps->getDefaultParticleTemplate().setColorRange(
00053         osgParticle::rangev4(osg::Vec4(0.3f,0.2f,0.01f,1.0f), osg::Vec4(0.15f,0.1f,0.01f,0.5f)) );
00054     ps->setDefaultAttributes( "smoke.rgb", true, false );
00055     
00056     rrc->setRateRange( 0, 0 );
00057 
00058     //Box placer creates particles inside a box
00059     //TODO: Box size should depend on dredge tool force
00060     osg::ref_ptr<osgParticle::BoxPlacer> placer = new osgParticle::BoxPlacer;
00061     placer->setXRange(-0.1,0.1);
00062     placer->setYRange(-0.1,0.1);
00063     placer->setZRange(0,0.2);
00064     
00065     //Create emitter
00066     osg::ref_ptr<osgParticle::ModularEmitter> emitter = new osgParticle::ModularEmitter;
00067     emitter->setPlacer(placer);
00068     emitter->setParticleSystem( ps.get() );
00069     emitter->setCounter( rrc );
00070     parent->addChild( emitter.get() );
00071 
00072 
00073     //Create AttractOperator program
00074     osgParticle::ModularProgram *moveDustInAir = new osgParticle::ModularProgram;
00075     moveDustInAir->setParticleSystem(ps);
00076     
00077     AttractOperator * attOp= new AttractOperator;
00078     attOp->setMagnitude(0.2);
00079     moveDustInAir->addOperator(attOp);
00080 
00081     //Add FluidFriction (not sure if it does something)
00082     osgParticle::FluidFrictionOperator *airFriction = new osgParticle::FluidFrictionOperator;
00083     airFriction->setFluidToWater();
00084     moveDustInAir->addOperator(airFriction);
00085 
00086     parent->addChild(moveDustInAir);
00087 
00088     return ps.get();
00089 }
00090 
00091 
00092 
00093 
00094 DredgeTool::DredgeTool(DredgeTool_Config * cfg, osg::ref_ptr<osg::Node> target) :
00095     SimulatedDevice(cfg)
00096 {
00097   this->target = target;
00098   particles=0;
00099 
00100   //Create particle system:
00101   rrc= new osgParticle::RandomRateCounter;
00102 
00103   smoke = createSmokeParticles(target->asGroup(),rrc);
00104 
00105   osg::ref_ptr<osgParticle::ParticleSystemUpdater> updater = new osgParticle::ParticleSystemUpdater;
00106   updater->addParticleSystem( smoke );
00107   
00108   osg::ref_ptr<osg::Geode> smokeGeode = new osg::Geode;
00109   smokeGeode->getOrCreateStateSet()->setAttributeAndModes(new osg::Program(), osg::StateAttribute::ON);
00110   smokeGeode->addDrawable( smoke );
00111 
00112   target->asGroup()->addChild( smokeGeode.get() );
00113   target->asGroup()->addChild( updater.get() );
00114 
00115 }
00116 
00117 boost::shared_ptr<osg::Matrix> DredgeTool::getDredgePosition()
00118 {
00119   return getWorldCoords(target);;
00120 }
00121 
00122 void DredgeTool::dredgedParticles(int nparticles)
00123 {
00124   //TODO: use time instead of function calls.
00125   particles*=0.9;
00126   particles+=nparticles;
00127 
00128   rrc->setRateRange( min(particles,50), min(particles*2,100) );
00129 
00130 }
00131 
00132 SimulatedDeviceConfig::Ptr DredgeTool_Factory::processConfig(const xmlpp::Node* node, ConfigFile * config)
00133 {
00134   DredgeTool_Config * cfg = new DredgeTool_Config(getType());
00135   xmlpp::Node::NodeList list = node->get_children();
00136   for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00137   {
00138     const xmlpp::Node* child = dynamic_cast<const xmlpp::Node*>(*iter);
00139     if(child->get_name() == "target")
00140       config->extractStringChar(child, cfg->target);
00141     else if(child->get_name() == "offsetp")
00142       config->extractPositionOrColor(child, cfg->offsetp);
00143     else if(child->get_name() == "offsetr")
00144       config->extractPositionOrColor(child, cfg->offsetr);
00145   }
00146   return SimulatedDeviceConfig::Ptr(cfg);
00147 }
00148 
00149 bool DredgeTool_Factory::applyConfig(SimulatedIAUV * auv, Vehicle &vehicleChars, SceneBuilder *sceneBuilder,
00150                                       size_t iteration)
00151 {
00152   if (iteration > 0)
00153     return true;
00154   for (size_t i = 0; i < vehicleChars.simulated_devices.size(); ++i)
00155     if (vehicleChars.simulated_devices[i]->getType() == this->getType())
00156     {
00157       DredgeTool_Config * cfg = dynamic_cast<DredgeTool_Config *>(vehicleChars.simulated_devices[i].get());
00158 
00159       osg::ref_ptr<osg::Node> target;
00160       for(int j=0;j<auv->urdf->link.size();j++)
00161       {
00162         if(auv->urdf->link[j]->getName()==cfg->target)
00163         {
00164           target=auv->urdf->link[j];
00165         }
00166       }
00167       if(target)
00168         auv->devices->all.push_back(DredgeTool::Ptr(new DredgeTool(cfg,target)));
00169       else
00170         OSG_FATAL << "DredgeTool device '" << vehicleChars.simulated_devices[i]->name << "' inside robot '"
00171             << vehicleChars.name << "' has empty info, discarding..." << std::endl;
00172     }
00173   return true;
00174 }
00175 
00176 
00177 
00178 #if ROS_VERSION_MINIMUM(1, 9, 0)
00179 // new pluginlib API in Groovy and Hydro
00180 PLUGINLIB_EXPORT_CLASS(DredgeTool_Factory, uwsim::SimulatedDeviceFactory)
00181 #else
00182 PLUGINLIB_REGISTER_CLASS(DredgeTool_Factory, DredgeTool_Factory, uwsim::SimulatedDeviceFactory)
00183 #endif
00184 


uwsim
Author(s): Mario Prats , Javier Perez
autogenerated on Fri Aug 28 2015 13:28:58