1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4 Date started: 12/21/2001
6 ------------- Copyright (C) 2001 Jon S. Berndt (jsb@hal-pc.org) -------------
8 This program is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 You should have received a copy of the GNU Lesser General Public License along with
19 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20 Place - Suite 330, Boston, MA 02111-1307, USA.
22 Further information about the GNU Lesser General Public License can also be found on
23 the world wide web at http://www.gnu.org.
26 --------------------------------------------------------------------------------
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
33 #ifndef FGSCRIPT_HEADER_H
34 #define FGSCRIPT_HEADER_H
36 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 #include "FGJSBBase.h"
42 #include "FGFDMExec.h"
43 #include <math/FGCondition.h>
46 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50 #define ID_FGSCRIPT "$Id$"
52 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
62 /** Encapsulates the JSBSim scripting capability.
63 <h4>Scripting support provided via FGScript.</h4>
65 <p>There is support for scripting provided in the FGScript
66 class. Commands are specified using the <em>Scripting
67 Directives for JSBSim</em>. The script file is in XML
68 format. A test condition (or conditions) can be set up in an event in a
69 script and when the condition evaluates to true, the specified
70 action[s] is/are taken. An event can be <em>persistent</em>,
71 meaning that at all times when the test condition evaluates to true
72 the specified <em>set</em> actions take place. When the set of
73 tests evaluates to true for a given
74 condition, an item may be set to another value. This value may
75 be a value, or a delta value, and the change from the
76 current value to the new value can be either via a step function,
77 a ramp, or an exponential approach. The speed of a ramp or
78 approach is specified via the time constant. Here is an example
79 illustrating the format of the script file:
83 <runscript name="C172-01A takeoff run">
85 This run is for testing the C172 altitude hold autopilot
88 <use aircraft="c172x"/>
89 <use initialize="reset00"/>
90 <run start="0.0" end="3000" dt="0.0083333">
92 <event name="engine start">
97 <set name="fcs/throttle-cmd-norm" value="1.0" action="FG_RAMP" tc ="0.5"/>
98 <set name="fcs/mixture-cmd-norm" value="0.87" action="FG_RAMP" tc ="0.5"/>
99 <set name="propulsion/magneto_cmd" value="3"/>
100 <set name="propulsion/starter_cmd" value="1"/>
103 <event name="set heading hold">
104 <!-- Set Heading when reach 5 ft -->
107 position/h-agl-ft >= 5
109 <set name="ap/heading_setpoint" value="200"/>
110 <set name="ap/attitude_hold" value="0"/>
111 <set name="ap/heading_hold" value="1"/>
114 <event name="set autopilot">
115 <!-- Set Autopilot for 20 ft -->
120 <set name="ap/altitude_setpoint" value="100.0" action="FG_EXP" tc ="2.0"/>
121 <set name="ap/altitude_hold" value="1"/>
122 <set name="fcs/flap-cmd-norm" value=".33"/>
125 <event name="set autopilot 2" persistent="true">
126 <!-- Set Autopilot for 6000 ft -->
131 <set name="ap/altitude_setpoint" value="6000.0"/>
134 <event name="Time Notify">
136 <condition> sim-time-sec >= 500 </condition>
139 <event name="Time Notify">
141 <condition> sim-time-sec >= 1000 </condition>
149 The first line must always be present - it identifies the file
150 as an XML format file. The second line
151 identifies this file as a script file, and gives a descriptive
152 name to the script file. Comments are next, delineated by the
153 <!-- and --> symbols. The aircraft and initialization files
154 to be used are specified in the "use" lines. Next,
155 comes the "run" section, where the conditions are
156 described in "event" clauses.</p>
157 @author Jon S. Berndt
161 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
165 class FGScript : public FGJSBBase
168 /// Default constructor
169 FGScript(FGFDMExec* exec);
171 /// Default destructor
174 /** Loads a script to drive JSBSim (usually in standalone mode).
175 The language is the Script Directives for JSBSim.
176 @param script the filename (including path name, if any) for the script.
177 @return true if successful */
178 bool LoadScript( string script );
180 /** This function is called each pass through the executive Run() method IF
181 scripting is enabled.
182 @return false if script should exit (i.e. if time limits are violated */
183 bool RunScript(void);
199 FGCondition *Condition;
208 vector <FGPropertyManager*> SetParam;
209 vector <FGPropertyManager*> NotifyProperties;
210 vector <eAction> Action;
212 vector <double> SetValue;
214 vector <double> newValue;
215 vector <double> OriginalValue;
216 vector <double> ValueSpan;
217 vector <bool> Transiting;
221 PrevTriggered = false;
235 value = new double(0.0);
243 vector <struct event> Events;
244 vector <LocalProps*> local_properties;
248 FGPropertyManager* PropertyManager;
249 void Debug(int from);
252 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%