跳转至

server端

1. 创建package

catkin_create_pkg demo_service roscpp rospy std_msgs

2. 编写server节点代码

demo_service目录的demo_service下创建scripts文件夹,文件夹下创建server.py文件,代码如下

#!/usr/bin/env python

from rospy_tutorials.srv import AddTwoInts, AddTwoIntsResponse
import rospy


def handle_add_two_ints(req):
    print("Returning [%s + %s = %s]" % (req.a, req.b, (req.a + req.b)))
    return AddTwoIntsResponse(req.a + req.b)


if __name__ == "__main__":
    rospy.init_node('server')
    s = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints)
    print("Ready to add two ints.")
    rospy.spin()

3. 配置工程

CMakeLists.txt文件中做如下修改:

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

3. 构建工程

在工程目录下执行如下命令

catkin_make