gazebo_ros_kobuki_loads.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, Yujin Robot.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Yujin Robot nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
37 
38 namespace gazebo
39 {
40 
41 /*
42  * Prepare receiving motor power commands
43  */
45  motors_enabled_ = true;
46 }
47 
48 /*
49  * Prepare joint state publishing
50  */
52 {
53  if (sdf_->HasElement("left_wheel_joint_name"))
54  {
55  left_wheel_joint_name_ = sdf_->GetElement("left_wheel_joint_name")->Get<std::string>();
56  }
57  else
58  {
59  ROS_ERROR_STREAM("Couldn't find left wheel joint in the model description!"
60  << " Did you specify the correct joint name?" << " [" << node_name_ <<"]");
61  return false;
62  }
63  if (sdf_->HasElement("right_wheel_joint_name"))
64  {
65  right_wheel_joint_name_ = sdf_->GetElement("right_wheel_joint_name")->Get<std::string>();
66  }
67  else
68  {
69  ROS_ERROR_STREAM("Couldn't find right wheel joint in the model description!"
70  << " Did you specify the correct joint name?" << " [" << node_name_ <<"]");
71  return false;
72  }
75  if (!joints_[LEFT] || !joints_[RIGHT])
76  {
77  ROS_ERROR_STREAM("Couldn't find specified wheel joints in the model! [" << node_name_ <<"]");
78  return false;
79  }
80  joint_state_.header.frame_id = "Joint States";
81  joint_state_.name.push_back(left_wheel_joint_name_);
82  joint_state_.position.push_back(0);
83  joint_state_.velocity.push_back(0);
84  joint_state_.effort.push_back(0);
85  joint_state_.name.push_back(right_wheel_joint_name_);
86  joint_state_.position.push_back(0);
87  joint_state_.velocity.push_back(0);
88  joint_state_.effort.push_back(0);
89 
90  return true;
91 }
92 
93 /*
94  * Prepare publishing odometry data
95  */
97 {
98  if (sdf_->HasElement("publish_tf"))
99  {
100  publish_tf_ = sdf_->GetElement("publish_tf")->Get<bool>();
101  if (publish_tf_)
102  {
103  ROS_INFO_STREAM("Will publish tf." << " [" << node_name_ <<"]");
104  }
105  else
106  {
107  ROS_INFO_STREAM("Won't publish tf." << " [" << node_name_ <<"]");
108  }
109  }
110  else
111  {
112  publish_tf_ = false;
113  ROS_INFO_STREAM("Couldn't find the 'publish tf' parameter in the model description."
114  << " Won't publish tf." << " [" << node_name_ <<"]");
115  }
116 }
117 
119 {
120  if (sdf_->HasElement("wheel_separation"))
121  {
122  wheel_sep_ = sdf_->GetElement("wheel_separation")->Get<double>();
123  }
124  else
125  {
126  ROS_ERROR_STREAM("Couldn't find the wheel separation parameter in the model description!"
127  << " Did you specify it?" << " [" << node_name_ <<"]");
128  return false;
129  }
130  if (sdf_->HasElement("wheel_diameter"))
131  {
132  wheel_diam_ = sdf_->GetElement("wheel_diameter")->Get<double>();
133  }
134  else
135  {
136  ROS_ERROR_STREAM("Couldn't find the wheel diameter parameter in the model description!"
137  << " Did you specify it?" << " [" << node_name_ <<"]");
138  return false;
139  }
140  if (sdf_->HasElement("torque"))
141  {
142  torque_ = sdf_->GetElement("torque")->Get<double>();
143  }
144  else
145  {
146  ROS_ERROR_STREAM("Couldn't find the torque parameter in the model description!"
147  << " Did you specify it?" << " [" << node_name_ <<"]");
148  return false;
149  }
150  return true;
151 }
152 
154 {
155  odom_pose_[0] = 0.0;
156  odom_pose_[1] = 0.0;
157  odom_pose_[2] = 0.0;
158 }
159 
160 /*
161  * Prepare receiving velocity commands
162  */
164 {
165  if (sdf_->HasElement("velocity_command_timeout"))
166  {
167  cmd_vel_timeout_ = sdf_->GetElement("velocity_command_timeout")->Get<double>();
168  }
169  else
170  {
171  ROS_ERROR_STREAM("Couldn't find the wheel separation parameter in the model description!"
172  << " Did you specify it?" << " [" << node_name_ <<"]");
173  return false;
174  }
175  #if GAZEBO_MAJOR_VERSION >= 9
176  last_cmd_vel_time_ = world_->SimTime();
177  #else
178  last_cmd_vel_time_ = world_->GetSimTime();
179  #endif
180  return true;
181 }
182 
184 {
185  /*
186  * Prepare cliff sensors
187  */
188  std::string cliff_sensor_left_name, cliff_sensor_center_name, cliff_sensor_right_name;
189  if (sdf_->HasElement("cliff_sensor_left_name"))
190  {
191  cliff_sensor_left_name = sdf_->GetElement("cliff_sensor_left_name")->Get<std::string>();
192  }
193  else
194  {
195  ROS_ERROR_STREAM("Couldn't find the name of left cliff sensor in the model description!"
196  << " Did you specify it?" << " [" << node_name_ <<"]");
197  return false;
198  }
199  if (sdf_->HasElement("cliff_sensor_center_name"))
200  {
201  cliff_sensor_center_name = sdf_->GetElement("cliff_sensor_center_name")->Get<std::string>();
202  }
203  else
204  {
205  ROS_ERROR_STREAM("Couldn't find the name of frontal cliff sensor in the model description!"
206  << " Did you specify it?" << " [" << node_name_ <<"]");
207  return false;
208  }
209  if (sdf_->HasElement("cliff_sensor_right_name"))
210  {
211  cliff_sensor_right_name = sdf_->GetElement("cliff_sensor_right_name")->Get<std::string>();
212  }
213  else
214  {
215  ROS_ERROR_STREAM("Couldn't find the name of right cliff sensor in the model description!"
216  << " Did you specify it?" << " [" << node_name_ <<"]");
217  return false;
218  }
219  cliff_sensor_left_ = std::dynamic_pointer_cast<sensors::RaySensor>(
220  sensors::SensorManager::Instance()->GetSensor(cliff_sensor_left_name));
221  cliff_sensor_center_ = std::dynamic_pointer_cast<sensors::RaySensor>(
222  sensors::SensorManager::Instance()->GetSensor(cliff_sensor_center_name));
223  cliff_sensor_right_ = std::dynamic_pointer_cast<sensors::RaySensor>(
224  sensors::SensorManager::Instance()->GetSensor(cliff_sensor_right_name));
225  if (!cliff_sensor_left_)
226  {
227  ROS_ERROR_STREAM("Couldn't find the left cliff sensor in the model! [" << node_name_ <<"]");
228  return false;
229  }
230  if (!cliff_sensor_center_)
231  {
232  ROS_ERROR_STREAM("Couldn't find the center cliff sensor in the model! [" << node_name_ <<"]");
233  return false;
234  }
235  if (!cliff_sensor_right_)
236  {
237  ROS_ERROR_STREAM("Couldn't find the right cliff sensor in the model! [" << node_name_ <<"]");
238  return false;
239  }
240  if (sdf_->HasElement("cliff_detection_threshold"))
241  {
242  cliff_detection_threshold_ = sdf_->GetElement("cliff_detection_threshold")->Get<double>();
243  }
244  else
245  {
246  ROS_ERROR_STREAM("Couldn't find the cliff detection threshold parameter in the model description!"
247  << " Did you specify it?" << " [" << node_name_ <<"]");
248  return false;
249  }
250  cliff_sensor_left_->SetActive(true);
251  cliff_sensor_center_->SetActive(true);
252  cliff_sensor_right_->SetActive(true);
253 
254  return true;
255 }
256 
257 
258 /*
259  * Prepare bumper
260  */
262 {
263  std::string bumper_name;
264  if (sdf_->HasElement("bumper_name"))
265  {
266  bumper_name = sdf_->GetElement("bumper_name")->Get<std::string>();
267  }
268  else
269  {
270  ROS_ERROR_STREAM("Couldn't find the name of bumper sensor in the model description!"
271  << " Did you specify it?" << " [" << node_name_ <<"]");
272  return false;
273  }
274  bumper_ = std::dynamic_pointer_cast<sensors::ContactSensor>(
275  sensors::SensorManager::Instance()->GetSensor(bumper_name));
276  if (!bumper_)
277  {
278  ROS_ERROR_STREAM("Couldn't find the bumpers in the model! [" << node_name_ <<"]");
279  return false;
280  }
281  bumper_->SetActive(true);
282  return true;
283 }
284 
285 /*
286  * Prepare IMU
287  */
289 {
290  std::string imu_name;
291  if (sdf_->HasElement("imu_name"))
292  {
293  imu_name = sdf_->GetElement("imu_name")->Get<std::string>();
294  }
295  else
296  {
297  ROS_ERROR_STREAM("Couldn't find the name of IMU sensor in the model description!"
298  << " Did you specify it?" << " [" << node_name_ <<"]");
299  return false;
300  }
301  #if GAZEBO_MAJOR_VERSION >= 9
302  imu_ = std::dynamic_pointer_cast<sensors::ImuSensor>(
303  sensors::get_sensor(world_->Name()+"::"+node_name_+"::base_footprint::"+imu_name));
304  #else
305  imu_ = std::dynamic_pointer_cast<sensors::ImuSensor>(
306  sensors::get_sensor(world_->GetName()+"::"+node_name_+"::base_footprint::"+imu_name));
307 
308  #endif
309  if (!imu_)
310  {
311  ROS_ERROR_STREAM("Couldn't find the IMU in the model! [" << node_name_ <<"]");
312  return false;
313  }
314  imu_->SetActive(true);
315  return true;
316 }
317 
318 void GazeboRosKobuki::setupRosApi(std::string& model_name)
319 {
320  std::string base_prefix;
321  gazebo_ros_->node()->param("base_prefix", base_prefix, std::string("mobile_base"));
322 
323  // Public topics
324 
325  // joint_states
326  std::string joint_states_topic = "joint_states";
327  joint_state_pub_ = gazebo_ros_->node()->advertise<sensor_msgs::JointState>(joint_states_topic, 1);
328  ROS_INFO("%s: Advertise joint_states[%s]!", gazebo_ros_->info(), joint_states_topic.c_str());
329 
330  // odom
331  std::string odom_topic = "odom";
332  odom_pub_ = gazebo_ros_->node()->advertise<nav_msgs::Odometry>(odom_topic, 1);
333  ROS_INFO("%s: Advertise Odometry[%s]!", gazebo_ros_->info(), odom_topic.c_str());
334 
335 
336  // Private Topics
337  // motor power
338  std::string motor_power_topic = base_prefix + "/commands/motor_power";
339  motor_power_sub_ = gazebo_ros_->node()->subscribe(motor_power_topic, 10, &GazeboRosKobuki::motorPowerCB, this);
340  ROS_INFO("%s: Try to subscribe to %s!", gazebo_ros_->info(), motor_power_topic.c_str());
341 
342 
343  std::string odom_reset_topic = base_prefix + "/commands/reset_odometry";
344  odom_reset_sub_ = gazebo_ros_->node()->subscribe(odom_reset_topic, 10, &GazeboRosKobuki::resetOdomCB, this);
345  ROS_INFO("%s: Try to subscribe to %s!", gazebo_ros_->info(), odom_reset_topic.c_str());
346 
347  // cmd_vel
348  std::string cmd_vel_topic = base_prefix + "/commands/velocity";
349  cmd_vel_sub_ = gazebo_ros_->node()->subscribe(cmd_vel_topic, 100, &GazeboRosKobuki::cmdVelCB, this);
350  ROS_INFO("%s: Try to subscribe to %s!", gazebo_ros_->info(), cmd_vel_topic.c_str());
351 
352  // cliff
353  std::string cliff_topic = base_prefix + "/events/cliff";
354  cliff_event_pub_ = gazebo_ros_->node()->advertise<kobuki_msgs::CliffEvent>(cliff_topic, 1);
355  ROS_INFO("%s: Advertise Cliff[%s]!", gazebo_ros_->info(), cliff_topic.c_str());
356 
357  // bumper
358  std::string bumper_topic = base_prefix + "/events/bumper";
359  bumper_event_pub_ = gazebo_ros_->node()->advertise<kobuki_msgs::BumperEvent>(bumper_topic, 1);
360  ROS_INFO("%s: Advertise Bumper[%s]!", gazebo_ros_->info(), bumper_topic.c_str());
361 
362  // IMU
363  std::string imu_topic = base_prefix + "/sensors/imu_data";
364  imu_pub_ = gazebo_ros_->node()->advertise<sensor_msgs::Imu>(imu_topic, 1);
365  ROS_INFO("%s: Advertise IMU[%s]!", gazebo_ros_->info(), imu_topic.c_str());
366 }
367 }
double odom_pose_[3]
Vector for pose.
double torque_
Max. torque applied to the wheels.
GazeboRosPtr gazebo_ros_
pointer to the gazebo ros node
float cliff_detection_threshold_
measured distance in meter for detecting a cliff
double wheel_sep_
Separation between the wheels.
ros::Publisher imu_pub_
ROS publisher for IMU data.
sensors::RaySensorPtr cliff_sensor_center_
Pointer to frontal cliff sensor.
ros::Subscriber cmd_vel_sub_
ROS subscriber for velocity commands.
sensors::ContactSensorPtr bumper_
Pointer to bumper sensor simulating Kobuki&#39;s left, centre and right bumper sensors.
physics::JointPtr joints_[2]
Pointers to Gazebo&#39;s joints.
sensors::ImuSensorPtr imu_
Pointer to IMU sensor model.
ros::Subscriber odom_reset_sub_
ROS subscriber for reseting the odometry data.
common::Time last_cmd_vel_time_
Simulation time of the last velocity command (used for time out)
ros::Publisher joint_state_pub_
ROS publisher for joint state messages.
double wheel_diam_
Diameter of the wheels.
void setupRosApi(std::string &model_name)
bool motors_enabled_
Flag indicating if the motors are turned on or not.
ros::Publisher bumper_event_pub_
ROS publisher for bumper events.
#define ROS_INFO(...)
physics::ModelPtr model_
pointer to the model
void motorPowerCB(const kobuki_msgs::MotorPowerPtr &msg)
Callback for incoming velocity commands.
ros::Publisher odom_pub_
ROS publisher for odometry messages.
std::string right_wheel_joint_name_
Right wheel&#39;s joint name.
void resetOdomCB(const std_msgs::EmptyConstPtr &msg)
Callback for resetting the odometry data.
double cmd_vel_timeout_
Time out for velocity commands in seconds.
#define ROS_INFO_STREAM(args)
std::string left_wheel_joint_name_
Left wheel&#39;s joint name.
void cmdVelCB(const geometry_msgs::TwistConstPtr &msg)
Callback for incoming velocity commands.
#define ROS_ERROR_STREAM(args)
std::string node_name_
node name
sensors::RaySensorPtr cliff_sensor_left_
Pointer to left cliff sensor.
physics::WorldPtr world_
pointer to simulated world
ros::Publisher cliff_event_pub_
ROS publisher for cliff detection events.
bool publish_tf_
Flag for (not) publish tf transform for odom -> robot.
sensor_msgs::JointState joint_state_
ROS message for joint sates.
sensors::RaySensorPtr cliff_sensor_right_
Pointer to left right sensor.
ros::Subscriber motor_power_sub_
ROS subscriber for motor power commands.


kobuki_gazebo_plugins
Author(s): Marcus Liebhardt
autogenerated on Mon Jun 10 2019 13:52:55