1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4 Date started: 12/21/2001
6 ------------- Copyright (C) 2001 Jon S. Berndt (jon@jsbsim.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/FGFunction.h"
44 #include "math/FGCondition.h"
46 #include "input_output/FGXMLFileRead.h"
48 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52 #define ID_FGSCRIPT "$Id$"
54 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
60 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
64 /** Encapsulates the JSBSim scripting capability.
65 <h4>Scripting support provided via FGScript.</h4>
67 <p>There is support for scripting provided in the FGScript
68 class. Commands are specified using the <em>Scripting
69 Directives for JSBSim</em>. The script file is in XML
70 format. A test condition (or conditions) can be set up in an event in a
71 script and when the condition evaluates to true, the specified
72 action[s] is/are taken. An event can be <em>persistent</em>,
73 meaning that at every time the test condition first evaluates to true
74 (toggling from false to true) then the specified <em>set</em> actions take
75 place. An event can also be defined to execute or evaluate continuously
76 while the condition is true. When the set of tests evaluates to true for a given
77 condition, an item may be set to another value. This value may
78 be a value, or a delta value, and the change from the
79 current value to the new value can be either via a step action,
80 a ramp, or an exponential approach. The speed of a ramp or exponential
81 approach is specified via the time constant. Here is an example
82 illustrating the format of the script file:
86 <runscript name="C172-01A takeoff run">
88 This run is for testing the C172 altitude hold autopilot
91 <use aircraft="c172x"/>
92 <use initialize="reset00"/>
93 <run start="0.0" end="3000" dt="0.0083333">
95 <event name="engine start">
100 <set name="fcs/throttle-cmd-norm" value="1.0" action="FG_RAMP" tc ="0.5"/>
101 <set name="fcs/mixture-cmd-norm" value="0.87" action="FG_RAMP" tc ="0.5"/>
102 <set name="propulsion/magneto_cmd" value="3"/>
103 <set name="propulsion/starter_cmd" value="1"/>
106 <event name="set heading hold">
107 <!-- Set Heading when reach 5 ft -->
110 position/h-agl-ft >= 5
112 <set name="ap/heading_setpoint" value="200"/>
113 <set name="ap/attitude_hold" value="0"/>
114 <set name="ap/heading_hold" value="1"/>
117 <event name="set autopilot">
118 <!-- Set Autopilot for 20 ft -->
123 <set name="ap/altitude_setpoint" value="100.0" action="FG_EXP" tc ="2.0"/>
124 <set name="ap/altitude_hold" value="1"/>
125 <set name="fcs/flap-cmd-norm" value=".33"/>
128 <event name="set autopilot 2" persistent="true">
129 <!-- Set Autopilot for 6000 ft -->
134 <set name="ap/altitude_setpoint" value="6000.0"/>
137 <event name="Time Notify">
139 <condition> sim-time-sec >= 500 </condition>
142 <event name="Time Notify">
144 <condition> sim-time-sec >= 1000 </condition>
152 The first line must always be present - it identifies the file
153 as an XML format file. The second line
154 identifies this file as a script file, and gives a descriptive
155 name to the script file. Comments are next, delineated by the
156 <!-- and --> symbols. The aircraft and initialization files
157 to be used are specified in the "use" lines. Next,
158 comes the "run" section, where the conditions are
159 described in "event" clauses.</p>
160 @author Jon S. Berndt
164 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
168 class FGScript : public FGJSBBase, public FGXMLFileRead
171 /// Default constructor
172 FGScript(FGFDMExec* exec);
174 /// Default destructor
177 /** Loads a script to drive JSBSim (usually in standalone mode).
178 The language is the Script Directives for JSBSim.
179 @param script the filename (including path name, if any) for the script.
180 @return true if successful */
181 bool LoadScript( string script );
183 /** This function is called each pass through the executive Run() method IF
184 scripting is enabled.
185 @return false if script should exit (i.e. if time limits are violated */
186 bool RunScript(void);
188 void ResetEvents(void) {
189 for (unsigned int i=0; i<Events.size(); i++) Events[i].reset();
206 FGCondition *Condition;
216 vector <FGPropertyManager*> SetParam;
217 vector <FGPropertyManager*> NotifyProperties;
218 vector <eAction> Action;
220 vector <double> SetValue;
222 vector <double> newValue;
223 vector <double> OriginalValue;
224 vector <double> ValueSpan;
225 vector <bool> Transiting;
226 vector <FGFunction*> Functions;
233 Notify = Notified = false;
249 LocalProps(double initial_value=0) {
250 value = new double(initial_value);
258 vector <struct event> Events;
259 vector <LocalProps*> local_properties;
263 FGPropertyManager* PropertyManager;
264 void Debug(int from);
267 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%