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 Open Source Robotics Foundation
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 <set>
28 #include <deque>
29 #include <string>
30 #include <boost/thread.hpp>
31 #include <boost/enable_shared_from_this.hpp>
32 #include <boost/shared_ptr.hpp>
33 
34 #include <sdf/sdf.hh>
35 
37 
38 #include "gazebo/msgs/msgs.hh"
39 
42 #include "gazebo/common/Event.hh"
43 
44 #include "gazebo/physics/Base.hh"
47 
48 namespace gazebo
49 {
50  namespace physics
51  {
54 
63  class World : public boost::enable_shared_from_this<World>
64  {
69  public: explicit World(const std::string &_name = "");
70 
72  public: ~World();
73 
78  public: void Load(sdf::ElementPtr _sdf);
79 
84  public: void Save(const std::string &_filename);
85 
89  public: void Init();
90 
96  public: void Run(unsigned int _iterations = 0);
97 
100  public: bool GetRunning() const;
101 
105  public: void Stop();
106 
110  public: void Fini();
111 
113  public: void Clear();
114 
117  public: std::string GetName() const;
118 
123  public: PhysicsEnginePtr GetPhysicsEngine() const;
124 
127  public: unsigned int GetModelCount() const;
128 
135  public: ModelPtr GetModel(unsigned int _index) const;
136 
139  public: Model_V GetModels() const;
140 
146  public: void ResetEntities(Base::EntityType _type = Base::BASE);
147 
149  public: void ResetTime();
150 
152  public: void Reset();
153 
158  public: EntityPtr GetSelectedEntity() const;
159 
163  public: void PrintEntityTree();
164 
168  public: common::Time GetSimTime() const;
169 
172  public: void SetSimTime(const common::Time &_t);
173 
176  public: common::Time GetPauseTime() const;
177 
180  public: common::Time GetStartTime() const;
181 
184  public: common::Time GetRealTime() const;
185 
188  public: bool IsPaused() const;
189 
192  public: void SetPaused(bool _p);
193 
200  public: BasePtr GetByName(const std::string &_name);
201 
208  public: ModelPtr GetModel(const std::string &_name);
209 
216  public: EntityPtr GetEntity(const std::string &_name);
217 
223  public: ModelPtr GetModelBelowPoint(const math::Vector3 &_pt);
224 
231  public: EntityPtr GetEntityBelowPoint(const math::Vector3 &_pt);
232 
235  public: void SetState(const WorldState &_state);
236 
241  public: void InsertModelFile(const std::string &_sdfFilename);
242 
247  public: void InsertModelString(const std::string &_sdfString);
248 
253  public: void InsertModelSDF(const sdf::SDF &_sdf);
254 
258  public: std::string StripWorldName(const std::string &_name) const;
259 
264  public: void EnableAllModels();
265 
270  public: void DisableAllModels();
271 
274  public: void StepWorld(int _steps);
275 
280  public: void LoadPlugin(const std::string &_filename,
281  const std::string &_name,
282  sdf::ElementPtr _sdf);
283 
286  public: void RemovePlugin(const std::string &_name);
287 
290  public: boost::mutex *GetSetWorldPoseMutex() const
291  {return this->setWorldPoseMutex;}
292 
295  public: bool GetEnablePhysicsEngine()
296  {return this->enablePhysicsEngine;}
297 
300  public: void EnablePhysicsEngine(bool _enable)
301  {this->enablePhysicsEngine = _enable;}
302 
304  public: void UpdateStateSDF();
305 
308  public: bool IsLoaded() const;
309 
314  public: void PublishModelPose(physics::ModelPtr _model);
315 
324  private: ModelPtr GetModelById(unsigned int _id);
326 
330  private: void LoadPlugins();
331 
335  private: void LoadEntities(sdf::ElementPtr _sdf, BasePtr _parent);
336 
341  private: ModelPtr LoadModel(sdf::ElementPtr _sdf, BasePtr _parent);
342 
347  private: ActorPtr LoadActor(sdf::ElementPtr _sdf, BasePtr _parent);
348 
353  private: RoadPtr LoadRoad(sdf::ElementPtr _sdf, BasePtr _parent);
354 
356  private: void RunLoop();
357 
359  private: void Step();
360 
362  private: void LogStep();
363 
365  private: void Update();
366 
369  private: void OnPause(bool _p);
370 
372  private: void OnStep();
373 
376  private: void OnControl(ConstWorldControlPtr &_data);
377 
380  private: void OnRequest(ConstRequestPtr &_msg);
381 
384  private: void SetSelectedEntityCB(const std::string &_name);
385 
390  private: void BuildSceneMsg(msgs::Scene &_scene, BasePtr _entity);
391 
394  private: void JointLog(ConstJointPtr &_msg);
395 
398  private: void OnFactoryMsg(ConstFactoryPtr &_data);
399 
402  private: void OnModelMsg(ConstModelPtr &_msg);
403 
405  private: void ModelUpdateTBB();
406 
408  private: void ModelUpdateSingleLoop();
409 
412  private: void LoadPlugin(sdf::ElementPtr _sdf);
413 
417  private: void FillModelMsg(msgs::Model &_msg, ModelPtr _model);
418 
421  private: void ProcessEntityMsgs();
422 
425  private: void ProcessRequestMsgs();
426 
429  private: void ProcessFactoryMsgs();
430 
433  private: void ProcessModelMsgs();
434 
436  private: bool OnLog(std::ostringstream &_stream);
437 
439  private: void ProcessMessages();
440 
442  private: void PublishWorldStats();
443 
445  private: void LogWorker();
446 
448  private: common::Time prevStepWallTime;
449 
451  private: PhysicsEnginePtr physicsEngine;
452 
454  private: BasePtr rootElement;
455 
457  private: boost::thread *thread;
458 
460  private: bool stop;
461 
463  private: EntityPtr selectedEntity;
464 
466  private: std::string name;
467 
469  private: common::Time simTime;
470 
472  private: common::Time pauseTime;
473 
475  private: common::Time startTime;
476 
478  private: bool pause;
479 
481  private: int stepInc;
482 
484  private: event::Connection_V connections;
485 
487  private: transport::NodePtr node;
488 
490  private: transport::PublisherPtr selectionPub;
491 
493  private: transport::PublisherPtr statPub;
494 
496  private: transport::PublisherPtr diagPub;
497 
499  private: transport::PublisherPtr responsePub;
500 
502  private: transport::PublisherPtr modelPub;
503 
505  private: transport::PublisherPtr guiPub;
506 
508  private: transport::PublisherPtr lightPub;
509 
511  private: transport::PublisherPtr posePub;
512 
514  private: transport::SubscriberPtr controlSub;
515 
517  private: transport::SubscriberPtr factorySub;
518 
520  private: transport::SubscriberPtr jointSub;
521 
523  private: transport::SubscriberPtr modelSub;
524 
526  private: transport::SubscriberPtr requestSub;
527 
529  private: msgs::WorldStatistics worldStatsMsg;
530 
532  private: msgs::Scene sceneMsg;
533 
535  private: void (World::*modelUpdateFunc)();
536 
538  private: common::Time prevStatTime;
539 
541  private: common::Time pauseStartTime;
542 
544  private: common::Time realTimeOffset;
545 
547  private: boost::recursive_mutex *receiveMutex;
548 
550  private: boost::mutex *loadModelMutex;
551 
556  private: boost::mutex *setWorldPoseMutex;
557 
564  private: boost::recursive_mutex *worldUpdateMutex;
565 
567  private: sdf::ElementPtr sdf;
568 
570  private: std::vector<WorldPluginPtr> plugins;
571 
573  private: std::list<std::string> deleteEntity;
574 
578  public: std::list<Entity*> dirtyPoses;
579 
581  private: std::list<msgs::Request> requestMsgs;
582 
584  private: std::list<msgs::Factory> factoryMsgs;
585 
587  private: std::list<msgs::Model> modelMsgs;
588 
590  private: bool needsReset;
591 
593  private: bool resetAll;
594 
596  private: bool resetTimeOnly;
597 
599  private: bool resetModelOnly;
600 
602  private: bool initialized;
603 
605  private: bool loaded;
606 
608  private: bool enablePhysicsEngine;
609 
611  private: RayShapePtr testRay;
612 
614  private: bool pluginsLoaded;
615 
617  private: common::Time sleepOffset;
618 
620  private: common::Time prevProcessMsgsTime;
621 
623  private: common::Time processMsgsPeriod;
624 
626  private: std::deque<WorldState> states[2];
627 
629  private: int currentStateBuffer;
630 
631  private: WorldState prevStates[2];
632  private: int stateToggle;
633 
634  private: sdf::ElementPtr logPlayStateSDF;
635  private: WorldState logPlayState;
636 
639  private: sdf::SDFPtr factorySDF;
640 
642  private: std::set<ModelPtr> publishModelPoses;
643 
645  private: common::UpdateInfo updateInfo;
646 
648  private: uint64_t iterations;
649 
651  private: uint64_t stopIterations;
652 
654  private: boost::condition_variable logCondition;
655 
658  private: boost::condition_variable logContinueCondition;
659 
661  private: uint64_t logPrevIteration;
662 
664  private: common::Time logRealTime;
665 
667  private: boost::mutex logMutex;
668 
670  private: boost::mutex logBufferMutex;
671 
673  private: boost::mutex entityDeleteMutex;
674 
676  private: boost::thread *logThread;
677  };
679  }
680 }
681 #endif