John Hsu avatar John Hsu committed 066cbbd

prototype to load model plugin via event.

Comments (0)

Files changed (2)

gazebo/physics/World.cc

 
   this->receiveMutex = new boost::mutex();
   this->loadModelMutex = new boost::mutex();
+  this->modelLoadPluginsMutex = new boost::mutex();
 
   this->initialized = false;
   this->stepInc = 0;
   this->receiveMutex = NULL;
   delete this->loadModelMutex;
   this->loadModelMutex = NULL;
+  delete this->modelLoadPluginsMutex;
+  this->modelLoadPluginsMutex = NULL;
   delete this->setWorldPoseMutex;
   this->setWorldPoseMutex = NULL;
   delete this->worldUpdateMutex;
         ModelPtr model = this->LoadModel(elem, this->rootElement);
         model->Init();
 
-        int iterations = 0;
-
-        // Wait for the sensors to be initialized before loading
-        // plugins.
-        while (!sensors::SensorManager::Instance()->SensorsInitialized() &&
-               iterations < 10)
         {
-          common::Time::MSleep(100);
-          iterations++;
-        }
-
-        if (iterations < 10)
-          model->LoadPlugins();
-        else
-        {
-          gzerr << "Sensors failed to initialize when loading model["
-                << model->GetName() << "] via the factory mechanism."
-                << "Plugins for the model will not be loaded.\n";
+          boost::mutex::scoped_lock pluginsLock(*this->modelLoadPluginsMutex);
+          this->modelsPendingLoadPlugin.push_back(model);
+          // Create an event that waits for the sensors to be
+          // initialized before loading plugins.
+          if (!this->modelLoadPluginsEvents)
+            this->modelLoadPluginsEvents =
+              event::Events::ConnectWorldUpdateStart(
+                boost::bind(&World::ModelLoadPlugins, this));
         }
       }
       else if (isLight)
   this->factoryMsgs.clear();
 }
 
+
+//////////////////////////////////////////////////
+void World::ModelLoadPlugins()
+{
+  if (sensors::SensorManager::Instance()->SensorsInitialized())
+  {
+    boost::mutex::scoped_lock pluginsLock(*this->modelLoadPluginsMutex);
+    for (Model_V::iterator iter = this->modelsPendingLoadPlugin.begin();
+                           iter != this->modelsPendingLoadPlugin.end();
+                           ++iter)
+      (*iter)->LoadPlugins();
+    this->modelsPendingLoadPlugin.clear();
+    event::Events::DisconnectWorldUpdateStart(this->modelLoadPluginsEvents);
+    this->modelLoadPluginsEvents.reset();
+  }
+}
+
 //////////////////////////////////////////////////
 ModelPtr World::GetModelBelowPoint(const math::Vector3 &_pt)
 {

gazebo/physics/World.hh

       /// \param[in] _state State to update from.
       private: void UpdateSDFFromState(const WorldState &_state);
 
+      /// \brief Used to load model plugins
+      private: void ModelLoadPlugins();
+
       /// \brief For keeping track of time step throttling
       private: common::Time prevStepWallTime;
 
 
       /// \brief True if the plugins have been loaded.
       private: bool pluginsLoaded;
+
+      /// \brief Used to load model
+      private: boost::mutex *modelLoadPluginsMutex;
+      private: event::ConnectionPtr modelLoadPluginsEvents;
+      private: Model_V modelsPendingLoadPlugin;
     };
     /// \}
   }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.