0. 简介

在前几讲我们讲了Matlab中的Message以及Topic的相关知识。而ROS主要支持的通信机制还有服务这一类。服务通过允许请求以及响应的通信方式,来给整个系统完成更紧密的耦合。服务客户端向服务服务器发送请求消息并等待响应。服务器将使用请求中的数据构造响应消息并将其发送回客户端。每个服务都有一个类型,该类型决定请求和响应消息的结构。

1. ROS1的服务端和客户端

1.1 创建服务端

在ROS1当中,如果希望创建一个简单的服务服务器,并在调用服务时能够显示“A service client is calling”。使用rosssvcserver命令创建服务。从而指定服务名称和服务消息类型。然后还要将回调函数定义为exampleHelperROSEmptyCallback。

为了获得更快的性能,一般我们在设置rossvcsrve时候带上结构格式消息类型。

testserver = rossvcserver("/test","std_srvs/Empty",@exampleHelperROSEmptyCallback,"DataFormat","struct")

testserver =
ServiceServer with properties:
ServiceType: ‘std_srvs/Empty’
ServiceName: ‘/test’
NewRequestFcn: @exampleHelperROSEmptyCallback
DataFormat: ‘struct’

当列出ROS网络中的所有服务时,可以看到新服务/test。

rosservice list

/add
/matlab_global_node_55791/get_loggers
/matlab_global_node_55791/set_logger_level
/node_1/get_loggers
/node_1/set_logger_level
/node_2/get_loggers
/node_2/set_logger_level
/node_3/get_loggers
/node_3/set_logger_level
/reply
/test

此外还可以使用rosservice info获取有关您的服务的更多信息。

rosservice info /test

Node: /matlab_global_node_55791
URI: rosrpc://ah-csalzber:51639
Type: std_srvs/Empty
Args: MessageType

1.2 创建客户端

使用服务客户机从ROS服务服务器请求信息。要创建客户端,请使用带有服务名称的rossvcclient作为参数。

为我们刚刚创建的/test服务创建一个服务客户机。

testclient = rossvcclient("/test","DataFormat","struct")

testclient =
ServiceClient with properties:
ServiceType: ‘std_srvs/Empty’
ServiceName: ‘/test’
DataFormat: ‘struct’

为服务创建一个空请求消息。使用rosmessage函数并将客户端作为第一个参数传递。这将创建一个具有服务指定的消息类型和格式的服务请求函数。

testreq = rosmessage(testclient)

testreq = struct with fields:
MessageType: ‘std_srvs/EmptyRequest’

确保服务已连接到客户端,并在必要时等待客户端连接。

waitForServer(testclient,"Timeout",3)

当您希望从服务器获得响应时,请使用call函数,该函数将调用服务服务器并返回响应。您之前创建的服务服务器将返回一个空响应。此外,它将调用exampleHelperROSEmptyCallback函数并显示字符串“A service client is calling”。您还可以定义一个Timeout参数,该参数指示客户机应该等待响应的时间。

testresp = call(testclient,testreq,"Timeout",3);

如果上面的调用函数失败,就会出错。如果希望使用条件对调用失败做出反应,则可以从调用函数返回状态和statustext输出,而不是错误。状态输出指示调用是否成功,而statustext提供其他信息。waitForServer也可以返回类似的输出。

numCallFailures = 0;
[testresp,status,statustext] = call(testclient,testreq,"Timeout",3);
if ~status
    numCallFailures = numCallFailues + 1;
    fprintf("Call failure number %d. Error cause: %s\n",numCallFailures,statustext)
else
    disp(testresp)
end
MessageType: 'std_srvs/EmptyResponse'

1.3 两数之和

如果说我们想要完成两数求和这样的功能,我们可以先将现有的服务类型roscpp_tutorials/TwoInts用于此任务。您可以通过调用rosmsg show来检查请求和响应消息的结构。请求包含两个整数A和B,响应包含它们在Sum中的加法。

rosmsg show roscpp_tutorials/TwoIntsRequest

int64 A
int64 B

rosmsg show roscpp_tutorials/TwoIntsResponse

int64 Sum

使用此消息类型创建服务服务器和计算加法的回调函数。为了方便起见,exampleHelperROSSumCallback函数已经实现了这个计算。将函数指定为回调函数。

sumserver = rossvcserver("/sum","roscpp_tutorials/TwoInts",@exampleHelperROSSumCallback,"DataFormat","struct")

sumserver =
ServiceServer with properties:
ServiceType: ‘roscpp_tutorials/TwoInts’
ServiceName: ‘/sum’
NewRequestFcn: @exampleHelperROSSumCallback
DataFormat: ‘struct’

要调用服务服务器,必须创建一个服务客户机。注意,这个客户端可以在ROS网络中的任何地方创建。为了本例的目的,我们将在MATLAB中为/sum服务创建一个客户端。

sumclient = rossvcclient("/sum","DataFormat","struct")
sumreq = rosmessage(sumclient);
sumreq.A = int64(2);
sumreq.B = int64(1)
if isServerAvailable(sumclient)
    sumresp = call(sumclient,sumreq,"Timeout",3)
else
    error("Service server not available on network")
end

sumresp = struct with fields:
MessageType: ‘roscpp_tutorials/TwoIntsResponse’
Sum: 3

2. ROS2的服务端和客户端

2.1 创建服务端

与我们上一章讲的Topic类似,ROS2的服务其函数也不太一致,如果说希望创建一个简单的服务服务器,在调用服务时显示“A service client is calling”。就需要使用ros2svcserver命令创建服务。指定服务名称、服务消息类型和附加服务器的节点。还要将回调函数定义为exampleHelperROSEmptyCallback。

testserver = ros2svcserver(node_1,"/service_example","std_srvs/Empty",@exampleHelperROS2EmptyCallback)

当列出ROS网络中的所有服务时,可以看到您的新服务/test

ros2("service","list");

/_matlab_introspec0_rmw_fastrtps_cpp/describe_parameters
/_matlab_introspec
0_rmw_fastrtps_cpp/get_parameter_types
/_matlab_introspec0_rmw_fastrtps_cpp/get_parameters
/_matlab_introspec
0_rmw_fastrtps_cpp/list_parameters
/_matlab_introspec0_rmw_fastrtps_cpp/set_parameters
/_matlab_introspec
0_rmw_fastrtps_cpp/set_parameters_atomically
/node_1/describe_parameters
/node_1/get_parameter_types
/node_1/get_parameters
/node_1/list_parameters
/node_1/set_parameters
/node_1/set_parameters_atomically
/node_2/describe_parameters
/node_2/get_parameter_types
/node_2/get_parameters
/node_2/list_parameters
/node_2/set_parameters
/node_2/set_parameters_atomically
/node_3/describe_parameters
/node_3/get_parameter_types
/node_3/get_parameters
/node_3/list_parameters
/node_3/set_parameters
/node_3/set_parameters_atomically
/service_example

2.2 创建客户端

为刚刚创建的/test服务创建一个服务客户端,并将其附加到不同的节点

testclient = ros2svcclient(node_2,"/service_example","std_srvs/Empty")

testclient =
ros2svcclient with properties:
ServiceType: ‘std_srvs/Empty’
ServiceName: ‘/service_example’
History: ‘keeplast’
Depth: 10
Reliability: ‘reliable’
Durability: ‘volatile’

然后下面的一些操作和设置和ROS1类似,这里就不详细阐述了

waitForServer(testclient,"Timeout",3)
testresp = call(testclient,testreq,"Timeout",3)

如果上面的调用函数失败,就会出错。如果希望使用条件对调用失败做出反应,则可以从调用函数返回状态和statustext输出,而不是错误。状态输出指示调用是否成功,而statustext提供其他信息。waitForServer也可以返回类似的输出。

numCallFailures = 0;
[testresp,status,statustext] = call(testclient,testreq,"Timeout",3);
if ~status
    numCallFailures = numCallFailues + 1;
    fprintf("Call failure number %d. Error cause: %s\n",numCallFailures,statustext)
else
    disp(testresp)
end

MessageType: ‘std_srvs/EmptyResponse’

https://ww2.mathworks.cn/help/ros/ug/call-and-provide-ros-services.html

https://ww2.mathworks.cn/help/ros/ug/call-and-provide-ros2-services.html