研究PX4一段时间,发现绕了很大的圈子,分析NUTTX系统是费时费力的,其实大可不必,Nuttx只是一个操作系统,作为飞控程序的载体,实际上我们只需要分析启动脚本就可以了。如何找到启动脚本及前面系统的启动本分的分析,参看一篇博文,写的非常好,在这里谢谢了。
http://blog.chinaunix.net/uid-29786319-id-4393303.html
对于os_start()的分析,这篇写的很不错。
http://blog.csdn.net/zhumaill/article/details/23261543
博文内容不贴了,最终作者,如查看请移步原帖。
前人的工作不说了,直说下他们的分析是基于前面版本,我下到的代码版本已经更新了许多,不过大差不差,对比着看肯定是可以看懂的。现在直接分析现有版本的rCS启动脚本。
sercon
set MODE autostart #设置启动变量模式
set FRC /fs/microsd/etc/rc.txt #设置了几个文件路径,后面估计用到
set FCONFIG /fs/microsd/etc/config.txt
set TUNE_ERR ML<<CP4CP4CP4CP4CP4
set LOG_FILE /fs/microsd/bootlog.txt
if mount -t vfat /dev/mmcsd0 /fs/microsd #挂载SD卡,失败成功声音不同
then
echo "[i] microSD mounted: /fs/microsd"
tone_alarm start
else
tone_alarm MBAGP
if mkfatfs /dev/mmcsd0
then
if mount -t vfat /dev/mmcsd0 /fs/microsd
then
echo "[i] microSD card formatted"
else
echo "[i] format failed"
tone_alarm MNBG
set LOG_FILE /dev/null
fi
else
set LOG_FILE /dev/null
fi
fi
if [ -f $FRC ] #找SD卡上的启动文件,如果发现,转入custom mode
then
echo "[i] Executing script: $FRC"
sh $FRC
set MODE custom
fi
unset FRC
if [ $MODE == autostart ]
then
#如果是autostart mode,尝试得到USB指令,新版本将其放到了这,系统启动就可以接受usb指令
nshterm /dev/ttyACM0 &
uorb start #启动uorb uorb为系统间非常重要的消息传递机制
set PARAM_FILE /fs/microsd/params #设置参数文件的路径,默认在SD卡里面
if mtd start #如果EEPROM启动成功,将参数设置路径设置到EEPROM中,PIXhawk中有 then #EEPROM,这就是在内存卡中无参数配置文件的原因
set PARAM_FILE /fs/mtd_params
fi
param select $PARAM_FILE #加载参数
if param load
then
echo "[param] Loaded: $PARAM_FILE"
else
echo "[param] FAILED loading $PARAM_FILE"
if param reset
then
fi
fi
if param compare RC_MAP_THROTTLE 0 #如果RC_MAP_throttle 是0 下面的几个比较都是对遥控器进行检测
then
if param compare RC3_MIN 1000
then
else
param set RC_MAP_THROTTLE 3
fi
fi
if param compare RC_MAP_ROLL 0
then
if param compare RC1_MIN 1000
then
else
param set RC_MAP_ROLL 1
fi
fi
if param compare RC_MAP_PITCH 0
then
if param compare RC2_MIN 1000
then
else
param set RC_MAP_PITCH 2
fi
fi
if param compare RC_MAP_YAW 0
then
if param compare RC4_MIN 1000
then
else
param set RC_MAP_YAW 4 #遥控器的四个通道
fi
fi
if rgbled start #如果有外界LED
then
else
if blinkm start
then
blinkm systemstate
fi
fi
set HIL no #设置一些默认参数,后面会被改动,不一一列举
set VEHICLE_TYPE none
set MIXER none
set MIXER_AUX none
set OUTPUT_MODE none
set PWM_OUT none
set PWM_RATE none
set PWM_DISARMED none
set PWM_MIN none
set PWM_MAX none
set PWM_AUX_OUT none
set PWM_AUX_RATE none
set PWM_AUX_DISARMED none
set PWM_AUX_MIN none
set PWM_AUX_MAX none
set FAILSAFE_AUX none
set MK_MODE none
set FMU_MODE pwm
set AUX_MODE pwm
set MAVLINK_F default
set EXIT_ON_END no
set MAV_TYPE none
set LOAD_DAPPS yes
set GPS yes
set GPS_FAKE no
set FAILSAFE none
if param compare SYS_AUTOCONFIG 1 #如果SYS_AUTOCONFIG 是1
then
param reset_nostart RC*
set AUTOCNF yes
else
set AUTOCNF no
fi
if param compare SYS_USE_IO 1 #如果SYS_USE_IO 1 是1
then
set USE_IO yes
else
set USE_IO no
fi
if param compare SYS_AUTOSTART 0 #如果SYS_AUTOSTART是0
then
echo "[i] No autostart"
else #如果SYS_AUTOSTART是1的话启动rc_autostart,关于此文件会另 sh /etc/init.d/rc.autostart #外开帖分析
fi
unset MODE
if [ -f $FCONFIG ] #如果用户配置文件config.txt存在,执行里面的设置
then
echo "[i] Custom config: $FCONFIG"
sh $FCONFIG
fi
unset FCONFIG
if [ $AUTOCNF == yes ] ##如果AUTOCNF为1,清零SYS_AUTOCONFIG
then
param set SYS_AUTOCONFIG 0
param save
fi
unset AUTOCNF
set IO_PRESENT no #初始化IO_PRESENT为0
if [ $USE_IO == yes ] #如果USE_IO为1,对Firmware更新
then
if [ -f /etc/extras/px4io-v2_default.bin ]
then
set IO_FILE /etc/extras/px4io-v2_default.bin
else
set IO_FILE /etc/extras/px4io-v1_default.bin
fi
if px4io checkcrc ${IO_FILE}
then
echo "PX4IO CRC OK" >> $LOG_FILE
set IO_PRESENT yes
else
echo "PX4IO Trying to update" >> $LOG_FILE
tone_alarm MLL32CP8MB
if px4io start
then
if px4io safety_on
then
else
px4io stop
fi
fi
if px4io forceupdate 14662 ${IO_FILE}
then
usleep 500000
if px4io checkcrc $IO_FILE
then
echo "PX4IO CRC OK after updating" >> $LOG_FILE
tone_alarm MLL8CDE
set IO_PRESENT yes
else
echo "ERROR: PX4IO update failed" >> $LOG_FILE
tone_alarm $TUNE_ERR
fi
else
echo "ERROR: PX4IO update failed" >> $LOG_FILE
tone_alarm $TUNE_ERR
fi
fi
unset IO_FILE
if [ $IO_PRESENT == no ]
then
echo "[i] ERROR: PX4IO not found"
echo "ERROR: PX4IO not found" >> $LOG_FILE
tone_alarm $TUNE_ERR
fi
fi
if [ $OUTPUT_MODE == none ] #判断OUTPUT_MODE,初始化为none,最终执行结果将 #OUTPUT_MODE 设置成了fmu
then
if [ $USE_IO == yes ]
then
set OUTPUT_MODE io
else
set OUTPUT_MODE fmu
fi
fi
if [ $OUTPUT_MODE == io -a $IO_PRESENT != yes ] #设置FMU mode
then
set OUTPUT_MODE none
echo "[i] ERROR: PX4IO not found, disabling output"
if ver hwcmp PX4FMU_V1
then
set FMU_MODE serial
fi
fi
if [ $OUTPUT_MODE == ardrone ]
then
set FMU_MODE gpio_serial
fi
if [ $HIL == yes ]
then
set OUTPUT_MODE hil
set GPS no
if ver hwcmp PX4FMU_V1
then
set FMU_MODE serial
fi
fi
unset HIL
if dataman start
then
fi
sh /etc/init.d/rc.sensors #启动各种传感器
if [ $GPS == yes ] #启动GPS
then
if [ $GPS_FAKE == yes ]
then
echo "[i] Faking GPS"
gps start -f
else
gps start
fi
fi
unset GPS
unset GPS_FAKE
commander start
set TTYS1_BUSY no
if [ $OUTPUT_MODE != none ]
then
if [ $OUTPUT_MODE == uavcan_esc ]
then
if param compare UAVCAN_ENABLE 0
then
echo "[i] OVERRIDING UAVCAN_ENABLE = 1"
param set UAVCAN_ENABLE 1
fi
fi
if [ $OUTPUT_MODE == io -o $OUTPUT_MODE == uavcan_esc ]
then
if px4io start
then
sh /etc/init.d/rc.io
else
echo "ERROR: PX4IO start failed" >> $LOG_FILE
tone_alarm $TUNE_ERR
fi
fi
if [ $OUTPUT_MODE == fmu -o $OUTPUT_MODE == ardrone ]
then
if fmu mode_$FMU_MODE
then
echo "[i] FMU mode_$FMU_MODE started" #这个是我们的选项
else
echo "[i] ERROR: FMU mode_$FMU_MODE start failed"
echo "ERROR: FMU start failed" >> $LOG_FILE
tone_alarm $TUNE_ERR
fi
if ver hwcmp PX4FMU_V1 #判断版本,启动pwm
then
if [ $FMU_MODE == pwm -o $FMU_MODE == gpio ]
then
set TTYS1_BUSY yes
fi
if [ $FMU_MODE == pwm_gpio -o $OUTPUT_MODE == ardrone ]
then
set TTYS1_BUSY yes
fi
fi
fi
if [ $OUTPUT_MODE == mkblctrl ]
then
set MKBLCTRL_ARG ""
if [ $MKBLCTRL_MODE == x ]
then
set MKBLCTRL_ARG "-mkmode x"
fi
if [ $MKBLCTRL_MODE == + ]
then
set MKBLCTRL_ARG "-mkmode +"
fi
if mkblctrl $MKBLCTRL_ARG
then
echo "[i] MK started"
else
echo "[i] ERROR: MK start failed"
echo "ERROR: MK start failed" >> $LOG_FILE
tone_alarm $TUNE_ERR
fi
unset MKBLCTRL_ARG
fi
unset MK_MODE
if [ $OUTPUT_MODE == hil ]
then
if pwm_out_sim mode_port2_pwm8
then
echo "[i] PWM SIM output started"
else
echo "[i] ERROR: PWM SIM output start failed"
echo "ERROR: PWM SIM output start failed" >> $LOG_FILE
tone_alarm $TUNE_ERR
fi
fi
if [ $IO_PRESENT == yes ]
then
if [ $OUTPUT_MODE != io ]
then
if px4io start
then
echo "[i] PX4IO started"
sh /etc/init.d/rc.io
else
echo "[i] ERROR: PX4IO start failed"
echo "ERROR: PX4IO start failed" >> $LOG_FILE
tone_alarm $TUNE_ERR
fi
fi
else
if [ $OUTPUT_MODE != fmu -a $OUTPUT_MODE != ardrone ]
then
if fmu mode_$FMU_MODE
then
echo "[i] FMU mode_$FMU_MODE started"
else
echo "[i] ERROR: FMU mode_$FMU_MODE start failed"
echo "ERROR: FMU mode_$FMU_MODE start failed" >> $LOG_FILE
tone_alarm $TUNE_ERR
fi
if ver hwcmp PX4FMU_V1
then
if [ $FMU_MODE == pwm -o $FMU_MODE == gpio ]
then
set TTYS1_BUSY yes
fi
if [ $FMU_MODE == pwm_gpio -o $OUTPUT_MODE == ardrone ]
then
set TTYS1_BUSY yes
fi
fi
fi
fi
fi
if [ $MAVLINK_F == default ] #下面几行启动MSVLINK
then
if [ $TTYS1_BUSY == yes ]
then
set MAVLINK_F "-r 1200 -d /dev/ttyS0"
set EXIT_ON_END yes
else
set MAVLINK_F "-r 1200"
fi
fi
mavlink start $MAVLINK_F
unset MAVLINK_F
if ver hwcmp PX4FMU_V2 #如果板子版本为V2,
then
if param compare SYS_COMPANION 921600 #检测mavlink存在,如果在则启动,这个不知道是啥
then
mavlink start -d /dev/ttyS2 -b 921600 -m onboard -r 80000 -x
fi
if param compare SYS_COMPANION 57600 #检测mavlink存在,如果在则启动,数传
then
mavlink start -d /dev/ttyS2 -b 57600 -m onboard -r 5000 -x
fi
if param compare SYS_COMPANION 157600 #检测mavlink存在,如果则启动,USB
then
mavlink start -d /dev/ttyS2 -b 57600 -m osd -r 1000
fi
if param compare SENS_EN_LL40LS 1 #检测某个距离传感器,官网上有介绍
then
set AUX_MODE pwm4
fi
if param greater TRIG_MODE 0 #摄像头触发模式检测
then
set MIXER_AUX none
set AUX_MODE none
camera_trigger start
fi
fi
sh /etc/init.d/rc.uavcan #启动了两个脚本,主要用于启动uavcan和sdlog这两个模块
sh /etc/init.d/rc.logging #
if [ $OUTPUT_MODE == ardrone ]
then
ardrone_interface start -d /dev/ttyS1
fi
if [ $VEHICLE_TYPE == fw ] #固定翼的设置,暂时不是研究对象
then
echo "[i] FIXED WING"
if [ $MIXER == none ]
then
set MIXER AERT
fi
if [ $MAV_TYPE == none ]
then
set MAV_TYPE 1
fi
param set MAV_TYPE $MAV_TYPE
sh /etc/init.d/rc.interface
if [ $LOAD_DAPPS == yes ]
then
sh /etc/init.d/rc.fw_apps
fi
fi
if [ $VEHICLE_TYPE == mc ] #多旋翼设置
then
echo "[i] MULTICOPTER"
if [ $MIXER == none ]
then
echo "Mixer undefined"
fi
if [ $MAV_TYPE == none ]
then
if [ $MIXER == quad_x -o $MIXER == quad_+ ]
then
set MAV_TYPE 2
fi
if [ $MIXER == quad_w -o $MIXER == sk450_deadcat ]
then
set MAV_TYPE 2
fi
if [ $MIXER == tri_y_yaw- -o $MIXER == tri_y_yaw+ ]
then
set MAV_TYPE 15
fi
if [ $MIXER == hexa_x -o $MIXER == hexa_+ ]
then
set MAV_TYPE 13
fi
if [ $MIXER == hexa_cox ]
then
set MAV_TYPE 13
fi
if [ $MIXER == octo_x -o $MIXER == octo_+ ]
then
set MAV_TYPE 14
fi
if [ $MIXER == octo_cox ]
then
set MAV_TYPE 14
fi
fi
if [ $MAV_TYPE == none ]
then
echo "Unknown MAV_TYPE"
param set MAV_TYPE 2
else
param set MAV_TYPE $MAV_TYPE
fi
sh /etc/init.d/rc.interface #PWM的设置和接口
if [ $LOAD_DAPPS == yes ]
then
sh /etc/init.d/rc.mc_apps #核心中的核心,在这里启动了位置和高度的计算算法,具体看这个文件
fi
fi
if [ $VEHICLE_TYPE == vtol ] #垂直起降机神马的云云,本人只研究四旋翼
then
echo "[init] Vehicle type: VTOL"
if [ $MIXER == none ]
then
echo "Default mixer for vtol not defined"
fi
if [ $MAV_TYPE == none ]
then
if [ $MIXER == caipirinha_vtol ]
then
set MAV_TYPE 19
fi
if [ $MIXER == firefly6 ]
then
set MAV_TYPE 21
fi
if [ $MIXER == quad_x_pusher_vtol ]
then
set MAV_TYPE 22
fi
fi
if [ $MAV_TYPE == none ]
then
echo "Unknown MAV_TYPE"
param set MAV_TYPE 19
else
param set MAV_TYPE $MAV_TYPE
fi
sh /etc/init.d/rc.interface
if [ $LOAD_DAPPS == yes ]
then
sh /etc/init.d/rc.vtol_apps
fi
fi
if [ $VEHICLE_TYPE == rover ] #无关飞机类型
then
set MAV_TYPE 10
sh /etc/init.d/rc.interface
if [ $LOAD_DAPPS == yes ]
then
sh /etc/init.d/rc.axialracing_ax10_apps
fi
param set MAV_TYPE 10
fi
unset MIXER
unset MAV_TYPE
unset OUTPUT_MODE
navigator start #启动导航
if [ $VEHICLE_TYPE == none ]
then
echo "[i] No autostart ID found"
fi
set FEXTRAS /fs/microsd/etc/extras.txt
if [ -f $FEXTRAS ]
then
echo "[i] Addons script: $FEXTRAS"
sh $FEXTRAS
fi
unset FEXTRAS
if [ $LOG_FILE == /dev/null ]
then
echo "[i] No microSD card found"
tone_alarm error
fi
fi
unset TUNE_ERR
mavlink boot_complete
if param compare SENS_EN_LL40LS 1
then
if pwm_input start
then
if ll40ls start pwm
then
fi
fi
fi
if ver hwcmp PX4FMU_V2
then
px4flow start & #v2板启动
fi
if [ $EXIT_ON_END == yes ]
then
echo "Exit from nsh"
exit
fi
unset EXIT_ON_END
rcS启动
最新推荐文章于 2024-05-10 08:54:09 发布