%% ROS Demo Workshop
% Start by initializing with Matlab as an host

rosinit

%
% Create a sample network with different nodes

exampleHelperROSCreateSampleNetwork

%
% Get all nodes that are part of the network

rosnode list

%
% Get all topics that are available

rostopic list

% To see a complete list of all message types available for topics and services use the rosmsg list command
%
%% Create a subscriber link
% This uses the rossubscriber function

scanner = rossubscriber('scan')

%
%% Three ways of accessing the data
% 1. Get the next message that arrives

laserdata = receive(scanner, 5)

% 2. Get the latest data that was received (might be empty)

laserdata = scanner.LatestMessage

% 3, Set an asynchronous callback for incoming messages

scanner.NewMessageFcn = @(~,msg)disp(msg.Header.Seq);

%% Delete callback

scanner.NewMessageFcn = [];

%
%% Plot laser scan
% Some of the most common messages encountered in robotics (images, compressed images, laser scans, point clouds, ...) have convenience functions that read or visualize.

figure
plot(scanner.LatestMessage, 'MaximumRange',4)

%
%% Explore message structure and get message data

posesub = rossubscriber('/pose')
posedata = receive(posesub,10)
posedata.Linear
posedata.Angular
xpos = posedata.Linear.X
showdetails(posedata)

%
%% Set Message Data
% You can also set message property values. Create a message with type geometry_msgs/Twist

twist = rosmessage(rostype.geometry_msgs_Twist)

% Default initialized by 0. You can modify any of the properties of this message.

twist.Linear.Y = 5;
twist.Linear

%% Clean up
% Remove the sample nodes, publishers and subscribers from the ROS network.

exampleHelperROSShutDownSampleNetwork

% Shut down the ROS master and delete the global node.

rosshutdown

%%