Practical ROS Operations and C++ Package Compilation

Creating a Python Package in ROS

To create a new ROS package, navigate to the src directory of your workspace and execute the following command:

catkin_create_pkg my_python_package std_msgs rospy roscpp rosmsg

The basic syntax is:

catkin_create_pkg <package_name> [dependency1] [dependency2] ...

This command generates the necessary directory structure and files, including CMakeLists.txt and package.xml. You can modify these files later to add or change package dependencies.

Your workspace structure will look like this:

workspace/
├── build/
├── devel/
│   └── setup.bash
└── src/
    ├── CMakeLists.txt
    └── my_python_package/
        ├── CMakeLists.txt
        ├── include/
        ├── package.xml
        └── src/

Within your package directory (my_python_package/), create a scripts subdirectory:

mkdir scripts

Place your Python script in the scripts folder. Ensure the script begins with the following lines:

#!/usr/bin/env python
# coding:utf-8

After writing your script, grant it execute permissions:

chmod +x your_python_script.py

Finally, compile your workspace and source the setup file:

cd workspace
catkin_make
source ./devel/setup.bash

Running ROS Nodes

There are two primary methods for running ROS nodes:

  1. Manual Startup: Start the ROS Master with roscore in one terminal, then run your node in another using rosrun <package_name> <node_name>.
  2. Launch Files: Use a .launch file to define and run multiple nodes simultaneously. This method automatically starts the ROS Master.

To run a node using a launch file:

roslaunch <package_name> <your_launch_file.launch>

Troubleshooting Compilation Issues

If you encounter import errors after recompiling, try deleting the corresponding package folder within devel/lib/ and then recompile.

Publisher and Subscriber Concepts

When implementing publishers and subscribers, consider the queue_size parameter to manage message buffering.

ROS Logging Management

  • View Logs: Use rqt_console to visualize ROS logging messages.
  • Check Log Status: rosclean check can report on log file usage.
  • Clean Logs: rosclean purge removes old log files.

Compiling C++ ROS Packages

To create a C++ ROS package:

cd catkin_ws/src
catkin_create_pkg my_cpp_package roscpp rospy std_msgs

Place your C++ source file(s) in catkin_ws/src/my_cpp_package/src/. Then, modify the CMakeLists.txt file within the my_cpp_package directory. Key modifications include:

  • Add necessary packages to find_package (e.g., roscpp, rospy, std_msgs).
  • Update include_directories.
  • Define you're executable: add_executable(my_cpp_package src/your_cpp_file.cpp).
  • Link libraries: target_link_libraries(my_cpp_package ${catkin_LIBRARIES}).

After modifying CMakeLists.txt, compile the workspace:

cd catkin_ws/
catkin_make

If compilation fails due to missing packages, install them using sudo apt-get install ros-<ros_distro>-<package_name> (e.g., sudo apt-get install ros-melodic-navigation). Recompile after installation.

Important: Always source the setup.bash file in your devel directory after successful compilation.

Tags: ROS python C++ Package Creation Node Execution

Posted on Thu, 07 May 2026 10:03:48 +0000 by LuiePL