1. 程式人生 > >帶有header的自定義ros meaasge

帶有header的自定義ros meaasge

最近由於需求,需要自定義一些topic格式進行資料的處理。基於常見的資料型別:int32 flosat32 bool 等可以定義出需要的訊息格式。 如:example.msg

int32  a
float32  b
bool  c

則在終端檢視訊息型別為example.msg定義的example_topic時

rostopic echo /example_topic

有類似如下顯示:

a: 1
b: 1.0
c: true
---
a: 1
b: 1.0
c: true
---
a: 1
b: 1.0
c: true
---
a: 1
b: 1.0
c: true
---

當顯示的頻率較大時,由於資料重新整理很快,對於資料的可讀性、分析和把握提出很高的要求。為了改善上述問題,可以在訊息前新增Header格式資料。 std_msgs/Header message :

# Standard metadata for higher-level stamped data types.
# This is generally used to communicate timestamped data 
# in a particular coordinate frame.
# 
# sequence ID: consecutively increasing ID 
uint32 seq
#Two-integer timestamp that is expressed as:
# * stamp.sec: seconds (stamp_secs) since epoch (in Python the variable is called 'secs')
# * stamp.nsec: nanoseconds since stamp_secs (in Python the variable is called 'nsecs')
# time-handling sugar is provided by the client library
time stamp
#Frame this data is associated with
# 0: no frame
# 1: global frame
string frame_id

原example.msg可以修改為:

Header header
int32  a
float32  b
bool  c

此時再次輸出example_topic時,得到類似下述顯示:

header:
  seq: 1
  stamp:
    secs: 1538813853
    nsecs: 968391761
  frame_id: frame_test
a: 1
b: 1.0
c: true
---
header:
  seq: 2
  stamp:
    secs: 1538813854
    nsecs:  67791173
  frame_id: frame_test
a: 1
b: 1.0
c: true
---
header:
  seq: 3
  stamp:
    secs: 1538813854
    nsecs: 168768708
  frame_id: frame_test
a: 1
b: 1.0
c: true
---

此時可以根據seq、secs和nsecs值可以更好的觀測和分析終端顯示的資料。 seq:topic當前釋出的次數 secs:topic釋出的系統時間 unit:秒 nsecs:topic釋出的系統時間 unit:納秒