%% Compare ROS String Parameters
% On ROS networks, strings parameters are stored as a |uint8| array. When
% you get from string parameters from the server, they are returned as a 
% |char| array. In Simulink(R), they are cast as |uint8|, so you must
% use |uint8| character vectors when comparing to the ROS string parameters.
% You can use this comparison to trigger subsystems for larger models or validate
% settings for specific algorithms.

%%
% Connect to a ROS network. Set up the ROS Parameter tree.
rosinit
ptree = rosparam;

%%
% Set a ROS parameter, |/camera_format|, to a string value. You can use
% string scalars or character vectors. The value is stored as a |uint8| 
% array on the ROS parameter server and returned as |'jpeg'| in MATLAB(R).
set(ptree,"/camera_format","jpeg")
pause(1)
pvalue = get(ptree,"/camera_format")

%%
% Run the attached Simulink(R) model. This model checks to see if the previously set 
% camera format parameter is named 'jpeg'. To get the parameter off the server, use 
% the <docid:ros_ref.bvad31k Get Parameter> block. Then, compare the 
% parameter to a character vector cast as |uint8| from a Constant block, 
% using a MATLAB function block. An output of 1 means the parameters match.

open_system("rosStringParameterCompare")
sim("rosStringParameterCompare");
%%
% Shutdown ROS network.
rosshutdown

%%
% The |stringCompare| function is defined as:
% 
%   function y = stringCompare(str1,str2)
%   %#codegen
%   minLength = min(length(str1),length(str2));
%   st1 = str1(1:minLength);
%   st2 = str2(1:minLength);
%   y = all(st1(:)==st2(:));
%
%%
% Copyright 2019 The MathWorks, Inc.


