好的,我们将分别为服务端和客户端使用不同的包名,例如服务端使用robot_server_package,客户端使用robot_client_package

一、服务端节点配置(robot_server)

创建包和定义服务类型

  1. 创建一个名为robot_server_package的包,并在srv目录下创建一个名为AddTwoInts.srv的文件:

    1
    2
    3
    4
    int64 a
    int64 b
    ---
    int64 sum
  2. 确保在CMakeLists.txtpackage.xml中正确配置,以便生成服务代码。

    CMakeLists.txt

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    cmake_minimum_required(VERSION 3.0.2)
    project(robot_server_package)

    find_package(catkin REQUIRED COMPONENTS
    rospy
    std_msgs
    message_generation
    )

    add_service_files(
    FILES
    AddTwoInts.srv
    )

    generate_messages(
    DEPENDENCIES
    std_msgs
    )

    catkin_package(
    CATKIN_DEPENDS rospy std_msgs message_runtime
    )

    include_directories(
    ${catkin_INCLUDE_DIRS}
    )

    package.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <package format="2">
    <name>robot_server_package</name>
    <version>0.0.0</version>
    <description>The robot_server_package</description>

    <maintainer email="your_email@example.com">Your Name</maintainer>

    <license>BSD</license>

    <buildtool_depend>catkin</buildtool_depend>
    <build_depend>rospy</build_depend>
    <build_depend>std_msgs</build_depend>
    <build_depend>message_generation</build_depend>
    <exec_depend>rospy</exec_depend>
    <exec_depend>std_msgs</exec_depend>
    <exec_depend>message_runtime</exec_depend>

    <export>
    </export>
    </package>
  3. 运行catkin_make生成消息和服务代码。

编写服务端节点

编写服务端节点add_two_ints_server.py,并将其放在robot_server_package/scripts目录下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env python

from robot_server_package.srv import AddTwoInts, AddTwoIntsResponse
import rospy

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

def add_two_ints_server():
rospy.init_node('add_two_ints_server')
s = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints)
rospy.loginfo("Ready to add two ints.")
rospy.spin()

if __name__ == "__main__":
add_two_ints_server()

二、客户端节点配置(robot_client)

创建包和定义服务类型

  1. 创建一个名为robot_client_package的包,并在srv目录下创建一个名为AddTwoInts.srv的文件,与服务端的服务类型相同:

    1
    2
    3
    4
    int64 a
    int64 b
    ---
    int64 sum
  2. 确保在CMakeLists.txtpackage.xml中正确配置,以便生成服务代码。

    CMakeLists.txt

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    cmake_minimum_required(VERSION 3.0.2)
    project(robot_client_package)

    find_package(catkin REQUIRED COMPONENTS
    rospy
    std_msgs
    message_generation
    )

    add_service_files(
    FILES
    AddTwoInts.srv
    )

    generate_messages(
    DEPENDENCIES
    std_msgs
    )

    catkin_package(
    CATKIN_DEPENDS rospy std_msgs message_runtime
    )

    include_directories(
    ${catkin_INCLUDE_DIRS}
    )

    package.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <package format="2">
    <name>robot_client_package</name>
    <version>0.0.0</version>
    <description>The robot_client_package</description>

    <maintainer email="your_email@example.com">Your Name</maintainer>

    <license>BSD</license>

    <buildtool_depend>catkin</buildtool_depend>
    <build_depend>rospy</build_depend>
    <build_depend>std_msgs</build_depend>
    <build_depend>message_generation</build_depend>
    <exec_depend>rospy</exec_depend>
    <exec_depend>std_msgs</exec_depend>
    <exec_depend>message_runtime</exec_depend>

    <export>
    </export>
    </package>
  3. 运行catkin_make生成消息和服务代码。

编写客户端节点

编写客户端节点add_two_ints_client.py,并将其放在robot_client_package/scripts目录下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python

import sys
import rospy
from robot_client_package.srv import AddTwoInts

def add_two_ints_client(x, y):
rospy.wait_for_service('add_two_ints')
try:
add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
resp = add_two_ints(x, y)
return resp.sum
except rospy.ServiceException as e:
rospy.logerr("Service call failed: %s" % e)

if __name__ == "__main__":
if len(sys.argv) == 3:
x = int(sys.argv[1])
y = int(sys.argv[2])
else:
print("Usage: %s [x y]" % sys.argv[0])
sys.exit(1)
print("Requesting %s+%s" % (x, y))
print("%s + %s = %s" % (x, y, add_two_ints_client(x, y)))

三、运行服务和客户端

  1. 在两台机器上设置ROS环境

确保/etc/hosts文件包含彼此的IP地址和主机名。例如,在robot_serverrobot_client机器上的/etc/hosts文件应该包含:

1
2
192.168.1.2 robot_server
192.168.1.3 robot_client

robot_server上设置环境变量:

1
2
export ROS_MASTER_URI=http://robot_server:11311
export ROS_IP=robot_server

robot_client上设置环境变量:

1
2
export ROS_MASTER_URI=http://robot_server:11311
export ROS_IP=robot_client
  1. 启动ROS master

robot_server上运行:

1
roscore

3.robot_server上运行服务端节点

确保脚本是可执行的:

1
chmod +x ~/catkin_ws/src/robot_server_package/scripts/add_two_ints_server.py

运行服务端节点:

1
rosrun robot_server_package add_two_ints_server.py
  1. robot_client上运行客户端节点

确保脚本是可执行的:

1
chmod +x ~/catkin_ws/src/robot_client_package/scripts/add_two_ints_client.py

运行客户端节点:

1
rosrun robot_client_package add_two_ints_client.py 2 3

如果配置正确,你应该会在robot_server上看到服务端处理请求的输出,并在robot_client上看到客户端请求的结果。

这样,你就可以在不同的机器上运行ROS节点,并调用共享的服务。