readMessages
설명
은 위에 나와 있는 입력 인수 세트 중 하나를 사용하여 구조체로 구성된 셀형 배열이나 메시지 객체로 구성된 셀형 배열로 데이터를 반환합니다. msgs
= readMessages(___,"DataFormat",Format)Format
은 "struct"
또는 "object"
로 지정합니다.
구조체를 사용하면 메시지 객체를 사용하는 것보다 훨씬 빠를 수 있으며, rosgenmsg
를 사용하여 메시지 정의를 불러오지 않고도 사용자 지정 메시지 데이터를 직접 읽을 수 있습니다.
참고
향후 릴리스에서 ROS Toolbox는 ROS 메시지에 대해 객체 대신 메시지 구조체를 사용할 예정입니다.
지금 메시지 구조체를 사용하려면 "DataFormat"
이름-값 인수를 "struct"
로 설정합니다. 자세한 내용은 ROS 메시지 구조체 항목을 참조하십시오.
예제
셀형 배열로 ROS 메시지 반환하기
rosbag을 읽고 토픽과 시간을 기준으로 필터링합니다.
bagselect = rosbag('ex_multiple_topics.bag'); bagselect2 = select(bagselect,'Time',... [bagselect.StartTime bagselect.StartTime + 1],'Topic','/odom');
모든 메시지를 셀형 배열로 반환합니다.
allMsgs = readMessages(bagselect2);
처음 10개의 메시지를 셀형 배열로 반환합니다.
firstMsgs = readMessages(bagselect2,1:10);
rosbag에서 구조체로 메시지 읽기
rosbag을 불러옵니다.
bag = rosbag('ros_turtlesim.bag');
특정 토픽을 선택합니다.
bSel = select(bag,'Topic','/turtle1/pose');
메시지를 구조체로 읽습니다. 메시지를 읽을 때 DataFormat
이름-값 쌍을 지정합니다. 반환된 구조체의 셀형 배열에서 첫 번째 구조체를 검사합니다.
msgStructs = readMessages(bSel,'DataFormat','struct'); msgStructs{1}
ans = struct with fields:
MessageType: 'turtlesim/Pose'
X: 5.5016
Y: 6.3965
Theta: 4.5377
LinearVelocity: 1
AngularVelocity: 0
메시지에서 xy 점을 추출하고 로봇 궤적을 플로팅합니다.
구조체에서 모든 X
필드와 Y
필드를 추출하기 위해 cellfun
을 사용합니다. 이 필드는 rosbag을 기록할 동안의 로봇의 xy 위치를 나타냅니다.
xPoints = cellfun(@(m) double(m.X),msgStructs); yPoints = cellfun(@(m) double(m.Y),msgStructs); plot(xPoints,yPoints)
rosbagreader
객체를 사용하여 rosbag 선택 만들기
rosbag 로그 파일을 불러오고 선택 기준에 따라 특정 메시지를 구문 분석합니다.
rosbag 로그 파일에 있는 모든 메시지의 rosbagreader
객체를 만듭니다.
bagMsgs = rosbagreader("ex_multiple_topics.bag")
bagMsgs = rosbagreader with properties: FilePath: '/tmp/Bdoc24a_2511836_3320146/tpd9b150c7/ros-ex26633229/ex_multiple_topics.bag' StartTime: 201.3400 EndTime: 321.3400 NumMessages: 36963 AvailableTopics: [4x3 table] AvailableFrames: {0x1 cell} MessageList: [36963x4 table]
타임스탬프와 토픽을 기준으로 메시지의 일부를 선택합니다.
bagMsgs2 = select(bagMsgs,... Time=[bagMsgs.StartTime bagMsgs.StartTime + 1],... Topic='/odom')
bagMsgs2 = rosbagreader with properties: FilePath: '/tmp/Bdoc24a_2511836_3320146/tpd9b150c7/ros-ex26633229/ex_multiple_topics.bag' StartTime: 201.3400 EndTime: 202.3200 NumMessages: 99 AvailableTopics: [1x3 table] AvailableFrames: {0x1 cell} MessageList: [99x4 table]
선택한 부분에서 셀형 배열로 메시지를 가져옵니다.
msgs = readMessages(bagMsgs2)
msgs=99×1 cell array
{1x1 Odometry}
{1x1 Odometry}
{1x1 Odometry}
{1x1 Odometry}
{1x1 Odometry}
{1x1 Odometry}
{1x1 Odometry}
{1x1 Odometry}
{1x1 Odometry}
{1x1 Odometry}
{1x1 Odometry}
{1x1 Odometry}
{1x1 Odometry}
{1x1 Odometry}
{1x1 Odometry}
{1x1 Odometry}
⋮
특정 메시지 속성을 시계열로 반환합니다.
ts = timeseries(bagMsgs2,... 'Pose.Pose.Position.X', ... 'Twist.Twist.Angular.Y')
timeseries Timeseries contains duplicate times. Common Properties: Name: '/odom Properties' Time: [99x1 double] TimeInfo: tsdata.timemetadata Data: [99x2 double] DataInfo: tsdata.datametadata
입력 인수
bag
— rosbag의 메시지 인덱스
BagSelection
객체 | rosbagreader
객체
rosbag의 메시지 인덱스로, BagSelection
객체 또는 rosbagreader
객체로 지정됩니다.
rows
— BagSelection
객체 또는 rosbagreader
객체의 행
요소를 n개 가진 벡터
BagSelection
객체 또는 rosbagreader
객체의 행으로, 요소를 n개 가진 벡터로 지정됩니다. 여기서 n은 메시지를 가져올 행의 개수입니다. 벡터의 각 요소는 bag의 번호가 매겨진 메시지에 해당합니다. 행의 범위는 [1, bag.NumMessage
]입니다.
출력 인수
msgs
— ROS 메시지 데이터
객체 | 메시지 객체로 구성된 셀형 배열 | 구조체로 구성된 셀형 배열
ROS 메시지 데이터로, 객체로 반환되거나 메시지 객체로 구성된 셀형 배열 또는 구조체로 구성된 셀형 배열로 반환됩니다. 데이터는 rosbag
을 사용하여 만들어진 BagSelection
객체 또는 rosbagreader
객체에서 가져옵니다.
메시지를 구조체로 구성된 셀형 배열로 가져오려면 rosbag 함수에서 "DataFormat","struct"
를 지정해야 합니다. 구조체를 사용하면 메시지 객체를 사용하는 것보다 훨씬 빠를 수 있으며, rosgenmsg
를 사용하여 메시지 정의를 불러오지 않고도 사용자 지정 메시지 데이터를 직접 읽을 수 있습니다.
버전 내역
R2019b에 개발됨R2021a: ROS 메시지 구조체
이제 메시지를 메시지 객체 속성과 일치하는 필드를 포함하는 구조체로 만들 수 있습니다. 구조체를 사용하면 일반적으로 ROS 메시지를 만들고, 업데이트하고, 사용하는 것이 훨씬 수월해지지만, 설정을 마친 메시지 필드에 대해서는 더 이상 유효성 검사가 이루어지지 않습니다. 구조체의 메시지 유형 및 대응하는 필드 값은 네트워크를 통해 전송될 때 유효성 검사가 이루어집니다.
ROS 메시지를 구조체로 사용하려면, publisher, subscriber 또는 기타 ROS 객체를 만들 때 "DataFormat"
이름-값 인수를 사용합니다. 이러한 객체에서 만들어진 모든 메시지는 구조체를 사용합니다.
pub = rospublisher("/scan","sensor_msgs/LaserScan","DataFormat","struct") msg = rosmessage(pub)
메시지를 구조체로 직접 만들 수도 있지만 publisher, subscriber 또는 기타 ROS 객체에 대한 데이터형을 "struct"
로 지정해야 합니다. ROS 객체는 여전히 메시지 객체를 기본적으로 사용합니다.
msg = rosmessage("/scan","sensor_msgs/LaserScan","DataFormat","struct") ... pub = rospublisher("/scan","sensor_msgs/LaserScan","DataFormat","struct")
향후 릴리스에서 ROS 메시지는 기본적으로 구조체를 사용하고 ROS 메시지 객체는 제거될 예정입니다.
자세한 내용은 Improve Performance of ROS Using Message Structures 항목을 참조하십시오.
참고 항목
select
| rosbag
| timeseries
| rosbagreader
MATLAB 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)