编写简单的服务器和客户端


根据tutorials,beginner tutorials中的src下建立对应的cpp文件,然后在cmakelist.txt(/root/catkin_ws/src/beginner_tutorials)中增加下面一段。


add_executable(add_two_ints_server src/add_two_ints_server.cpp)
target_link_libraries(add_two_ints_server ${catkin_LIBRARIES})
add_dependencies(add_two_ints_server beginner_tutorials_gencpp)

add_executable(add_two_ints_client src/add_two_ints_client.cpp)
target_link_libraries(add_two_ints_client ${catkin_LIBRARIES})
add_dependencies(add_two_ints_client beginner_tutorials_gencpp)

然后编译

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_lisp
[  0%] Built target std_msgs_generate_messages_eus
[  0%] Built target std_msgs_generate_messages_py
[  0%] Built target std_msgs_generate_messages_nodejs
[ 14%] Built target beginner_tutorials_generate_messages_eus
[ 23%] Built target beginner_tutorials_generate_messages_cpp
[ 33%] Built target beginner_tutorials_generate_messages_lisp
[ 61%] Built target beginner_tutorials_generate_messages_nodejs
[ 57%] Built target beginner_tutorials_generate_messages_py
Scanning dependencies of target beginner_tutorials_gencpp
[ 71%] Built target listener
[ 71%] Built target beginner_tutorials_gencpp
[ 71%] Built target beginner_tutorials_generate_messages
Scanning dependencies of target add_two_ints_server
[ 80%] Built target talker
Scanning dependencies of target add_two_ints_client
[ 85%] Building CXX object beginner_tutorials/CMakeFiles/add_two_ints_server.dir/src/add_two_ints_server.cpp.o
[ 90%] Building CXX object beginner_tutorials/CMakeFiles/add_two_ints_client.dir/src/add_two_ints_client.cpp.o
[ 95%] Linking CXX executable /root/catkin_ws/devel/lib/beginner_tutorials/add_two_ints_server
[100%] Linking CXX executable /root/catkin_ws/devel/lib/beginner_tutorials/add_two_ints_client

[100%] Built target add_two_ints_client
[100%] Built target add_two_ints_server

结果如下图
 

 

python版本

还是先创建服务器和客户端的py文件,

注意要设置模式为可执行

chmod +x scripts/add_two_ints_client.py

然后编译,注意需要增加到这里

from distutils.core import setup
 
setup(
    version='0.0.0',
    scripts=['scripts/talker.py','scripts/listener.py','scripts/add_two_ints_server.py','scripts/add_two_ints_client.py'],
    packages=['beginner_tutorials'],
    package_dir={'': 'scripts'}
)

分别打开两个终端,执行如下,

$ cd ~/catkin_ws
$ . devel/setup.bash
$ rosrun beginner_tutorials add_two_ints_server.py
$ cd ~/catkin_ws
$ . devel/setup.bash
$ rosrun beginner_tutorials add_two_ints_client.py 4 5

注意,一定要执行 . devel/setup.bash ,因为新编译好的,需要重新执行,否则会有问题,如下报错

rosrun beginner_tutorials add_two_ints_client.py 4 5
Traceback (most recent call last):
  File "/root/catkin_ws/src/beginner_tutorials/scripts/add_two_ints_client.py", line 5, in
    from beginner_tutorials.srv import *
ImportError: No module named beginner_tutorials.srv
root@ben-virtual-machine:~/catkin_ws# rosrun beginner_tutorials add_two_ints_client.py
Traceback (most recent call last):
  File "/root/catkin_ws/src/beginner_tutorials/scripts/add_two_ints_client.py", line 5, in
    from beginner_tutorials.srv import *
ImportError: No module named beginner_tutorials.srv
 

正确结果如下

你可能感兴趣的:(Robot)