PhysicsEngine.hh
Go to the documentation of this file.
1/*
2 * Copyright (C) 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#ifndef _PHYSICSENGINE_HH_
18#define _PHYSICSENGINE_HH_
19
20#include <boost/thread/recursive_mutex.hpp>
21#include <boost/any.hpp>
22#include <string>
23#include <ignition/transport/Node.hh>
24
26#include "gazebo/msgs/msgs.hh"
27
29#include "gazebo/util/system.hh"
30
31namespace gazebo
32{
33 namespace physics
34 {
35 class ContactManager;
36
39
42 class GZ_PHYSICS_VISIBLE PhysicsEngine
43 {
46 public: explicit PhysicsEngine(WorldPtr _world);
47
49 public: virtual ~PhysicsEngine();
50
53 public: virtual void Load(sdf::ElementPtr _sdf);
54
56 public: virtual void Init() = 0;
57
59 public: virtual void Fini();
60
62 public: virtual void Reset() {}
63
65 public: virtual void InitForThread() = 0;
66
90 public: virtual void UpdateCollision() = 0;
91
94 public: virtual std::string GetType() const = 0;
95
98 public: virtual void SetSeed(uint32_t _seed) = 0;
99
102 public: double GetUpdatePeriod();
103
106 public: double GetTargetRealTimeFactor() const;
107
110 public: double GetRealTimeUpdateRate() const;
111
114 public: double GetMaxStepSize() const;
115
118 public: void SetTargetRealTimeFactor(double _factor);
119
122 public: void SetRealTimeUpdateRate(double _rate);
123
126 public: void SetMaxStepSize(double _stepSize);
127
132 public: virtual void UpdatePhysics() {}
133
136 public: virtual ModelPtr CreateModel(BasePtr _base);
137
140 public: virtual LinkPtr CreateLink(ModelPtr _parent) = 0;
141
146 const std::string &_shapeType, LinkPtr _link) = 0;
147
151 public: CollisionPtr CreateCollision(const std::string &_shapeType,
152 const std::string &_linkName);
153
157 public: virtual ShapePtr CreateShape(const std::string &_shapeType,
158 CollisionPtr _collision) = 0;
159
163 public: virtual JointPtr CreateJoint(const std::string &_type,
164 ModelPtr _parent = ModelPtr()) = 0;
165
166
169 public: virtual void SetGravity(
170 const ignition::math::Vector3d &_gravity) = 0;
171
176 public: virtual void SetAutoDisableFlag(bool _autoDisable);
177
182 public: virtual void SetMaxContacts(unsigned int _maxContacts);
183
188 public: virtual bool GetAutoDisableFlag() {return 0;}
189
221 public: virtual bool SetParam(const std::string &_key,
222 const boost::any &_value);
223
228 public: virtual boost::any GetParam(const std::string &_key) const;
229
235 public: virtual bool GetParam(const std::string &_key,
236 boost::any &_value) const;
237
239 public: virtual void DebugPrint() const = 0;
240
243 public: WorldPtr World() const;
244
248
251 public: boost::recursive_mutex *GetPhysicsUpdateMutex() const
252 {return this->physicsUpdateMutex;}
253
256 public: sdf::ElementPtr GetSDF() const;
257
268 public:
269 template <typename T>
270 static T any_cast(const boost::any &_value)
271 {
272 try
273 {
274 auto value = boost::any_cast<std::any>(_value);
275 return std::any_cast<T>(value);
276 }
277 catch(boost::bad_any_cast &)
278 {
279 return boost::any_cast<T>(_value);
280 }
281 }
282
285 protected: virtual void OnRequest(ConstRequestPtr &_msg);
286
289 protected: virtual void OnPhysicsMsg(ConstPhysicsPtr &_msg);
290
292 protected: WorldPtr world;
293
295 protected: sdf::ElementPtr sdf;
296
299
302
305
308
310 protected: boost::recursive_mutex *physicsUpdateMutex;
311
315
317 protected: double realTimeUpdateRate;
318
320 protected: double targetRealTimeFactor;
321
323 protected: double maxStepSize;
324
325 // Place ignition::transport objects at the end of this file to
326 // guarantee they are destructed first.
327
329 protected: ignition::transport::Node nodeIgn;
330
332 protected: ignition::transport::Node::Publisher responsePubIgn;
333 };
335 }
336}
337#endif
default namespace for gazebo
Forward declarations for transport.
Aggregates all the contact information generated by the collision detection engine.
Definition ContactManager.hh:73
Base class for a physics engine.
Definition PhysicsEngine.hh:43
double GetMaxStepSize() const
Get max step size.
virtual bool GetParam(const std::string &_key, boost::any &_value) const
Get a parameter from the physics engine with a boolean to indicate success or failure.
virtual ~PhysicsEngine()
Destructor.
virtual CollisionPtr CreateCollision(const std::string &_shapeType, LinkPtr _link)=0
Create a collision.
double targetRealTimeFactor
Target real time factor.
Definition PhysicsEngine.hh:320
ignition::transport::Node::Publisher responsePubIgn
Response publisher.
Definition PhysicsEngine.hh:332
virtual void Fini()
Finilize the physics engine.
double GetRealTimeUpdateRate() const
Get real time update rate.
double realTimeUpdateRate
Real time update rate.
Definition PhysicsEngine.hh:317
void SetMaxStepSize(double _stepSize)
Set max step size.
sdf::ElementPtr GetSDF() const
Get a pointer to the SDF element for this physics engine.
virtual void Reset()
Rest the physics engine.
Definition PhysicsEngine.hh:62
virtual void Init()=0
Initialize the physics engine.
double maxStepSize
Real time update rate.
Definition PhysicsEngine.hh:323
virtual void DebugPrint() const =0
Debug print out of the physic engine state.
virtual void UpdatePhysics()
Update the physics engine.
Definition PhysicsEngine.hh:132
transport::NodePtr node
Node for communication.
Definition PhysicsEngine.hh:298
virtual void OnRequest(ConstRequestPtr &_msg)
virtual callback for gztopic "~/request".
transport::PublisherPtr responsePub
Response publisher.
Definition PhysicsEngine.hh:301
transport::SubscriberPtr requestSub
Subscribe to the request topic.
Definition PhysicsEngine.hh:307
ignition::transport::Node nodeIgn
Ignition node for communication.
Definition PhysicsEngine.hh:329
boost::recursive_mutex * GetPhysicsUpdateMutex() const
returns a pointer to the PhysicsEngine::physicsUpdateMutex.
Definition PhysicsEngine.hh:251
PhysicsEngine(WorldPtr _world)
Default constructor.
virtual std::string GetType() const =0
Return the physics engine type (ode|bullet|dart|simbody).
void SetRealTimeUpdateRate(double _rate)
Set real time update rate.
virtual void SetGravity(const ignition::math::Vector3d &_gravity)=0
Set the gravity vector.
WorldPtr World() const
Get a pointer to the world.
virtual ShapePtr CreateShape(const std::string &_shapeType, CollisionPtr _collision)=0
Create a physics::Shape object.
virtual bool GetAutoDisableFlag()
\TODO: Remove this function, and replace it with a more generic property map
Definition PhysicsEngine.hh:188
transport::SubscriberPtr physicsSub
Subscribe to the physics topic.
Definition PhysicsEngine.hh:304
sdf::ElementPtr sdf
Our SDF values.
Definition PhysicsEngine.hh:295
virtual void OnPhysicsMsg(ConstPhysicsPtr &_msg)
virtual callback for gztopic "~/physics".
virtual JointPtr CreateJoint(const std::string &_type, ModelPtr _parent=ModelPtr())=0
Create a new joint.
double GetTargetRealTimeFactor() const
Get target real time factor.
virtual boost::any GetParam(const std::string &_key) const
Get an parameter of the physics engine.
virtual void SetAutoDisableFlag(bool _autoDisable)
\TODO: Remove this function, and replace it with a more generic property map
virtual void UpdateCollision()=0
Update the physics engine collision.
virtual ModelPtr CreateModel(BasePtr _base)
Create a new model.
ContactManager * GetContactManager() const
Get a pointer to the contact manger.
WorldPtr world
Pointer to the world.
Definition PhysicsEngine.hh:292
virtual void Load(sdf::ElementPtr _sdf)
Load the physics engine.
double GetUpdatePeriod()
Get the simulation update period.
virtual bool SetParam(const std::string &_key, const boost::any &_value)
Set a parameter of the physics engine.
void SetTargetRealTimeFactor(double _factor)
Set target real time factor.
boost::recursive_mutex * physicsUpdateMutex
Mutex to protect the update cycle.
Definition PhysicsEngine.hh:310
CollisionPtr CreateCollision(const std::string &_shapeType, const std::string &_linkName)
Create a collision.
virtual LinkPtr CreateLink(ModelPtr _parent)=0
Create a new body.
ContactManager * contactManager
Class that handles all contacts generated by the physics engine.
Definition PhysicsEngine.hh:314
virtual void SetSeed(uint32_t _seed)=0
Set the random number seed for the physics engine.
virtual void InitForThread()=0
Init the engine for threads.
static T any_cast(const boost::any &_value)
Helper function for performing any_cast operations in SetParam.
Definition PhysicsEngine.hh:270
virtual void SetMaxContacts(unsigned int _maxContacts)
\TODO: Remove this function, and replace it with a more generic property map
boost::shared_ptr< Link > LinkPtr
Definition PhysicsTypes.hh:110
boost::shared_ptr< Collision > CollisionPtr
Definition PhysicsTypes.hh:114
boost::shared_ptr< Base > BasePtr
Definition PhysicsTypes.hh:78
boost::shared_ptr< Shape > ShapePtr
Definition PhysicsTypes.hh:142
boost::shared_ptr< World > WorldPtr
Definition PhysicsTypes.hh:90
boost::shared_ptr< Model > ModelPtr
Definition PhysicsTypes.hh:94
boost::shared_ptr< Joint > JointPtr
Definition PhysicsTypes.hh:118
boost::shared_ptr< Subscriber > SubscriberPtr
Definition TransportTypes.hh:53
boost::shared_ptr< Publisher > PublisherPtr
Definition TransportTypes.hh:49
boost::shared_ptr< Node > NodePtr
Definition TransportTypes.hh:57
Forward declarations for the common classes.
Definition Animation.hh:27