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 2012 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 <deque>
28 #include <string>
29 #include <boost/enable_shared_from_this.hpp>
30 #include <boost/shared_ptr.hpp>
31 
33 
34 #include "gazebo/msgs/msgs.hh"
35 
37 #include "gazebo/common/Event.hh"
38 
39 #include "gazebo/physics/Base.hh"
42 #include "gazebo/sdf/sdf.hh"
43 
44 namespace boost
45 {
46  class thread;
47  class mutex;
48  class recursive_mutex;
49 }
50 
51 namespace gazebo
52 {
53  namespace physics
54  {
57 
66  class World : public boost::enable_shared_from_this<World>
67  {
72  public: explicit World(const std::string &_name = "");
73 
75  public: ~World();
76 
81  public: void Load(sdf::ElementPtr _sdf);
82 
87  public: void Save(const std::string &_filename);
88 
92  public: void Init();
93 
97  public: void Run();
98 
102  public: void Stop();
103 
107  public: void Fini();
108 
110  public: void Clear();
111 
114  public: std::string GetName() const;
115 
120  public: PhysicsEnginePtr GetPhysicsEngine() const;
121 
124  public: unsigned int GetModelCount() const;
125 
132  public: ModelPtr GetModel(unsigned int _index) const;
133 
136  public: Model_V GetModels() const;
137 
143  public: void ResetEntities(Base::EntityType _type = Base::BASE);
144 
146  public: void ResetTime();
147 
149  public: void Reset();
150 
155  public: EntityPtr GetSelectedEntity() const;
156 
160  public: void PrintEntityTree();
161 
165  public: common::Time GetSimTime() const;
166 
169  public: void SetSimTime(const common::Time &_t);
170 
173  public: common::Time GetPauseTime() const;
174 
177  public: common::Time GetStartTime() const;
178 
181  public: common::Time GetRealTime() const;
182 
185  public: bool IsPaused() const;
186 
189  public: void SetPaused(bool _p);
190 
197  public: BasePtr GetByName(const std::string &_name);
198 
200  public: ModelPtr GetModelByName(const std::string &name)GAZEBO_DEPRECATED;
201 
208  public: ModelPtr GetModel(const std::string &_name);
209 
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: void SetState(const WorldState &_state);
240 
245  public: void InsertModelFile(const std::string &_sdfFilename);
246 
251  public: void InsertModelString(const std::string &_sdfString);
252 
257  public: void InsertModelSDF(const sdf::SDF &_sdf);
258 
262  public: std::string StripWorldName(const std::string &_name) const;
263 
268  public: void EnableAllModels();
269 
274  public: void DisableAllModels();
275 
278  public: void StepWorld(int _steps);
279 
284  public: void LoadPlugin(const std::string &_filename,
285  const std::string &_name,
286  sdf::ElementPtr _sdf);
287 
290  public: void RemovePlugin(const std::string &_name);
291 
294  public: boost::mutex *GetSetWorldPoseMutex() const
295  {return this->setWorldPoseMutex;}
296 
299  public: bool GetEnablePhysicsEngine()
300  {return this->enablePhysicsEngine;}
301 
304  public: void EnablePhysicsEngine(bool _enable)
305  {this->enablePhysicsEngine = _enable;}
306 
308  public: void UpdateStateSDF();
309 
316  private: ModelPtr GetModelById(unsigned int _id);
317 
321  private: void LoadPlugins();
322 
326  private: void LoadEntities(sdf::ElementPtr _sdf, BasePtr _parent);
327 
332  private: ModelPtr LoadModel(sdf::ElementPtr _sdf, BasePtr _parent);
333 
338  private: ActorPtr LoadActor(sdf::ElementPtr _sdf, BasePtr _parent);
339 
344  private: RoadPtr LoadRoad(sdf::ElementPtr _sdf, BasePtr _parent);
345 
347  private: void RunLoop();
348 
350  private: void Step();
351 
353  private: void LogStep();
354 
356  private: void Update();
357 
360  private: void OnPause(bool _p);
361 
363  private: void OnStep();
364 
367  private: void OnControl(ConstWorldControlPtr &_data);
368 
371  private: void OnRequest(ConstRequestPtr &_msg);
372 
375  private: void SetSelectedEntityCB(const std::string &_name);
376 
381  private: void BuildSceneMsg(msgs::Scene &_scene, BasePtr _entity);
382 
385  private: void JointLog(ConstJointPtr &_msg);
386 
389  private: void OnFactoryMsg(ConstFactoryPtr &_data);
390 
393  private: void OnModelMsg(ConstModelPtr &_msg);
394 
396  private: void ModelUpdateTBB();
397 
399  private: void ModelUpdateSingleLoop();
400 
403  private: void LoadPlugin(sdf::ElementPtr _sdf);
404 
408  private: void FillModelMsg(msgs::Model &_msg, ModelPtr _model);
409 
411  private: void ProcessEntityMsgs();
412 
414  private: void ProcessRequestMsgs();
415 
417  private: void ProcessFactoryMsgs();
418 
420  private: void ProcessModelMsgs();
421 
423  private: bool OnLog(std::ostringstream &_stream);
424 
426  private: void ProcessMessages();
427 
429  private: void PublishWorldStats();
430 
432  private: common::Time prevStepWallTime;
433 
435  private: PhysicsEnginePtr physicsEngine;
436 
438  private: BasePtr rootElement;
439 
441  private: boost::thread *thread;
442 
444  private: bool stop;
445 
447  private: EntityPtr selectedEntity;
448 
450  private: std::vector<google::protobuf::Message> messages;
451 
453  private: std::string name;
454 
456  private: common::Time simTime;
457 
459  private: common::Time pauseTime;
460 
462  private: common::Time startTime;
463 
465  private: bool pause;
466 
468  private: int stepInc;
469 
471  private: event::Connection_V connections;
472 
474  private: transport::NodePtr node;
475 
477  private: transport::PublisherPtr selectionPub;
478 
480  private: transport::PublisherPtr statPub;
481 
483  private: transport::PublisherPtr responsePub;
484 
486  private: transport::PublisherPtr modelPub;
487 
489  private: transport::PublisherPtr guiPub;
490 
492  private: transport::PublisherPtr lightPub;
493 
495  private: transport::SubscriberPtr controlSub;
496 
498  private: transport::SubscriberPtr factorySub;
499 
501  private: transport::SubscriberPtr jointSub;
502 
504  private: transport::SubscriberPtr modelSub;
505 
507  private: transport::SubscriberPtr requestSub;
508 
510  private: msgs::WorldStatistics worldStatsMsg;
511 
513  private: msgs::Scene sceneMsg;
514 
516  private: void (World::*modelUpdateFunc)();
517 
519  private: common::Time statPeriod;
520 
522  private: common::Time prevStatTime;
523 
525  private: common::Time pauseStartTime;
526 
528  private: common::Time realTimeOffset;
529 
531  private: boost::mutex *receiveMutex;
532 
534  private: boost::mutex *loadModelMutex;
535 
540  private: boost::mutex *setWorldPoseMutex;
541 
548  private: boost::recursive_mutex *worldUpdateMutex;
549 
551  private: sdf::ElementPtr sdf;
552 
554  private: std::vector<WorldPluginPtr> plugins;
555 
557  private: std::list<std::string> deleteEntity;
558 
562  public: std::list<Entity*> dirtyPoses;
563 
565  private: std::list<msgs::Request> requestMsgs;
566 
568  private: std::list<msgs::Factory> factoryMsgs;
569 
571  private: std::list<msgs::Model> modelMsgs;
572 
574  private: bool needsReset;
575 
577  private: bool resetAll;
578 
580  private: bool resetTimeOnly;
581 
583  private: bool resetModelOnly;
584 
586  private: bool initialized;
587 
589  private: bool enablePhysicsEngine;
590 
592  private: RayShapePtr testRay;
593 
595  private: bool pluginsLoaded;
596 
598  private: common::Time sleepOffset;
599 
601  private: common::Time prevProcessMsgsTime;
602 
604  private: common::Time processMsgsPeriod;
605 
607  private: std::deque<WorldState> states;
608  private: WorldState prevStates[2];
609  private: int stateToggle;
610 
611  private: sdf::ElementPtr logPlayStateSDF;
612  private: WorldState logPlayState;
613 
616  private: sdf::SDFPtr factorySDF;
617  };
619  }
620 }
621 #endif