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

来源:互联网 发布:java并发是什么意思 编辑:程序博客网 时间:2024/06/04 20:05
origiin: http://blog.csdn.net/lyonlui/article/details/53734388
前面说到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
  1. #include <px4_config.h>  
  2. #include <px4_tasks.h>  
  3. #include <px4_posix.h>  
  4. #include <unistd.h>  
  5. #include <stdio.h>  
  6. #include <poll.h>  
  7. #include <string.h>  
  8.   
  9.   
  10. __EXPORT int hello_world_main(int argc, char *argv[]);  
  11.   
  12. int hello_world_main(int argc, char *argv[]){  
  13.   
  14.     PX4_INFO("hello world!");  
  15.   
  16.     return 0;  
  17. }  

  然后编辑CMakeLists.txt文件

 

[cpp] view plain copy
  1. px4_add_module(  
  2.     MODULE examples__hello_world  
  3.     MAIN hello_world  
  4.     STACK_MAIN 2000  
  5.     SRCS  
  6.         hello_world.c  
  7.     DEPENDS  
  8.         platforms__common  
  9.     )  


最后最重要的是我们要将这个程序注册到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
  1. include(nuttx/px4_impl_nuttx)  
  2.   
  3. set(CMAKE_TOOLCHAIN_FILE ${PX4_SOURCE_DIR}/cmake/toolchains/Toolchain-arm-none-eabi.cmake)  
  4.   
  5. set(config_uavcan_num_ifaces 2)  
  6.   
  7. set(config_module_list  
  8.     #  
  9.     # Board support modules  
  10.     #  
  11.     drivers/device  
  12.     drivers/stm32  
  13.     drivers/stm32/adc  
  14.     drivers/stm32/tone_alarm  
  15.     drivers/led  
  16.     drivers/px4fmu  
  17.     drivers/px4io  
  18.     drivers/boards/px4fmu-v2  
  19.     drivers/rgbled  
  20.     drivers/mpu6000  
  21.     #drivers/mpu9250  
  22.     drivers/lsm303d  
  23.     drivers/l3gd20  
  24.     drivers/hmc5883  
  25.     drivers/ms5611  
  26.     #drivers/mb12xx  
  27.     #drivers/srf02  
  28.     drivers/sf0x  
  29.     #drivers/ll40ls  
  30.     drivers/trone  
  31.     drivers/gps  
  32.     drivers/pwm_out_sim  
  33.     #drivers/hott  
  34.     #drivers/hott/hott_telemetry  
  35.     #drivers/hott/hott_sensors  
  36.     #drivers/blinkm  
  37.     drivers/airspeed  
  38.     drivers/ets_airspeed  
  39.     drivers/meas_airspeed  
  40.     #drivers/frsky_telemetry  
  41.     modules/sensors  
  42.     #drivers/mkblctrl  
  43.     drivers/px4flow  
  44.     #drivers/oreoled  
  45.     #drivers/vmount  
  46.     drivers/pwm_input  
  47.     drivers/camera_trigger  
  48.     drivers/bst  
  49.     #drivers/snapdragon_rc_pwm  
  50.     drivers/lis3mdl  
  51.   
  52.     #example  
  53.     examples/px4_simple_app  
  54.     <span style="color:#FF0000;">examples/hello_world</span>  
  55.   
  56.     #  
  57.     # System commands  
  58.     #  
  59.     systemcmds/bl_update  
  60.     systemcmds/config  
  61.     systemcmds/dumpfile  
  62.     #systemcmds/esc_calib  
  63.     systemcmds/mixer  
  64.     #systemcmds/motor_ramp  
  65.     systemcmds/mtd  
  66.     systemcmds/nshterm  
  67.     systemcmds/param  
  68.     systemcmds/perf  
  69.     systemcmds/pwm  
  70.     systemcmds/reboot  
  71.     #systemcmds/sd_bench  
  72.     systemcmds/top  
  73.     #systemcmds/topic_listener  
  74.     systemcmds/ver  
  75.   
  76.     #  
  77.     # Testing  
  78.     #  
  79.     #drivers/sf0x/sf0x_tests  
  80.     #drivers/test_ppm  
  81.     #lib/rc/rc_tests  
  82.     #modules/commander/commander_tests  
  83.     #modules/controllib_test  
  84.     #modules/mavlink/mavlink_tests  
  85.     #modules/unit_test  
  86.     #modules/uORB/uORB_tests  
  87.     #systemcmds/tests  
  88.   
  89.     #  
  90.     # General system control  
  91.     #  
  92.     modules/commander  
  93.     modules/load_mon  
  94.     modules/navigator  
  95.     modules/mavlink  
  96.     modules/gpio_led  
  97.     modules/uavcan  
  98.     modules/land_detector  
  99.   
  100.     #  
  101.     # Estimation modules  
  102.     #  
  103.     modules/attitude_estimator_q  
  104.     #modules/position_estimator_inav  
  105.     modules/local_position_estimator  
  106.     modules/ekf2  
  107.   
  108.     #  
  109.     # Vehicle Control  
  110.     #  
  111.     modules/fw_pos_control_l1  
  112.     modules/fw_att_control  
  113.     modules/mc_att_control  
  114.     modules/mc_pos_control  
  115.     modules/vtol_att_control  
  116.   
  117.     #  
  118.     # Logging  
  119.     #  
  120.     #modules/logger  
  121.     modules/sdlog2  
  122.   
  123.     #  
  124.     # Library modules  
  125.     #  
  126.     modules/param  
  127.     modules/systemlib  
  128.     modules/systemlib/mixer  
  129.     modules/uORB  
  130.     modules/dataman  
  131.   
  132.     #  
  133.     # Libraries  
  134.     #  
  135.     lib/controllib  
  136.     lib/mathlib  
  137.     lib/mathlib/math/filter  
  138.     lib/ecl  
  139.     lib/external_lgpl  
  140.     lib/geo  
  141.     lib/geo_lookup  
  142.     lib/conversion  
  143.     lib/launchdetection  
  144.     lib/terrain_estimation  
  145.     lib/runway_takeoff  
  146.     lib/tailsitter_recovery  
  147.     lib/DriverFramework/framework  
  148.     platforms/nuttx  
  149.   
  150.     # had to add for cmake, not sure why wasn't in original config  
  151.     platforms/common  
  152.     platforms/nuttx/px4_layer  
  153.   
  154.     #  
  155.     # OBC challenge  
  156.     #  
  157.     #modules/bottle_drop  
  158.   
  159.     #  
  160.     # Rover apps  
  161.     #  
  162.     #examples/rover_steering_control  
  163.   
  164.     #  
  165.     # Demo apps  
  166.     #  
  167.     #examples/math_demo  
  168.     # Tutorial code from  
  169.     # https://px4.io/dev/px4_simple_app  
  170.     #examples/px4_simple_app  
  171.   
  172.     # Tutorial code from  
  173.     # https://px4.io/dev/daemon  
  174.     #examples/px4_daemon_app  
  175.   
  176.     # Tutorial code from  
  177.     # https://px4.io/dev/debug_values  
  178.     #examples/px4_mavlink_debug  
  179.   
  180.     # Tutorial code from  
  181.     # https://px4.io/dev/example_fixedwing_control  
  182.     #examples/fixedwing_control  
  183.   
  184.     # Hardware test  
  185.     #examples/hwtest  
  186. )  
  187.   
  188. set(config_extra_builtin_cmds  
  189.     serdis  
  190.     sercon  
  191.     )  
  192.   
  193. set(config_io_board  
  194.     px4io-v2  
  195.     )  
  196.   
  197. set(config_extra_libs  
  198.     uavcan  
  199.     uavcan_stm32_driver  
  200.     )  
  201.   
  202. set(config_io_extra_libs  
  203.     )  
  204.   
  205. add_custom_target(sercon)  
  206. set_target_properties(sercon PROPERTIES  
  207.     PRIORITY "SCHED_PRIORITY_DEFAULT"  
  208.     MAIN "sercon" STACK_MAIN "2048"  
  209.     COMPILE_FLAGS "-Os")  
  210.   
  211. add_custom_target(serdis)  
  212. set_target_properties(serdis PROPERTIES  
  213.     PRIORITY "SCHED_PRIORITY_DEFAULT"  
  214.     MAIN "serdis" STACK_MAIN "2048"  
  215.     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
  1. #include <px4_config.h>  
  2. #include <px4_tasks.h>  
  3. #include <px4_posix.h>  
  4. #include <unistd.h>  
  5. #include <stdio.h>  
  6. #include <poll.h>  
  7. #include <string.h>  

这些库文件你可以认为是编写基于Nuttx操作系统修改的px4程序程序必不可少的库文件包含(其实px4团队修改了不少Nuttx操作系统的东西,使其使用更加方便,所以这些程序并不是真正意义上的Nuttx程序)


然后是main函数(这里要提醒一下广大同学,可能px4基于stm32的编译器语法规则过于严格,所以在编写一个函数之前,必须要去申明这个函数,即使这个函数是main函数也要申明,不然编译报错,无法通过)

[cpp] view plain copy
  1. int hello_world_main(int argc, char *argv[]){  
  2.   
  3.     PX4_INFO("hello world!");  
  4.   
  5.     return 0;  
  6. }  

可以看到基于Nuttx操作系统的main函数和其它系统的命名有很大不同,但也有自己的规律,那就是函数名+_+main,即name_main(),程序主函数里的参数(int argc, char *argv[])和其它系统main函数所带的参数没什么不同(如果不懂main函数带形参的同学最好自己百度一下,因为px4的那些程序基本都带参数的)。

 PX4_INFO();是一个类似于printf的函数(事实上他就是是基于printf实现的),用来输出PX4的一些信息的。

 再来看看CMakeLists.txt文件

[cpp] view plain copy
  1. px4_add_module(  
  2.     MODULE examples__hello_world  
  3.     MAIN hello_world  
  4.     STACK_MAIN 2000  
  5.     SRCS  
  6.         hello_world.c  
  7.     DEPENDS  
  8.         platforms__common  
  9.     )  

从字面上可以了解到每个程序在CMake里面都是一个模块,最后总的Cmake文件会去自动将这些模块添加到最后的生成的Makefile文件里面。所以我们要做的就是把我们写的模块的一些属性描写清楚,然后将其注册到nuttx_px4fmu-v2_default.cmake文件当中。

  首先是模块名MODULE:这个名字的命名规则是当前文件夹+__+主函数名。

  然后是MIAN :这个就是用来确定代码中那个函数为主函数的,填写主函数名即可。

  STACK_MAIN:目前暂不清楚用途

  SRCS:所用到的源文件。

  DEPENDS :依赖。


 以上就是关于编写一个基于Nuttx操作系统hello world程序的全过程。

因为我本人对Cmake的一些语法还不是很清楚,所以以上有些东西可能描述的不是很清楚,如有大神,还望指点。


下面一篇文章我将简介一下大家都很关心和迫切想知道的问题,就是px4的飞控程序是怎么开始执行的,程序入口在哪。