Matlab and ROS (1/2) --- server and client data communication (5)

0. Introduction

In the previous lectures, we talked about Message and Topic in Matlab. The main communication mechanisms supported by ROS are services. Services provide tighter coupling to the entire system by allowing requests and responses to be communicated. The service client sends a request message to the service server and waits for a response. The server will use the data in the request to construct a response message and send it back to the client. Every service has a type, which determines the structure of request and response messages.

1. ROS1 server and client

insert image description here

1.1 Create a server

In ROS1, if you want to create a simple service server and display "A service client is calling" when calling the service. Use the rosssvcserver command to create a service. Thereby specifying the service name and service message type. Then also define the callback function as exampleHelperROSEmptyCallback.

In order to obtain faster performance, generally we bring the structured format message type when setting rossvcsrve.

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

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

When listing all services in the ROS network, the new service /test can be seen.

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

Also use rosservice info to get more information about your service.

rosservice info /test

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

1.2 Create a client

Use a service client to request information from a ROS service server. To create a client, use rossvcclient with the service name as an argument.

Create a service client for the /test service we just created.

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

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

Create an empty request message for the service. Use the rosmessage function and pass the client as the first argument. This will create a service request function with the message type and format specified by the service.

testreq = rosmessage(testclient)

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

Make sure the service is connected to the client, and wait for the client to connect if necessary.

waitForServer(testclient,"Timeout",3)

When you want to get a response from the server, use the call function, which will call the service server and return the response. The service server you created earlier will return an empty response. Additionally, it will call the exampleHelperROSEmptyCallback function and display the string "A service client is calling". You can also define a Timeout parameter that indicates how long the client should wait for a response.

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

If the calling function above fails, an error occurs. If you wish to react to call failures using conditions, you can return status and statustext output from the calling function instead of errors. The status output indicates whether the call was successful, while statustext provides additional information. waitForServer can also return similar output.

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 Sum of two numbers

If we want to complete the function of summing two numbers, we can first use the existing service type roscpp_tutorials/TwoInts for this task. You can inspect the structure of the request and response messages by calling rosmsg show. The request contains two integers A and B, and the response contains their addition in Sum.

rosmsg show roscpp_tutorials/TwoIntsRequest

int64 A
int64 B

rosmsg show roscpp_tutorials/TwoIntsResponse

int64 Sum

Use this message type to create callback functions that serve servers and compute additions. For convenience, the exampleHelperROSSumCallback function has implemented this calculation. Specifies a function as a callback function.

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

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

To invoke a service server, a service client must be created. Note that this client can be created anywhere in the ROS network. For the purposes of this example, we will create a client for the /sum service in MATLAB.

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 server and client

…For details, please refer to Gu Yueju

Guess you like

Origin blog.csdn.net/lovely_yoshino/article/details/128308238