%% 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.
%
% 
%%
% Connect to a ROS network and set up the ROS parameter server.
rosinit
ptree = rosparam;

%%
% Set the |"/camera/rgb/image_raw/compressed/format"| parameter, and set up 
% a publisher for the |"/camera/rgb/image_raw/compressed"| topic.
set(ptree,"/camera/rgb/image_raw/compressed/format","jpeg")
pub = rospublisher("/camera/rgb/image_raw/compressed","sensor_msgs/CompressedImage");

%%
% Open the Simulink(R) model. This model checks the image format
% parameter and compares the value to a |uint8| cast character vector, 
% |uint8('jpeg')| using a MATLAB(R) Function block. The boolean output is
% fed to an AND operator with the IsNew output of a Subscribe block that gets the image
% off the network. If the parameter value is correct and a new message is
% received, the Subsystem "Process Image" is triggered.
%
% Run the model and use the buttons in the model to change the image format
% parameter and verify the |strcmp| function works. The |eq| output 
% should be 1 when the parameter is set to |'jpeg'|. While the model is
% running, it is expected that image messages are being published on the
% network.

open_system("rosImageFormatParameter")


%%
% Shut down the ROS network.
rosshutdown
%%
% The |strcmp| function in the MATLAB(R) Function block is defined as:
% 
%   function eq = strcmp(s1, n1, s2)
%   %#codegen
% 
%   % Convert to proper strings
%   string1 = char(s1(1:n1));
%   string2 = char(s2);
% 
%   eq = strcmp(string1, string2);

% Copyright 2019 The MathWorks, Inc.

