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 https://github.com/ROBOTIS-GIT/turtlebot3_simulations.git
git clone https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git
git clone https://github.com/ROBOTIS-GIT/turtlebot3.git
Modified the 2nd line of the launch file turtlebot3_simulations/turtlebot3_gazebo/launch/turtlebot3_empty_world.launch, to use the burger model:
<launch>
<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/empty.world"/>
<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"/>
</include>
<param name="robot_description" command="$(find xacro)/xacro.py $(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" />
</launch>
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;
something_msg.data = 5;
chatter_pub.publish(something_msg);
}
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);
ros::spin();
}
Uncomment these lines in your CMakeLists.txt file:
add_executable(${PROJECT_NAME}_node src/my_pkg_node.cpp)
target_link_libraries(${PROJECT_NAME}_node
${catkin_LIBRARIES}
I have created a video showing it step-by-step. I think it's easier to reproduce it.
Hope it can help you!