ROS quick start to implement helloworld program

1. Reference materials

ROS Quick Experience
My ROS Getting Started Tutorial (1)

2. Common commands

1. Create a workspace

catkin_wsis the most commonly used ROS workspace name.

# 创建目录,用于存储ROS工作空间
mkdir -p ~/catkin_ws/src

# 初始化工作空间
cd ~/catkin_ws/src
catkin_init_workspace

# 编译工作空间
cd ~/catkin_ws
catkin_make

2. Set environment variables

# 临时生效
source ~/catkin_ws/devel/setup.bash

# 永久生效
echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc

Check whether the environment variable is valid.

echo $ROS_PACKAGE_PATH

If the following information appears, it means that the environment variable has taken effect.

yoyo@yoyo:~/catkin_ws$ echo $ROS_PACKAGE_PATH
/home/yoyo/catkin_ws/src:/opt/ros/kinetic/share

3. Create a feature package

# 创建功能包
cd ~/catkin_ws/src
catkin_create_pkg <package_name> [depend1] [depend2] [depend3]

# 编译功能包,生成 build、devel 文件夹
cd ~/catkin_ws
catkin_make

parameter explanation

  • <package_name>: Indicates the name of the function package, which can be customized;
  • [depend1] [depend2] [depend3]: Indicates dependencies, commonly used dependencies include:
    • roscpp: Use C++ to implement the ros function;
    • rospy: use python to implement ros function;
    • std_msgs: Indicates the basic message type.

4. Delete the ROS workspace

# 编译清理
catkin_make clean

# 删除build和devel文件夹
rm -rf build
rm -rf devel

# 删除ROS_PACKAGE_PATH环境变量
unset ROS_PACKAGE_PATH

# 注释掉 ~/.bashrc 中无关的工作空间bash,只保留想要的工作空间,重新source
sudo gedit ~/.bashrc
source ~/.bashrc

# 删除工作空间文件夹
rm -rf <workspace_name>

5. Other instructions

# 查看正在使用的工作空间
$ROS_PACKAGE_PATH

# 查找功能包
cd ~/catkin_ws
rospack find [package_name]

3. ROS quick start (C++ version)

The programming languages ​​of ROS are mainly C++ and Python, and the implementation processes of the two programming languages ​​are roughly similar.
This chapter takes the HelloWorld program as an example to introduce the implementation process of the ROS program. The key processes mainly include:

  1. Create a workspace;
  2. Create feature packs;
  3. edit source code;
  4. Edit the configuration file;
  5. Compile and execute.

1. Create a workspace

yoyo@yoyo:~/catkin_ws/src$ catkin_init_workspace
Creating symlink "/home/yoyo/catkin_ws/src/CMakeLists.txt" pointing to "/opt/ros/kinetic/share/catkin/cmake/toplevel.cmake"
yoyo@yoyo:~/catkin_ws$ catkin_make
Base path: /home/yoyo/catkin_ws
Source space: /home/yoyo/catkin_ws/src
Build space: /home/yoyo/catkin_ws/build
Devel space: /home/yoyo/catkin_ws/devel
Install space: /home/yoyo/catkin_ws/install
####
#### Running command: "cmake /home/yoyo/catkin_ws/src -DCATKIN_DEVEL_PREFIX=/home/yoyo/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/yoyo/catkin_ws/install -G Unix Makefiles" in "/home/yoyo/catkin_ws/build"
####
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Using CATKIN_DEVEL_PREFIX: /home/yoyo/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /opt/ros/kinetic
-- This workspace overlays: /opt/ros/kinetic
-- Found PythonInterp: /usr/bin/python2 (found suitable version "2.7.12", minimum required is "2") 
-- Using PYTHON_EXECUTABLE: /usr/bin/python2
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/yoyo/catkin_ws/build/test_results
-- Found gtest sources under '/usr/src/gmock': gtests will be built
-- Found gmock sources under '/usr/src/gmock': gmock will be built
CMake Deprecation Warning at /usr/src/gmock/CMakeLists.txt:41 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


CMake Deprecation Warning at /usr/src/gtest/CMakeLists.txt:43 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


-- Found PythonInterp: /usr/bin/python2 (found version "2.7.12") 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.29
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- Configuring done
-- Generating done
-- Build files have been written to: /home/yoyo/catkin_ws/build
####
#### Running command: "make -j12 -l12" in "/home/yoyo/catkin_ws/build"
####

2. Create a feature package

Enter the src subdirectory, create the kitti_tutorial function package, and add dependencies.

yoyo@yoyo:~/catkin_ws/src$ catkin_create_pkg kitti_tutorial roscpp rospy std_msgs
Created file kitti_tutorial/CMakeLists.txt
Created file kitti_tutorial/package.xml
Created folder kitti_tutorial/include/kitti_tutorial
Created folder kitti_tutorial/src
Successfully created files in /home/yoyo/catkin_ws/src/kitti_tutorial. Please adjust the values in package.xml.

The above command will generate the kitti_tutorial function package in the workspace, which depends on roscpp, rospy and std_msgs. Among them, roscpp is a library implemented in C++, rospy is a library implemented in python, and std_msgs is a standard information library. When creating a ROS function package, these three libraries are generally relied upon.

3. Edit source code

Enter the src directory of the kitti_tutorial function package, and edit helloworld.cppthe source file.

cd ~/catkin_ws/src/kitti_tutorial/src

helloworld.cpp

#include "ros/ros.h"

int main(int argc, char *argv[])
{
    // 初始化ros节点
    ros::init(argc, argv, "hello");

    // 控制台输出 hello world
    ROS_INFO("hello world!");

    return 0;
}

4. Edit configuration file

CMakeLists.txt

Enter the kitti_tutorial function package directory and edit CMakeLists.txtthe file.

cd ~/catkin_ws/src/kitti_tutorial
add_executable(helloworld
  src/helloworld.cpp
)
target_link_libraries(helloworld
  ${catkin_LIBRARIES}
)

5. Compile and execute

Enter the workspace directory and recompile the workspace.

cd  ~/catkin_ws
catkin_make

Execute the helloworld program.

# 打开新终端,启动roscore
roscore

# 如果没有设置环境变量,则source环境变量
source ~/catkin_ws/devel/setup.bash

# 打开新终端,启动节点,运行helloworld程序
cd ~/catkin_ws
rosrun [package_name] [C++节点]
yoyo@yoyo:~/catkin_ws$ catkin_make
Base path: /home/yoyo/catkin_ws
Source space: /home/yoyo/catkin_ws/src
Build space: /home/yoyo/catkin_ws/build
Devel space: /home/yoyo/catkin_ws/devel
Install space: /home/yoyo/catkin_ws/install
####
#### Running command: "cmake /home/yoyo/catkin_ws/src -DCATKIN_DEVEL_PREFIX=/home/yoyo/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/yoyo/catkin_ws/install -G Unix Makefiles" in "/home/yoyo/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/yoyo/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /home/yoyo/catkin_ws/devel;/opt/ros/kinetic
-- This workspace overlays: /home/yoyo/catkin_ws/devel;/opt/ros/kinetic
-- Found PythonInterp: /usr/bin/python2 (found suitable version "2.7.12", minimum required is "2") 
-- Using PYTHON_EXECUTABLE: /usr/bin/python2
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/yoyo/catkin_ws/build/test_results
-- Found gtest sources under '/usr/src/gmock': gtests will be built
-- Found gmock sources under '/usr/src/gmock': gmock will be built
CMake Deprecation Warning at /usr/src/gmock/CMakeLists.txt:41 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


CMake Deprecation Warning at /usr/src/gtest/CMakeLists.txt:43 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


-- Found PythonInterp: /usr/bin/python2 (found version "2.7.12") 
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.29
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 1 packages in topological order:
-- ~~  - kitti_tutorial
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'kitti_tutorial'
-- ==> add_subdirectory(kitti_tutorial)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/yoyo/catkin_ws/build
####
#### Running command: "make -j12 -l12" in "/home/yoyo/catkin_ws/build"
####
[ 50%] Building CXX object kitti_tutorial/CMakeFiles/helloworld.dir/src/helloworld.cpp.o
[100%] Linking CXX executable /home/yoyo/catkin_ws/devel/lib/kitti_tutorial/helloworld
[100%] Built target helloworld
yoyo@yoyo:~/catkin_ws$ rosrun kitti_tutorial helloworld
[ INFO] [1690344312.055698992]: hello world!

4. ROS quick start (Python version)

The ROS program implementation process of the Python version is similar to that of C++, and this chapter only introduces the differences.
create workspaceandCreate feature packThe process is the same, see above.

1. Edit the source code

Enter the kitti_tutorial function package directory, add the scripts directory, and edit the python file.

cd ~/catkin_ws/src/kitti_tutorial
mkdir scripts

# 完成源代码之后,修改python文件的权限
chmod +x kitti.py 

helloworld.py

#! /usr/bin/env python


import rospy

if __name__ == "__main__":
    # 初始化ros节点
    rospy.init_node("Hello")
    # 控制台输出 hello world
    rospy.loginfo("Hello World!!!")

2. Edit configuration file

Enter the ros function package directory and edit the files under the function package CMakeLists.txt.

CamkeList.txt

catkin_install_python(PROGRAMS scripts/helloworld.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

3. Compile and execute

Enter the workspace directory and recompile the workspace.

cd ~/catkin_ws
catkin_make

Execute the helloworld program.

# 打开新终端,启动roscore
roscore

# 如果没有设置环境变量,则source环境变量
source ~/catkin_ws/devel/setup.bash

# 打开新终端,启动节点,运行helloworld程序
cd ~/catkin_ws
rosrun [package_name] [python节点]
yoyo@yoyo:~/catkin_ws$ catkin_make
Base path: /home/yoyo/catkin_ws
Source space: /home/yoyo/catkin_ws/src
Build space: /home/yoyo/catkin_ws/build
Devel space: /home/yoyo/catkin_ws/devel
Install space: /home/yoyo/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/yoyo/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/yoyo/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /home/yoyo/catkin_ws/devel;/opt/ros/kinetic
-- This workspace overlays: /home/yoyo/catkin_ws/devel;/opt/ros/kinetic
-- Found PythonInterp: /usr/bin/python2 (found suitable version "2.7.12", minimum required is "2") 
-- Using PYTHON_EXECUTABLE: /usr/bin/python2
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/yoyo/catkin_ws/build/test_results
-- Found gtest sources under '/usr/src/gmock': gtests will be built
-- Found gmock sources under '/usr/src/gmock': gmock will be built
CMake Deprecation Warning at /usr/src/gmock/CMakeLists.txt:41 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


CMake Deprecation Warning at /usr/src/gtest/CMakeLists.txt:43 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


-- Found PythonInterp: /usr/bin/python2 (found version "2.7.12") 
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.29
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 1 packages in topological order:
-- ~~  - kitti_tutorial
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'kitti_tutorial'
-- ==> add_subdirectory(kitti_tutorial)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/yoyo/catkin_ws/build
####
#### Running command: "make -j12 -l12" in "/home/yoyo/catkin_ws/build"
####
Consolidate compiler generated dependencies of target helloworld
[ 50%] Building CXX object kitti_tutorial/CMakeFiles/helloworld.dir/src/helloworld.cpp.o
[100%] Linking CXX executable /home/yoyo/catkin_ws/devel/lib/kitti_tutorial/helloworld
[100%] Built target helloworld
yoyo@yoyo:~/catkin_ws$ rosrun kitti_tutorial helloworld.py
[INFO] [1690346567.544713]: Hello World!!!!

Five, common experience

1. ROS workspace coverage

ROS workspace overlay

1.1 Problem description

The so-called workspace coverage refers to the situation where there are function packages with the same name in different workspaces.

In ROS development, the workspace will be customized, and multiple custom workspaces can exist at the same time. There may be a situation: although the function package in a specific workspace cannot have the same name, the function package of the custom workspace is the same as the built-in function package can have the same name, or a function package with the same name can appear in different customized workspaces, so which one will be called when calling the function package with this name? For example: the custom workspace A has the function package turtlesim, and the custom workspace B also has the function package turtlesim. Of course, the system built-in space also has turtlesim. If the turtlesim package is called, which workspace will be called?

1.2 Examples

  1. Create new workspace A and workspace B , and create function packages in both workspaces: turtlesim.

  2. The bash format for appending the current workspace under the file is as follows ~/.bashrc:

    source /home/用户/路径/工作空间A/devel/setup.bash
    source /home/用户/路径/工作空间B/devel/setup.bash
    
  3. Open a new terminal to execute the command: source .bashrcload environment variables;

  4. View the ROS environment environment variables echo $ROS_PACKAGE_PATH, the results show:Custom workspace B: Custom space A: System built-in space

  5. The call command roscd turtlesimwill enter the custom workspace B.

1.3 Problem Analysis

When .bashrcmultiple workspaces are sourced in the file, the ROS workspace may be overwritten. Specifically, ROS will parse .bashrcthe file and generate ROS_PACKAGE_PATHthe path of the workspace. In this variable, .bashrcthe priority of the workspace is set according to the configuration in . The priority of the workspace follows the following principles: ROS_PACKAGE_PATHthe value in .bashrcis opposite to the configuration order of .Post configuration has higher priorityWhen the workspace has the same name, it will ROS_PACKAGE_PATHbe searched according to , and the one configured earlier will be executed first

1.4 Solutions

buildYou can delete the and directories of the custom workspace devel, re- catkin_make, and then reload .bashrcthe file to solve the problem of ROS workspace coverage.

cd ~/catkin_ws
rm -rf build
rm -rf devel
catkin_make

echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc

6. FAQ

Q:[rosrun] Couldn't find executable named helloworld.py

yoyo@yoyo:~/catkin_ws$ rosrun kitti_tutorial helloworld.py
[rosrun] Couldn't find executable named helloworld.py below /home/yoyo/catkin_ws/src/kitti_tutorial
[rosrun] Found the following, but they're either not files,
[rosrun] or not executable:
[rosrun]   /home/yoyo/catkin_ws/src/kitti_tutorial/scripts/helloworld.py
错误原因:
没有给helloworld.py文件添加权限

解决办法:
给helloworld.py文件添加权限
chmod +x helloworld.py

Guess you like

Origin blog.csdn.net/m0_37605642/article/details/131931130