1. Create a workspace and initialize it
mkdir -p 自定义空间名称/src
cd 自定义空间名称
First, a workspace and a src subdirectory will be created, and then enter the workspace and call the catkin_make command to compile, as shown in the figure below.
catkin_make: A compilation command in ros, the log will be output after compiling, as shown in the figure below.
After opening the workspace, the devel file and build file appear, as shown in the figure below.
2. Create a function package
Enter the src directory in the workspace
cd src
Create a function package roscpp rospy std_msgs: There are three dependent packages, among which roscpp is a library implemented using C++, while rospy is a library implemented using python, and std_msgs is a standard message library. When creating a ROS function package, you generally rely on these three library implementation.
catkin_create_pkg 自定义ROS包名 roscpp rospy std_msgs
The folder will change after creation, as shown in the figure below.
There will be some folders and some cmake files
Three, C++ version implementation
1. Enter the src directory of the ros package to edit the source file
cd 自定义的包
The source file is placed under the second src file
Create source files
The source file includes: ros header file, main function, initialization node, output log.
Vim compiler compiles helloworld.Cpp file
#include "ros/ros.h"
int main(int argc, char *argv[])
{
//执行 ros 节点初始化
ros::init(argc,argv,"hello");
//创建 ros 节点句柄(非必须)
ros::NodeHandle n;
//控制台输出 hello world
ROS_INFO("hello world!");
return 0;
}
Type i to enter the editing mode, press ESC after editing the content and input ":wq" to save and exit.
2. Edit the Cmakelist.txt file under the ros package
Enter cd .. to return to the previous directory to edit CMakeLists.tt
vim CMakeLists.txt
Remove the annotation at the cutting head finger in the figure below
Change the above two lines of code to:
add_executable(源文件名(可以随便,这里的名字,但要和下面那句代码名字对应一致)
src/步骤3的源文件名.cpp
)
target_link_libraries(步骤3的源文件名
${catkin_LIBRARIES}
)
Example:
add_executable(helloworld src/helloworld.cpp)
target_link_libraries(helloworld
${catkin_LIBRARIES}
)
After the input is complete, also type ":wq" to save and exit.
3. Enter the workspace directory and compile
Type "cd ../../" to return to the workspace
Type "catkin_make" to compile
The following figure shows that the compilation is successful
If there is an error , there will be a red error prompt.
4. Execution
To execute, you must first start the ros core, and here you need to create a new window (Ctrl+Shift+E)
instruction:
cd 工作空间
source ./devel/setup.bash 构建环境变量
rosrun 包名 C++节点
The explanation of source ./devel/setup.bash (using source to build environment variables) adds the path of the corresponding workspace to the environment variable ROS_PACKAGE_PATH. If you open a new terminal command line, you must first add the path of the workspace to the environment variable ROS_PACKAGE_PATH before using the workspace. The purpose of this sentence is to run this setup.bash when opening a new terminal, and The role of this setup.bash is to make some commands starting with ROS* available. There are several environment variable setting scripts in the form of setup.*sh in the devel folder of the workspace. Use the source command to run these script files, then the environment variable settings of the workspace can take effect (for example, the projects in the workspace can be found)
final output
[ INFO] [1680504844.008447821]: hello world!
So far, a program in C++ has been successfully completed!
4. Python version
1. Enter the ros package to add the scripts directory and edit the python file
cd ros包 进入功能包
mkdir scripts 创建scripts文件夹
cd scripts 进入script文件夹
touch helloworld.py 创建helloworld.py文件
Edit with vim and add content:
#! /usr/bin/env python
#指定解释器
"""
Python 版 HelloWorld
"""
import rospy
#主入口
if __name__ == "__main__":
rospy.init_node("Hello") #初始化ros节点
rospy.loginfo("Hello World!!!!") #输出日志
The above code is divided into:
- 1. Specify the interpreter
- 2. Guide package
- 3. Write the main entry
- 4. Initialize the ros node
- 5. Output log
After writing, type "esc" ":wq" to save and exit
2. Add executable permissions to python files
Type "ll" command to view execution permissions
The above file does not have execute permission
Execute "chmod +x custom file name.py" to add execution permissions
3. Edit the CamkeList.txt file under the ros package
Go back to the previous directory and edit the CamkeList.txt file. Use "vim ../CamkeList.txt" directly here
Remove the comments here and change (remember to save and exit), the changes are as follows:
4. Enter the workspace directory and compile
same as c++ version
5. Enter the workspace directory and execute
cd 工作空间
source ./devel/setup.bash
rosrun package name custom file name.py
operation result
Five, the subtle differences between the two versions
6. Problem summary
The following situations occur during operation:
Solution:
When writing Chinese comments or outputting Chinese in Python 2.X files, compilation errors often occur (there is no such error in Python 3.X.):
File "/home/l/ros/demo01_ws/src/helloworld/scripts/helloworld.py", line 2
SyntaxError: Non-ASCII character '\xe6' in file /home/l/ros/demo01_ws/src/helloworld/scripts/helloworld.py on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
This is because the default encoding file of Python 2.X uses ASCII code. If you want to compile it, you only need to add a line of code at the beginning of the file to save the file in UTF-8 format.
Add "# -*- coding:utf-8 -*-" to the first line of the code python code
# -*- coding:utf-8 -*-