All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
World.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Nate Koenig
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 /* Desc: The world; all models are collected here
18  * Author: Andrew Howard and Nate Koenig
19  * Date: 3 Apr 2007
20  */
21 
22 #ifndef _WORLD_HH_
23 #define _WORLD_HH_
24 
25 #include <vector>
26 #include <list>
27 #include <string>
28 #include <boost/enable_shared_from_this.hpp>
29 #include <boost/shared_ptr.hpp>
30 
32 
33 #include "msgs/msgs.hh"
34 
35 #include "common/CommonTypes.hh"
36 #include "common/Event.hh"
37 
38 #include "physics/Base.hh"
39 #include "physics/PhysicsTypes.hh"
40 #include "physics/WorldState.hh"
41 #include "sdf/sdf.hh"
42 
43 namespace boost
44 {
45  class thread;
46  class mutex;
47  class recursive_mutex;
48 }
49 
50 namespace gazebo
51 {
52  namespace physics
53  {
56 
65  class World : public boost::enable_shared_from_this<World>
66  {
71  public: World(const std::string &_name ="");
72 
74  public: ~World();
75 
80  public: void Load(sdf::ElementPtr _sdf);
81 
86  public: void Save(const std::string &_filename);
87 
91  public: void Init();
92 
96  public: void Run();
97 
101  public: void Stop();
102 
106  public: void Fini();
107 
109  public: void Clear();
110 
113  public: std::string GetName() const;
114 
119  public: PhysicsEnginePtr GetPhysicsEngine() const;
120 
123  public: unsigned int GetModelCount() const;
124 
131  public: ModelPtr GetModel(unsigned int _index) const;
132 
135  public: std::list<ModelPtr> GetModels() const;
136 
142  public: void ResetEntities(Base::EntityType _type = Base::BASE);
143 
145  public: void ResetTime();
146 
148  public: void Reset();
149 
154  public: EntityPtr GetSelectedEntity() const;
155 
159  public: void PrintEntityTree();
160 
164  public: common::Time GetSimTime() const;
165 
168  public: void SetSimTime(common::Time _t);
169 
172  public: common::Time GetPauseTime() const;
173 
176  public: common::Time GetStartTime() const;
177 
180  public: common::Time GetRealTime() const;
181 
184  public: bool IsPaused() const;
185 
188  public: void SetPaused(bool _p);
189 
196  public: BasePtr GetByName(const std::string &_name);
197 
199  public: ModelPtr GetModelByName(const std::string &name)GAZEBO_DEPRECATED;
200 
207  public: ModelPtr GetModel(const std::string &_name);
208 
211  public: EntityPtr GetEntityByName(
212  const std::string &_name) GAZEBO_DEPRECATED;
213 
220  public: EntityPtr GetEntity(const std::string &_name);
221 
227  public: ModelPtr GetModelBelowPoint(const math::Vector3 &_pt);
228 
235  public: EntityPtr GetEntityBelowPoint(const math::Vector3 &_pt);
236 
239  public: WorldState GetState();
240 
243  public: void SetState(const WorldState &_state);
244 
249  public: void InsertModelFile(const std::string &_sdfFilename);
250 
255  public: void InsertModelString(const std::string &_sdfString);
256 
261  public: void InsertModelSDF(const sdf::SDF &_sdf);
262 
266  public: std::string StripWorldName(const std::string &_name) const;
267 
272  public: void EnableAllModels();
273 
278  public: void DisableAllModels();
279 
282  public: void StepWorld(int _steps);
283 
288  public: void LoadPlugin(const std::string &_filename,
289  const std::string &_name,
290  sdf::ElementPtr _sdf);
291 
294  public: void RemovePlugin(const std::string &_name);
295 
298  public: boost::mutex *GetSetWorldPoseMutex() const
299  {return this->setWorldPoseMutex;}
300 
303  public: bool GetEnablePhysicsEngine()
304  {return this->enablePhysicsEngine;}
305 
308  public: void EnablePhysicsEngine(bool _enable)
309  {this->enablePhysicsEngine = _enable;}
310 
317  private: ModelPtr GetModelById(unsigned int _id);
318 
322  private: void LoadPlugins();
323 
327  private: void LoadEntities(sdf::ElementPtr _sdf , BasePtr _parent);
328 
333  private: ModelPtr LoadModel(sdf::ElementPtr _sdf, BasePtr _parent);
334 
339  private: ActorPtr LoadActor(sdf::ElementPtr _sdf, BasePtr _parent);
340 
345  private: RoadPtr LoadRoad(sdf::ElementPtr _sdf , BasePtr _parent);
346 
348  private: void RunLoop();
349 
351  private: void Step();
352 
354  private: void Update();
355 
358  private: void OnPause(bool _p);
359 
361  private: void OnStep();
362 
365  private: void OnControl(ConstWorldControlPtr &_data);
366 
369  private: void OnRequest(ConstRequestPtr &_msg);
370 
373  private: void SetSelectedEntityCB(const std::string &_name);
374 
379  private: void BuildSceneMsg(msgs::Scene &_scene, BasePtr _entity);
380 
383  private: void JointLog(ConstJointPtr &_msg);
384 
387  private: void OnFactoryMsg(ConstFactoryPtr &_data);
388 
391  private: void OnModelMsg(ConstModelPtr &_msg);
392 
394  private: void ModelUpdateTBB();
395 
397  private: void ModelUpdateSingleLoop();
398 
401  private: void LoadPlugin(sdf::ElementPtr _sdf);
402 
406  private: void FillModelMsg(msgs::Model &_msg, ModelPtr _model);
407 
409  private: void ProcessEntityMsgs();
410 
412  private: void ProcessRequestMsgs();
413 
415  private: void ProcessFactoryMsgs();
416 
418  private: void ProcessModelMsgs();
419 
421  private: void UpdateStateSDF();
422 
425  private: void UpdateSDFFromState(const WorldState &_state);
426 
428  private: common::Time prevStepWallTime;
429 
431  private: PhysicsEnginePtr physicsEngine;
432 
434  private: BasePtr rootElement;
435 
437  private: boost::thread *thread;
438 
440  private: bool stop;
441 
443  private: EntityPtr selectedEntity;
444 
446  private: std::vector<google::protobuf::Message> messages;
447 
449  private: std::string name;
450 
452  private: common::Time simTime;
453 
455  private: common::Time pauseTime;
456 
458  private: common::Time startTime;
459 
461  private: bool pause;
462 
464  private: int stepInc;
465 
467  private: event::Connection_V connections;
468 
470  private: transport::NodePtr node;
471 
473  private: transport::PublisherPtr selectionPub;
474 
476  private: transport::PublisherPtr statPub;
477 
479  private: transport::PublisherPtr responsePub;
480 
482  private: transport::PublisherPtr modelPub;
483 
485  private: transport::PublisherPtr guiPub;
486 
488  private: transport::PublisherPtr lightPub;
489 
491  private: transport::SubscriberPtr controlSub;
492 
494  private: transport::SubscriberPtr factorySub;
495 
497  private: transport::SubscriberPtr jointSub;
498 
500  private: transport::SubscriberPtr modelSub;
501 
503  private: transport::SubscriberPtr requestSub;
504 
506  private: msgs::WorldStatistics worldStatsMsg;
507 
509  private: msgs::Scene sceneMsg;
510 
512  private: void (World::*modelUpdateFunc)();
513 
515  private: common::Time statPeriod;
516 
518  private: common::Time prevStatTime;
519 
521  private: common::Time pauseStartTime;
522 
524  private: common::Time realTimeOffset;
525 
527  private: boost::mutex *receiveMutex;
528 
530  private: boost::mutex *loadModelMutex;
531 
537  private: boost::mutex *setWorldPoseMutex;
538 
545  private: boost::recursive_mutex *worldUpdateMutex;
546 
548  private: sdf::ElementPtr sdf;
549 
551  private: std::vector<WorldPluginPtr> plugins;
552 
554  private: std::list<std::string> deleteEntity;
555 
559  public: std::list<Entity*> dirtyPoses;
560 
562  private: std::list<msgs::Request> requestMsgs;
563 
565  private: std::list<msgs::Factory> factoryMsgs;
566 
568  private: std::list<msgs::Model> modelMsgs;
569 
571  private: bool needsReset;
572 
574  private: bool resetAll;
575 
577  private: bool resetTimeOnly;
578 
580  private: bool resetModelOnly;
581 
583  private: bool initialized;
584 
586  private: bool enablePhysicsEngine;
587 
589  private: RayShapePtr testRay;
590 
592  private: bool pluginsLoaded;
593 
595  private: common::Time sleepOffset;
596 
598  private: common::Time prevProcessMsgsTime;
599 
601  private: common::Time processMsgsPeriod;
602  };
604  }
605 }
606 #endif