生活随笔
收集整理的這篇文章主要介紹了
pixhawk/px4如何获取及使用传感器数据
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
pixhawk/px4如何獲取及使用傳感器數(shù)據(jù)
第一步:讀取傳感器數(shù)據(jù)
上一篇博文已經(jīng)介紹了如何給pixhawk/px4創(chuàng)建一個(gè)應(yīng)用程序,現(xiàn)在我們?cè)谏弦粋€(gè)應(yīng)用程序的基礎(chǔ)上使用傳感器數(shù)據(jù)。
應(yīng)用程序?yàn)榱藢?shí)現(xiàn)一些有用的功能,需要訂閱輸入和發(fā)布輸出(比如電機(jī)或舵機(jī)輸出的命令)。注意在這里,PX4平臺(tái)真正的硬件抽象的概念在這里體現(xiàn)---當(dāng)硬件平臺(tái)或者傳感器升級(jí)更新,你完全不需要和傳感器驅(qū)動(dòng)打交道,也不需要更新你的應(yīng)用程序或者更新傳感器驅(qū)動(dòng)程序。
在PX4中,應(yīng)用程序間發(fā)送信息的獨(dú)立通道叫做“topics”,在本教程中,我們關(guān)心的話題是“多傳感器間的uORB消息機(jī)制”(sensor_combinedtopic)。這些消息機(jī)制使得整個(gè)系統(tǒng)能夠同步傳感器數(shù)據(jù)。
訂閱一個(gè)消息十分快速簡(jiǎn)潔:
[cpp] view plaincopy
#include<uORB/topics/sensor_combined.h>????..????int sensor_sub_fd?=?orb_subscribe(ORB_ID(sensor_combined));??
“sensor_sub_fd”是一個(gè)消息句柄,能夠十分有效處理新數(shù)據(jù)的到來之前的延遲等待。當(dāng)前線程會(huì)休眠,直到新的傳感器數(shù)據(jù)到來時(shí),被調(diào)度器喚醒,并且在等待時(shí)不需要占用任何的CPU時(shí)間。為了實(shí)現(xiàn)這個(gè)功能,我們使用poll()函數(shù)即POSIX系統(tǒng)調(diào)用。
在消息讀取中加入“poll()”機(jī)制,完整程序如下
[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;??}??
編譯應(yīng)用程序:
make
注意,
make命令要在
Firware目錄下執(zhí)行,
因?yàn)?/span>Makfile文件在該目錄下,結(jié)果如下圖所示。
第二步:測(cè)試uORB消息讀取機(jī)制
最后一步,運(yùn)行你的應(yīng)用程序,并且切換到后臺(tái)應(yīng)用。注意這需要把程序重新編譯上傳到飛控板,拔掉sd卡,然后連接nsh控制臺(tái),運(yùn)行以下命令。
[plain] view plaincopypx4_simple_app &??
你的應(yīng)用程序會(huì)向串口輸出當(dāng)前傳感器的值:
[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
我測(cè)試的結(jié)果如下圖所示
它會(huì)在輸出5次數(shù)據(jù)后退出。接下來會(huì)介紹如何編寫一個(gè)能通過命令行控制的后臺(tái)應(yīng)用。
第三步:打印數(shù)據(jù)
為了能獲取到計(jì)算后的數(shù)據(jù),下一步就是“打印”這些結(jié)果。如果我們知道某一個(gè)消息是使用mavlink協(xié)議轉(zhuǎn)發(fā)給地面控制站的,我們可以通過這個(gè)消息去查看結(jié)果。例如我們通過這個(gè)方法來獲得高度信息的消息。
接口非常簡(jiǎn)單--初始化消息的結(jié)構(gòu)體,然后廣播這條消息:
#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);
在主循環(huán)中,當(dāng)消息準(zhǔn)備好時(shí),打印這條消息。
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;??}??
第四步:運(yùn)行整個(gè)示例
在nsh終端運(yùn)行你的應(yīng)用程序:
px4_simple_app
如果打開地面站QGroundControl,你就能通過實(shí)時(shí)繪圖(Tools-> Analyze)來獲得實(shí)時(shí)的傳感器數(shù)據(jù)。
注意事項(xiàng):
1.打開nsh前需要拔掉sd卡.
2.連接nsh時(shí)不要地面站qgc運(yùn)行.
3.連接地面站時(shí)需要插上sd卡.
4.運(yùn)行編譯命令是要在含有Makefile的.../Firware/目錄下進(jìn)行.
參考資料
First App Tutorial (Hello Sky)
總結(jié)
以上是生活随笔為你收集整理的pixhawk/px4如何获取及使用传感器数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。