再分割线

编译部分暂时放放,先看px4如何工作的。

px4是有rootfs的,rootfs的启动在rsS上,即px4如何启动/工作可以从rcS上看出来,所以先看rcS。

显示版本

ver all


   51 /* string constants for version commands */                                                          
   52 static const char sz_ver_hw_str[]   = "hw";                                                          
   53 static const char sz_ver_hwcmp_str[]    = "hwcmp";                                                   
   54 static const char sz_ver_hwtypecmp_str[]    = "hwtypecmp";                                           
   55 static const char sz_ver_git_str[]  = "git";                                                         
   56 static const char sz_ver_bdate_str[]    = "bdate";                                                   
   57 static const char sz_ver_buri_str[]     = "uri";                                                     
   58 static const char sz_ver_gcc_str[]  = "gcc";                                                         
   59 static const char sz_ver_all_str[]  = "all";                                                         
   60 static const char mcu_ver_str[]     = "mcu";                                                         
   61 static const char px4_guid_str[]         = "px4guid"; 

这个没啥说的

sdcard的处理

   45 # Try to mount the microSD card.
   46 #
   47 if [ -b "/dev/mmcsd0" ]  
   48 then
   49     if mount -t vfat /dev/mmcsd0 /fs/microsd 
   50     then                 
   51         if [ -f "/fs/microsd/.format" ] 
   52         then             
   53             echo "INFO [init] format /dev/mmcsd0 requested (/fs/microsd/.format)"
   54             set SDCARD_FORMAT yes           
   55             rm /fs/microsd/.format          
   56             umount /fs/microsd
   57 
   58         else
   59             set SDCARD_AVAILABLE yes        
   60         fi
   61     fi
   62 
   63     if [ $SDCARD_AVAILABLE = no -o $SDCARD_FORMAT = yes ]
   64     then
   65         echo "INFO [init] formatting /dev/mmcsd0"
   66         set STARTUP_TUNE 15 # tune 15 = SD_ERROR (overridden to SD_INIT if format + mount succeeds)
   67                          
   68         if mkfatfs -F 32 /dev/mmcsd0    
   69         then             
   70             echo "INFO [init] card formatted"
   71 
   72             if mount -t vfat /dev/mmcsd0 /fs/microsd 
   73             then         
   74                 set SDCARD_AVAILABLE yes        
   75                 set STARTUP_TUNE 14 # tune 14 = SD_INIT
   76             else         
   77                 echo "ERROR [init] card mount failed"
   78             fi
   79         else             
   80             echo "ERROR [init] format failed"
   81         fi
   82     fi
   83 
   84     if [ $SDCARD_AVAILABLE = yes ]  
   85     then
   86         if hardfault_log check
   87         then             
   88             set STARTUP_TUNE 2 # tune 2 = ERROR_TUNE
   89             if hardfault_log commit         
   90             then         
   91                 hardfault_log reset             
   92             fi           
   93         fi               
   94     fi
   95 
   96     set PARAM_FILE /fs/microsd/params
   97     set PARAM_BACKUP_FILE "/fs/microsd/parameters_backup.bson"
   98 fi
  1. 检查块设备 /dev/mmcsd0
  2. 尝试挂载/dev/mmcsd0/fs/microsd
  3. 尝试检查/fs/microsd/.format。没找到的话设置SDCARD_AVAILABLEyes(默认为no)。否则设置SDCARD_FORMATyes(默认为no)。
  4. 根据SDCARD_AVAILABLESDCARD_FORMAT的值做后续处理。
    最终需要SDCARD_AVAILABLEyes才行。

在SDCARD处理的最后,SDCARD_AVAILABLEyes时,会有hardfault相关处理(具体作用目前不明)。

最最后,设置变量 PARAM_FILE PARAM_BACKUP_FILE

启动 启动脚本

sdcard上的启动脚本

  104 if [ -f $FRC ]                                                                                                                                                                                                  
  105 then
  106     . $FRC

set FRC /fs/microsd/etc/rc.txt

不使用sdcard上的启动脚本

小工具

mft

参数检查工具,支持的参数有:

  1. -q slient
  2. -k type 带查询的参数的分类——推测。
  3. -s 参数名称
  4. -v 参数值
    例如mft query -q -k MTD -s MTD_CALDATA -v /fs/mtd_caldata,意思是检查MTD类型的参数MTD_CALDATA是否为/fs/mtd_caldata

param

参数工具——通过shell或脚本访问和操作参数。
支持的参数有:

  1. load: Load params from a file (overwrite all);
    A. "<file>", "File name (use default if not given)"
  2. import:Import params from a file
    A. "<file>", "File name (use default if not given)"
  3. save:Save params to a file
    A. "<file>", "File name (use default if not given)"
  4. dump:Dump params from a file
    "<file>", "File name (use default if not given)"
  5. select:Select default file
    "<file>", "File name"
  6. select-backup:Select default file
    "<file>", "File name"
  7. show:Show parameter values
    'a', "Show all parameters (not just used)"
    'c', "Show only changed params (unused too)"
    'q', "quiet mode, print only param value (name needs to be exact)"
    "<filter>", "Filter by param name (wildcard at end allowed, eg. sys_*)"
  8. show-for-airframe:Show changed params for airframe config
  9. status:Print status of parameter system
  10. set:Set parameter to a value
    "<param_name> <value>", "Parameter name and value to set"
    "fail", "If provided, let the command fail if param is not found"
  11. set-default:Set parameter default to a value
    's', "If provided, silent errors if parameter doesn't exists"
    "<param_name> <value>", "Parameter name and value to set"
    "fail", "If provided, let the command fail if param is not found"
  12. compare:Compare a param with a value. Command will succeed if equal
    's', "If provided, silent errors if parameter doesn't exists"
    "<param_name> <value>", "Parameter name and value to compare"
  13. greater:"Compare a param with a value. Command will succeed if param is greater than the value"
    's', "If provided, silent errors if parameter doesn't exists"
    "<param_name> <value>", "Parameter name and value to compare"
    "<param_name> <value>", "Parameter name and value to compare"
  14. touch:Mark a parameter as used
    "<param_name1> [<param_name2>]", "Parameter name (one or more)"
  15. reset:Reset only specified params to default
    "<param1> [<param2>]", "Parameter names to reset (wildcard at end allowed)"
  16. reset_all:Reset all params to default
    "<exclude1> [<exclude2>]", "Do not reset matching params (wildcard at end allowed)"
  17. index:Show param for a given index
    "<index>", "Index: an integer >= 0"
  18. index_used:Show used param for a given index
    "<index>", "Index: an integer >= 0"
  19. find:Show index of a param
    "<param>", "param name"

ver

Tool to print various version information

  1. hw:Hardware architecture
  2. mcu:MCU info
  3. git:git version information
  4. bdate:Build date and time
  5. gcc:Compiler info
  6. bdate:Build date and time
  7. px4guid:PX4 GUID
  8. uri:Build URI
  9. hwcmp:Compare hardware version (returns 0 on match)
    "<hw> [<hw2>]","Hardware to compare against (eg. PX4_FMU_V4). An OR comparison is used if multiple are specified"
  10. hwtypecmp:Compare hardware type (returns 0 on match)
    "<hwtype> [<hwtype2>]","Hardware type to compare against (eg. V2). An OR comparison is used if multiple are specified"

netman

  1. show:Display the current persistent network settings to the console.
  2. update:Check SD card for net.cfg and update network persistent network settings.
    'i', "eth0","Set the interface name"
  3. save:Save the current network parameters to the SD card.

开始启动

  109     #
  110     # Set the parameter file the board supports params on
  111     # MTD device.
  112     #
  113     if mft query -q -k MTD -s MTD_PARAMETERS -v /fs/mtd_params                                                                                                                                                  
  114     then
  115         set PARAM_FILE /fs/mtd_params
  116     fi

如果MTD_PARAMETERS等于/fs/mtd_params,则修改PARAM_FILE/fs/mtd_params(该变量在SDCARD初始化后为/fs/microsd/params,即参数文件原先默认是在SDCARD上,这里改为从mtd设备上取参数文件)。

  118     #
  119     # Load parameters.
  120     #
  121     # if the board has a storage for (factory) calibration data
  122     if mft query -q -k MTD -s MTD_CALDATA -v /fs/mtd_caldata
  123     then
  124         param load /fs/mtd_caldata
  125     fi

检查是否有板载校验数据,有的话载入。

  127     param select $PARAM_FILE
  128     if ! param import
  129     then
  130 +--- 28 行: echo "ERROR [init] param import failed"-------------------------------------------------------------------------------------------------------------------------------------------------------------
  158     fi

select参数文件,失败的情况不看。

  160     if [ $SDCARD_AVAILABLE = yes ]
  161     then
  162         param select-backup $PARAM_BACKUP_FILE
  163     fi

如果SDCARD有效的话,选中备份文件。

  165     if ver hwcmp PX4_FMU_V5X PX4_FMU_V6X                                                                                                                                                                        
  166     then
  167         netman update -i eth0
  168     fi

如果硬件版本是V5X或者V6X则设置网卡名称eth0

  170     #
  171     # If the airframe has been previously reset SYS_AUTCONFIG will have been set to 1 and other params will be reset on the next boot.
  172     #
  173     if param greater SYS_AUTOCONFIG 0
  174     then
  175         # Reset params except Airframe, RC calibration, sensor calibration, flight modes, total flight time, and next flight UUID.
  176         param reset_all SYS_AUTOSTART RC* CAL_* COM_FLTMODE* LND_FLIGHT* TC_* COM_FLIGHT*
  177     fi

如果参数 SYS_AUTOCONFIG大于 0,则重置SYS_AUTOSTART RC* CAL_* COM_FLTMODE* LND_FLIGHT* TC_* COM_FLIGHT*这些参数(*是通配符)

  179     #
  180     # Optional board architecture defaults: rc.board_arch_defaults
  181     #
  182     set BOARD_ARCH_RC_DEFAULTS ${R}etc/init.d/rc.board_arch_defaults
  183     if [ -f $BOARD_ARCH_RC_DEFAULTS ]
  184     then
  185         echo "Board architecture defaults: ${BOARD_ARCH_RC_DEFAULTS}"
  186         . $BOARD_ARCH_RC_DEFAULTS
  187     fi
  188     unset BOARD_ARCH_RC_DEFAULTS

如果有${R}etc/init.d/rc.board_arch_defaultssource一下。

  190     #
  191     # Optional board defaults: rc.board_defaults
  192     #
  193     set BOARD_RC_DEFAULTS ${R}etc/init.d/rc.board_defaults                                                                                                                                                      
  194     if [ -f $BOARD_RC_DEFAULTS ]
  195     then
  196         echo "Board defaults: ${BOARD_RC_DEFAULTS}"
  197         . $BOARD_RC_DEFAULTS
  198     fi
  199     unset BOARD_RC_DEFAULTS

如果${R}etc/init.d/rc.board_defaults存在,则source一下。

  201     #
  202     # Set parameters and env variables for selected SYS_AUTOSTART.
  203     #
  204     set AUTOSTART_PATH etc/init.d/rc.autostart
  205     if ! param compare SYS_AUTOSTART 0
  206     then
  207         if param greater SYS_AUTOSTART 1000000
  208         then
  209             # Use external startup file
  210             if [ $SDCARD_AVAILABLE = yes ]
  211             then
  212                 set AUTOSTART_PATH etc/init.d/rc.autostart_ext
  213             else
  214                 echo "ERROR [init] SD card not mounted - trying to load airframe from ROMFS"
  215             fi
  216         fi
  217         . ${R}$AUTOSTART_PATH
  218     fi
  219     unset AUTOSTART_PATH

如果参数SYS_AUTOSTART不等于0,并且大于1000000则尝试加载自动启动脚本

  222     # Start the tone_alarm driver.
  223     # Needs to be started after the parameters are loaded (for CBRK_BUZZER).
  224     #
  225     tone_alarm start

不知道是啥,估计跟警报有关。

  227     #
  228     # Waypoint storage.
  229     # REBOOTWORK this needs to start in parallel.
  230     #
  231     if param compare -s SYS_DM_BACKEND 1
  232     then
  233         dataman start -r
  234     else
  235         if param compare SYS_DM_BACKEND 0
  236         then
  237             # dataman start default
  238             dataman start
  239         fi
  240     fi

dataman启动。下面是dataman的描述。

Description

Module to provide persistent storage for the rest of the system in form of a simple database through a C API.
Multiple backends are supported:

  • a file (eg. on the SD card)
  • RAM (this is obviously not persistent)

It is used to store structured data of different types: mission waypoints, mission state and geofence polygons.
Each type has a specific type and a fixed maximum amount of storage items, so that fast random access is possible.

 242     #
 243     # Start the socket communication send_event handler.
 244     #
 245     send_event start

启动send_event,下面是描述。

Description

Background process running periodically on the LP work queue to perform housekeeping tasks.
It is currently only responsible for tone alarm on RC Loss.

The tasks can be started via CLI or uORB topics (vehicle_command from MAVLink, etc.).

  247     #
  248     # Start the resource load monitor.
  249     #
  250     load_mon start

启动load_mon,描述如下:

Description

Background process running periodically on the low priority work queue to calculate the CPU load and RAM
usage and publish the cpuload topic.

On NuttX it also checks the stack usage of each process and if it falls below 300 bytes, a warning is output,
which will also appear in the log file.

  252     #
  253     # Start system state indicator.
  254     #
  255     rgbled start -X -q
  256     rgbled_ncp5623c start -X -q

启动3色led,指示系统状态。led初步看是I2C设备。

  258     #
  259     # Override parameters from user configuration file.
  260     #
  261     if [ -f $FCONFIG ]                                                                                                                                                                                          
  262     then
  263         echo "Custom: ${FCONFIG}"
  264         . $FCONFIG
  265     fi

set FCONFIG /fs/microsd/etc/config.txt

如果卡上有配置文件,就source一下。

  267     #
  268     # Check if UAVCAN is enabled, default to it for ESCs.
  269     #
  270     if param greater -s UAVCAN_ENABLE 0
  271     then
  272         # Start core UAVCAN module.
  273         if ! uavcan start
  274         then
  275             tune_control play error
  276         fi
  277     else
  278         if param greater -s CYPHAL_ENABLE 0
  279         then
  280             cyphal start
  281         fi
  282     fi

检查参数UAVCAN_ENABLE CYPHAL_ENABLE,优先启动uavcan,否则启动cyphal
tune_control用于发送警告或者通知
uavcan作用未知
cyphal作用未知
这部分作用估计是控制电调 —— ESC。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容