主要内容

在MATLAB中发布可变长度的嵌套ROS消息

This example shows how to work with complex ROS messages in MATLAB, such as messages with nested submessages and variable-length arrays.

Some ROS message types have nested submessages that are of different message types. Such nested ROS messages can be arrays whose length (number of elements) cannot be predetermined. Typical examples of such message types include:

  • geometry_msgs/姿势Array:此消息类型包含类型的姿势数组geometry_msgs/姿势。它通常用于在特定的时间步骤中向机器人发送一堆航路点。

  • nav_msgs/Path:此消息类型包含类型的姿势数组geometry_msgs/姿势Stamped。It is typically used for the output of motion planners that send a path for the robot to follow. The path is represented as a sequence of poses, each with its own header and timestamp.

在此示例中,您在单个主题上发送不同长度的姿势阵列,该主题发布类型的消息geometry_msgs/姿势Array

加载和查看航点

Load the source data, which contains waypoints of different lengths that need to be published on a single topic, for the robot to follow. The MAT filewayPointSets.matloads two sets of waypoints. These can be used to specify the pose array message. The waypoints are in the form of XYZ coordinates.

loadwayPointSets.mat;

使用该路点可视化两组plot3功能。请注意,这两组包含不同数量的路点。

figure plot3(wayPointSet1(:,1),wayPointSet1(:,2),wayPointSet1(:,3),“* - ”) 网格Xlabel("X")ylabel("Y") zlabel("Z") 标题(“ Waypoint Set 1”)

图包含一个轴对象。带有标题WayPoint集1的轴对象包含一个类型行的对象。

figure plot3(wayPointSet2(:,1),wayPointSet2(:,2),wayPointSet2(:,3),"*-r") 网格Xlabel("X")ylabel("Y") zlabel("Z") 标题(“ Waypoint Set 2”)

图包含一个轴对象。The axes object with title Waypoint Set 2 contains an object of type line.

Initialize and Configure ROS Network

利用石榴石create a ROS master in MATLAB and start a global node that is connected to the master.

石榴石
启动ROS Core ...在0.42665秒内完成。在http://172.29.206.152:55465初始化ROS Master。初始化带有Nodeuri的全局节点/Matlab_global_node_16105 http://dcc030405glnxa64:37551/and Masteruri http:// localhost:55465。

利用Rospublisher创建用于发送类型消息的ROS发布者geometry_msgs/姿势Array。Specify the name of the topic as/waypoints。添加一个ROS订户,该订户使用rossubscriber。利用messages in struct format for better efficiency.

pub = rospublisher("/waypoints",“ geometry_msgs/posearray”,"DataFormat",“结构”);sub = rossubscriber("/waypoints","DataFormat",“结构”);

利用rosmessagecreate an empty message based on the topic published by the publisher,pub

poseArrayMsg = rosmessage(pub);

Populate Message and Publish

指定与您要发布的Waypoint集相对应的工作区变量。然后,用geometry_msgs/姿势消息。从Waypoint集数据分配单个姿势消息元素的XYZ位置字段。继续添加新的单独的姿势消息元素,直到姿势数组消息包含所有航点集数据。因为结构消息不是处理的,因此在填充姿势数组时可以重复使用相同的结构。

%指定要发布的航点设置wayPointsToPublish = wayPointSet1;%填充姿势阵列消息poseMsg = rosmessage(“ geometry_msgs/pose”,"DataFormat",“结构”);fork = 1:size(wayPointsToPublish,1) poseMsg.Position.X = wayPointsToPublish(k,1); poseMsg.Position.Y = wayPointsToPublish(k,2); poseMsg.Position.Z = wayPointsToPublish(k,3); poseArrayMsg.Poses(k) = poseMsg;end

利用the发送将姿势数组消息发布到主题的功能/waypoints,使用ROS Publisher对象,pub

发送(pub,posearraymsg);暂停(0.5)

使用订阅者收到的姿势数组消息数据,使用LatestMessage属性Subscriber目的。利用horzcat将从接收的消息提取到结构阵列中的位置信息为了可视化的目的。利用plot3to visualize the waypoints as received by the subscriber. Note that the visualization matches that of the corresponding source waypoint data set.

receivedPoseArrayMsg1 = sub.LatestMessage; waypointPositions1 = horzcat(receivedPoseArrayMsg1.Poses.Position); figure plot3([waypointPositions1.X],[waypointPositions1.Y],[waypointPositions1.Z],“* - ”) 网格Xlabel("X")ylabel("Y") zlabel("Z") 标题(“通过ROS主题接收的Waypoint集1”)

图包含一个轴对象。The axes object with title Waypoint Set 1 Received Through ROS Topic contains an object of type line.

Now publish the second waypoint using the same procedure. Populate the pose array message with the new set of waypoint information.

%指定要发布的航点设置wayPointsToPublish = wayPointSet2;% Populate the Pose Array MessageposeMsg = rosmessage(“ geometry_msgs/pose”,"DataFormat",“结构”);fork = 1:size(wayPointsToPublish,1) poseMsg.Position.X = wayPointsToPublish(k,1); poseMsg.Position.Y = wayPointsToPublish(k,2); poseMsg.Position.Z = wayPointsToPublish(k,3); poseArrayMsg.Poses(k) = poseMsg;end

利用the发送function to publish the new pose array message to the same topic via the same ROS publisher object,pub

发送(pub,posearraymsg);暂停(0.5)

Visualize the pose array message data received by the subscriber by following the same procedure as before.

接收posearraymsg2 = sub.latestMessage;waypointpositions2 = vertcat(rectionposearraymsg2.2.poses.position);图3([waypointpositions2.x],[waypointpositions2.y],[waypointpositions2.z],"*-r") 网格Xlabel("X")ylabel("Y") zlabel("Z") 标题("Waypoint Set 2 Received Through ROS Topic")

图包含一个轴对象。The axes object with title Waypoint Set 2 Received Through ROS Topic contains an object of type line.

可视化匹配相应的源航路点数据集的可视化,这表明在单个主题上具有不同长度的两组姿势阵列的成功广播。利用rosshutdown关闭MATLAB中的ROS网络。这样做,关闭由ROS主掌握石榴石并删除全局节点。使用rosshutdown一旦您完成使用ROS网络,建议的过程是推荐的过程。

rosshutdown
使用Nodeuri http://dcc030405glnxa64:37551/and Masteruri http:// localhost:55465关闭Nodeuri http://dcc030405glnxa64:37551/http:// dcc030405glnxa64:55465。在http://172.29.206.152:55465上关闭ROS Master。

If the waypoint set data has orientation information, you can populate it in the quaternion orientation fields of the individual pose message elements before publishing. To publish messages of typenav_msgs/Path,use the same procedure, but specify the individual pose message elements asgeometry_msgs/姿势Stampedtype要发布任何其他类型的消息,请将适当的嵌套消息类型指定为单个数组元素,并确保源数据集包含您要发布的所需信息。

See Also

Work with ROS Messages in Simulink

ROS自定义消息支持金宝app