All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ODEPhysics.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 ODE physics engine wrapper
18  * Author: Nate Koenig
19  * Date: 11 June 2007
20  */
21 
22 #ifndef ODEPHYSICS_HH
23 #define ODEPHYSICS_HH
24 
25 #include <tbb/spin_mutex.h>
26 #include <tbb/concurrent_vector.h>
27 #include <map>
28 #include <string>
29 #include <vector>
30 #include <utility>
31 
32 #include <boost/thread/thread.hpp>
33 
34 #include "physics/ode/ode_inc.h"
35 
36 #include "physics/ode/ODETypes.hh"
37 
38 #include "physics/PhysicsEngine.hh"
39 #include "physics/Contact.hh"
40 #include "physics/Shape.hh"
41 #include "gazebo_config.h"
42 
43 namespace gazebo
44 {
45  namespace physics
46  {
50 
53  {
54  public: Contact contact;
55  public: dJointFeedback feedbacks[MAX_CONTACT_JOINTS];
56  public: int feedbackCount;
57  };
58 
60  class ODEPhysics : public PhysicsEngine
61  {
63  public: ODEPhysics(WorldPtr world);
64 
66  public: virtual ~ODEPhysics();
67 
69  public: virtual void Load(sdf::ElementPtr _sdf);
70 
72  public: virtual void Init();
73 
75  public: void Reset();
76 
78  public: virtual void InitForThread();
79 
81  public: virtual void UpdateCollision();
82 
84  public: virtual void UpdatePhysics();
85 
87  public: virtual void Fini();
88 
90  public: void SetStepTime(double _value);
91 
93  public: virtual double GetStepTime();
94 
96  public: virtual LinkPtr CreateLink(ModelPtr _parent);
97 
99  public: virtual CollisionPtr CreateCollision(
100  const std::string &_shapeType, LinkPtr _parent);
101 
103  public: virtual ShapePtr CreateShape(const std::string &_shapeType,
104  CollisionPtr _collision);
105 
107  public: virtual JointPtr CreateJoint(const std::string &type,
108  ModelPtr _parent);
109 
111  public: dSpaceID GetSpaceId() const;
112 
114  public: dWorldID GetWorldId();
115 
117  public: static void ConvertMass(InertialPtr _interial, void *odeMass);
118 
120  public: static void ConvertMass(void *odeMass, InertialPtr _inertial);
121 
123  public: virtual std::string GetStepType() const;
124 
126  public: virtual void SetStepType(const std::string &_type);
127 
129  public: virtual void SetGravity(const gazebo::math::Vector3 &gravity);
130 
132  public: math::Vector3 GetGravity() const;
133 
135  public: void SetWorldCFM(double cfm);
137  public: void SetWorldERP(double erp);
139  public: void SetSORPGSPreconIters(unsigned int iters);
141  public: void SetSORPGSIters(unsigned int iters);
143  public: void SetSORPGSW(double w);
145  public: void SetContactMaxCorrectingVel(double vel);
147  public: void SetContactSurfaceLayer(double layer_depth);
149  public: void SetMaxContacts(unsigned int max_contacts);
150 
152  public: double GetWorldCFM();
154  public: double GetWorldERP();
156  public: int GetSORPGSPreconIters();
158  public: int GetSORPGSIters();
160  public: double GetSORPGSW();
162  public: double GetContactMaxCorrectingVel();
164  public: double GetContactSurfaceLayer();
166  public: int GetMaxContacts();
167 
169  public: void CreateContact(ODECollision *collision1,
170  ODECollision *collision2);
171 
173  private: static void CollisionCallback(void *data,
174  dGeomID o1, dGeomID o2);
175 
177  public: void Collide(ODECollision *collision1, ODECollision *collision2,
178  dContactGeom *contactCollisions);
179 
181  public: void ProcessContactFeedback(ContactFeedback* feedback,
182  msgs::Contact *_msg);
183 
184  public: virtual void DebugPrint() const;
185 
187  protected: virtual void OnRequest(
188  ConstRequestPtr &/*_msg*/);
189 
191  protected: virtual void OnPhysicsMsg(
192  ConstPhysicsPtr &/*_msg*/);
193 
194  private: void AddTrimeshCollider(ODECollision *_collision1,
195  ODECollision *_collision2);
196 
197 
198  private: void AddCollider(ODECollision *_collision1,
199  ODECollision *_collision2);
200 
202  private: dWorldID worldId;
203 
205  private: dSpaceID spaceId;
206 
208  private: dJointGroupID contactGroup;
209 
212  private: double stepTimeDouble;
213  private: std::string stepType;
214 
215  private: std::vector<ContactFeedback*> contactFeedbacks;
216  private: unsigned int contactFeedbackIndex;
217 
218  private: std::map<std::string, dSpaceID> spaces;
219 
220  private: std::vector< std::pair<ODECollision*, ODECollision*> > colliders;
221  private: std::vector< std::pair<ODECollision*, ODECollision*> >
222  trimeshColliders;
223 
224  private: unsigned int collidersCount, trimeshCollidersCount;
225 
226  private: tbb::spin_mutex collideMutex;
227 
228  private: dContactGeom contactCollisions[MAX_COLLIDE_RETURNS];
229  private: int (*physicsStepFunc)(dxWorld*, dReal);
230 
231  private: int indices[MAX_CONTACT_JOINTS];
232  };
233  typedef boost::shared_ptr<ODEPhysics> ODEPhysicsPtr;
234 
236  }
237 }
238 #endif