Creating Nodes

In this exercise, you will create a simple ROS node into your new fake_sonar_driver package.

  1. In the fake_sonar_driver package folder, edit the CMakeLists.txt file using gedit. Browse through the example rules, and add an executable(add_executable), node named fake_sonar_driver_node, source file named fake_sonar_driver.cpp. Also within the CMakeLists.txt, make sure your new fake_sonar_driver_node gets linked (‘target_link_libraries’) to the catkin libraries.

    For reference, take a look at Understanding Nodes.

    add_compile_options(-std=c++11)
    add_executable(fake_sonar_driver_node src/sonar_driver_node.cpp)
    target_link_libraries(fake_sonar_driver_node ${catkin_LIBRARIES})
    

    These lines can be placed anywhere in CMakeLists.txt, but a good approach is:

    • Uncomment existing template examples for add_compile_options near the top (just below project())
    • Uncomment and edit existing template examples for add_executable and target_link_libraries near the bottom
    • This helps make sure these rules are defined in the correct order, and makes it easy to remember the proper syntax.

    Note

    You’re also allowed to spread most of the CMakeLists rules across multiple lines, as shown in the target_link_libraries template code.

  2. In the package folder, create the file src/fake_sonar_driver_node.cpp and open it in a text editor.

  3. Add the ros header (include ros.h).

    /**
    **  Simple ROS Node
    **/
    #include <ros/ros.h>
    
  4. Add a main function (typical in c++ programs).

    /**
    **  Simple ROS Node
    **/
    #include <ros/ros.h>
    
    int main(int argc, char* argv[])
    {
    
    }
    
  5. Initialize your ROS node (within the main).

    /**
    **  Simple ROS Node
    **/
    #include <ros/ros.h>
    
    int main(int argc, char* argv[])
    {
      // This must be called before anything else ROS-related
      ros::init(argc, argv, "fake_sonar_driver_node");
    }
    
  6. Create a ROS node handle.

    /**
    **  Simple ROS Node
    **/
    #include <ros/ros.h>
    
    int main(int argc, char* argv[])
    {
      // This must be called before anything else ROS-related
      ros::init(argc, argv, "fake_sonar_driver_node");
    
      // Create a ROS node handle
      ros::NodeHandle nh;
    }
    
  7. Print a “Hello World” message using ROS print tools.

    /**
    **  Simple ROS Node
    **/
    #include <ros/ros.h>
    
    int main(int argc, char* argv[])
    {
      // This must be called before anything else ROS-related
      ros::init(argc, argv, "fake_sonar_driver_node");
    
      // Create a ROS node handle
      ros::NodeHandle nh;
    
      ROS_INFO("Hello, World!");
    }
    
  8. Do not exit the program automatically - keep the node alive.

    /**
    **  Simple ROS Node
    **/
    #include <ros/ros.h>
    
    int main(int argc, char* argv[])
    {
      // This must be called before anything else ROS-related
      ros::init(argc, argv, "fake_sonar_driver_node");
    
      // Create a ROS node handle
      ros::NodeHandle nh;
    
      ROS_INFO("Hello, World!");
    
      // Don't exit the program.
      ros::spin();
    }
    

    ROS_INFO is one of the many logging methods.

    • It will print the message to the terminal output, and send it to the /rosout topic for other nodes to monitor.
    • There are 5 levels of logging: DEBUG, INFO, WARNING, ERROR, & FATAL.
    • To use a different logging level, replace INFO in ROS_INFO or ROS_INFO_STREAM with the appropriate level.
    • Use ROS_INFO for printf-style logging, and ROS_INFO_STREAM for cout-style logging.
  9. Build your program (node), by running catkin build in a terminal window

    • Remember that you must run catkin build from within your catkin_ws (or any subdirectory)
    • This will build all of the programs, libraries, etc. in fake_sonar_driver package.
    • In this case, it’s just a single ROS node fake_sonar_driver_node

Run a Node

  1. Open a terminal and start the ROS master.

    roscore
    

    Note

    The ROS Master must be running before any ROS nodes can function.

  2. Open a second terminal to run your node.

    • In a previous exercise, we added a line to our .bashrc to automatically source devel/setup.bash in new terminal windows

    • This will automatically export the results of the build into your new terminal session.

    • If you’re reusing an existing terminal, you’ll need to manually source the setup files (since we added a new node):

      source ~/catkin_ws/devel/setup.bash
      
  3. Run your node.

    rosrun fake_sonar_driver fake_sonar_driver_node
    

    Tip

    This runs the program we just created. Remember to use TAB to help speed-up typing and reduce errors.

  4. In a third terminal, check what nodes are running.

    rosnode list
    

    In addition to the /rosout node, you should now see a new /fake_sonar_driver_node listed.

  5. Enter rosnode kill /fake_sonar_driver_node. This will stop the node.

    Note

    It is more common to use Ctrl+C to stop a running node in the current terminal window.

Challenge

  1. Modify the node so that it prints your name. This will require you to run through the build process again.
  2. Demonstrate the working node to the instructor.