gazebo_controller_interface.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 
23 
24 #include "ConnectGazeboToRosTopic.pb.h"
25 #include "ConnectRosToGazeboTopic.pb.h"
26 
27 namespace gazebo {
28 
30 }
31 
32 void GazeboControllerInterface::Load(physics::ModelPtr _model,
33  sdf::ElementPtr _sdf) {
34  if (kPrintOnPluginLoad) {
35  gzdbg << __FUNCTION__ << "() called." << std::endl;
36  }
37 
38  // Store the pointer to the model.
39  model_ = _model;
40 
41  world_ = model_->GetWorld();
42 
43  namespace_.clear();
44 
45  //==============================================//
46  //========== READ IN PARAMS FROM SDF ===========//
47  //==============================================//
48 
49  if (_sdf->HasElement("robotNamespace")) {
50  namespace_ = _sdf->GetElement("robotNamespace")->Get<std::string>();
51  } else {
52  gzerr << "[gazebo_motor_model] Please specify a robotNamespace.\n";
53  }
54 
55  node_handle_ = gazebo::transport::NodePtr(new transport::Node());
56 
57  // Initialise with default namespace (typically /gazebo/default/)
58  node_handle_->Init();
59 
60  getSdfParam<std::string>(_sdf, "commandMotorSpeedSubTopic",
63  getSdfParam<std::string>(_sdf, "motorSpeedCommandPubTopic",
66 
67  // Listen to the update event. This event is broadcast every
68  // simulation iteration.
69  updateConnection_ = event::Events::ConnectWorldUpdateBegin(
70  boost::bind(&GazeboControllerInterface::OnUpdate, this, _1));
71 }
72 
73 void GazeboControllerInterface::OnUpdate(const common::UpdateInfo& /*_info*/) {
74  if (kPrintOnUpdates) {
75  gzdbg << __FUNCTION__ << "() called." << std::endl;
76  }
77 
81  }
82 
84  return;
85  }
86 
87  common::Time now = world_->SimTime();
88 
89  gz_sensor_msgs::Actuators turning_velocities_msg;
90 
91  for (int i = 0; i < input_reference_.size(); i++) {
92  turning_velocities_msg.add_angular_velocities((double)input_reference_[i]);
93  }
94 
95  turning_velocities_msg.mutable_header()->mutable_stamp()->set_sec(now.sec);
96  turning_velocities_msg.mutable_header()->mutable_stamp()->set_nsec(now.nsec);
97 
98  // Frame ID is not used for this particular message
99  turning_velocities_msg.mutable_header()->set_frame_id("");
100 
101  motor_velocity_reference_pub_->Publish(turning_velocities_msg);
102 }
103 
105  gzdbg << __FUNCTION__ << "() called." << std::endl;
106 
107  // Create temporary "ConnectGazeboToRosTopic" publisher and message
108  gazebo::transport::PublisherPtr gz_connect_gazebo_to_ros_topic_pub =
109  node_handle_->Advertise<gz_std_msgs::ConnectGazeboToRosTopic>(
110  "~/" + kConnectGazeboToRosSubtopic, 1);
111 
112  // Create temporary "ConnectRosToGazeboTopic" publisher and message
113  gazebo::transport::PublisherPtr gz_connect_ros_to_gazebo_topic_pub =
114  node_handle_->Advertise<gz_std_msgs::ConnectRosToGazeboTopic>(
115  "~/" + kConnectRosToGazeboSubtopic, 1);
116 
117  // ============================================================ //
118  // === ACTUATORS (MOTOR VELOCITY) MSG SETUP (GAZEBO -> ROS) === //
119  // ============================================================ //
120 
121  // TODO This topic is missing the "~" and is in a completely different
122  // namespace, fix?
123 
124  gzdbg << "GazeboControllerInterface creating Gazebo publisher on \""
126  << std::endl;
128  node_handle_->Advertise<gz_sensor_msgs::Actuators>(
130 
131  // Connect to ROS
132  gz_std_msgs::ConnectGazeboToRosTopic connect_gazebo_to_ros_topic_msg;
133  connect_gazebo_to_ros_topic_msg.set_gazebo_topic(
134  namespace_ + "/" + motor_velocity_reference_pub_topic_);
135  connect_gazebo_to_ros_topic_msg.set_ros_topic(
136  namespace_ + "/" + motor_velocity_reference_pub_topic_);
137  connect_gazebo_to_ros_topic_msg.set_msgtype(
138  gz_std_msgs::ConnectGazeboToRosTopic::ACTUATORS);
139  gz_connect_gazebo_to_ros_topic_pub->Publish(connect_gazebo_to_ros_topic_msg,
140  true);
141 
142  // ================================================ //
143  // ===== MOTOR SPEED MSG SETUP (ROS -> GAZEBO) ==== //
144  // ================================================ //
145  gzdbg << "Subscribing to Gazebo topic \""
146  << "~/" + namespace_ + "/" + command_motor_speed_sub_topic_ << "\"."
147  << std::endl;
148  cmd_motor_sub_ = node_handle_->Subscribe(
151 
152  // Connect to ROS
153  gz_std_msgs::ConnectRosToGazeboTopic connect_ros_to_gazebo_topic_msg;
154  connect_ros_to_gazebo_topic_msg.set_ros_topic(namespace_ + "/" +
156  // connect_ros_to_gazebo_topic_msg.set_gazebo_namespace(namespace_);
157  connect_ros_to_gazebo_topic_msg.set_gazebo_topic(
159  connect_ros_to_gazebo_topic_msg.set_msgtype(
160  gz_std_msgs::ConnectRosToGazeboTopic::ACTUATORS);
161  gz_connect_ros_to_gazebo_topic_pub->Publish(connect_ros_to_gazebo_topic_msg,
162  true);
163 
164  gzdbg << __FUNCTION__ << "() called." << std::endl;
165 }
166 
168  GzActuatorsMsgPtr& actuators_msg) {
169  if (kPrintOnMsgCallback) {
170  gzdbg << __FUNCTION__ << "() called." << std::endl;
171  }
172 
173  input_reference_.resize(actuators_msg->angular_velocities_size());
174  for (int i = 0; i < actuators_msg->angular_velocities_size(); ++i) {
175  input_reference_[i] = actuators_msg->angular_velocities(i);
176  }
177 
178  // We have received a motor command reference (it may not be the first, but
179  // this
180  // does not matter)
182 }
183 
185 }
static const std::string kConnectRosToGazeboSubtopic
Definition: common.h:57
void CommandMotorCallback(GzActuatorsMsgPtr &actuators_msg)
gazebo::transport::SubscriberPtr cmd_motor_sub_
static const bool kPrintOnPluginLoad
Definition: common.h:41
event::ConnectionPtr updateConnection_
Pointer to the update event connection.
static const bool kPrintOnUpdates
Definition: common.h:42
void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
bool pubs_and_subs_created_
Flag that is set to true once CreatePubsAndSubs() is called, used to prevent CreatePubsAndSubs() from...
gazebo::transport::PublisherPtr motor_velocity_reference_pub_
GZ_REGISTER_MODEL_PLUGIN(GazeboRosP3D)
void CreatePubsAndSubs()
Creates all required publishers and subscribers, incl. routing of messages to/from ROS if required...
static const std::string kConnectGazeboToRosSubtopic
Definition: common.h:56
void OnUpdate(const common::UpdateInfo &)
bool received_first_reference_
Gets set to true the first time a motor command is received.
static const bool kPrintOnMsgCallback
Definition: common.h:43


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