ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Subscribe to a Gazebo topic and publish in a ROS node

asked 2017-10-25 02:40:40 -0500

Hi. My idea is quite simple. Launching the turtlebot3 simulation, I would like to get /LaserScan information from gazebo and use it in a ROS node. Is there any example I can follow? I would like to do it in only one .pp file, not 2 separated (like the talker/listener tutorial).

Any help or reference would be really appreciated!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2017-10-30 08:03:02 -0500

updated 2017-10-30 08:25:00 -0500

If you are working with that turtlebot3 simulation, you don't have to subscribe a gazebo topic, since the simulation is configured already with ROS.

In order to have the turtlebot3 simulation running, I've cloned these packages:

git clone
git clone
git clone

Modified the 2nd line of the launch file turtlebot3_simulations/turtlebot3_gazebo/launch/turtlebot3_empty_world.launch, to use the burger model:

  <arg name="model" default="burger" doc="model type [burger, waffle]"/>
  <arg name="x_pos" default="0.0"/>
  <arg name="y_pos" default="0.0"/>
  <arg name="z_pos" default="0.0"/>

  <include file="$(find gazebo_ros)/launch/empty_world.launch">
    <arg name="world_name" value="$(find turtlebot3_gazebo)/models/"/>
    <arg name="paused" value="false"/>
    <arg name="use_sim_time" value="true"/>
    <arg name="gui" value="true"/>
    <arg name="headless" value="false"/>
    <arg name="debug" value="false"/>

  <param name="robot_description" command="$(find xacro)/ $(find turtlebot3_description)/urdf/turtlebot3_$(arg model).urdf.xacro" />

  <node name="spawn_urdf" pkg="gazebo_ros" type="spawn_model" args="-urdf -model turtlebot3_burger -x $(arg x_pos) -y $(arg y_pos) -z $(arg z_pos) -param robot_description" />

And launched like this:

roslaunch turtlebot_gazebo turtlebot3_empty_world.launch

To subscribe the laser scanner and publish something else in the same node, you can do the following:

Create a pkg with these dependencies

catkin_create_pkg my_pkg roscpp std_msgs sensor_msgs

Create your node (src/my_pkg_node.cpp)

#include <ros/ros.h>

#include <sensor_msgs/LaserScan.h>
#include <std_msgs/Int32.h>

ros::Publisher chatter_pub;

void clbk_laser(const sensor_msgs::LaserScan::ConstPtr& msg) {
    ROS_INFO("here we are!!!");

    std_msgs::Int32 something_msg; = 5;



int main(int argc, char **argv) {

    ros::init(argc, argv, "my_node");

    ros::NodeHandle n;

    ros::Subscriber sub_laser = n.subscribe("/scan", 1, clbk_laser);

    chatter_pub = n.advertise<std_msgs::Int32>("something", 1);



Uncomment these lines in your CMakeLists.txt file:

add_executable(${PROJECT_NAME}_node src/my_pkg_node.cpp)


I have created a video showing it step-by-step. I think it's easier to reproduce it.

Hope it can help you!

edit flag offensive delete link more


I have a camera and a laser sensor in the gazebo environment (in the .world SDF file not in the robot model). How to proceed in such case to publish those external sensor data in rostopic.

ArpanHalder gravatar image ArpanHalder  ( 2018-03-21 11:04:04 -0500 )edit

Hi @ArpanHalder, I'm not familiar with SDF but I believe, since that's the native world description of gazebo, you should do that using a gazebo plugin. Almost sure it's not possible to work with it in ROS without a plugin as a bridge.

marcoarruda gravatar image marcoarruda  ( 2018-04-27 07:59:14 -0500 )edit

answered 2017-10-25 08:57:32 -0500

I think the best place to go is on the how to read gazebo topics tutorial, and then how to publish then to ROS.

But instead of follow only the two tutorials mentioned (tutorials 5 and 6), I would go from tutorial 1 to 6.

edit flag offensive delete link more

Question Tools



Asked: 2017-10-25 02:40:40 -0500

Seen: 8,823 times

Last updated: Oct 30 '17