Model.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 GAZEBO_PHYSICS_MODEL_HH_
18 #define GAZEBO_PHYSICS_MODEL_HH_
19 
20 #include <string>
21 #include <map>
22 #include <mutex>
23 #include <vector>
24 #include <boost/function.hpp>
25 #include <boost/thread/recursive_mutex.hpp>
26 
30 #include "gazebo/physics/Entity.hh"
32 #include "gazebo/util/system.hh"
33 
34 namespace boost
35 {
36  class recursive_mutex;
37 }
38 
39 // Forward declare reference and pointer parameters
40 namespace ignition
41 {
42  namespace msgs
43  {
44  class Plugin_V;
45  }
46 }
47 
48 namespace gazebo
49 {
50  namespace physics
51  {
52  class Gripper;
53 
56 
59  class GZ_PHYSICS_VISIBLE Model : public Entity
60  {
63  public: explicit Model(BasePtr _parent);
64 
66  public: virtual ~Model();
67 
70  public: void Load(sdf::ElementPtr _sdf);
71 
73  public: void LoadJoints();
74 
76  public: virtual void Init();
77 
79  public: void Update();
80 
82  public: virtual void Fini();
83 
86  public: virtual void UpdateParameters(sdf::ElementPtr _sdf);
87 
90  public: virtual const sdf::ElementPtr GetSDF();
91 
97  public: virtual const sdf::ElementPtr UnscaledSDF();
98 
101  public: virtual void RemoveChild(EntityPtr _child);
102  using Base::RemoveChild;
103 
105  public: void Reset();
106  using Entity::Reset;
107 
110  public: void ResetPhysicsStates();
111 
114  public: void SetLinearVel(const ignition::math::Vector3d &_vel);
115 
118  public: void SetAngularVel(const ignition::math::Vector3d &_vel);
119 
124  public: void SetLinearAccel(const ignition::math::Vector3d &_vel)
125  GAZEBO_DEPRECATED(9.0);
126 
131  public: void SetAngularAccel(const ignition::math::Vector3d &_vel)
132  GAZEBO_DEPRECATED(9.0);
133 
137  public: virtual ignition::math::Vector3d RelativeLinearVel() const;
138 
142  public: virtual ignition::math::Vector3d WorldLinearVel() const;
143 
147  public: virtual ignition::math::Vector3d RelativeAngularVel() const;
148 
152  public: virtual ignition::math::Vector3d WorldAngularVel() const;
153 
157  public: virtual ignition::math::Vector3d RelativeLinearAccel() const;
158 
162  public: virtual ignition::math::Vector3d WorldLinearAccel() const;
163 
167  public: virtual ignition::math::Vector3d RelativeAngularAccel() const;
168 
172  public: virtual ignition::math::Vector3d WorldAngularAccel() const;
173 
176  public: virtual ignition::math::Box BoundingBox() const;
177 
180  public: unsigned int GetJointCount() const;
181 
185  public: ModelPtr NestedModel(const std::string &_name) const;
186 
189  public: const Model_V &NestedModels() const;
190 
194  public: const Link_V &GetLinks() const;
195 
198  public: const Joint_V &GetJoints() const;
199 
203  public: JointPtr GetJoint(const std::string &name);
204 
209  public: LinkPtr GetLinkById(unsigned int _id) const;
211 
215  public: LinkPtr GetLink(const std::string &_name ="canonical") const;
216 
224  public: virtual bool GetSelfCollide() const;
225 
229  public: virtual void SetSelfCollide(bool _self_collide);
230 
233  public: void SetGravityMode(const bool &_value);
234 
239  public: void SetCollideMode(const std::string &_mode);
240 
243  public: void SetLaserRetro(const float _retro);
244 
247  public: virtual void FillMsg(msgs::Model &_msg);
248 
251  public: void ProcessMsg(const msgs::Model &_msg);
252 
257  public: void SetJointPosition(const std::string &_jointName,
258  double _position, int _index = 0);
259 
263  public: void SetJointPositions(
264  const std::map<std::string, double> &_jointPositions);
265 
270  public: void SetJointAnimation(
271  const std::map<std::string, common::NumericAnimationPtr> &_anims,
272  boost::function<void()> _onComplete = NULL);
273 
275  public: virtual void StopAnimation();
276 
291  public: void AttachStaticModel(ModelPtr &_model,
292  ignition::math::Pose3d _offset);
293 
297  public: void DetachStaticModel(const std::string &_model);
298 
301  public: void SetState(const ModelState &_state);
302 
308  public: void SetScale(const ignition::math::Vector3d &_scale,
309  const bool _publish = false);
310 
315  public: ignition::math::Vector3d Scale() const;
316 
319  public: void SetEnabled(bool _enabled);
320 
327  public: void SetLinkWorldPose(const ignition::math::Pose3d &_pose,
328  std::string _linkName);
329 
336  public: void SetLinkWorldPose(const ignition::math::Pose3d &_pose,
337  const LinkPtr &_link);
338 
342  public: void SetAutoDisable(bool _disable);
343 
346  public: bool GetAutoDisable() const;
347 
351  public: void LoadPlugins();
352 
355  public: unsigned int GetPluginCount() const;
356 
360  public: unsigned int GetSensorCount() const;
361 
374  public: std::vector<std::string> SensorScopedName(
375  const std::string &_name) const;
376 
379  public: JointControllerPtr GetJointController();
380 
383  public: GripperPtr GetGripper(size_t _index) const;
384 
388  public: size_t GetGripperCount() const;
389 
393  public: double GetWorldEnergyPotential() const;
394 
399  public: double GetWorldEnergyKinetic() const;
400 
405  public: double GetWorldEnergy() const;
406 
419  public: gazebo::physics::JointPtr CreateJoint(
420  const std::string &_name, const std::string &_type,
421  physics::LinkPtr _parent, physics::LinkPtr _child);
422 
432  public: gazebo::physics::JointPtr CreateJoint(sdf::ElementPtr _sdf);
433 
437  public: bool RemoveJoint(const std::string &_name);
438 
441  public: virtual void SetWindMode(const bool _mode);
442 
445  public: virtual bool WindMode() const;
446 
449  public: boost::shared_ptr<Model> shared_from_this();
450 
455  public: LinkPtr CreateLink(const std::string &_name);
456 
474  public: void PluginInfo(const common::URI &_pluginUri,
475  ignition::msgs::Plugin_V &_plugins, bool &_success);
476 
478  protected: virtual void OnPoseChange();
479 
481  protected: virtual void RegisterIntrospectionItems();
482 
484  private: void LoadLinks();
485 
487  private: void LoadModels();
488 
491  private: void LoadJoint(sdf::ElementPtr _sdf);
492 
495  private: void LoadPlugin(sdf::ElementPtr _sdf);
496 
499  private: void LoadGripper(sdf::ElementPtr _sdf);
500 
504  private: void RemoveLink(const std::string &_name);
505 
507  private: virtual void PublishScale();
508 
510  protected: std::vector<ModelPtr> attachedModels;
511 
513  protected: std::vector<ignition::math::Pose3d> attachedModelsOffset;
514 
517 
519  private: LinkPtr canonicalLink;
520 
522  private: Joint_V joints;
523 
525  private: Link_V links;
526 
528  private: Model_V models;
529 
531  private: std::vector<GripperPtr> grippers;
532 
534  private: std::vector<ModelPluginPtr> plugins;
535 
537  private: std::map<std::string, common::NumericAnimationPtr>
538  jointAnimations;
539 
541  private: boost::function<void()> onJointAnimationComplete;
542 
544  private: JointControllerPtr jointController;
545 
547  private: mutable boost::recursive_mutex updateMutex;
548 
550  private: std::mutex receiveMutex;
551  };
553  }
554 }
555 #endif
std::vector< JointPtr > Joint_V
Definition: PhysicsTypes.hh:213
void Load(sdf::ElementPtr _sdf)
Load the model.
A complete URI.
Definition: URI.hh:176
void LoadJoints()
Load all the joints.
double GetWorldEnergyPotential() const
Returns the potential energy of all links and joint springs in the model.
void SetLinkWorldPose(const ignition::math::Pose3d &_pose, std::string _linkName)
Set the Pose of the entire Model by specifying desired Pose of a Link within the Model.
Definition: Model.hh:40
virtual ignition::math::Vector3d RelativeAngularAccel() const
Get the angular acceleration of the entity.
void AttachStaticModel(ModelPtr &_model, ignition::math::Pose3d _offset)
Attach a static model to this model.
virtual ~Model()
Destructor.
Forward declarations for the common classes.
Definition: Animation.hh:26
boost::shared_ptr< JointController > JointControllerPtr
Definition: PhysicsTypes.hh:121
virtual bool GetSelfCollide() const
If true, all links within the model will collide by default.
Model(BasePtr _parent)
Constructor.
void SetLinearAccel(const ignition::math::Vector3d &_vel) GAZEBO_DEPRECATED(9.0)
Set the linear acceleration of the model, and all its links.
boost::shared_ptr< Model > ModelPtr
Definition: PhysicsTypes.hh:93
#define NULL
Definition: CommonTypes.hh:31
std::vector< ModelPtr > Model_V
Definition: PhysicsTypes.hh:205
virtual ignition::math::Vector3d RelativeAngularVel() const
Get the angular velocity of the entity.
virtual const sdf::ElementPtr GetSDF()
Get the SDF values for the model.
void SetCollideMode(const std::string &_mode)
\TODO This is not implemented in Link, which means this function doesn't do anything.
virtual void StopAnimation()
Stop the current animations.
gazebo::physics::JointPtr CreateJoint(const std::string &_name, const std::string &_type, physics::LinkPtr _parent, physics::LinkPtr _child)
Create a joint for this model.
boost::shared_ptr< Publisher > PublisherPtr
Definition: TransportTypes.hh:49
virtual void Fini()
Finalize the model.
virtual ignition::math::Vector3d WorldLinearVel() const
Get the linear velocity of the entity in the world frame.
virtual bool WindMode() const
Get the wind mode.
Forward declarations for transport.
void SetJointAnimation(const std::map< std::string, common::NumericAnimationPtr > &_anims, boost::function< void()> _onComplete=NULL)
Joint Animation.
Definition: JointMaker.hh:44
size_t GetGripperCount() const
Get the number of grippers in this model.
virtual void SetWindMode(const bool _mode)
Set whether wind affects this body.
void SetLaserRetro(const float _retro)
Set the laser retro reflectiveness of the model.
double GetWorldEnergyKinetic() const
Returns sum of the kinetic energies of all links in this model.
unsigned int GetPluginCount() const
Get the number of plugins this model has.
virtual ignition::math::Vector3d WorldAngularVel() const
Get the angular velocity of the entity in the world frame.
virtual void UpdateParameters(sdf::ElementPtr _sdf)
Update the parameters using new sdf values.
virtual void Reset()
Reset the entity.
default namespace for gazebo
unsigned int GetSensorCount() const
Get the number of sensors attached to this model.
virtual void Init()
Initialize the model.
void Update()
Update the model.
LinkPtr GetLink(const std::string &_name="canonical") const
Get a link by name.
JointPtr GetJoint(const std::string &name)
Get a joint.
double GetWorldEnergy() const
Returns this model's total energy, or sum of Model::GetWorldEnergyPotential() and Model::GetWorldEner...
void SetJointPositions(const std::map< std::string, double > &_jointPositions)
Set the positions of a set of joints.
void SetAngularAccel(const ignition::math::Vector3d &_vel) GAZEBO_DEPRECATED(9.0)
Set the angular acceleration of the model, and all its links.
void SetAngularVel(const ignition::math::Vector3d &_vel)
Set the angular velocity of the model, and all its links.
std::vector< std::string > SensorScopedName(const std::string &_name) const
Get scoped sensor name(s) in the model that matches sensor name.
GripperPtr GetGripper(size_t _index) const
Get a gripper based on an index.
virtual void OnPoseChange()
Callback when the pose of the model has been changed.
virtual ignition::math::Vector3d RelativeLinearVel() const
Get the linear velocity of the entity.
boost::shared_ptr< Model > shared_from_this()
Allow Model class to share itself as a boost shared_ptr.
void Reset()
Reset the model.
JointControllerPtr GetJointController()
Get a handle to the Controller for the joints in this model.
virtual void RemoveChild(unsigned int _id)
Remove a child from this entity.
virtual void FillMsg(msgs::Model &_msg)
Fill a model message.
virtual ignition::math::Vector3d RelativeLinearAccel() const
Get the linear acceleration of the entity.
void ResetPhysicsStates()
Reset the velocity, acceleration, force and torque of all child links.
A model is a collection of links, joints, and plugins.
Definition: Model.hh:59
Store state information of a physics::Model object.
Definition: ModelState.hh:48
unsigned int GetJointCount() const
Get the number of joints.
boost::shared_ptr< Base > BasePtr
Definition: PhysicsTypes.hh:77
LinkPtr CreateLink(const std::string &_name)
Create a new link for this model.
void SetGravityMode(const bool &_value)
Set the gravity mode of the model.
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
virtual ignition::math::Box BoundingBox() const
Get the size of the bounding box.
boost::shared_ptr< Link > LinkPtr
Definition: PhysicsTypes.hh:109
void SetAutoDisable(bool _disable)
Allow the model the auto disable.
ignition::math::Vector3d Scale() const
Get the scale of model.
std::vector< ModelPtr > attachedModels
used by Model::AttachStaticModel
Definition: Model.hh:510
ModelPtr NestedModel(const std::string &_name) const
Get a nested model that is a direct child of this model.
void LoadPlugins()
Load all plugins.
void SetJointPosition(const std::string &_jointName, double _position, int _index=0)
Set the positions of a Joint by name.
boost::shared_ptr< Entity > EntityPtr
Definition: PhysicsTypes.hh:85
boost::shared_ptr< Joint > JointPtr
Definition: PhysicsTypes.hh:117
Base class for all physics objects in Gazebo.
Definition: Entity.hh:52
bool GetAutoDisable() const
Return the value of the SDF <allow_auto_disable> element.
void SetState(const ModelState &_state)
Set the current model state.
#define GAZEBO_DEPRECATED(version)
Definition: system.hh:328
std::vector< ignition::math::Pose3d > attachedModelsOffset
used by Model::AttachStaticModel
Definition: Model.hh:513
std::vector< LinkPtr > Link_V
Definition: PhysicsTypes.hh:225
virtual ignition::math::Vector3d WorldAngularAccel() const
Get the angular acceleration of the entity in the world frame.
virtual const sdf::ElementPtr UnscaledSDF()
virtual ignition::math::Vector3d WorldLinearAccel() const
Get the linear acceleration of the entity in the world frame.
void SetEnabled(bool _enabled)
Enable all the links in all the models.
virtual void SetSelfCollide(bool _self_collide)
Set this model's self_collide property.
boost::shared_ptr< Gripper > GripperPtr
Definition: PhysicsTypes.hh:197
virtual void RemoveChild(EntityPtr _child)
Remove a child.
const Link_V & GetLinks() const
Construct and return a vector of Link's in this model Note this constructs the vector of Link's on th...
const Joint_V & GetJoints() const
Get the joints.
void SetLinearVel(const ignition::math::Vector3d &_vel)
Set the linear velocity of the model, and all its links.
bool RemoveJoint(const std::string &_name)
Remove a joint for this model.
virtual void RegisterIntrospectionItems()
Register items in the introspection service.
void SetScale(const ignition::math::Vector3d &_scale, const bool _publish=false)
Set the scale of model.
void DetachStaticModel(const std::string &_model)
Detach a static model from this model.
transport::PublisherPtr jointPub
Publisher for joint info.
Definition: Model.hh:516
void PluginInfo(const common::URI &_pluginUri, ignition::msgs::Plugin_V &_plugins, bool &_success)
Get information about plugins in this model or one of its children, according to the given _pluginUri...
const Model_V & NestedModels() const
Get all the nested models.
void ProcessMsg(const msgs::Model &_msg)
Update parameters from a model message.