PX4學習筆記之uorb

PX4學習筆記之uorb
——添加topic以及消息的發佈、訂閱
說明
添加自定義 topic,簡單例子實現new topic 的publish和subscribe
平臺    
        Pixhawk、PX4原生固件Firmware
要求
       學會px4_simple_app例程,瞭解uorb通訊機制

       本文檔講詳細介紹如何在Firmware原生代碼添加我們自己的topic,然後如何進行消息的發佈和訂閱。

添加自定義topic
       首先在msg/文件夾下添加後綴名.msg的文件,文件名要和你將要添加的topic名稱一樣。可以參考其他現有的文件。例如,要聲明的topic爲myuorb_test,那文件名就應該爲myuorb_test.msg,在文件裏聲明你的存放數據的結構體成員變量(注意:僅僅是成員變量。如結構體爲struct myuorb_test_s{int r};,那麼.msg文件中就寫int r 就可以了)。
       然後在該文件夾下的CMakeLists.txt裏註冊新添加的.msg文件。註冊後,在編譯固件時會自動生成myuorb_test.h文件,該文件裏定義了數據結構體myuorb_test_s及ORB_DECLARE。
       最後在需要用到topic的地方包含相應頭文件就行了。#include <uORB/topics/myuorb_test.h>。
       這樣新的topic就添加成功了。
訂閱、發佈消息
      如果瞭解了uorb的話,這部分應該比較簡單。在此之前必須學會寫PX4應用程序,如果不會可以先學習px4_simple_app例程。下面直接給出我的測試程序。

myuorb_test.c

//include head files
#include <px4_config.h>
#include <px4_tasks.h>
#include <px4_posix.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <poll.h>
#include <string.h>
#include <uORB/uORB.h>
#include <uORB/topics/myuorb_test.h>
//ORB_DEFINE(myuorb_test,struct myuorb_test_s);
__EXPORT int myuorb_test_publisher_main(int argc,char *argv[]);
static orb_advert_t topic_handle;
int topics_handle;
int myuorb_test_publisher_main(int argc,char *argv[])
{
      PX4_INFO("uorb publishint test");
      topics_handle = orb_subscribe(ORB_ID(myuorb_test));
      //publish data
             //update data
             struct myuorb_test_s rd = {.r=rand()%10000};
             //advertise the topic
             topic_handle = orb_advertise(ORB_ID(myuorb_test),&rd);
             //publish data,update the topic
             orb_publish(ORB_ID(myuorb_test),topic_handle,&rd);
     PX4_WARN("the new data is: \t%d",rd.r);
    bool updated;
    struct myuorb_test_s rds;
    //subscribe a topic
    //check to see whether the topic has updated since the last                 //time we read it
    orb_check(topics_handle,&updated);
    if(updated)
    {
        //make a local copy of the updated data structure
        orb_copy(ORB_ID(myuorb_test),topics_handle,&rds);
        PX4_WARN("the updated data is: \t%d",rds.r);
    }
    else
    {
        PX4_WARN("data is not updated");
    }
        return 0;
}


       其中需要注意的是    topics_handle = orb_subscribe(ORB_ID(myuorb_test)) 這個訂閱消息的函數一定要寫在前面,不然訂閱不成功。其他部分註釋也比較詳細,就不再詳細說了。
測試
        使用make px4fmu-v2_default命令生成固件,使用make px4fmu-v2_default uoload命令上傳固件到飛控。
        啓動系統終端(快捷鍵Ctrl+Alt+T),輸入screen /dev/ttyACM0 57600 8N1連接飛控。也可以使Qgroundcontrol地面站的terminal終端,不過此功能新版本沒添加,需要使用舊版本(如v2.2)。注意:連接終端必須取下SD卡,否則電腦會接收到許多亂碼的數據導致終端崩潰。

        在NSH終端輸入myuorb_test,如果程序沒有問題,則會出現附件圖片上的信息。



        這樣就成功地發佈以及訂閱了消息。
        官方鏈接:http://dev.px4.io/advanced-uorb.html 
                          http://dev.px4.io/tutorial-hello-sky.html
                          http://dev.px4.io/advanced-system-console.html

        由於本人也是新手,如果有什麼寫的不正確的地方,歡迎大家提出一起交流心得。第一次發文章,格式等可能不太對,還請多包含。
        QQ:1322901615
        Email:[email protected]   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章