gazebo_multirotor_base_plugin.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Fadri Furrer, ASL, ETH Zurich, Switzerland
3  * Copyright 2015 Michael Burri, ASL, ETH Zurich, Switzerland
4  * Copyright 2015 Mina Kamel, ASL, ETH Zurich, Switzerland
5  * Copyright 2015 Janosch Nikolic, ASL, ETH Zurich, Switzerland
6  * Copyright 2015 Markus Achtelik, ASL, ETH Zurich, Switzerland
7  * Copyright 2016 Geoffrey Hunter <gbmhunter@gmail.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 // MODULE HEADER INCLUDE
24 
25 // STANDARD LIB INCLUDES
26 #include <ctime>
27 
28 #include "ConnectGazeboToRosTopic.pb.h"
29 
30 namespace gazebo {
31 
33 
34 }
35 
36 void GazeboMultirotorBasePlugin::Load(physics::ModelPtr _model,
37  sdf::ElementPtr _sdf) {
38  if (kPrintOnPluginLoad) {
39  gzdbg << __FUNCTION__ << "() called." << std::endl;
40  }
41 
42  model_ = _model;
43  world_ = model_->GetWorld();
44  namespace_.clear();
45 
46  getSdfParam<std::string>(_sdf, "robotNamespace", namespace_, namespace_,
47  true);
48  getSdfParam<std::string>(_sdf, "linkName", link_name_, link_name_, true);
49  getSdfParam<std::string>(_sdf, "motorPubTopic", actuators_pub_topic_,
51  getSdfParam<double>(_sdf, "rotorVelocitySlowdownSim",
54 
55  node_handle_ = gazebo::transport::NodePtr(new transport::Node());
56 
57  // Initialise with default namespace (typically /gazebo/default/)
58  node_handle_->Init();
59 
61 
62  link_ = model_->GetLink(link_name_);
63  if (link_ == NULL)
64  gzthrow("[gazebo_multirotor_base_plugin] Couldn't find specified link \""
65  << link_name_ << "\".");
66 
67  // Listen to the update event. This event is broadcast every
68  // simulation iteration.
69  update_connection_ = event::Events::ConnectWorldUpdateBegin(
70  boost::bind(&GazeboMultirotorBasePlugin::OnUpdate, this, _1));
71 
72  child_links_ = link_->GetChildJointsLinks();
73  for (unsigned int i = 0; i < child_links_.size(); i++) {
74  std::string link_name = child_links_[i]->GetScopedName();
75 
76  // Check if link contains rotor_ in its name.
77  int pos = link_name.find("rotor_");
78  if (pos != link_name.npos) {
79  std::string motor_number_str = link_name.substr(pos + 6);
80  unsigned int motor_number = std::stoi(motor_number_str);
81  std::string joint_name = child_links_[i]->GetName() + "_joint";
82  physics::JointPtr joint = this->model_->GetJoint(joint_name);
83  motor_joints_.insert(MotorNumberToJointPair(motor_number, joint));
84  }
85  }
86 }
87 
88 // This gets called by the world update start event.
89 void GazeboMultirotorBasePlugin::OnUpdate(const common::UpdateInfo& _info) {
90  if (kPrintOnUpdates) {
91  gzdbg << __FUNCTION__ << "() called." << std::endl;
92  }
93 
97  }
98 
99  // Get the current simulation time.
100  common::Time now = world_->SimTime();
101 
102  actuators_msg_.mutable_header()->mutable_stamp()->set_sec(now.sec);
103  actuators_msg_.mutable_header()->mutable_stamp()->set_nsec(now.nsec);
104  actuators_msg_.mutable_header()->set_frame_id(frame_id_);
105 
106  joint_state_msg_.mutable_header()->mutable_stamp()->set_sec(now.sec);
107  joint_state_msg_.mutable_header()->mutable_stamp()->set_nsec(now.nsec);
108  joint_state_msg_.mutable_header()->set_frame_id(frame_id_);
109 
110  actuators_msg_.clear_angular_velocities();
111 
112  joint_state_msg_.clear_name();
113  joint_state_msg_.clear_position();
114 
115  MotorNumberToJointMap::iterator m;
116  for (m = motor_joints_.begin(); m != motor_joints_.end(); ++m) {
117  double motor_rot_vel =
118  m->second->GetVelocity(0) * rotor_velocity_slowdown_sim_;
119 
120  actuators_msg_.add_angular_velocities(motor_rot_vel);
121 
122  joint_state_msg_.add_name(m->second->GetName());
123  joint_state_msg_.add_position(m->second->Position(0));
124  }
125 
127  motor_pub_->Publish(actuators_msg_);
128 }
129 
131  // Create temporary "ConnectGazeboToRosTopic" publisher and message
132  gazebo::transport::PublisherPtr connect_gazebo_to_ros_topic_pub =
133  node_handle_->Advertise<gz_std_msgs::ConnectGazeboToRosTopic>(
134  "~/" + kConnectGazeboToRosSubtopic, 1);
135 
136  gz_std_msgs::ConnectGazeboToRosTopic connect_gazebo_to_ros_topic_msg;
137 
138  // ============================================ //
139  // =========== ACTUATORS MSG SETUP ============ //
140  // ============================================ //
141  motor_pub_ = node_handle_->Advertise<gz_sensor_msgs::Actuators>(
142  "~/" + namespace_ + "/" + actuators_pub_topic_, 10);
143 
144  // connect_gazebo_to_ros_topic_msg.set_gazebo_namespace(namespace_);
145  connect_gazebo_to_ros_topic_msg.set_gazebo_topic("~/" + model_->GetName() +
146  "/" + actuators_pub_topic_);
147  connect_gazebo_to_ros_topic_msg.set_ros_topic(namespace_ + "/" +
148  actuators_pub_topic_);
149  connect_gazebo_to_ros_topic_msg.set_msgtype(
150  gz_std_msgs::ConnectGazeboToRosTopic::ACTUATORS);
151  connect_gazebo_to_ros_topic_pub->Publish(connect_gazebo_to_ros_topic_msg,
152  true);
153 
154  // ============================================ //
155  // ========== JOINT STATE MSG SETUP =========== //
156  // ============================================ //
157  joint_state_pub_ = node_handle_->Advertise<gz_sensor_msgs::JointState>(
158  "~/" + namespace_ + "/" + joint_state_pub_topic_, 1);
159 
160  // connect_gazebo_to_ros_topic_msg.set_gazebo_namespace(namespace_);
161  connect_gazebo_to_ros_topic_msg.set_gazebo_topic("~/" + namespace_ + "/" +
162  joint_state_pub_topic_);
163  connect_gazebo_to_ros_topic_msg.set_ros_topic(namespace_ + "/" +
164  joint_state_pub_topic_);
165  connect_gazebo_to_ros_topic_msg.set_msgtype(
166  gz_std_msgs::ConnectGazeboToRosTopic::JOINT_STATE);
167  connect_gazebo_to_ros_topic_pub->Publish(connect_gazebo_to_ros_topic_msg,
168  true);
169 }
170 
172 
173 } // namespace gazebo
void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
Load the plugin.
ssize_t pos
This plugin publishes the motor speeds of your multirotor model.
event::ConnectionPtr update_connection_
Pointer to the update event connection.
void CreatePubsAndSubs()
Creates all required publishers and subscribers, incl. routing of messages to/from ROS if required...
gazebo::transport::PublisherPtr joint_state_pub_
bool pubs_and_subs_created_
Flag that is set to true once CreatePubsAndSubs() is called, used to prevent CreatePubsAndSubs() from...
std::pair< const unsigned int, const physics::JointPtr > MotorNumberToJointPair
static const bool kPrintOnPluginLoad
Definition: common.h:41
gazebo::transport::PublisherPtr motor_pub_
static const bool kPrintOnUpdates
Definition: common.h:42
GZ_REGISTER_MODEL_PLUGIN(GazeboRosP3D)
static const std::string kConnectGazeboToRosSubtopic
Definition: common.h:56
void OnUpdate(const common::UpdateInfo &)
Called when the world is updated.


rotors_gazebo_plugins
Author(s): Fadri Furrer, Michael Burri, Mina Kamel, Janosch Nikolic, Markus Achtelik
autogenerated on Mon Feb 28 2022 23:39:03