gazebo_ros_mimic_joint.cpp
Go to the documentation of this file.
2 
4 {
5 
6 void MimicJoint::Load(gazebo::physics::ModelPtr _parent, sdf::ElementPtr _sdf )
7 {
8  ROS_INFO_NAMED("mimic_joint", "Starting Mimic Joint Plugin");
9 
10  this->model_ = _parent;
11 
12  if (!_sdf->HasElement("jointName"))
13  {
14  ROS_FATAL("mimic_joint missing <jointName>, cannot proceed");
15  return;
16  }
17  else
18  this->joint_name_ = _sdf->GetElement("jointName")->Get<std::string>();
19  ROS_INFO_NAMED("mimic_joint", "joint_name_: %s", this->joint_name_.c_str() );
20 
21  if (!_sdf->HasElement("mimicJoint"))
22  {
23  ROS_FATAL("mimic_joint missing <mimicJoint>, cannot proceed");
24  return;
25  }
26  else
27  this->mimic_joint_name_ = _sdf->GetElement("mimicJoint")->Get<std::string>();
28  ROS_INFO_NAMED("mimic_joint", "mimic_joint_name_: %s", this->mimic_joint_name_.c_str() );
29 
30  if (!_sdf->HasElement("offset"))
31  {
32  ROS_INFO("mimic_joint missing <offset>, set default to 0.0");
33  this->offset_ = 0.0;
34  }
35  else
36  this->offset_ = _sdf->GetElement("offset")->Get<double>();
37 
38  if (!_sdf->HasElement("multiplier"))
39  {
40  ROS_INFO("mimic_joint missing <multiplier>, set default to 1.0");
41  this->multiplier_ = 1.0;
42  }
43  else
44  this->multiplier_ = _sdf->GetElement("multiplier")->Get<double>();
45 
46 
47  // Get the name of the parent model
48  this->joint_ = model_->GetJoint(joint_name_);
49  this->mimic_joint_ = model_->GetJoint(mimic_joint_name_);
50 
51 #if GAZEBO_MAJOR_VERSION > 2
52  this->joint_->SetParam("fmax", 0, 100.0);
53 #else
54  this->joint_->SetMaxForce(0, 100.0);
55 #endif
56 
57  // Listen to the update event. This event is broadcast every
58  // simulation iteration.
59  this->update_connection_ = gazebo::event::Events::ConnectWorldUpdateBegin(
60  std::bind(&MimicJoint::OnUpdate, this));
61 }
62 
64 {
65 #if GAZEBO_MAJOR_VERSION >= 8
66  const double desired_angle = this->mimic_joint_->Position(0)*this->multiplier_ + this->offset_;
67 #else
68  const double desired_angle = this->mimic_joint_->GetAngle(0).Radian()*this->multiplier_ + this->offset_;
69 #endif
70 
71 #if GAZEBO_MAJOR_VERSION >= 9
72  this->joint_->SetPosition(0, desired_angle, true);
73 #else
74  this->joint_->SetPosition(0, desired_angle);
75 #endif
76 }
77 
78 
79 // Register this plugin with the simulator
80 GZ_REGISTER_MODEL_PLUGIN(MimicJoint)
81 } // namespace
cob_gazebo_ros_control::MimicJoint::model_
gazebo::physics::ModelPtr model_
A pointer to the Gazebo model.
Definition: gazebo_ros_mimic_joint.h:45
cob_gazebo_ros_control::MimicJoint::offset_
double offset_
offset
Definition: gazebo_ros_mimic_joint.h:66
cob_gazebo_ros_control
Definition: gazebo_ros_mimic_joint.h:10
cob_gazebo_ros_control::MimicJoint::joint_name_
std::string joint_name_
store bodyname
Definition: gazebo_ros_mimic_joint.h:51
cob_gazebo_ros_control::MimicJoint::mimic_joint_name_
std::string mimic_joint_name_
store name of mimic joint
Definition: gazebo_ros_mimic_joint.h:54
cob_gazebo_ros_control::MimicJoint::Load
void Load(gazebo::physics::ModelPtr _parent, sdf::ElementPtr _sdf)
Load the controller.
Definition: gazebo_ros_mimic_joint.cpp:6
ROS_INFO_NAMED
#define ROS_INFO_NAMED(name,...)
cob_gazebo_ros_control::MimicJoint
MimicJoint plugin This plugin allows to simulate mimic joints in gazebo.
Definition: gazebo_ros_mimic_joint.h:36
cob_gazebo_ros_control::MimicJoint::mimic_joint_
gazebo::physics::JointPtr mimic_joint_
A pointer to the Gazebo joint.
Definition: gazebo_ros_mimic_joint.h:60
cob_gazebo_ros_control::MimicJoint::joint_
gazebo::physics::JointPtr joint_
A pointer to the Gazebo joint.
Definition: gazebo_ros_mimic_joint.h:57
cob_gazebo_ros_control::MimicJoint::OnUpdate
void OnUpdate()
Update the controller.
Definition: gazebo_ros_mimic_joint.cpp:63
ROS_FATAL
#define ROS_FATAL(...)
cob_gazebo_ros_control::MimicJoint::update_connection_
gazebo::event::ConnectionPtr update_connection_
Definition: gazebo_ros_mimic_joint.h:48
ROS_INFO
#define ROS_INFO(...)
cob_gazebo_ros_control::MimicJoint::multiplier_
double multiplier_
multiplier
Definition: gazebo_ros_mimic_joint.h:63
gazebo_ros_mimic_joint.h


cob_gazebo_plugins
Author(s):
autogenerated on Fri Aug 2 2024 09:41:22