Call ROS Service in Simulink
Use the Call Service block to call a service on the ROS service server.
Connect to a ROS network.
rosinit
Launching ROS Core... Done in 0.3788 seconds. Initializing ROS master on http://172.29.205.71:58938. Initializing global node /matlab_global_node_47374 with NodeURI http://dcc066343glnxa64:39425/ and MasterURI http://localhost:58938.
Set up a roscpp_tutorials/TwoInts
service server message type and specify a example helper callback function. The call back function provided sums the A
and B
elements of a roscpp_tutorials/TwoIntsRequest
message. The service server must be set up before you can call a service client.
sumserver = rossvcserver('/sum','roscpp_tutorials/TwoInts',@exampleHelperROSSumCallback);
Open a Simulink® model with the Call Service block. Use the Blank Message block to output a request message with the roscpp_tutorials/TwoIntsRequest
message type. Populate the bus with two values to sum together.
open_system('ros_twoint_service_simulink_example.slx')
Run the model. The service call should return 0 in the Resp output as part of the response message. An error code of 0 indicates the service call was successful. You can ignore warnings about converting data types.
sim('ros_twoint_service_simulink_example.slx')
Warning: The property "A" in ROS message type "roscpp_tutorials/TwoIntsRequest" with datatype (int64) is converted to datatype (double) in the Simulink Bus. Suggested Actions: • Remove conversion of ROS message properties with "int64" datatype to "double" in the Simulink Bus. - Fix
Warning: The property "B" in ROS message type "roscpp_tutorials/TwoIntsRequest" with datatype (int64) is converted to datatype (double) in the Simulink Bus. Suggested Actions: • Remove conversion of ROS message properties with "int64" datatype to "double" in the Simulink Bus. - Fix
Warning: The property "Sum" in ROS message type "roscpp_tutorials/TwoIntsResponse" with datatype (int64) is converted to datatype (double) in the Simulink Bus. Suggested Actions: • Remove conversion of ROS message properties with "int64" datatype to "double" in the Simulink Bus. - Fix
Shut down the ROS network to disconnect.
rosshutdown
Shutting down global node /matlab_global_node_47374 with NodeURI http://dcc066343glnxa64:39425/ and MasterURI http://localhost:58938. Shutting down ROS master on http://172.29.205.71:58938.