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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

px4原生源码学习四--Nuttx 实时操作系统编程

發布時間:2024/4/18 windows 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 px4原生源码学习四--Nuttx 实时操作系统编程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?前面說到px4是基于Nuttx實時操作系統上的,那么px4也是由一些程序所構成,這些程序實現了飛行器的自主控制,只不過這些程序并不是我們通常所見到的單片機或者windows編程那樣的程序,但基本編程思想是一致的。我認為如果要看懂px4的源碼,那你一定要了解px4的那些程序是怎么編寫出來,怎么運行的。所以本章我就大概介紹一下基于Nuttx的編程,我以一個所有編程入門都會介紹的一個程序作為例子。這個程序就是大名鼎鼎的hello world



???? 在講解編程之前,我得交代兩個重要的東西,因為這兩個東西存在于px4的源碼系統當中,非常重要,它們就是makeCmake

????? 首先談談何為make,熟悉linux系統的朋友對make肯定不陌生,它就是用來讀取Makefile文件然后執行,把源碼編譯鏈接為可執行程序的一個軟件。我們只要把待編譯的源碼文件和這些源碼所需要用到的庫寫到Makefile文件里面執行make命令就能得到可執行或者可燒錄的二進制文件。

?????

???? 那何為Cmake呢?

???? 我們可以這樣理解,早期人們直接使用gcc命令編譯程序,當源碼文件多起了之后,直接用gcc命令過長,鏈接的庫過多,這樣就太麻煩了。這時候Make就應運而生,解決了人們對于代碼文件過多的困擾。但隨著項目越來越大,文件越來越多,人們發現make也捉襟見肘,因為編寫Makefile文件又會變得異常復雜。這個時候聰明的程序猿就想能不能有個程序可以幫我寫Makefile文件呢?這樣就引出了Cmake,當然Cmake不是智能的,它不可能自己去辨別那些是代碼,那些是文件,需要什么庫。這樣就引入了另外一套規則,也引入了一個文件CMakeLists.txt,這個文件就是Cmake程序識別的文件,有這個文件,Cmake就能幫助程序猿自動生成Makefile文件。

?? 總的來說流程應該是這樣的:

??????????????????????????? ? ? cmake??????????????????????????? make

?? CMakeLists.txt-------------->Makefile---------------------->可執行文件

???????????????????????????????????????????????????????????????????????? src,lib


??? 看過px4源碼文件的朋友肯定會發現里面有很多CMakeLists.txt文件,事實上整個px4源碼的文件都是基于CMakeLists.txt的(Nuttx系統不是,Nuttx是基于Makefile的,px4源碼基本都在Firmware/src下,Nuttx操作系統源碼在Firmware/NuttX下)

?? 有了上面這個兩個概念之后,我們就開始著手編寫我們的hello world程序。

?? 首先進入/Firmware/src/examples文件夾,然后在這個文件夾下面建立一個文件夾hello_world,再進入hello_world文件夾,在該文件夾下建立兩個文件:CMakeLists.txt,hello_world.c。

?? 首先編輯hello_world.c文件。

??

[cpp]?view plain?copy
  • #include?<px4_config.h>??
  • #include?<px4_tasks.h>??
  • #include?<px4_posix.h>??
  • #include?<unistd.h>??
  • #include?<stdio.h>??
  • #include?<poll.h>??
  • #include?<string.h>??
  • ??
  • ??
  • __EXPORT?int?hello_world_main(int?argc,?char?*argv[]);??
  • ??
  • int?hello_world_main(int?argc,?char?*argv[]){??
  • ??
  • ????PX4_INFO("hello?world!");??
  • ??
  • ????return?0;??
  • }??

  • ? 然后編輯CMakeLists.txt文件

    ?

    [cpp]?view plain?copy
  • px4_add_module(??
  • ????MODULE?examples__hello_world??
  • ????MAIN?hello_world??
  • ????STACK_MAIN?2000??
  • ????SRCS??
  • ????????hello_world.c??
  • ????DEPENDS??
  • ????????platforms__common??
  • ????)??


  • 最后最重要的是我們要將這個程序注冊到Nuttx的系統當中

    找到文件/Firmware/cmake/configs/nuttx_px4fmu-v2_default.cmake

    針對不同的硬件所注冊的文件是不同的,下面是不同硬件的注冊方式:

    • Posix SITL:?Firmware/cmake/configs/posix_sitl_default.cmake
    • Pixhawk v1/2:?Firmware/cmake/configs/nuttx_px4fmu-v2_default.cmake
    • Pixracer:?Firmware/cmake/configs/nuttx_px4fmu-v4_default.cmake

    在cmake文件中添加“examples/hello_world”

    像下面這樣:

    ?

    [cpp]?view plain?copy
  • include(nuttx/px4_impl_nuttx)??
  • ??
  • set(CMAKE_TOOLCHAIN_FILE?${PX4_SOURCE_DIR}/cmake/toolchains/Toolchain-arm-none-eabi.cmake)??
  • ??
  • set(config_uavcan_num_ifaces?2)??
  • ??
  • set(config_module_list??
  • ????#??
  • ????#?Board?support?modules??
  • ????#??
  • ????drivers/device??
  • ????drivers/stm32??
  • ????drivers/stm32/adc??
  • ????drivers/stm32/tone_alarm??
  • ????drivers/led??
  • ????drivers/px4fmu??
  • ????drivers/px4io??
  • ????drivers/boards/px4fmu-v2??
  • ????drivers/rgbled??
  • ????drivers/mpu6000??
  • ????#drivers/mpu9250??
  • ????drivers/lsm303d??
  • ????drivers/l3gd20??
  • ????drivers/hmc5883??
  • ????drivers/ms5611??
  • ????#drivers/mb12xx??
  • ????#drivers/srf02??
  • ????drivers/sf0x??
  • ????#drivers/ll40ls??
  • ????drivers/trone??
  • ????drivers/gps??
  • ????drivers/pwm_out_sim??
  • ????#drivers/hott??
  • ????#drivers/hott/hott_telemetry??
  • ????#drivers/hott/hott_sensors??
  • ????#drivers/blinkm??
  • ????drivers/airspeed??
  • ????drivers/ets_airspeed??
  • ????drivers/meas_airspeed??
  • ????#drivers/frsky_telemetry??
  • ????modules/sensors??
  • ????#drivers/mkblctrl??
  • ????drivers/px4flow??
  • ????#drivers/oreoled??
  • ????#drivers/vmount??
  • ????drivers/pwm_input??
  • ????drivers/camera_trigger??
  • ????drivers/bst??
  • ????#drivers/snapdragon_rc_pwm??
  • ????drivers/lis3mdl??
  • ??
  • ????#example??
  • ????examples/px4_simple_app??
  • ????<span?style="color:#FF0000;">examples/hello_world</span>??
  • ??
  • ????#??
  • ????#?System?commands??
  • ????#??
  • ????systemcmds/bl_update??
  • ????systemcmds/config??
  • ????systemcmds/dumpfile??
  • ????#systemcmds/esc_calib??
  • ????systemcmds/mixer??
  • ????#systemcmds/motor_ramp??
  • ????systemcmds/mtd??
  • ????systemcmds/nshterm??
  • ????systemcmds/param??
  • ????systemcmds/perf??
  • ????systemcmds/pwm??
  • ????systemcmds/reboot??
  • ????#systemcmds/sd_bench??
  • ????systemcmds/top??
  • ????#systemcmds/topic_listener??
  • ????systemcmds/ver??
  • ??
  • ????#??
  • ????#?Testing??
  • ????#??
  • ????#drivers/sf0x/sf0x_tests??
  • ????#drivers/test_ppm??
  • ????#lib/rc/rc_tests??
  • ????#modules/commander/commander_tests??
  • ????#modules/controllib_test??
  • ????#modules/mavlink/mavlink_tests??
  • ????#modules/unit_test??
  • ????#modules/uORB/uORB_tests??
  • ????#systemcmds/tests??
  • ??
  • ????#??
  • ????#?General?system?control??
  • ????#??
  • ????modules/commander??
  • ????modules/load_mon??
  • ????modules/navigator??
  • ????modules/mavlink??
  • ????modules/gpio_led??
  • ????modules/uavcan??
  • ????modules/land_detector??
  • ??
  • ????#??
  • ????#?Estimation?modules??
  • ????#??
  • ????modules/attitude_estimator_q??
  • ????#modules/position_estimator_inav??
  • ????modules/local_position_estimator??
  • ????modules/ekf2??
  • ??
  • ????#??
  • ????#?Vehicle?Control??
  • ????#??
  • ????modules/fw_pos_control_l1??
  • ????modules/fw_att_control??
  • ????modules/mc_att_control??
  • ????modules/mc_pos_control??
  • ????modules/vtol_att_control??
  • ??
  • ????#??
  • ????#?Logging??
  • ????#??
  • ????#modules/logger??
  • ????modules/sdlog2??
  • ??
  • ????#??
  • ????#?Library?modules??
  • ????#??
  • ????modules/param??
  • ????modules/systemlib??
  • ????modules/systemlib/mixer??
  • ????modules/uORB??
  • ????modules/dataman??
  • ??
  • ????#??
  • ????#?Libraries??
  • ????#??
  • ????lib/controllib??
  • ????lib/mathlib??
  • ????lib/mathlib/math/filter??
  • ????lib/ecl??
  • ????lib/external_lgpl??
  • ????lib/geo??
  • ????lib/geo_lookup??
  • ????lib/conversion??
  • ????lib/launchdetection??
  • ????lib/terrain_estimation??
  • ????lib/runway_takeoff??
  • ????lib/tailsitter_recovery??
  • ????lib/DriverFramework/framework??
  • ????platforms/nuttx??
  • ??
  • ????#?had?to?add?for?cmake,?not?sure?why?wasn't?in?original?config??
  • ????platforms/common??
  • ????platforms/nuttx/px4_layer??
  • ??
  • ????#??
  • ????#?OBC?challenge??
  • ????#??
  • ????#modules/bottle_drop??
  • ??
  • ????#??
  • ????#?Rover?apps??
  • ????#??
  • ????#examples/rover_steering_control??
  • ??
  • ????#??
  • ????#?Demo?apps??
  • ????#??
  • ????#examples/math_demo??
  • ????#?Tutorial?code?from??
  • ????#?https://px4.io/dev/px4_simple_app??
  • ????#examples/px4_simple_app??
  • ??
  • ????#?Tutorial?code?from??
  • ????#?https://px4.io/dev/daemon??
  • ????#examples/px4_daemon_app??
  • ??
  • ????#?Tutorial?code?from??
  • ????#?https://px4.io/dev/debug_values??
  • ????#examples/px4_mavlink_debug??
  • ??
  • ????#?Tutorial?code?from??
  • ????#?https://px4.io/dev/example_fixedwing_control??
  • ????#examples/fixedwing_control??
  • ??
  • ????#?Hardware?test??
  • ????#examples/hwtest??
  • )??
  • ??
  • set(config_extra_builtin_cmds??
  • ????serdis??
  • ????sercon??
  • ????)??
  • ??
  • set(config_io_board??
  • ????px4io-v2??
  • ????)??
  • ??
  • set(config_extra_libs??
  • ????uavcan??
  • ????uavcan_stm32_driver??
  • ????)??
  • ??
  • set(config_io_extra_libs??
  • ????)??
  • ??
  • add_custom_target(sercon)??
  • set_target_properties(sercon?PROPERTIES??
  • ????PRIORITY?"SCHED_PRIORITY_DEFAULT"??
  • ????MAIN?"sercon"?STACK_MAIN?"2048"??
  • ????COMPILE_FLAGS?"-Os")??
  • ??
  • add_custom_target(serdis)??
  • set_target_properties(serdis?PROPERTIES??
  • ????PRIORITY?"SCHED_PRIORITY_DEFAULT"??
  • ????MAIN?"serdis"?STACK_MAIN?"2048"??
  • ????COMPILE_FLAGS?"-Os")??


  • 這樣cmake的編譯系統就會將這個程序加入到編譯鏈中去了。

    ?在Firmware文件夾下面執行make px4fmu-v2_default,如果不出問題的話,編譯成功會顯示下面的畫面:


    ?然后將硬件連接至虛擬機,執行燒錄命令:make px4fmu-v2_default?upload

    ??按照上一篇文章所講的那樣同Nuttx shell通信?


    ./mavlink_shell.py /dev/ttyACM0




    在nsh中輸入help命令之后,你就會在Builtin Apps下面找到hello_world程序


    執行hello_world程序:



    可以看到輸出了hello world!

    那么這一切是怎么做到的呢?首先看看代碼文件即hello_world.c文件

    首先是include

    [cpp]?view plain?copy
  • #include?<px4_config.h>??
  • #include?<px4_tasks.h>??
  • #include?<px4_posix.h>??
  • #include?<unistd.h>??
  • #include?<stdio.h>??
  • #include?<poll.h>??
  • #include?<string.h>??

  • 這些庫文件你可以認為是編寫基于Nuttx操作系統修改的px4程序程序必不可少的庫文件包含(其實px4團隊修改了不少Nuttx操作系統的東西,使其使用更加方便,所以這些程序并不是真正意義上的Nuttx程序)


    然后是main函數(這里要提醒一下廣大同學,可能px4基于stm32的編譯器語法規則過于嚴格,所以在編寫一個函數之前,必須要去申明這個函數,即使這個函數是main函數也要申明,不然編譯報錯,無法通過)

    [cpp]?view plain?copy
  • int?hello_world_main(int?argc,?char?*argv[]){??
  • ??
  • ????PX4_INFO("hello?world!");??
  • ??
  • ????return?0;??
  • }??

  • 可以看到基于Nuttx操作系統的main函數和其它系統的命名有很大不同,但也有自己的規律,那就是函數名+_+main,即?name_main(),程序主函數里的參數(int argc, char *argv[])和其它系統main函數所帶的參數沒什么不同(如果不懂main函數帶形參的同學最好自己百度一下,因為px4的那些程序基本都帶參數的)。

    ?PX4_INFO();是一個類似于printf的函數(事實上他就是是基于printf實現的),用來輸出PX4的一些信息的。

    ?再來看看CMakeLists.txt文件

    [cpp]?view plain?copy
  • px4_add_module(??
  • ????MODULE?examples__hello_world??
  • ????MAIN?hello_world??
  • ????STACK_MAIN?2000??
  • ????SRCS??
  • ????????hello_world.c??
  • ????DEPENDS??
  • ????????platforms__common??
  • ????)??

  • 從字面上可以了解到每個程序在CMake里面都是一個模塊,最后總的Cmake文件會去自動將這些模塊添加到最后的生成的Makefile文件里面。所以我們要做的就是把我們寫的模塊的一些屬性描寫清楚,然后將其注冊到nuttx_px4fmu-v2_default.cmake文件當中。

    ? 首先是模塊名MODULE:這個名字的命名規則是當前文件夾+__+主函數名。

    ??然后是MIAN :這個就是用來確定代碼中那個函數為主函數的,填寫主函數名即可。

    ? STACK_MAIN:目前暫不清楚用途

    ? SRCS:所用到的源文件。

    ? DEPENDS :依賴。


    ?以上就是關于編寫一個基于Nuttx操作系統hello world程序的全過程。

    因為我本人對Cmake的一些語法還不是很清楚,所以以上有些東西可能描述的不是很清楚,如有大神,還望指點。


    下面一篇文章我將簡介一下大家都很關心和迫切想知道的問題,就是px4的飛控程序是怎么開始執行的,程序入口在哪。

    總結

    以上是生活随笔為你收集整理的px4原生源码学习四--Nuttx 实时操作系统编程的全部內容,希望文章能夠幫你解決所遇到的問題。

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