使用c++和python两种方式
首先使用c++
1:创建一个发布器节点
mkdir -p ~/catkin_ws/src/beginner_tutorials/src 先创建一个文件夹,用于存储该代码包的源码
具体代码参考tutorial
2:创建一个接收节点
3:编译
在beginner_tutorials中的cmakelist.txt中添加如下。保持原来上一个例子的改动。添加的msg和srv
include_directories(include ${catkin_INCLUDE_DIRS}) add_executable(talker src/talker.cpp) target_link_libraries(talker ${catkin_LIBRARIES}) add_executable(listener src/listener.cpp) target_link_libraries(listener ${catkin_LIBRARIES})
对可执行文件添加对消息文件的依赖,举例如下,对talker添加依赖
add_dependencies(talker beginner_tutorials_generate_messages_cpp)
这样就可以确保自定义消息的头文件在被使用之前已经被生成。因为 catkin 把所有的 package 并行的编译,所以如果你要使用其他 catkin 工作空间中其他 package 的消息,你同样也需要添加对他们各自生成的消息文件的依赖
然后可执行catkin_make, 对应的打印信息如下,可见对应的talker和listener的生成。
root@ben-virtual-machine:~/catkin_ws# catkin_make
Base path: /root/catkin_ws
Source space: /root/catkin_ws/src
Build space: /root/catkin_ws/build
Devel space: /root/catkin_ws/devel
Install space: /root/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/root/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /root/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /root/catkin_ws/devel;/opt/ros/kinetic
-- This workspace overlays: /root/catkin_ws/devel;/opt/ros/kinetic
-- Using PYTHON_EXECUTABLE: /usr/bin/python
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /root/catkin_ws/build/test_results
-- Found gmock sources under '/usr/src/gmock': gmock will be built
-- Found gtest sources under '/usr/src/gmock': gtests will be built
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.14
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~ traversing 1 packages in topological order:
-- ~~ - beginner_tutorials
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'beginner_tutorials'
-- ==> add_subdirectory(beginner_tutorials)
-- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy
-- beginner_tutorials: 1 messages, 1 services
-- Configuring done
-- Generating done
-- Build files have been written to: /root/catkin_ws/build
####
#### Running command: "make -j2 -l2" in "/root/catkin_ws/build"
####
[ 0%] Built target std_msgs_generate_messages_cpp
[ 0%] Built target _beginner_tutorials_generate_messages_check_deps_Num
[ 0%] Built target _beginner_tutorials_generate_messages_check_deps_AddTwoInts
[ 0%] Built target std_msgs_generate_messages_eus
[ 0%] Built target std_msgs_generate_messages_lisp
[ 0%] Built target std_msgs_generate_messages_py
[ 0%] Built target std_msgs_generate_messages_nodejs
[ 11%] Built target beginner_tutorials_generate_messages_cpp
[ 29%] Built target beginner_tutorials_generate_messages_eus
[ 41%] Built target beginner_tutorials_generate_messages_lisp
[ 64%] Built target beginner_tutorials_generate_messages_py
[ 76%] Built target beginner_tutorials_generate_messages_nodejs
Scanning dependencies of target listener
Scanning dependencies of target talker
[ 88%] Building CXX object beginner_tutorials/CMakeFiles/listener.dir/src/listener.cpp.o
[ 88%] Building CXX object beginner_tutorials/CMakeFiles/talker.dir/src/talker.cpp.o
[ 94%] Linking CXX executable /root/catkin_ws/devel/lib/beginner_tutorials/talker
[100%] Linking CXX executable /root/catkin_ws/devel/lib/beginner_tutorials/listener
[100%] Built target talker
[100%] Built target listener
[100%] Built target beginner_tutorials_generate_messages
测试结果,如果新打开终端,需要执行下面语句,否则找不到packet
cd ~/catkin_ws source ./devel/setup.bash
启动一个发布器,
rosrun beginner_tutorials talker
启动一个订阅器
rosrun beginner_tutorials listener
结果如下,右边是发布器,左边是订阅器
python 版本
首先创建目录 $ mkdir scripts $ cd scripts
将这一行打开,支持python
然后编译,发现下面的错误,没有找到setup.py文件,记录下面的log
root@ben-virtual-machine:~/catkin_ws# catkin_make
Base path: /root/catkin_ws
Source space: /root/catkin_ws/src
Build space: /root/catkin_ws/build
Devel space: /root/catkin_ws/devel
Install space: /root/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/root/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /root/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /root/catkin_ws/devel;/opt/ros/kinetic
-- This workspace overlays: /root/catkin_ws/devel;/opt/ros/kinetic
-- Using PYTHON_EXECUTABLE: /usr/bin/python
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /root/catkin_ws/build/test_results
-- Found gmock sources under '/usr/src/gmock': gmock will be built
-- Found gtest sources under '/usr/src/gmock': gtests will be built
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.14
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~ traversing 1 packages in topological order:
-- ~~ - beginner_tutorials
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'beginner_tutorials'
-- ==> add_subdirectory(beginner_tutorials)
-- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy
CMake Error at /opt/ros/kinetic/share/catkin/cmake/catkin_python_setup.cmake:31 (message):
catkin_python_setup() called without 'setup.py' in project folder '
/root/catkin_ws/src/beginner_tutorials'
Call Stack (most recent call first):
beginner_tutorials/CMakeLists.txt:24 (catkin_python_setup)
-- Configuring incomplete, errors occurred!
See also "/root/catkin_ws/build/CMakeFiles/CMakeOutput.log".
See also "/root/catkin_ws/build/CMakeFiles/CMakeError.log".
Makefile:738: recipe for target 'cmake_check_build_system' failed
make: *** [cmake_check_build_system] Error 1
Invoking "make cmake_check_build_system" failed
在beginner tutorials的软件包的根目录创建setup.py文件,文件内容如下
from distutils.core import setup
setup(
version='0.0.0',
scripts=['scripts/talker.py','scripts/listener.py'],
packages=['beginner_tutorials'],
package_dir={'': 'scripts'}
)
再次执行编译, 注意,复制的setup.py格式有可能不对,最好自己输入,下面是记录的结果
root@ben-virtual-machine:~/catkin_ws# catkin_make
Base path: /root/catkin_ws
Source space: /root/catkin_ws/src
Build space: /root/catkin_ws/build
Devel space: /root/catkin_ws/devel
Install space: /root/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/root/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /root/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /root/catkin_ws/devel;/opt/ros/kinetic
-- This workspace overlays: /root/catkin_ws/devel;/opt/ros/kinetic
-- Using PYTHON_EXECUTABLE: /usr/bin/python
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /root/catkin_ws/build/test_results
-- Found gmock sources under '/usr/src/gmock': gmock will be built
-- Found gtest sources under '/usr/src/gmock': gtests will be built
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.14
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~ traversing 1 packages in topological order:
-- ~~ - beginner_tutorials
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'beginner_tutorials'
-- ==> add_subdirectory(beginner_tutorials)
-- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy
-- beginner_tutorials: 1 messages, 1 services
-- Configuring done
-- Generating done
-- Build files have been written to: /root/catkin_ws/build
####
#### Running command: "make -j2 -l2" in "/root/catkin_ws/build"
####
[ 0%] Built target std_msgs_generate_messages_cpp
[ 0%] Built target _beginner_tutorials_generate_messages_check_deps_Num
[ 0%] Built target _beginner_tutorials_generate_messages_check_deps_AddTwoInts
[ 0%] Built target std_msgs_generate_messages_eus
[ 0%] Built target std_msgs_generate_messages_py
[ 0%] Built target std_msgs_generate_messages_lisp
[ 0%] Built target std_msgs_generate_messages_nodejs
[ 11%] Built target beginner_tutorials_generate_messages_cpp
[ 29%] Built target beginner_tutorials_generate_messages_eus
[ 52%] Built target beginner_tutorials_generate_messages_py
[ 64%] Built target beginner_tutorials_generate_messages_lisp
[ 76%] Built target listener
[ 88%] Built target beginner_tutorials_generate_messages_nodejs
[100%] Built target talker
[100%] Built target beginner_tutorials_generate_messages
测试,结果和c++一样,生成的两个文件在devel/bin下面
rosrun beginner_tutorials talker.py
rosrun beginner_tutorials listener.py