ROS String Parameters

To use ROS string parameters in Simulink®, cast them to uint8 arrays. These examples show how to get, set, compare, and manipulate strings for ROS parameters. To run these examples, you must first set up a ROS network. Use rosinit.

Set String Parameter on ROS Network

To create your string parameter, use a Constant block and cast it to uint8 by specifying uint8(['my_string_parameter']) in Constant Value of the block mask. The string is passed into the Set Parameter block along with the extra input, Length, specified with a second Constant block. The Length refers to the maximum expected string length and is required for all string parameters. For more information, see the Set Parameter block.

Get ROS String Parameter and Compare to Specified String

You can compare string parameters to specified strings to validate settings or trigger subsystems. To get the parameter off the server, use the Get Parameter block. Then, use the MATLAB® Function block to compare the parameter to a uint8 string from a Constant block. This model checks to see if a previously set camera format parameter is named 'jpeg'.

The following code is used inside the stringCompare MATLAB Function block. The function compares each character of its two input strings to see if they match. The output is a single Boolean indicating whether the strings match.

function y = stringCompare(str1,str2)
minLength = min(length(str1),length(str2));
st1 = str1(1:minLength);
st2 = str2(1:minLength);
y = all(st1(:)==st2(:));

Check Image Encoding Parameter for ROS Image Message

This model shows how to access string parameters and use them to trigger subsystem operations. It gets an image format off the set up ROS parameter server. It is retrieved as a uint8 array that is compared using the strcmp MATLAB function block. When a new image is received from the Subscribe block and the format is uint8('jpeg'), it triggers the "Process Image" block to perform a task on the image data.

This model requires you to be connected to a ROS network. Call rosinit in the MATLAB® command window. The '/camera/rgb/image_raw/compressed/format' parameter must be set and the '/camera/rgb/image_raw/compressed' topic must have image messages being published. Use the buttons in the model to set the image format parameters to check the strcmp block. The eq output should be 1 when the parameter is set to 'jpeg'.

See Also


Related Topics