1.3.1 HelloWorld Implementation Overview
In ROS, the main programming languages used are C++ and Python. Most programs in ROS can be implemented in either language. In this tutorial series, each example will be demonstrated using both C++ and Python. You can choose the implementation that best suits your needs.
Even when using different programming languages, the implementation process for programs in ROS is generally similar. Taking the current HelloWorld program as an example, the implementation process is roughly as follows:
- First, create a workspace.
- Then, create a functional package.
- Edit the source files.
- Edit the configuration files.
- Compile and execute.
In the above process, the differences between C++ and Python lie mainly in the implementation details of steps 3 and 4, while the other steps are largely the same. This section will first cover the common steps 1 and 2 for both C++ and Python programming. Sections 1.3.2 and 1.3.3 will then demonstrate how to write HelloWorld using C++ and Python, respectively.
1. Create and Initialize a Workspace
text
mkdir -p custom_workspace_name/src
cd custom_workspace_name
catkin_makeThe above commands first create a workspace and a src subdirectory, then navigate into the workspace and compile it using the catkin_make command.
2. Navigate to src, Create a ROS Package, and Add Dependencies
text
cd src
catkin_create_pkg custom_ROS_package_name roscpp rospy std_msgsThe above command will generate a functional package in the workspace. This package depends on roscpp, rospy, and std_msgs. Here, roscpp is a library implemented in C++, rospy is implemented in Python, and std_msgs is the standard message library. When creating a ROS functional package, these three libraries are generally included as dependencies.
Note: In ROS, while C++ and Python can be used interchangeably for implementing the same functionality, the choice of language depends on the specific requirements. This is because the two languages have complementary characteristics: C++ offers high runtime efficiency but lower coding efficiency, whereas Python is the opposite. Based on these complementary traits, ROS designers developed the roscpp and rospy libraries. The former is intended to be a high-performance library for ROS, while the latter is typically used in scenarios where performance is not critical, aiming to improve development efficiency.