再分割线

编译部分暂时放放,先看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。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,539评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,911评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,337评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,723评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,795评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,762评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,742评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,508评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,954评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,247评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,404评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,104评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,736评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,352评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,557评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,371评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,292评论 2 352

推荐阅读更多精彩内容