World.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2016 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 #ifndef _GAZEBO_WORLD_HH_
18 #define _GAZEBO_WORLD_HH_
19 
20 #ifdef _WIN32
21  // Ensure that Winsock2.h is included before Windows.h, which can get
22  // pulled in by anybody (e.g., Boost).
23  #include <Winsock2.h>
24 #endif
25 
26 #include <vector>
27 #include <list>
28 #include <set>
29 #include <deque>
30 #include <string>
31 #include <boost/thread.hpp>
32 #include <boost/enable_shared_from_this.hpp>
33 #include <boost/shared_ptr.hpp>
34 
35 #include <sdf/sdf.hh>
36 
38 
39 #include "gazebo/msgs/msgs.hh"
40 
43 #include "gazebo/common/Event.hh"
44 
45 #include "gazebo/physics/Base.hh"
48 #include "gazebo/util/system.hh"
49 
50 namespace gazebo
51 {
52  namespace physics
53  {
55  class WorldPrivate;
56 
59 
68  class GZ_PHYSICS_VISIBLE World :
69  public boost::enable_shared_from_this<World>
70  {
74  public: explicit World(const std::string &_name = "");
75 
77  public: ~World();
78 
82  public: void Load(sdf::ElementPtr _sdf);
83 
87  public: void Save(const std::string &_filename);
88 
91  public: void Init();
92 
97  public: void Run(unsigned int _iterations = 0);
98 
101  public: bool GetRunning() const;
102 
105  public: void Stop();
106 
109  public: void Fini();
110 
114  public: void Clear();
115 
118  public: std::string GetName() const;
119 
123  public: PhysicsEnginePtr GetPhysicsEngine() const;
124 
127  public: physics::Atmosphere &Atmosphere() const;
128 
131  public: PresetManagerPtr GetPresetManager() const;
132 
135  public: physics::Wind &Wind() const;
136 
139  public: common::SphericalCoordinatesPtr GetSphericalCoordinates() const;
140 
143  public: ignition::math::Vector3d Gravity() const;
144 
147  public: void SetGravity(const ignition::math::Vector3d &_gravity);
148 
151  public: void SetGravitySDF(const ignition::math::Vector3d &_gravity);
152 
155  public: virtual ignition::math::Vector3d MagneticField() const;
156 
159  public: void SetMagneticField(const ignition::math::Vector3d &_mag);
160 
163  public: unsigned int GetModelCount() const;
164 
170  public: ModelPtr GetModel(unsigned int _index) const;
171 
174  public: Model_V GetModels() const;
175 
178  public: unsigned int LightCount() const;
179 
182  public: Light_V Lights() const;
183 
188  public: void ResetEntities(Base::EntityType _type = Base::BASE);
189 
191  public: void ResetTime();
192 
194  public: void Reset();
195 
198  public: void PrintEntityTree();
199 
203  public: common::Time GetSimTime() const;
204 
207  public: void SetSimTime(const common::Time &_t);
208 
211  public: common::Time GetPauseTime() const;
212 
215  public: common::Time GetStartTime() const;
216 
219  public: common::Time GetRealTime() const;
220 
223  public: bool IsPaused() const;
224 
227  public: void SetPaused(bool _p);
228 
234  public: BasePtr GetByName(const std::string &_name);
235 
241  public: ModelPtr GetModel(const std::string &_name);
242 
248  public: LightPtr Light(const std::string &_name);
249 
255  public: EntityPtr GetEntity(const std::string &_name);
256 
264  public: ModelPtr GetModelBelowPoint(const math::Vector3 &_pt);
265 
271  public: EntityPtr GetEntityBelowPoint(const math::Vector3 &_pt);
272 
275  public: void SetState(const WorldState &_state);
276 
280  public: void InsertModelFile(const std::string &_sdfFilename);
281 
285  public: void InsertModelString(const std::string &_sdfString);
286 
290  public: void InsertModelSDF(const sdf::SDF &_sdf);
291 
295  public: std::string StripWorldName(const std::string &_name) const;
296 
300  public: void EnableAllModels();
301 
305  public: void DisableAllModels();
306 
309  public: void Step(unsigned int _steps);
310 
315  public: void LoadPlugin(const std::string &_filename,
316  const std::string &_name,
317  sdf::ElementPtr _sdf);
318 
321  public: void RemovePlugin(const std::string &_name);
322 
325  public: boost::mutex *GetSetWorldPoseMutex() const;
326 
329  public: bool GetEnablePhysicsEngine();
330 
333  public: void EnablePhysicsEngine(bool _enable);
334 
337  public: bool WindEnabled() const;
338 
341  public: void SetWindEnabled(const bool _enable);
342 
345  public: bool AtmosphereEnabled() const;
346 
349  public: void SetAtmosphereEnabled(const bool _enable);
350 
352  public: void UpdateStateSDF();
353 
356  public: bool IsLoaded() const;
357 
360  public: void ClearModels();
361 
366  public: void PublishModelPose(physics::ModelPtr _model);
367 
372  public: void PublishModelScale(physics::ModelPtr _model);
373 
378  public: void PublishLightPose(const physics::LightPtr _light);
379 
382  public: uint32_t GetIterations() const;
383 
386  public: msgs::Scene GetSceneMsg() const;
387 
392  public: void RunBlocking(unsigned int _iterations = 0);
393 
398  public: void RemoveModel(ModelPtr _model);
399 
404  public: void RemoveModel(const std::string &_name);
405 
408  public: void ResetPhysicsStates();
409 
416  public: void _AddDirty(Entity *_entity);
417 
420  public: bool SensorsInitialized() const;
421 
426  public: void _SetSensorsInitialized(const bool _init);
427 
435  private: ModelPtr GetModelById(unsigned int _id);
437 
441  private: void LoadPlugins();
442 
446  private: void LoadEntities(sdf::ElementPtr _sdf, BasePtr _parent);
447 
452  private: ModelPtr LoadModel(sdf::ElementPtr _sdf, BasePtr _parent);
453 
458  private: LightPtr LoadLight(const sdf::ElementPtr &_sdf,
459  const BasePtr &_parent);
460 
465  private: ActorPtr LoadActor(sdf::ElementPtr _sdf, BasePtr _parent);
466 
471  private: RoadPtr LoadRoad(sdf::ElementPtr _sdf, BasePtr _parent);
472 
474  private: void RunLoop();
475 
477  private: void Step();
478 
480  private: void LogStep();
481 
483  private: void Update();
484 
487  private: void OnPause(bool _p);
488 
490  private: void OnStep();
491 
494  private: void OnControl(ConstWorldControlPtr &_data);
495 
498  private: void OnPlaybackControl(ConstLogPlaybackControlPtr &_data);
499 
502  private: void OnRequest(ConstRequestPtr &_msg);
503 
508  private: void BuildSceneMsg(msgs::Scene &_scene, BasePtr _entity);
509 
512  private: void JointLog(ConstJointPtr &_msg);
513 
516  private: void OnFactoryMsg(ConstFactoryPtr &_data);
517 
520  private: void OnModelMsg(ConstModelPtr &_msg);
521 
523  private: void ModelUpdateTBB();
524 
526  private: void ModelUpdateSingleLoop();
527 
530  private: void LoadPlugin(sdf::ElementPtr _sdf);
531 
535  private: void FillModelMsg(msgs::Model &_msg, ModelPtr _model);
536 
539  private: void ProcessEntityMsgs();
540 
543  private: void ProcessRequestMsgs();
544 
547  private: void ProcessFactoryMsgs();
548 
551  private: void ProcessModelMsgs();
552 
555  private: void ProcessLightFactoryMsgs();
556 
559  private: void ProcessLightModifyMsgs();
560 
562  private: bool OnLog(std::ostringstream &_stream);
563 
565  private: void ProcessMessages();
566 
568  private: void PublishWorldStats();
569 
571  private: void LogWorker();
572 
578  private: void OnLightMsg(ConstLightPtr &_msg);
579 
583  private: void OnLightFactoryMsg(ConstLightPtr &_msg);
584 
588  private: void OnLightModifyMsg(ConstLightPtr &_msg);
589 
592  private: WorldPrivate *dataPtr;
593 
595  private: friend class DARTLink;
596 
598  private: friend class SimbodyPhysics;
599  };
601  }
602 }
603 #endif
boost::shared_ptr< Actor > ActorPtr
Definition: PhysicsTypes.hh:97
boost::shared_ptr< PresetManager > PresetManagerPtr
Definition: PhysicsTypes.hh:129
A light entity.
Definition: physics/Light.hh:33
std::vector< ModelPtr > Model_V
Definition: PhysicsTypes.hh:205
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:39
std::vector< LightPtr > Light_V
Definition: PhysicsTypes.hh:221
Forward declarations for transport.
default namespace for gazebo
Base type.
Definition: Base.hh:83
boost::shared_ptr< PhysicsEngine > PhysicsEnginePtr
Definition: PhysicsTypes.hh:125
The world provides access to all other object within a simulated environment.
Definition: World.hh:68
Simbody physics engine.
Definition: SimbodyPhysics.hh:42
boost::shared_ptr< SphericalCoordinates > SphericalCoordinatesPtr
Definition: CommonTypes.hh:140
Base class for wind.
Definition: Wind.hh:41
Base class for all physics objects in Gazebo.
Definition: Entity.hh:58
boost::shared_ptr< Entity > EntityPtr
Definition: PhysicsTypes.hh:85
boost::shared_ptr< Road > RoadPtr
Definition: PhysicsTypes.hh:161
boost::shared_ptr< Model > ModelPtr
Definition: PhysicsTypes.hh:93
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
EntityType
Unique identifiers for all entity types.
Definition: Base.hh:81
boost::shared_ptr< Light > LightPtr
Definition: PhysicsTypes.hh:105
Store state information of a physics::World object.
Definition: WorldState.hh:47
This models a base atmosphere class to serve as a common interface to any derived atmosphere models...
Definition: Atmosphere.hh:42
boost::shared_ptr< Base > BasePtr
Definition: PhysicsTypes.hh:77
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:44