1.说明
JSBSim是一个用C++实现的开源跨平台飞行器动力学模型软件。Flightgear也采用了JSBSim作为其中的飞行器动力模型之一。JSBSim同样可以作为一个单独的动力学模型软件进行运行,本文的目标是介绍在Windows命令行环境下如何使用JSBSim程序。
2.使用方法
2.1编译和运行
编译JSBSim工程的方法可以参考《使用VS2015编译JSBSim》,同样也可以参考英文版的《Building_JSBSim_with_Visual_Cpp_2010》,在编译完成之后,可以得到一个可执行文件JSBSim.exe,配合相应的飞行器文件、初始化文件和脚本文件即可实现飞行器飞行过程的仿真。
JSBSim.exe需要从命令行中输入相应参数指定飞行器和飞行器的初始状态才能开始仿真运行,在打开cmd.exe,切换到编译好的JSBSim工程目录下,并输入” Release\JSBSim.exe --help”,得到结果如下。
C:\jsbsim-master>Release\JSBSim.exe --help
JSBSim version 1.0.0 Aug 6 2018 17:32:13
Usage: jsbsim [script file name] [output file names] <options>
options:
--help returns this message
--version returns the version number
--outputlogfile=<filename> sets (overrides) the name of a data output file
--logdirectivefile=<filename> specifies the name of a data logging directives file
(can appear multiple times)
--root=<path> specifies the JSBSim root directory (where aircraft/, engine/, etc. reside)
--aircraft=<filename> specifies the name of the aircraft to be modeled
--script=<filename> specifies a script to run
--realtime specifies to run in actual real world time
--nice specifies to run at lower CPU usage
--nohighlight specifies that console output should be pure text only (no color)
--suspend specifies to suspend the simulation after initialization
--initfile=<filename> specifies an initilization file
--catalog specifies that all properties for this aircraft model should be printed
(catalog=aircraftname is an optional format)
--property=<name=value> e.g. --property=simulation/integrator/rate/rotational=1
--simulation-rate=<rate (double)> specifies the sim dT time or frequency
If rate specified is less than 1, it is interpreted as
a time step size, otherwise it is assumed to be a rate in Hertz.
--end=<time (double)> specifies the sim end time
NOTE: There can be no spaces around the = sign when
an option is followed by a filename
下面将对这些输入参数的用处进行说明。
2.2命令行输入选项说明
--help
打印帮助信息
--version
打印版本信息
--outputlogfile=<filename>
sets (overrides) the name of a data output file
设置或者(覆盖)数据记录文件名
举例:--outputlogfile=c172x.csv
效果:数据记录文件名会改为c172x.csv
--logdirectivefile=<filename>
specifies the name of a data logging directives file(can appear multiple times)
指定数据日志指示文件的名称
--root=<path>
specifies the JSBSim root directory (where aircraft/, engine/, etc. reside)
指定运行目录,如果指定了这个选项,JSBSim在运行时就会在指定目录下寻找相应的aircraft/, engine/目录。
--aircraft=<filename>
specifies the name of the aircraft to be modeled
指定飞行器的名称,如果指定了这个选项,JSBSim就会在aircraft目录下寻找相应的飞行器定义文件来建立飞行器模型。
举例:--aircraft=c172x
效果:JSBSim会加载aircraft\c172x\c172x.xml,然后根据该xml定义建立飞行器模型
--script=<filename>
specifies a script to run
指定要执行的脚本名称,如果指定了这个选项,JSBSim就会在运行目录下寻找对应的脚本加载并执行
举例:--script=scripts\c1723.xml
效果:JSBSim会加载scripts\c1723.xml,然后按照该脚本中指定的aircraft和engine建立飞行器模型,并执行其中指定的操作。
--realtime
specifies to run in actual real world time
指定仿真进行实时运行,即仿真时间和真实时间是同步的
--nice
specifies to run at lower CPU usage
指定仿真以低CPU占用率运行(方法是在每次迭代计算中间加上sleep)
--nohighlight
specifies that console output should be pure text only (no color)
指定命令行输出不带颜色(应该是在Linux系统中运行时才需要,因为在Windows中没有效果)
--suspend
specifies to suspend the simulation after initialization
在仿真初始化完成后暂停仿真计算
--initfile=<filename>
specifies an initilization file
指定初始化文件,需要和—aircraft配合使用
举例:--aircraft=c172x –initfile=reset00
效果:JSBSim会加载aircraft\c172x\reset00.xml文件,并按照该文件指定的初始状态初始化飞行器模型。
--catalog
specifies that all properties for this aircraft model should be printed
(catalog=aircraftname is an optional format)
开启日志记录,在仿真过程中打印飞行器的所有属性
--property=<name=value>
e.g. --property=simulation/integrator/rate/rotational=1
指定JSBSim仿真过程中某一个属性的值
--simulation-rate=<rate (double)>
specifies the sim dT time or frequency
If rate specified is less than 1, it is interpreted as
a time step size, otherwise it is assumed to be a rate in Hertz.
设置仿真频率或者步长,如果rate小于1,那么这个选项就是设置仿真步长等于rate,如果rate大于1,那么这个选项就是设置仿真频率的。
举例:--simulation-rate=0.01
举例:--simulation-rate=100
效果:两种选项效果相同,都是设置仿真步长为0.01,即仿真频率为100Hz
--end=<time (double)>
specifies the sim end time
指定仿真终止时间
举例:--end=100
效果:JSBSim仿真会在仿真时间到达100秒时停止
3.简单运行
为了简化运行仿真的操作,本文编写了批处理脚本来运行JSBSim,一个运行JSBSim仿真的最简脚本如下,该脚本将会指定JSBSim加载一个scripts目录下的c1723.xml,然后按照该脚本文件中的指令执行相应操作,完成飞行器仿真。
simplerun.bat
Release\JSBSim --script=scripts\c1723.xml
实时上,使用JSBSim命令仿真的方法有两种,分别是加载script脚本运行仿真或者是直接加载飞行器定义文件进行仿真,接下来将对两种方法的使用进行说明。
4.加载Script脚本运行仿真
4.1脚本运行示例
一个加载script文件运行JSBSim仿真的批处理脚本如下
runJSBSim_withscript.bat
::Set the scriptname
set scriptname=c1723
::Del old outputlogfile
del -Q %scriptname%.csv
Release\JSBSim --script=scripts\%scriptname%.xml --outputlogfile=%scriptname%.csv > JSBSim.out
pause
该脚本指定了script文件名为c1723.xml,并通过“--script=scripts\%scriptname%.xml”指定JSBSim加载scripts目录下的c1723.xml文件,“--outputlogfile=%scriptname%.csv”选项指定了数据记录的文件名为c1723.csv,仿真结束后我们就能找到这个文件,并查看其中记录的数据,“> JSBSim.out”则会将JSBSim命令运行时的输出记录到JSBSim.out文件中,以备查看。
4.2Script脚本说明
Script文件中给出了JSBSim要加载的飞行器和初始化文件,并且通过一系列操作对仿真进行了初始化和运行控制,我们打开c1723.xml,找到<use>标签和<run>标签。
scripts\c1723.xml
<use aircraft="c172x" initialize="reset00"/>
<run start="0.0" end="200" dt="0.00833333333333333333">
<use>标签中的aircraft=“c172x”指定了要使用的飞行器名称为c172x,JSBSim将会加载在工程目录下aircraft\c172x\c172x.xml飞行器定义文件建立飞行器的模型,initialize=”reset00”指定了初始化文件名为reset00,JSBSim将会加载和c172x.xml同目录下的reset00.xml来执行对飞行器状态的初始化工作。而在标签<run>中,start和end分别指定了仿真开始和结束的时间,为0~200秒,dt="0.00833333333333333333代表仿真的步长为0.00833333333333333333秒,意味着我们的仿真迭代计算频率为120Hz。
我们在<run>标签之后,可以看到跟着一些属性设置标签,这些数据script脚本的初始化工作。
scripts\c1723.xml
<!-- Initialize with brakes on -->
<property> simulation/notify-time-trigger </property>
<property value="1"> simulation/run_id </property>
<property value="&on;"> fcs/left-brake-cmd-norm </property>
<property value="&on;"> fcs/right-brake-cmd-norm </property>
<property value="&on;"> fcs/center-brake-cmd-norm </property>
<property value="3.49"> guidance/specified-heading-rad </property>
<property value="&specifiedHeading;"> guidance/heading-selector-switch </property>
<property value="2498"> simulation/randomseed </property>
在<run>标签之内还包含着一些<event>标签,其中每一个<event>标签代表着仿真运行过程中要执行的一个事件,这个事件会在特定的条件下触发执行对应的操作并且显示对应的信息。<event name="engine start">就代表了一个启动发动机的事件。其中<description>标签会显示该事件的描述;<condition>标签代表了事件触发的条件;” simulation/sim-time-sec >= 0.25”意味着该事件会在仿真时间大于0.25秒时触发;而一系列的<set>标签则会设置相应的属性变量,完成事件的操作;<notify>标签中包含着事件触发时会打印的变量,用于显示信息。
scripts\c1723.xml
<event name="engine start">
<description>Start the engine and set roll control to heading hold</description>
<condition> simulation/sim-time-sec >= 0.25 </condition>
<set name="fcs/throttle-cmd-norm" value="1.0" action="FG_RAMP" tc ="0.5"/>
<set name="propulsion/magneto_cmd" value="&both;"/>
<set name="propulsion/starter_cmd" value="&on;"/>
<set name="ap/roll-attitude-mode" value="&rollAngleHold;"/>
<set name="ap/autopilot-roll-on" value="&on;"/>
<notify>
<property caption="Sim Run ID: ">simulation/run_id</property>
<property>ap/hdg-roll-err-c1</property>
<property>accelerations/a-pilot-x-ft_sec2</property>
<!-- (省略一些内容以便节约空间) -->
<property>velocities/mach</property>
<property>velocities/vg-fps</property>
</notify>
</event>
Script文件中通常包含了不止一个event,其中的具体操作以及语法可以参考《JSBSimReferenceManual》和《JSBSim_Script_Tutorial_2010》。
4.3 output设置
我们接下来要关注的是仿真过程中记录的数据在哪里,以及如何设置要记录的数据。打开脚本所指定的c172x.xml文件,在文件的末尾处我们可以找到<output>标签,该标签的内容指定了文件记录的文件名,记录频率以及要记录那些数据。在<output>标签内,name="JSBout172B.csv”数据记录的文件名,该文件名可以通过命令行中的输入选项进行更改,而type="CSV"则指定了记录的类型,type同样可以指定其他类型的记录,具体可以参考《JSBSimReferenceManual》,rate=”10”代表了数据记录的频率,即每0.1秒记录一次数据。
aircraft\c172x\c172x.xml
<output name="JSBout172B.csv" type="CSV" rate="10">
<rates> ON </rates>
<velocities> ON </velocities>
<position> ON </position>
<atmosphere> OFF </atmosphere>
<fcs> ON </fcs>
<ground_reactions> OFF </ground_reactions>
<propulsion> ON </propulsion>
<simulation> ON </simulation>
<massprops> ON </massprops>
<forces> OFF </forces>
<moments> OFF </moments>
<aerosurfaces> OFF </aerosurfaces>
<property> position/vrp-gc-latitude_deg </property>
<property> position/vrp-longitude_deg </property>
<property> position/vrp-radius-ft </property>
<function name="velocities/pi-deg_sec">
<todegrees> <p> velocities/pi-rad_sec </p> </todegrees>
</function>
</output>
其中,<rates> ON </rates>将开启角速率数据的记录,同理其他的标签分别代表着速度、位置、大气数据、飞行控制等各类数据的记录开关,ON代表开,OFF代表关。而如果想要记录某些特定属性的数据,则需要在<output>标签中添加<property>标签内容,则JSBSim在仿真过程中就会记录对应的属性到文件中。
4.4仿真结果
运行批处理脚本后,我们就可以得到仿真结果,仿真结果存储在名为c1723.csv的文件中,文件中的数据用逗号进行分隔,可以使用Excel打开来查看,但是我们的最终目标是利用数据记录文件画出对应的曲线,从而分析我们的仿真效果。在本文所参考的教程中,一般都是使用gnuplot来画图的,由于我使用matlab更加顺手,所以写了一个m文件来执行画图操作,画出的部分仿真曲线如下:
5.加载aircraft文件运行仿真
上一章使用JSBSim加载了一个script脚本,完成了一个情景下的飞行仿真,接下来将介绍如何使用JSBSim直接加载飞行器定义文件,并且实现飞行仿真。
5.1运行示例
一个加载aircraft文件运行JSBSim仿真的批处理脚本如下
runJSBSim_withAircraft.bat
::设置使用的飞行器名称
set aircraftname=c172x
::设置初始化文件名称
set resetname=reset00
del -Q %aircraftname%.csv
Release\JSBSim --aircraft=\%aircraftname% --initfile=%resetname%^
--outputlogfile=%aircraftname%.csv --simulation-rate=120 --end=200 > JSBSim.out
pause
该脚本指定了要使用的飞行器文件名为c172x.xml,初始化文件名为reset00.xml。在加载script脚本运行仿真时,解算频率和仿真时间都由script文件决定,但是在加载指定飞行器进行仿真时需要通过命令行设置。其中“—simulation-rate=120”代表仿真解算频率为120Hz,即仿真步长为1/120秒,而“—end=200”则会使仿真在200秒时停止。
上述命令中加载的飞行器名称和初始化文件和c1723.xml文件中给出的是一样的,但是两种仿真的结果却必定是不同的。因为在script文件中还对飞行器进行了其他操作。而要通过命令行输入指令给飞行器,则需要使用“--property”选项来设置相应属性。以下批处理脚本中则给出了对应的属性设置实例,完成了飞行器打开发动机,打开刹车,打开自动驾驶仪等等操作,完成了从滑跑到起飞的仿真。
runJSBSim_withAircraft2.bat
::设置使用的飞行器名称
set aircraftname=c172x
::设置初始化文件名称
set resetname=reset00
del -Q %aircraftname%.csv
Release\JSBSim --aircraft=\%aircraftname% --initfile=%resetname%^
--outputlogfile=%aircraftname%.csv --simulation-rate=120 --end=200^
--property=fcs/throttle-cmd-norm=1.0^
--property=propulsion/magneto_cmd=1^
--property=propulsion/starter_cmd=1^
--property=fcs/left-brake-cmd-norm=0^
--property=fcs/right-brake-cmd-norm=0^
--property=fcs/center-brake-cmd-norm=0^
--property=ap/autopilot-roll-on=1^
--property=ap/altitude_setpoint=1000.0^
--property=ap/altitude_hold=1^
> JSBSim.out
pause
其中各个属性的设置可以参考script文件c1723.xml中的设置,但是由于命令行中只能在仿真过程中设置一次属性值,无法完成象script脚本那样的一系列操作,所以只能实现简单情境下的仿真。
5.2 飞行器(aircraft)定义文件说明
在JSBSim的工程中的aircraft文件夹下,存放着各种型号飞行器的定义文件。这些文件定义的飞行器的尺寸、重量、重心、气动系数、接地点以及所使用的发动机等信息。有了这些信息,JSBSim在加载这个aircraft文件之后就可以建立飞行器的数学模型,从而可以实现仿真。aircraft文件的具体语法可以参考JSBSim手册。
5.3初始化(initfile)文件说明
在命令行中加载aircraft文件运行仿真时,必须同时指定一个initfile,即初始化文件,否则就会报错。因为我们有了飞行器的数学模型,但是仍然需要给出这个飞行器的初始状态,才能开始运行仿真。一般要确定一个飞行器的状态,要给出飞行器的位置、速度、姿态、角速度。通过这些信息,结合飞行解算,我们就能确定一个飞行器的所有状态。
在本文给出的例子中,给出的初始化文件名为reset00.xml,该文件在和c172x.xml同一目录下,打开该文件内容如下:
reset00.xml
<?xml version="1.0"?>
<initialize name="reset00">
<!--
This file sets up the aircraft to start off
from the runway in preparation for takeoff.
-->
<ubody unit="FT/SEC"> 0.0 </ubody>
<vbody unit="FT/SEC"> 0.0 </vbody>
<wbody unit="FT/SEC"> 0.0 </wbody>
<longitude unit="DEG"> -95.163839 </longitude>
<latitude unit="DEG"> 29.593978 </latitude>
<phi unit="DEG"> 0.0 </phi>
<theta unit="DEG"> 0.0 </theta>
<psi unit="DEG"> 0.0 </psi>
<altitude unit="FT"> 4.305 </altitude>
<elevation unit="FT"> 0.0 </elevation>
<hwind> 0.0 </hwind>
</initialize>
从该初始化文件的注释可以看到,该文件给定义了飞行器起飞前在跑道上的初始状态,其中<ubody>、<vbody>、<wbody>三个标签给出了机体轴下飞行器的三轴速度;<longitude>、<latitude>、<altitude>给出了飞行器的经纬高;<phi>、<theta>、<psi>给出了飞行器的姿态角。
还有两个特殊的标签定义了地形和大气环境的数据:<elevation>给出了当地的当地的地形高程;<hwind>给出了机头方向的风速。
至于其他的状态量则有两种情况:未定义的角速率等状态量则默认为零;而有一些量比如迎角、侧滑角等其他参考系中同类的状态量可以通过模型解算之后计算得到等效的值。
5.4仿真结果
综上,我们使用JSBSim命令加载了一个aircraft定义文件,并且加载了对应的初始化文件,而且通过命令行设置了相应的属性,完成了飞行仿真,其中的仿真结果如下
资源文件:
[1]JSBSim运行文件+仿真结果+画图脚本(下载地址)
参考文献: