---恢复内容开始---
本文假设你已经有了ROS的开发环境,其实无论是源码安装还是使用安装包安装的ROS,都是一样的。
一、创建工作区并完成适当的设置工作
1、新建目录,必须包含src子目录
mkdir -p ~/catkin_wks/src
2、初步初始化工作空间,创建的/opt/ros/melodic/share/catkin/cmake/toplevel.cmake文件的本地链接而以
cd catkin_wks/src/ && catkin_init_workspace
返回信息:
Creating symlink "/home/openlib/catkin_wks/src/CMakeLists.txt" pointing to "/opt/ros/melodic/share/catkin/cmake/toplevel.cmake"
3、再次初始化工作空间
cd ../ && catkin_make
返回信息:
Base path: /home/openlib/catkin_wks
Source space: /home/openlib/catkin_wks/src
Build space: /home/openlib/catkin_wks/build
Devel space: /home/openlib/catkin_wks/devel
Install space: /home/openlib/catkin_wks/install
####
#### Running command: "cmake /home/openlib/catkin_wks/src -DCATKIN_DEVEL_PREFIX=/home/openlib/catkin_wks/devel -DCMAKE_INSTALL_P
REFIX=/home/openlib/catkin_wks/install -G Unix Makefiles" in "/home/openlib/catkin_wks/build"
####
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Using CATKIN_DEVEL_PREFIX: /home/openlib/catkin_wks/devel
-- Using CMAKE_PREFIX_PATH: /opt/ros/melodic
-- This workspace overlays: /opt/ros/melodic
-- Found PythonInterp: /usr/bin/python2 (found suitable version "2.7.15", 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/openlib/catkin_wks/build/test_results
-- Found gmock sources under '/usr/src/googletest': gmock will be built
-- Found PythonInterp: /usr/bin/python2 (found version "2.7.15")
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- 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
-- Found gtest sources under '/usr/src/googletest': gtests will be built
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.17
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- Configuring done
-- Generating done
-- Build files have been written to: /home/openlib/catkin_wks/build
####
#### Running command: "make -j4 -l4" in "/home/openlib/catkin_wks/build"
####
根据提示,如果使用c++开发ROS节点,必须执行命令,否则,可以不予理会,直接跳过:
cd build/ && make -j8 -l8
配置终端环境,这个是一次性的配置:
cd ../ && source devel/setup.bash
4、永久配置终端工作区,只要打开终端就自动配置好工作区:将source ~/catkin_wks/devel/setup.bash写入.bashrc文件中
echo "source ~/catkin_wks/devel/setup.bash" >> ~/.bashrc
二、创建包:
Ros中无论代码、数据、文档都是以包的形式来管理;包一般位于工作区中的的src目录中;创建成功的包至少包含两个文件CMakeLists.txt和package.xml文件,一个目录src子目录;所谓的包管理只是目录管理的别称而以;src中主要存放python代码
1、包的创建,创建名称为myAwesomeCode的包,并且该包依赖rospy包,如果有更多的依赖,一并跟到后边,构成依赖列表即可:
cd ~/catkin_wks/src/ && catkin_create_pkg myAwesomeCode rospy
返回信息,提示信息抱怨不能包含大写字母,个人习惯在单词分割处采用大写字母,有的人喜欢用下划线分割,ros更倾向于后者:
WARNING: Package name "myAwesomeCode" does not follow the naming conventions. It should start with a lower case letter and only
contain lower case letters, digits, underscores, and dashes.
Created file myAwesomeCode/CMakeLists.txt
Created file myAwesomeCode/package.xml
Created folder myAwesomeCode/src
Successfully created files in /home/openlib/catkin_wks/src/myAwesomeCode. Please adjust the values in package.xml.
根据提示信息,可见包创建中主要在工作区的src目录完成了创建包同名的字母的创建,并在该目录下下创建两个文件CMakeLists.txt和package.xml的创建和一个目录src子目录的创建
其中CMakeLists.txt的内容:
cmake_minimum_required(VERSION 2.8.3)
project(myAwesomeCode)
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
rospy
)
## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)
## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()
################################################
## Declare ROS messages, services and actions ##
################################################
## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
## * add a build_depend tag for "message_generation"
## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
## but can be declared for certainty nonetheless:
## * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
## * add "message_generation" and every package in MSG_DEP_SET to
## find_package(catkin REQUIRED COMPONENTS ...)
## * add "message_runtime" and every package in MSG_DEP_SET to
## catkin_package(CATKIN_DEPENDS ...)
## * uncomment the add_*_files sections below as needed
## and list every .msg/.srv/.action file to be processed
## * uncomment the generate_messages entry below
## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
## Generate messages in the 'msg' folder
# add_message_files(
# FILES
# Message1.msg
# Message2.msg
# )
## Generate services in the 'srv' folder
# add_service_files(
# FILES
# Service1.srv
# Service2.srv
# )
## Generate actions in the 'action' folder
# add_action_files(
# FILES
# Action1.action
# Action2.action
# )
## Generate added messages and services with any dependencies listed here
# generate_messages(
# DEPENDENCIES
# std_msgs # Or other packages containing msgs
# )
################################################
## Declare ROS dynamic reconfigure parameters ##
################################################
## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
## * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
## * add "dynamic_reconfigure" to
## find_package(catkin REQUIRED COMPONENTS ...)
## * uncomment the "generate_dynamic_reconfigure_options" section below
## and list every .cfg file to be processed
## Generate dynamic reconfigure parameters in the 'cfg' folder
# generate_dynamic_reconfigure_options(
# cfg/DynReconf1.cfg
# cfg/DynReconf2.cfg
# )
###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES myAwesomeCode
# CATKIN_DEPENDS rospy
# DEPENDS system_lib
)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
## Declare a C++ library
# add_library(${PROJECT_NAME}
# src/${PROJECT_NAME}/myAwesomeCode.cpp
# )
## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
# add_executable(${PROJECT_NAME}_node src/myAwesomeCode_node.cpp)
## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")
## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Specify libraries to link a library or executable target against
# target_link_libraries(${PROJECT_NAME}_node
# ${catkin_LIBRARIES}
# )
#############
## Install ##
#############
# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html
## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
# install(PROGRAMS
# scripts/my_python_script
# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
## Mark executables for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html
# install(TARGETS ${PROJECT_NAME}_node
# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
## Mark libraries for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
# install(TARGETS ${PROJECT_NAME}
# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
# )
## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
# FILES_MATCHING PATTERN "*.h"
# PATTERN ".svn" EXCLUDE
# )
## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
# # myfile1
# # myfile2
# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )
#############
## Testing ##
#############
## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_myAwesomeCode.cpp)
# if(TARGET ${PROJECT_NAME}-test)
# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()
## Add folders to be run by python nosetests
# catkin_add_nosetests(test)
该文件主要是为下一步的camke作准备,如果需要cmake时,该文件将非常关键。
主要有find_package()、add_message_files() generate_messages() catkin_package()方法的配置
而package.xml的内容:
<?xml version="1.0"?> <package format="2"> <name>myAwesomeCode</name> <version>0.0.0</version> <description>The myAwesomeCode package</description> <!-- One maintainer tag required, multiple allowed, one person per tag --> <!-- Example: --> <!-- <maintainer email="[email protected]">Jane Doe</maintainer> --> <maintainer email="[email protected]">openlib</maintainer> <!-- One license tag required, multiple allowed, one license per tag --> <!-- Commonly used license strings: --> <!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 --> <license>TODO</license> <!-- Url tags are optional, but multiple are allowed, one per tag --> <!-- Optional attribute type can be: website, bugtracker, or repository --> <!-- Example: --> <!-- <url type="website">http://wiki.ros.org/myAwesomeCode</url> --> <!-- Author tags are optional, multiple are allowed, one per tag --> <!-- Authors do not have to be maintainers, but could be --> <!-- Example: --> <!-- <author email="[email protected]">Jane Doe</author> --> <!-- The *depend tags are used to specify dependencies --> <!-- Dependencies can be catkin packages or system dependencies --> <!-- Examples: --> <!-- Use depend as a shortcut for packages that are both build and exec dependencies --> <!-- <depend>roscpp</depend> --> <!-- Note that this is equivalent to the following: --> <!-- <build_depend>roscpp</build_depend> --> <!-- <exec_depend>roscpp</exec_depend> --> <!-- Use build_depend for packages you need at compile time: --> <!-- <build_depend>message_generation</build_depend> --> <!-- Use build_export_depend for packages you need in order to build against this package: --> <!-- <build_export_depend>message_generation</build_export_depend> --> <!-- Use buildtool_depend for build tool packages: --> <!-- <buildtool_depend>catkin</buildtool_depend> --> <!-- Use exec_depend for packages you need at runtime: --> <!-- <exec_depend>message_runtime</exec_depend> --> <!-- Use test_depend for packages you need only for testing: --> <!-- <test_depend>gtest</test_depend> --> <!-- Use doc_depend for packages you need only for building documentation: --> <!-- <doc_depend>doxygen</doc_depend> --> <buildtool_depend>catkin</buildtool_depend> <build_depend>rospy</build_depend> <build_export_depend>rospy</build_export_depend> <exec_depend>rospy</exec_depend> <!-- The export tag contains other, unspecified, tags --> <export> <!-- Other tools can request additional information be placed here --> </export> </package>
该文件主要描述了包的内容与catkin的交互方式,包括以下9个部分:
1、程序包名
2、程序版本号
3、功能描述
4、维护和修复着信息
5、程序许可证信息
6、程序包的URL信息
7、作者们的信息
8、包的依赖信息
9、其他相关信息
三、创建代码
该步骤先略过
四、运行例程代码个查看运行信息
开第一个终端运行:roscore
返回信息:
... logging to /home/openlib/.ros/log/a8243616-f3bc-11e9-97b3-3464a91cb6c2/roslaunch-developz-5178.log
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.
started roslaunch server http://developz:40643/
ros_comm version 1.14.3
SUMMARY
========
PARAMETERS
* /rosdistro: melodic
* /rosversion: 1.14.3
NODES
auto-starting new master
process[master]: started with pid [5188]
ROS_MASTER_URI=http://developz:11311/
setting /run_id to a8243616-f3bc-11e9-97b3-3464a91cb6c2
WARNING: Package name "myAwesomeCode" does not follow the naming conventions. It should start with a lower case letter and only c
ontain lower case letters, digits, underscores, and dashes.
process[rosout-1]: started with pid [5199]
started core service [/rosout]
rosrun的格式:rosrun packageName executName [ARGS]
开第二个终端运行:rosrun rospy_tutorials talker
返回信息:
rosrun rospy_tutorials talker
[INFO] [1571632778.697190]: hello world 1571632778.7
[INFO] [1571632778.797637]: hello world 1571632778.8
[INFO] [1571632778.897323]: hello world 1571632778.9
[INFO] [1571632778.997688]: hello world 1571632779.0
[INFO] [1571632779.097660]: hello world 1571632779.1
[INFO] [1571632779.197704]: hello world 1571632779.2
[INFO] [1571632779.297709]: hello world 1571632779.3
[INFO] [1571632779.397694]: hello world 1571632779.4
[INFO] [1571632779.497683]: hello world 1571632779.5
[INFO] [1571632779.597744]: hello world 1571632779.6
[INFO] [1571632779.697699]: hello world 1571632779.7
[INFO] [1571632779.797760]: hello world 1571632779.8
[INFO] [1571632779.897667]: hello world 1571632779.9
[INFO] [1571632779.997731]: hello world 1571632780.0
[INFO] [1571632780.097680]: hello world 1571632780.1
[INFO] [1571632780.197637]: hello world 1571632780.2
[INFO] [1571632780.297651]: hello world 1571632780.3
[INFO] [1571632780.397748]: hello world 1571632780.4
[INFO] [1571632780.497736]: hello world 1571632780.5
[INFO] [1571632780.597785]: hello world 1571632780.6
[INFO] [1571632780.697701]: hello world 1571632780.7
[INFO] [1571632780.797470]: hello world 1571632780.8
[INFO] [1571632780.897671]: hello world 1571632780.9
[INFO] [1571632780.997640]: hello world 1571632781.0
[INFO] [1571632781.097389]: hello world 1571632781.1
[INFO] [1571632781.197665]: hello world 1571632781.2
[INFO] [1571632781.297650]: hello world 1571632781.3
[INFO] [1571632781.397795]: hello world 1571632781.4
开第三个终端运行:rosrun rospy_tutorials listener
返回信息:
rosrun rospy_tutorials listener
[INFO] [1571632853.901133]: /listener_5587_1571632853679I heard hello world 1571632853.9
[INFO] [1571632853.998517]: /listener_5587_1571632853679I heard hello world 1571632854.0
[INFO] [1571632854.100438]: /listener_5587_1571632853679I heard hello world 1571632854.1
[INFO] [1571632854.201572]: /listener_5587_1571632853679I heard hello world 1571632854.2
[INFO] [1571632854.301579]: /listener_5587_1571632853679I heard hello world 1571632854.3
[INFO] [1571632854.401040]: /listener_5587_1571632853679I heard hello world 1571632854.4
[INFO] [1571632854.501363]: /listener_5587_1571632853679I heard hello world 1571632854.5
[INFO] [1571632854.600271]: /listener_5587_1571632853679I heard hello world 1571632854.6
[INFO] [1571632854.701491]: /listener_5587_1571632853679I heard hello world 1571632854.7
[INFO] [1571632854.801139]: /listener_5587_1571632853679I heard hello world 1571632854.8
[INFO] [1571632854.901557]: /listener_5587_1571632853679I heard hello world 1571632854.9
[INFO] [1571632855.001366]: /listener_5587_1571632853679I heard hello world 1571632855.0
[INFO] [1571632855.101437]: /listener_5587_1571632853679I heard hello world 1571632855.1
[INFO] [1571632855.200990]: /listener_5587_1571632853679I heard hello world 1571632855.2
[INFO] [1571632855.301296]: /listener_5587_1571632853679I heard hello world 1571632855.3
[INFO] [1571632855.401455]: /listener_5587_1571632853679I heard hello world 1571632855.4
[INFO] [1571632855.501047]: /listener_5587_1571632853679I heard hello world 1571632855.5
[INFO] [1571632855.601032]: /listener_5587_1571632853679I heard hello world 1571632855.6
[INFO] [1571632855.701651]: /listener_5587_1571632853679I heard hello world 1571632855.7
[INFO] [1571632855.801294]: /listener_5587_1571632853679I heard hello world 1571632855.8
[INFO] [1571632855.901156]: /listener_5587_1571632853679I heard hello world 1571632855.9
[INFO] [1571632856.001304]: /listener_5587_1571632853679I heard hello world 1571632856.0
[INFO] [1571632856.101461]: /listener_5587_1571632853679I heard hello world 1571632856.1
[INFO] [1571632856.201528]: /listener_5587_1571632853679I heard hello world 1571632856.2
[INFO] [1571632856.301237]: /listener_5587_1571632853679I heard hello world 1571632856.3
[INFO] [1571632856.401189]: /listener_5587_1571632853679I heard hello world 1571632856.4
[INFO] [1571632856.501317]: /listener_5587_1571632853679I heard hello world 1571632856.5
[INFO] [1571632856.601370]: /listener_5587_1571632853679I heard hello world 1571632856.6
[INFO] [1571632856.701299]: /listener_5587_1571632853679I heard hello world 1571632856.7
[INFO] [1571632856.801316]: /listener_5587_1571632853679I heard hello world 1571632856.8
[INFO] [1571632856.901329]: /listener_5587_1571632853679I heard hello world 1571632856.9
[INFO] [1571632856.999119]: /listener_5587_1571632853679I heard hello world 1571632857.0
[INFO] [1571632857.101316]: /listener_5587_1571632853679I heard hello world 1571632857.1
开第四个终端运行:rqt_graph
返回信息:
rqt_graph
WARNING: Package name "myAwesomeCode" does not follow the naming conventions. It should start with a lower case letter and only c
ontain lower case letters, digits, underscores, and dashes.
WARNING: Package name "myAwesomeCode" does not follow the naming conventions. It should start with a lower case letter and only c
ontain lower case letters, digits, underscores, and dashes.
很明显可以看出ROS完成talker和listener的链接,无需人工操作。
如果再开新的终端,分别运行:rosrun rospy_tutorials listener和rosrun rospy_tutorials talker,则ROS图如下:
ROS又一次完成了自动链接,随着节点的增多,ROS自动完成节点的链接。
五、roslaunch
ros中的命名对象包括:节点名、话题名、服务名、参数名、文件系统名、网络URL和其他领域需要命名的场合
---恢复内容结束---