日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pixhawk/px4如何获取及使用传感器数据

發布時間:2024/4/18 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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
  • /****************************************************************************?
  • ?*?
  • ?*???Copyright?(c)?2012-2015?PX4?Development?Team.?All?rights?reserved.?
  • ?*?
  • ?*?Redistribution?and?use?in?source?and?binary?forms,?with?or?without?
  • ?*?modification,?are?permitted?provided?that?the?following?conditions?
  • ?*?are?met:?
  • ?*?
  • ?*?1.?Redistributions?of?source?code?must?retain?the?above?copyright?
  • ?*????notice,?this?list?of?conditions?and?the?following?disclaimer.?
  • ?*?2.?Redistributions?in?binary?form?must?reproduce?the?above?copyright?
  • ?*????notice,?this?list?of?conditions?and?the?following?disclaimer?in?
  • ?*????the?documentation?and/or?other?materials?provided?with?the?
  • ?*????distribution.?
  • ?*?3.?Neither?the?name?PX4?nor?the?names?of?its?contributors?may?be?
  • ?*????used?to?endorse?or?promote?products?derived?from?this?software?
  • ?*????without?specific?prior?written?permission.?
  • ?*?
  • ?*?THIS?SOFTWARE?IS?PROVIDED?BY?THE?COPYRIGHT?HOLDERS?AND?CONTRIBUTORS?
  • ?*?"AS?IS"?AND?ANY?EXPRESS?OR?IMPLIED?WARRANTIES,?INCLUDING,?BUT?NOT?
  • ?*?LIMITED?TO,?THE?IMPLIED?WARRANTIES?OF?MERCHANTABILITY?AND?FITNESS?
  • ?*?FOR?A?PARTICULAR?PURPOSE?ARE?DISCLAIMED.?IN?NO?EVENT?SHALL?THE?
  • ?*?COPYRIGHT?OWNER?OR?CONTRIBUTORS?BE?LIABLE?FOR?ANY?DIRECT,?INDIRECT,?
  • ?*?INCIDENTAL,?SPECIAL,?EXEMPLARY,?OR?CONSEQUENTIAL?DAMAGES?(INCLUDING,?
  • ?*?BUT?NOT?LIMITED?TO,?PROCUREMENT?OF?SUBSTITUTE?GOODS?OR?SERVICES;?LOSS?
  • ?*?OF?USE,?DATA,?OR?PROFITS;?OR?BUSINESS?INTERRUPTION)?HOWEVER?CAUSED?
  • ?*?AND?ON?ANY?THEORY?OF?LIABILITY,?WHETHER?IN?CONTRACT,?STRICT?
  • ?*?LIABILITY,?OR?TORT?(INCLUDING?NEGLIGENCE?OR?OTHERWISE)?ARISING?IN?
  • ?*?ANY?WAY?OUT?OF?THE?USE?OF?THIS?SOFTWARE,?EVEN?IF?ADVISED?OF?THE?
  • ?*?POSSIBILITY?OF?SUCH?DAMAGE.?
  • ?*?
  • ?****************************************************************************/??
  • ??
  • /**?
  • ?*?@file?px4_simple_app.c?
  • ?*?Minimal?application?example?for?PX4?autopilot?
  • ?*?
  • ?*?@author?Example?User?<mail@example.com>?
  • ?*/??
  • ??
  • #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[])??
  • {??
  • ????/*?subscribe?to?sensor_combined?topic?*/??
  • ????int?sensor_sub_fd?=?orb_subscribe(ORB_ID(sensor_combined));??
  • ????orb_set_interval(sensor_sub_fd,?1000);??
  • ??
  • ??
  • ??
  • ????/*?one?could?wait?for?multiple?topics?with?this?technique,?just?using?one?here?*/??
  • ????px4_pollfd_struct_t?fds[]?=?{??
  • ????????{?.fd?=?sensor_sub_fd,???.events?=?POLLIN?},??
  • ????????/*?there?could?be?more?file?descriptors?here,?in?the?form?like:?
  • ?????????*?{?.fd?=?other_sub_fd,???.events?=?POLLIN?},?
  • ?????????*/??
  • ????};??
  • ??
  • ????int?error_counter?=?0;??
  • ??
  • ????for?(int?i?=?0;?i?<?5;?i++)?{??
  • ????????/*?wait?for?sensor?update?of?1?file?descriptor?for?1000?ms?(1?second)?*/??
  • ????????int?poll_ret?=?px4_poll(fds,?1,?1000);??
  • ??
  • ????????/*?handle?the?poll?result?*/??
  • ????????if?(poll_ret?==?0)?{??
  • ????????????/*?this?means?none?of?our?providers?is?giving?us?data?*/??
  • ????????????PX4_ERR("[px4_simple_app]?Got?no?data?within?a?second");??
  • ??
  • ????????}?else?if?(poll_ret?<?0)?{??
  • ????????????/*?this?is?seriously?bad?-?should?be?an?emergency?*/??
  • ????????????if?(error_counter?<?10?||?error_counter?%?50?==?0)?{??
  • ????????????????/*?use?a?counter?to?prevent?flooding?(and?slowing?us?down)?*/??
  • ????????????????PX4_ERR("[px4_simple_app]?ERROR?return?value?from?poll():?%d"??
  • ????????????????????,?poll_ret);??
  • ????????????}??
  • ??
  • ????????????error_counter++;??
  • ??
  • ????????}?else?{??
  • ??
  • ????????????if?(fds[0].revents?&?POLLIN)?{??
  • ????????????????/*?obtained?data?for?the?first?file?descriptor?*/??
  • ????????????????struct?sensor_combined_s?raw;??
  • ????????????????/*?copy?sensors?raw?data?into?local?buffer?*/??
  • ????????????????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
  • /****************************************************************************?
  • ?*?
  • ?*???Copyright?(c)?2012-2015?PX4?Development?Team.?All?rights?reserved.?
  • ?*?
  • ?*?Redistribution?and?use?in?source?and?binary?forms,?with?or?without?
  • ?*?modification,?are?permitted?provided?that?the?following?conditions?
  • ?*?are?met:?
  • ?*?
  • ?*?1.?Redistributions?of?source?code?must?retain?the?above?copyright?
  • ?*????notice,?this?list?of?conditions?and?the?following?disclaimer.?
  • ?*?2.?Redistributions?in?binary?form?must?reproduce?the?above?copyright?
  • ?*????notice,?this?list?of?conditions?and?the?following?disclaimer?in?
  • ?*????the?documentation?and/or?other?materials?provided?with?the?
  • ?*????distribution.?
  • ?*?3.?Neither?the?name?PX4?nor?the?names?of?its?contributors?may?be?
  • ?*????used?to?endorse?or?promote?products?derived?from?this?software?
  • ?*????without?specific?prior?written?permission.?
  • ?*?
  • ?*?THIS?SOFTWARE?IS?PROVIDED?BY?THE?COPYRIGHT?HOLDERS?AND?CONTRIBUTORS?
  • ?*?"AS?IS"?AND?ANY?EXPRESS?OR?IMPLIED?WARRANTIES,?INCLUDING,?BUT?NOT?
  • ?*?LIMITED?TO,?THE?IMPLIED?WARRANTIES?OF?MERCHANTABILITY?AND?FITNESS?
  • ?*?FOR?A?PARTICULAR?PURPOSE?ARE?DISCLAIMED.?IN?NO?EVENT?SHALL?THE?
  • ?*?COPYRIGHT?OWNER?OR?CONTRIBUTORS?BE?LIABLE?FOR?ANY?DIRECT,?INDIRECT,?
  • ?*?INCIDENTAL,?SPECIAL,?EXEMPLARY,?OR?CONSEQUENTIAL?DAMAGES?(INCLUDING,?
  • ?*?BUT?NOT?LIMITED?TO,?PROCUREMENT?OF?SUBSTITUTE?GOODS?OR?SERVICES;?LOSS?
  • ?*?OF?USE,?DATA,?OR?PROFITS;?OR?BUSINESS?INTERRUPTION)?HOWEVER?CAUSED?
  • ?*?AND?ON?ANY?THEORY?OF?LIABILITY,?WHETHER?IN?CONTRACT,?STRICT?
  • ?*?LIABILITY,?OR?TORT?(INCLUDING?NEGLIGENCE?OR?OTHERWISE)?ARISING?IN?
  • ?*?ANY?WAY?OUT?OF?THE?USE?OF?THIS?SOFTWARE,?EVEN?IF?ADVISED?OF?THE?
  • ?*?POSSIBILITY?OF?SUCH?DAMAGE.?
  • ?*?
  • ?****************************************************************************/??
  • ??
  • /**?
  • ?*?@file?px4_simple_app.c?
  • ?*?Minimal?application?example?for?PX4?autopilot?
  • ?*?
  • ?*?@author?Example?User?<mail@example.com>?
  • ?*/??
  • ??
  • #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[])??
  • {??
  • ????/*?subscribe?to?sensor_combined?topic?*/??
  • ????int?sensor_sub_fd?=?orb_subscribe(ORB_ID(sensor_combined));??
  • ????orb_set_interval(sensor_sub_fd,?1000);??
  • ??
  • ????/*?advertise?attitude?topic?*/??
  • ????struct?vehicle_attitude_s?att;??
  • ????memset(&att,?0,?sizeof(att));??
  • ????orb_advert_t?att_pub?=?orb_advertise(ORB_ID(vehicle_attitude),?&att);??
  • ??
  • ????/*?one?could?wait?for?multiple?topics?with?this?technique,?just?using?one?here?*/??
  • ????px4_pollfd_struct_t?fds[]?=?{??
  • ????????{?.fd?=?sensor_sub_fd,???.events?=?POLLIN?},??
  • ????????/*?there?could?be?more?file?descriptors?here,?in?the?form?like:?
  • ?????????*?{?.fd?=?other_sub_fd,???.events?=?POLLIN?},?
  • ?????????*/??
  • ????};??
  • ??
  • ????int?error_counter?=?0;??
  • ??
  • ????for?(int?i?=?0;?i?<?5;?i++)?{??
  • ????????/*?wait?for?sensor?update?of?1?file?descriptor?for?1000?ms?(1?second)?*/??
  • ????????int?poll_ret?=?px4_poll(fds,?1,?1000);??
  • ??
  • ????????/*?handle?the?poll?result?*/??
  • ????????if?(poll_ret?==?0)?{??
  • ????????????/*?this?means?none?of?our?providers?is?giving?us?data?*/??
  • ????????????PX4_ERR("[px4_simple_app]?Got?no?data?within?a?second");??
  • ??
  • ????????}?else?if?(poll_ret?<?0)?{??
  • ????????????/*?this?is?seriously?bad?-?should?be?an?emergency?*/??
  • ????????????if?(error_counter?<?10?||?error_counter?%?50?==?0)?{??
  • ????????????????/*?use?a?counter?to?prevent?flooding?(and?slowing?us?down)?*/??
  • ????????????????PX4_ERR("[px4_simple_app]?ERROR?return?value?from?poll():?%d"??
  • ????????????????????,?poll_ret);??
  • ????????????}??
  • ??
  • ????????????error_counter++;??
  • ??
  • ????????}?else?{??
  • ??
  • ????????????if?(fds[0].revents?&?POLLIN)?{??
  • ????????????????/*?obtained?data?for?the?first?file?descriptor?*/??
  • ????????????????struct?sensor_combined_s?raw;??
  • ????????????????/*?copy?sensors?raw?data?into?local?buffer?*/??
  • ????????????????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]);??
  • ??
  • ????????????????/*?set?att?and?publish?this?information?for?other?apps?*/??
  • ????????????????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);??
  • ????????????}??
  • ??
  • ????????????/*?there?could?be?more?file?descriptors?here,?in?the?form?like:?
  • ?????????????*?if?(fds[1..n].revents?&?POLLIN)?{}?
  • ?????????????*/??
  • ????????}??
  • ????}??
  • ??
  • ????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如何获取及使用传感器数据的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。