生活随笔
收集整理的這篇文章主要介紹了
pixhawk/px4如何获取及使用传感器数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
pixhawk/px4如何獲取及使用傳感器數據
第一步:讀取傳感器數據
上一篇博文已經介紹了如何給pixhawk/px4創建一個應用程序,現在我們在上一個應用程序的基礎上使用傳感器數據。
應用程序為了實現一些有用的功能,需要訂閱輸入和發布輸出(比如電機或舵機輸出的命令)。注意在這里,PX4平臺真正的硬件抽象的概念在這里體現---當硬件平臺或者傳感器升級更新,你完全不需要和傳感器驅動打交道,也不需要更新你的應用程序或者更新傳感器驅動程序。
在PX4中,應用程序間發送信息的獨立通道叫做“topics”,在本教程中,我們關心的話題是“多傳感器間的uORB消息機制”(sensor_combinedtopic)。這些消息機制使得整個系統能夠同步傳感器數據。
訂閱一個消息十分快速簡潔:
[cpp] view plaincopy
#include<uORB/topics/sensor_combined.h>????..????int sensor_sub_fd?=?orb_subscribe(ORB_ID(sensor_combined));??
“sensor_sub_fd”是一個消息句柄,能夠十分有效處理新數據的到來之前的延遲等待。當前線程會休眠,直到新的傳感器數據到來時,被調度器喚醒,并且在等待時不需要占用任何的CPU時間。為了實現這個功能,我們使用poll()函數即POSIX系統調用。
在消息讀取中加入“poll()”機制,完整程序如下
[cpp] view plaincopy
????????????????????????????????????????????#include?<px4_config.h>??#include?<px4_tasks.h>??#include?<px4_posix.h>??#include?<unistd.h>??#include?<stdio.h>??#include?<poll.h>??#include?<string.h>????#include?<uORB/uORB.h>??#include?<uORB/topics/sensor_combined.h>??#include?<uORB/topics/vehicle_attitude.h>????__EXPORT?int?px4_simple_app_main(int?argc,?char?*argv[]);????int?px4_simple_app_main(int?argc,?char?*argv[])??{????????????int?sensor_sub_fd?=?orb_subscribe(ORB_ID(sensor_combined));??????orb_set_interval(sensor_sub_fd,?1000);??????????????????px4_pollfd_struct_t?fds[]?=?{??????????{?.fd?=?sensor_sub_fd,???.events?=?POLLIN?},??????????????????};????????int?error_counter?=?0;????????for?(int?i?=?0;?i?<?5;?i++)?{????????????????????int?poll_ret?=?px4_poll(fds,?1,?1000);??????????????????????if?(poll_ret?==?0)?{????????????????????????????PX4_ERR("[px4_simple_app]?Got?no?data?within?a?second");????????????}?else?if?(poll_ret?<?0)?{????????????????????????????if?(error_counter?<?10?||?error_counter?%?50?==?0)?{????????????????????????????????????PX4_ERR("[px4_simple_app]?ERROR?return?value?from?poll():?%d"??????????????????????,?poll_ret);??????????????}????????????????error_counter++;????????????}?else?{????????????????if?(fds[0].revents?&?POLLIN)?{????????????????????????????????????struct?sensor_combined_s?raw;????????????????????????????????????orb_copy(ORB_ID(sensor_combined),?sensor_sub_fd,?&raw);??????????????????PX4_WARN("[px4_simple_app]?Accelerometer:\t%8.4f\t%8.4f\t%8.4f",???????????????????????(double)raw.accelerometer_m_s2[0],???????????????????????(double)raw.accelerometer_m_s2[1],???????????????????????(double)raw.accelerometer_m_s2[2]);????????????????}????????????}??????}??????????PX4_INFO("exiting");??????return?0;??}??
編譯應用程序:
make
注意,
make命令要在
Firware目錄下執行,
因為Makfile文件在該目錄下,結果如下圖所示。
第二步:測試uORB消息讀取機制
最后一步,運行你的應用程序,并且切換到后臺應用。注意這需要把程序重新編譯上傳到飛控板,拔掉sd卡,然后連接nsh控制臺,運行以下命令。
[plain] view plaincopy
px4_simple_app &??
你的應用程序會向串口輸出當前傳感器的值:
[px4_simple_app]
Accelerometer: 0.0483 0.0821 0.0332
[px4_simple_app]
Accelerometer: 0.0486 0.0820 0.0336
[px4_simple_app]
Accelerometer: 0.0487 0.0819 0.0327
[px4_simple_app]
Accelerometer: 0.0482 0.0818 0.0323
[px4_simple_app]
Accelerometer: 0.0482 0.0827 0.0331
[px4_simple_app]
Accelerometer: 0.0489 0.0804 0.0328
我測試的結果如下圖所示
它會在輸出5次數據后退出。接下來會介紹如何編寫一個能通過命令行控制的后臺應用。
第三步:打印數據
為了能獲取到計算后的數據,下一步就是“打印”這些結果。如果我們知道某一個消息是使用mavlink協議轉發給地面控制站的,我們可以通過這個消息去查看結果。例如我們通過這個方法來獲得高度信息的消息。
接口非常簡單--初始化消息的結構體,然后廣播這條消息:
#include
<uORB/topics/vehicle_attitude.h>
..
/*
advertise attitude topic */
struct
vehicle_attitude_s att;
memset(&att,
0, sizeof(att));
orb_advert_t
att_pub_fd = orb_advertise(ORB_ID(vehicle_attitude), &att);
在主循環中,當消息準備好時,打印這條消息。
orb_publish(ORB_ID(vehicle_attitude),
att_pub_fd, &att);
修改后的完整的示例代碼如下:
[cpp] view plaincopy
????????????????????????????????????????????#include?<px4_config.h>??#include?<px4_tasks.h>??#include?<px4_posix.h>??#include?<unistd.h>??#include?<stdio.h>??#include?<poll.h>??#include?<string.h>????#include?<uORB/uORB.h>??#include?<uORB/topics/sensor_combined.h>??#include?<uORB/topics/vehicle_attitude.h>????__EXPORT?int?px4_simple_app_main(int?argc,?char?*argv[]);????int?px4_simple_app_main(int?argc,?char?*argv[])??{????????????int?sensor_sub_fd?=?orb_subscribe(ORB_ID(sensor_combined));??????orb_set_interval(sensor_sub_fd,?1000);??????????????struct?vehicle_attitude_s?att;??????memset(&att,?0,?sizeof(att));??????orb_advert_t?att_pub?=?orb_advertise(ORB_ID(vehicle_attitude),?&att);??????????????px4_pollfd_struct_t?fds[]?=?{??????????{?.fd?=?sensor_sub_fd,???.events?=?POLLIN?},??????????????????};????????int?error_counter?=?0;????????for?(int?i?=?0;?i?<?5;?i++)?{????????????????????int?poll_ret?=?px4_poll(fds,?1,?1000);??????????????????????if?(poll_ret?==?0)?{????????????????????????????PX4_ERR("[px4_simple_app]?Got?no?data?within?a?second");????????????}?else?if?(poll_ret?<?0)?{????????????????????????????if?(error_counter?<?10?||?error_counter?%?50?==?0)?{????????????????????????????????????PX4_ERR("[px4_simple_app]?ERROR?return?value?from?poll():?%d"??????????????????????,?poll_ret);??????????????}????????????????error_counter++;????????????}?else?{????????????????if?(fds[0].revents?&?POLLIN)?{????????????????????????????????????struct?sensor_combined_s?raw;????????????????????????????????????orb_copy(ORB_ID(sensor_combined),?sensor_sub_fd,?&raw);??????????????????PX4_WARN("[px4_simple_app]?Accelerometer:\t%8.4f\t%8.4f\t%8.4f",???????????????????????(double)raw.accelerometer_m_s2[0],???????????????????????(double)raw.accelerometer_m_s2[1],???????????????????????(double)raw.accelerometer_m_s2[2]);??????????????????????????????????????att.roll?=?raw.accelerometer_m_s2[0];??????????????????att.pitch?=?raw.accelerometer_m_s2[1];??????????????????att.yaw?=?raw.accelerometer_m_s2[2];??????????????????orb_publish(ORB_ID(vehicle_attitude),?att_pub,?&att);??????????????}????????????????????????????}??????}????????PX4_INFO("exiting");????????return?0;??}??
第四步:運行整個示例
在nsh終端運行你的應用程序:
px4_simple_app
如果打開地面站QGroundControl,你就能通過實時繪圖(Tools-> Analyze)來獲得實時的傳感器數據。
注意事項:
1.打開nsh前需要拔掉sd卡.
2.連接nsh時不要地面站qgc運行.
3.連接地面站時需要插上sd卡.
4.運行編譯命令是要在含有Makefile的.../Firware/目錄下進行.
參考資料
First App Tutorial (Hello Sky)
總結
以上是生活随笔為你收集整理的pixhawk/px4如何获取及使用传感器数据的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。