function jntPos = exampleHelperJointMsgToStruct(robot,jntState)
%exampleHelperJointMsgToStruct The order of body names in the received 
%   jntState message is different from that the pr2 model in MATLAB would
%   expect. This function is to provide a convenient conversion.

% Copyright 2016 The MathWorks, Inc.

jntPos = robot.homeConfiguration;

for i = 1:length(jntState.Name)
    idx = strcmp({jntPos.JointName}, jntState.Name{i});
    jntPos(idx).JointPosition = jntState.Position(i);
end

for i = 1:robot.NumBodies
    joint = robot.Bodies{i}.Joint;
    if ~strcmp(joint.Type,'fixed')
        idx = strcmp({jntPos.JointName}, joint.Name);
        jntPos(idx).JointPosition = max(min(jntPos(idx).JointPosition, joint.PositionLimits(2)), joint.PositionLimits(1));
    end
end
end