Link.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 /* Desc: Link class
18  * Author: Nate Koenig
19  */
20 
21 #ifndef _LINK_HH_
22 #define _LINK_HH_
23 
24 #ifdef _WIN32
25  // Ensure that Winsock2.h is included before Windows.h, which can get
26  // pulled in by anybody (e.g., Boost).
27  #include <Winsock2.h>
28 #endif
29 
30 #include <map>
31 #include <vector>
32 #include <string>
33 
34 #include "gazebo/msgs/msgs.hh"
36 
37 #include "gazebo/util/UtilTypes.hh"
38 #include "gazebo/common/Event.hh"
40 
42 #include "gazebo/physics/Entity.hh"
44 #include "gazebo/physics/Joint.hh"
45 #include "gazebo/util/system.hh"
46 
47 namespace gazebo
48 {
49  namespace util
50  {
51  class OpenALSource;
52  class OpenALSink;
53  }
54 
55  namespace physics
56  {
57  class Model;
58  class Collision;
59  class Battery;
60 
63 
68  class GZ_PHYSICS_VISIBLE Link : public Entity
69  {
72  public: explicit Link(EntityPtr _parent);
73 
75  public: virtual ~Link();
76 
79  public: virtual void Load(sdf::ElementPtr _sdf);
80 
82  public: virtual void Init();
83 
85  public: void Fini();
86 
88  public: void Reset();
89  using Entity::Reset;
90 
92  public: void ResetPhysicsStates();
93 
96  public: virtual void UpdateParameters(sdf::ElementPtr _sdf);
97 
100  public: void Update(const common::UpdateInfo &_info);
101  using Base::Update;
102 
105  public: void SetScale(const math::Vector3 &_scale);
106 
109  public: virtual void SetEnabled(bool _enable) const = 0;
110 
113  public: virtual bool GetEnabled() const = 0;
114 
118  public: virtual bool SetSelected(bool _set);
119 
122  public: virtual void SetGravityMode(bool _mode) = 0;
123 
126  public: virtual bool GetGravityMode() const = 0;
127 
130  public: virtual void SetWindMode(const bool _mode);
131 
134  public: virtual bool WindMode() const;
135 
140  public: virtual void SetSelfCollide(bool _collide) = 0;
141 
150  public: void SetCollideMode(const std::string &_mode);
151 
159  public: bool GetSelfCollide() const;
160 
163  public: void SetLaserRetro(float _retro);
164 
167  public: virtual void SetLinearVel(const math::Vector3 &_vel) = 0;
168 
171  public: virtual void SetAngularVel(const math::Vector3 &_vel) = 0;
172 
175  public: void SetLinearAccel(const math::Vector3 &_accel);
176 
179  public: void SetAngularAccel(const math::Vector3 &_accel);
180 
183  public: virtual void SetForce(const math::Vector3 &_force) = 0;
184 
187  public: virtual void SetTorque(const math::Vector3 &_torque) = 0;
188 
191  public: virtual void AddForce(const math::Vector3 &_force) = 0;
192 
196  public: virtual void AddRelativeForce(const math::Vector3 &_force) = 0;
197 
201  public: virtual void AddForceAtWorldPosition(const math::Vector3 &_force,
202  const math::Vector3 &_pos) = 0;
203 
208  public: virtual void AddForceAtRelativePosition(
209  const math::Vector3 &_force,
210  const math::Vector3 &_relPos) = 0;
211 
218  public: virtual void AddLinkForce(const math::Vector3 &_force,
219  const math::Vector3 &_offset = math::Vector3::Zero) = 0;
220 
223  public: virtual void AddTorque(const math::Vector3 &_torque) = 0;
224 
228  public: virtual void AddRelativeTorque(const math::Vector3 &_torque) = 0;
229 
234  public: math::Pose GetWorldCoGPose() const;
235 
239  public: virtual math::Vector3 GetWorldLinearVel() const
240  {return this->GetWorldLinearVel(math::Vector3::Zero);}
241 
249  public: virtual math::Vector3 GetWorldLinearVel(
250  const math::Vector3 &_offset) const = 0;
251 
259  public: virtual math::Vector3 GetWorldLinearVel(
260  const math::Vector3 &_offset,
261  const math::Quaternion &_q) const = 0;
262 
267  public: virtual math::Vector3 GetWorldCoGLinearVel() const = 0;
268 
271  public: math::Vector3 GetRelativeLinearVel() const;
272 
275  public: math::Vector3 GetRelativeAngularVel() const;
276 
279  public: math::Vector3 GetRelativeLinearAccel() const;
280 
283  public: math::Vector3 GetWorldLinearAccel() const;
284 
287  public: math::Vector3 GetRelativeAngularAccel() const;
288 
294  public: math::Vector3 GetWorldAngularMomentum() const;
295 
303  public: math::Vector3 GetWorldAngularAccel() const;
304 
307  public: math::Vector3 GetRelativeForce() const;
308 
311  public: virtual math::Vector3 GetWorldForce() const = 0;
312 
315  public: math::Vector3 GetRelativeTorque() const;
316 
319  public: virtual math::Vector3 GetWorldTorque() const = 0;
320 
323  public: ModelPtr GetModel() const;
324 
327  public: InertialPtr GetInertial() const {return this->inertial;}
328 
331  public: void SetInertial(const InertialPtr &_inertial);
332 
338  public: math::Pose GetWorldInertialPose() const;
339 
343  public: math::Matrix3 GetWorldInertiaMatrix() const;
344 
350  public: CollisionPtr GetCollisionById(unsigned int _id) const;
352 
356  public: CollisionPtr GetCollision(const std::string &_name);
357 
361  public: CollisionPtr GetCollision(unsigned int _index) const;
362 
365  public: Collision_V GetCollisions() const;
366 
370  public: virtual math::Box GetBoundingBox() const;
371 
374  public: virtual void SetLinearDamping(double _damping) = 0;
375 
378  public: virtual void SetAngularDamping(double _damping) = 0;
379 
382  public: double GetLinearDamping() const;
383 
386  public: double GetAngularDamping() const;
387 
391  public: virtual void SetKinematic(const bool &_kinematic);
392 
396  public: virtual bool GetKinematic() const {return false;}
397 
404  public: unsigned int GetSensorCount() const;
405 
417  public: std::string GetSensorName(unsigned int _index) const;
418 
422  public: template<typename T>
424  {return enabledSignal.Connect(_subscriber);}
425 
429  {enabledSignal.Disconnect(_conn);}
430 
433  public: void FillMsg(msgs::Link &_msg);
434 
437  public: void ProcessMsg(const msgs::Link &_msg);
438 
441  public: void AddChildJoint(JointPtr _joint);
442 
445  public: void AddParentJoint(JointPtr _joint);
446 
449  public: void RemoveParentJoint(const std::string &_jointName);
450 
453  public: void RemoveChildJoint(const std::string &_jointName);
454 
455  // Documentation inherited.
456  public: virtual void RemoveChild(EntityPtr _child);
457  using Base::RemoveChild;
458 
462  public: void AttachStaticModel(ModelPtr &_model,
463  const math::Pose &_offset);
464 
467  public: void DetachStaticModel(const std::string &_modelName);
468 
470  public: void DetachAllStaticModels();
471 
474  public: virtual void OnPoseChange();
475 
478  public: void SetState(const LinkState &_state);
479 
481  public: virtual void UpdateMass() {}
482 
484  public: virtual void UpdateSurface() {}
485 
488  public: virtual void SetAutoDisable(bool _disable) = 0;
489 
492  public: Link_V GetChildJointsLinks() const;
493 
496  public: Link_V GetParentJointsLinks() const;
497 
500  public: void SetPublishData(bool _enable);
501 
503  public: Joint_V GetParentJoints() const;
504 
506  public: Joint_V GetChildJoints() const;
507 
510  public: void RemoveCollision(const std::string &_name);
511 
515  public: double GetWorldEnergyPotential() const;
516 
520  public: double GetWorldEnergyKinetic() const;
521 
526  public: double GetWorldEnergy() const;
527 
531  public: msgs::Visual GetVisualMessage(const std::string &_name) const;
532 
536  public: virtual void SetLinkStatic(bool _static) = 0;
537 
546  public: void MoveFrame(const math::Pose &_worldReferenceFrameSrc,
547  const math::Pose &_worldReferenceFrameDst);
548 
563  public: bool FindAllConnectedLinksHelper(
564  const LinkPtr &_originalParentLink,
565  Link_V &_connectedLinks, bool _fistLink = false);
566 
569  public: void SetWindEnabled(const bool _enable);
570 
574  public: const ignition::math::Vector3d WorldWindLinearVel() const;
575 
578  public: const ignition::math::Vector3d RelativeWindLinearVel() const;
579 
582  public: void UpdateWind(const common::UpdateInfo &_info);
583 
587  public: common::BatteryPtr Battery(const std::string &_name) const;
588 
591  public: common::BatteryPtr Battery(const size_t _index) const;
592 
596  public: size_t BatteryCount() const;
597 
602  public: bool VisualId(const std::string &_visName, uint32_t &_visualId)
603  const;
604 
609  public: bool VisualPose(const uint32_t _id,
610  ignition::math::Pose3d &_pose) const;
611 
616  public: bool SetVisualPose(const uint32_t _id,
617  const ignition::math::Pose3d &_pose);
618 
620  private: void PublishData();
621 
624  private: void LoadCollision(sdf::ElementPtr _sdf);
625 
628  private: void SetInertialFromCollisions();
629 
632  private: void OnCollision(ConstContactsPtr &_msg);
633 
635  private: void ParseVisuals();
636 
641  private: bool ContainsLink(const Link_V &_vector, const LinkPtr &_value);
642 
645  private: void UpdateVisualGeomSDF(const math::Vector3 &_scale);
646 
648  private: void UpdateVisualMsg();
649 
653  private: void OnWrenchMsg(ConstWrenchPtr &_msg);
654 
657  private: void ProcessWrenchMsg(const msgs::Wrench &_msg);
658 
661  private: void LoadBattery(const sdf::ElementPtr _sdf);
662 
664  protected: InertialPtr inertial;
665 
667  protected: std::vector<std::string> cgVisuals;
668 
671  typedef std::map<uint32_t, msgs::Visual> Visuals_M;
672 
674  protected: Visuals_M visuals;
675 
678 
681 
683  protected: std::vector<math::Pose> attachedModelsOffset;
684 
686  protected: bool initialized;
687 
689  private: event::EventT<void (bool)> enabledSignal;
690 
692  private: bool enabled;
693 
695  private: std::vector<std::string> sensors;
696 
698  private: std::vector<JointPtr> parentJoints;
699 
701  private: std::vector<JointPtr> childJoints;
702 
704  private: std::vector<ModelPtr> attachedModels;
705 
707  private: transport::PublisherPtr dataPub;
708 
710  private: msgs::LinkData linkDataMsg;
711 
713  private: bool publishData;
714 
716  private: boost::recursive_mutex *publishDataMutex;
717 
719  private: Collision_V collisions;
720 
722  private: transport::SubscriberPtr wrenchSub;
723 
725  private: std::vector<msgs::Wrench> wrenchMsgs;
726 
728  private: boost::mutex wrenchMsgMutex;
729 
731  private: ignition::math::Vector3d windLinearVel;
732 
734  private: event::ConnectionPtr updateConnection;
735 
737  private: std::vector<common::BatteryPtr> batteries;
738 
739 #ifdef HAVE_OPENAL
740  private: std::vector<util::OpenALSourcePtr> audioSources;
742 
744  private: util::OpenALSinkPtr audioSink;
745 
748  private: transport::SubscriberPtr audioContactsSub;
749 #endif
750  };
752  }
753 }
754 #endif
boost::shared_ptr< Link > LinkPtr
Definition: PhysicsTypes.hh:109
Encapsulates a position and rotation in three space.
Definition: Pose.hh:37
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:39
Mathematical representation of a box and related functions.
Definition: Box.hh:35
Forward declarations for transport.
boost::shared_ptr< Publisher > PublisherPtr
Definition: TransportTypes.hh:49
boost::shared_ptr< Subscriber > SubscriberPtr
Definition: TransportTypes.hh:53
boost::shared_ptr< Joint > JointPtr
Definition: PhysicsTypes.hh:117
virtual void Update()
Update the object.
Definition: Base.hh:171
virtual void RemoveChild(unsigned int _id)
Remove a child from this entity.
Information for use in an update event.
Definition: UpdateInfo.hh:30
A 3x3 matrix class.
Definition: Matrix3.hh:34
std::vector< JointPtr > Joint_V
Definition: PhysicsTypes.hh:213
std::vector< CollisionPtr > Collision_V
Definition: PhysicsTypes.hh:229
OpenAL Source.
Definition: OpenAL.hh:113
A quaternion class.
Definition: Quaternion.hh:42
std::shared_ptr< Battery > BatteryPtr
Definition: CommonTypes.hh:144
boost::shared_ptr< Inertial > InertialPtr
Definition: PhysicsTypes.hh:157
boost::shared_ptr< Connection > ConnectionPtr
Definition: CommonTypes.hh:153
Base class for all physics objects in Gazebo.
Definition: Entity.hh:58
boost::shared_ptr< Entity > EntityPtr
Definition: PhysicsTypes.hh:85
boost::shared_ptr< Model > ModelPtr
Definition: PhysicsTypes.hh:93
std::shared_ptr< OpenALSink > OpenALSinkPtr
Definition: UtilTypes.hh:44
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
virtual void Reset()
Reset the entity.
static const Vector3 Zero
math::Vector3(0, 0, 0)
Definition: Vector3.hh:42
std::vector< LinkPtr > Link_V
Definition: PhysicsTypes.hh:225
Store state information of a physics::Link object.
Definition: LinkState.hh:49
boost::shared_ptr< Collision > CollisionPtr
Definition: PhysicsTypes.hh:113
OpenAL Listener.
Definition: OpenAL.hh:92