(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Writing a Simple Subscriber for Bumper

Description: Writing a simple subscriber which reads bumper sensor data.

Tutorial Level: BEGINNER

Use the catkin_create_pkg script to create a new package called 'evarobot_bumper_subs' which depends on im_msgs, roscpp, and rospy:

> cd ~/catkin_ws/src
> catkin_create_pkg evarobot_bumper_subs im_msgs rospy roscpp

Create a src directory in the evarobot_bumper_subs package directory.

> mkdir -p ~/catkin_ws/src/evarobot_bumper_subs/src

Create the src/bumper_listener.cpp file within the evarobot_bumper_subs package.

> cd ~/catkin_ws/src/evarobot_bumper_subs/src
> gedit bumper_listener.cpp

And paste the following inside bumper_listener.cpp:

   1 #include "ros/ros.h"
   2 #include "im_msgs/Bumper.h"
   3 
   4 /**
   5  * This tutorial demonstrates simple receipt of bumper sensor data over the ROS system.
   6  */
   7 
   8 /**
   9  * Callback function executes when new topic data comes.
  10  * Task of the callback function is to print data to screen.
  11  */
  12 void chatterCallback(const im_msgs::Bumper::ConstPtr& msg)
  13 {
  14   ROS_INFO("New Data: %d", msg->header.seq);
  15   for(int i = 0; i < msg->state.size(); i++)
  16   {
  17     if(msg->state[i].bumper_state)
  18           ROS_INFO("Bumper[%d] True", i);
  19         else
  20           ROS_INFO("Bumper[%d] False", i);
  21   }
  22 }
  23 
  24 int main(int argc, char **argv)
  25 {
  26   /**
  27    * The ros::init() function needs to see argc and argv so that it can perform
  28    * any ROS arguments and name remapping that were provided at the command line.
  29    * For programmatic remappings you can use a different version of init() which takes
  30    * remappings directly, but for most command-line programs, passing argc and argv is
  31    * the easiest way to do it.  The third argument to init() is the name of the node.
  32    *
  33    * You must call one of the versions of ros::init() before using any other
  34    * part of the ROS system.
  35    */
  36   ros::init(argc, argv, "bumper_listener");
  37 
  38   /**
  39    * NodeHandle is the main access point to communications with the ROS system.
  40    * The first NodeHandle constructed will fully initialize this node, and the last
  41    * NodeHandle destructed will close down the node.
  42    */
  43   ros::NodeHandle n;
  44 
  45   /**
  46    * The subscribe() call is how you tell ROS that you want to receive messages
  47    * on a given topic.  This invokes a call to the ROS
  48    * master node, which keeps a registry of who is publishing and who
  49    * is subscribing.  Messages are passed to a callback function, here
  50    * called chatterCallback.  subscribe() returns a Subscriber object that you
  51    * must hold on to until you want to unsubscribe.  When all copies of the Subscriber
  52    * object go out of scope, this callback will automatically be unsubscribed from
  53    * this topic.
  54    *
  55    * The second parameter to the subscribe() function is the size of the message
  56    * queue.  If messages are arriving faster than they are being processed, this
  57    * is the number of messages that will be buffered up before beginning to throw
  58    * away the oldest ones.
  59    */
  60   ros::Subscriber sub = n.subscribe("bumper", 1000, chatterCallback);
  61 
  62   /**
  63    * ros::spin() will enter a loop, pumping callbacks.  With this version, all
  64    * callbacks will be called from within this thread (the main one).  ros::spin()
  65    * will exit when Ctrl-C is pressed, or the node is shutdown by the master.
  66    */
  67   ros::spin();
  68 
  69   return 0;
  70 }

> cd ..
> gedit CMakeLists.txt

The generated CMakeLists.txt should look like this

cmake_minimum_required(VERSION 2.8.3)
project(evarobot_bumper_subs)

find_package(catkin REQUIRED COMPONENTS
  im_msgs
  roscpp
  rospy
)

catkin_package()

include_directories(
  ${catkin_INCLUDE_DIRS}
)

add_executable(bumper_listener src/bumper_listener.cpp)
add_dependencies(bumper_listener im_msgs_generate_messages_cpp)
 target_link_libraries(bumper_listener
   ${catkin_LIBRARIES}
 )

Now run catkin_make

> cd ~/catkin_ws/
> catkin_make

To run bumper_listener,

> rosrun evarobot_bumper_subs bumper_listener

Wiki: evarobot_bumper/Tutorials/indigo/Writing a Simple Subscriber for Bumper (last edited 2015-09-17 07:02:35 by makcakoca)