Main Content

readMessages

rosbag에서 메시지 읽기

R2019b 이후

설명

예제

msgs = readMessages(bag)BagSelection 객체 또는 rosbagreader 객체 bag의 모든 메시지에서 데이터를 반환합니다. 메시지는 메시지로 구성된 셀형 배열로 반환됩니다. BagSelection 객체를 가져오려면 rosbag 함수를 사용합니다.

예제

msgs = readMessages(bag,rows)rows로 지정된 행의 메시지에서 데이터를 반환합니다. 행의 범위는 [1, bag.NumMessages]입니다.

예제

msgs = readMessages(___,"DataFormat",Format)은 위에 나와 있는 입력 인수 세트 중 하나를 사용하여 구조체로 구성된 셀형 배열이나 메시지 객체로 구성된 셀형 배열로 데이터를 반환합니다. Format"struct" 또는 "object"로 지정합니다.

구조체를 사용하면 메시지 객체를 사용하는 것보다 훨씬 빠를 수 있으며, rosgenmsg를 사용하여 메시지 정의를 불러오지 않고도 사용자 지정 메시지 데이터를 직접 읽을 수 있습니다.

참고

향후 릴리스에서 ROS Toolbox는 ROS 메시지에 대해 객체 대신 메시지 구조체를 사용할 예정입니다.

지금 메시지 구조체를 사용하려면 "DataFormat" 이름-값 인수를 "struct"로 설정합니다. 자세한 내용은 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을 불러옵니다.

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)

Figure contains an axes object. The axes object contains an object of type line.

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

입력 인수

모두 축소

rosbag의 메시지 인덱스로, BagSelection 객체 또는 rosbagreader 객체로 지정됩니다.

BagSelection 객체 또는 rosbagreader 객체의 행으로, 요소를 n개 가진 벡터로 지정됩니다. 여기서 n은 메시지를 가져올 행의 개수입니다. 벡터의 각 요소는 bag의 번호가 매겨진 메시지에 해당합니다. 행의 범위는 [1, bag.NumMessage]입니다.

출력 인수

모두 축소

ROS 메시지 데이터로, 객체로 반환되거나 메시지 객체로 구성된 셀형 배열 또는 구조체로 구성된 셀형 배열로 반환됩니다. 데이터는 rosbag을 사용하여 만들어진 BagSelection 객체 또는 rosbagreader 객체에서 가져옵니다.

메시지를 구조체로 구성된 셀형 배열로 가져오려면 rosbag 함수에서 "DataFormat","struct"를 지정해야 합니다. 구조체를 사용하면 메시지 객체를 사용하는 것보다 훨씬 빠를 수 있으며, rosgenmsg를 사용하여 메시지 정의를 불러오지 않고도 사용자 지정 메시지 데이터를 직접 읽을 수 있습니다.

버전 내역

R2019b에 개발됨

모두 확장