进程应用之间的通信(例如将传感器应用的传感器数据发送到姿态滤波应用)是PX4软件架构的关键部分。进程(通常又被叫做节点(
ode))通过命名为总线(buses)进行消息交换的过程被称作订阅主题(topics)。在PX4中,一个订阅主题仅包含一个消息类型,比如vehicle_attitude订阅主题传输一个包含姿态结构(滚转、俯仰和偏航估计)的消息。节点可以在总线订阅主题上发布(publish)一个消息(发送数据)或者订阅(subscribe)一个总线订阅主题(接收数据)。应用并不知道在与谁通信,1个订阅主题可以有多个发布器和订阅器。这种设计模式阻止了锁定的问题(locki
gissues),在机器人领域非常常见。为了使这更为高效,通常在总线上只有一个消息,并且不会有队列。这种发布订阅机制由微对象请求处理器microobjectrequestbrokeruORB实现。
快速开始
这是一个简单但是完整的发布器(publisher)订阅器(subscriber)组合,发布器发布一个叫做ra
dom_i
teger的订阅主题,并使用随机的整数更新订阅主题。订阅器检查并打印这些更新。topich
123456
declarethetopicORB_DECLAREra
dom_i
tegerdefi
ethedatastructurethatwillbepublishedwheresubscribersca
seeitstructra
dom_i
teger_datai
tr
publisherc
12345
i
cludetopichcreatetopicmetadataORB_DEFINEra
dom_i
tegerfileha
dlethatwillbeusedforpublishi
gstatici
ttopic_ha
dle
f678910111213141516171819
i
ti
itge
eratethei
itialdataforfirstpublicatio
structra
dom_i
teger_datardrra
domadvertisethetopica
dmakethei
itialpublicatio
topic_ha
dleorb_advertiseORB_IDra
dom_i
tegerrdi
tupdate_topicge
eratea
ewra
dom
umberforpublicatio
structra
dom_i
teger_datardrra
dompublishthe
ewdatastructureorb_publishORB_IDra
dom_i
tegertopic_ha
dlerd
subscriberc
1234567891011121314151617181920
i
cludetopichfileha
dlethatwillbeusedforsubscribi
gstatici
ttopic_ha
dlei
ti
itsubscribetothetopictopic_ha
dleorb_subscribeORB_IDra
dom_i
tegervoidcheck_topicboolupdatedstructra
dom_i
teger_datardchecktoseewhetherthetopichasupdatedsi
cethelasttimewereaditorb_checktopic_ha
dleupdatedifupdatedmakealocalcopyoftheupdateddatastructureorb_copyORB_IDra
dom_i
tegertopic_ha
dlerdpri
tf