Branch data Line data Source code
1 : : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 : : Header: FGScript.h
3 : : Author: Jon Berndt
4 : : Date started: 12/21/2001
5 : :
6 : : ------------- Copyright (C) 2001 Jon S. Berndt (jon@jsbsim.org) -------------
7 : :
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
11 : : version.
12 : :
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
16 : : details.
17 : :
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.
21 : :
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.
24 : :
25 : : HISTORY
26 : : --------------------------------------------------------------------------------
27 : : 12/21/01 JSB Created
28 : :
29 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30 : : SENTRY
31 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
32 : :
33 : : #ifndef FGSCRIPT_HEADER_H
34 : : #define FGSCRIPT_HEADER_H
35 : :
36 : : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 : : INCLUDES
38 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39 : :
40 : : #include "FGJSBBase.h"
41 : : #include "FGFDMExec.h"
42 : : #include "math/FGFunction.h"
43 : : #include "math/FGCondition.h"
44 : : #include <vector>
45 : : #include "input_output/FGXMLFileRead.h"
46 : :
47 : : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 : : DEFINITIONS
49 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50 : :
51 : : #define ID_FGSCRIPT "$Id: FGScript.h,v 1.18 2010/04/11 13:44:42 jberndt Exp $"
52 : :
53 : : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 : : FORWARD DECLARATIONS
55 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56 : :
57 : : namespace JSBSim {
58 : :
59 : : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 : : CLASS DOCUMENTATION
61 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
62 : :
63 : : /** Encapsulates the JSBSim scripting capability.
64 : : <h4>Scripting support provided via FGScript.</h4>
65 : :
66 : : <p>There is support for scripting provided in the FGScript
67 : : class. Commands are specified using the <em>Scripting
68 : : Directives for JSBSim</em>. The script file is in XML
69 : : format. A test condition (or conditions) can be set up in an event in a
70 : : script and when the condition evaluates to true, the specified
71 : : action[s] is/are taken. An event can be <em>persistent</em>,
72 : : meaning that at every time the test condition first evaluates to true
73 : : (toggling from false to true) then the specified <em>set</em> actions take
74 : : place. An event can also be defined to execute or evaluate continuously
75 : : while the condition is true. When the set of tests evaluates to true for a given
76 : : condition, an item may be set to another value. This value may
77 : : be a value, or a delta value, and the change from the
78 : : current value to the new value can be either via a step action,
79 : : a ramp, or an exponential approach. The speed of a ramp or exponential
80 : : approach is specified via the time constant. Here is an example
81 : : illustrating the format of the script file:
82 : :
83 : : @code
84 : : <?xml version="1.0"?>
85 : : <runscript name="C172-01A takeoff run">
86 : : <!--
87 : : This run is for testing the C172 altitude hold autopilot
88 : : -->
89 : :
90 : : <use aircraft="c172x"/>
91 : : <use initialize="reset00"/>
92 : : <run start="0.0" end="3000" dt="0.0083333">
93 : :
94 : : <event name="engine start">
95 : : <notify/>
96 : : <condition>
97 : : sim-time-sec >= 0.25
98 : : </condition>
99 : : <set name="fcs/throttle-cmd-norm" value="1.0" action="FG_RAMP" tc ="0.5"/>
100 : : <set name="fcs/mixture-cmd-norm" value="0.87" action="FG_RAMP" tc ="0.5"/>
101 : : <set name="propulsion/magneto_cmd" value="3"/>
102 : : <set name="propulsion/starter_cmd" value="1"/>
103 : : </event>
104 : :
105 : : <event name="set heading hold">
106 : : <!-- Set Heading when reach 5 ft -->
107 : : <notify/>
108 : : <condition>
109 : : position/h-agl-ft >= 5
110 : : </condition>
111 : : <set name="ap/heading_setpoint" value="200"/>
112 : : <set name="ap/attitude_hold" value="0"/>
113 : : <set name="ap/heading_hold" value="1"/>
114 : : </event>
115 : :
116 : : <event name="set autopilot">
117 : : <!-- Set Autopilot for 20 ft -->
118 : : <notify/>
119 : : <condition>
120 : : aero/qbar-psf >= 4
121 : : </condition>
122 : : <set name="ap/altitude_setpoint" value="100.0" action="FG_EXP" tc ="2.0"/>
123 : : <set name="ap/altitude_hold" value="1"/>
124 : : <set name="fcs/flap-cmd-norm" value=".33"/>
125 : : </event>
126 : :
127 : : <event name="set autopilot 2" persistent="true">
128 : : <!-- Set Autopilot for 6000 ft -->
129 : : <notify/>
130 : : <condition>
131 : : aero/qbar-psf > 5
132 : : </condition>
133 : : <set name="ap/altitude_setpoint" value="6000.0"/>
134 : : </event>
135 : :
136 : : <event name="Time Notify">
137 : : <notify/>
138 : : <condition> sim-time-sec >= 500 </condition>
139 : : </event>
140 : :
141 : : <event name="Time Notify">
142 : : <notify/>
143 : : <condition> sim-time-sec >= 1000 </condition>
144 : : </event>
145 : :
146 : : </run>
147 : :
148 : : </runscript>
149 : : @endcode
150 : :
151 : : The first line must always be present - it identifies the file
152 : : as an XML format file. The second line
153 : : identifies this file as a script file, and gives a descriptive
154 : : name to the script file. Comments are next, delineated by the
155 : : <!-- and --> symbols. The aircraft and initialization files
156 : : to be used are specified in the "use" lines. Next,
157 : : comes the "run" section, where the conditions are
158 : : described in "event" clauses.</p>
159 : : @author Jon S. Berndt
160 : : @version "$Id: FGScript.h,v 1.18 2010/04/11 13:44:42 jberndt Exp $"
161 : : */
162 : :
163 : : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164 : : CLASS DECLARATION
165 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
166 : :
167 : : class FGScript : public FGJSBBase, public FGXMLFileRead
168 : : {
169 : : public:
170 : : /// Default constructor
171 : : FGScript(FGFDMExec* exec);
172 : :
173 : : /// Default destructor
174 : : ~FGScript();
175 : :
176 : : /** Loads a script to drive JSBSim (usually in standalone mode).
177 : : The language is the Script Directives for JSBSim. If a simulation step size
178 : : has been supplied on the command line, it will be override the script-
179 : : specified simulation step size.
180 : : @param script the filename (including path name, if any) for the script.
181 : : @param deltaT a simulation step size from the command line
182 : : @return true if successful */
183 : : bool LoadScript(string script, double deltaT);
184 : :
185 : : /** This function is called each pass through the executive Run() method IF
186 : : scripting is enabled.
187 : : @return false if script should exit (i.e. if time limits are violated */
188 : : bool RunScript(void);
189 : :
190 : : void ResetEvents(void) {
191 [ # # ]: 0 : for (unsigned int i=0; i<Events.size(); i++) Events[i].reset();
192 : : }
193 : :
194 : : private:
195 : : enum eAction {
196 : : FG_RAMP = 1,
197 : : FG_STEP = 2,
198 : : FG_EXP = 3
199 : : };
200 : :
201 : : enum eType {
202 : : FG_VALUE = 1,
203 : : FG_DELTA = 2,
204 : : FG_BOOL = 3
205 : : };
206 : :
207 : 105 : struct event {
208 : : FGCondition *Condition;
209 : : bool Persistent;
210 : : bool Continuous;
211 : : bool Triggered;
212 : : bool Notify;
213 : : bool Notified;
214 : : double Delay;
215 : : double StartTime;
216 : : double TimeSpan;
217 : : string Name;
218 : : vector <FGPropertyManager*> SetParam;
219 : : vector <FGPropertyManager*> NotifyProperties;
220 : : vector <eAction> Action;
221 : : vector <eType> Type;
222 : : vector <double> SetValue;
223 : : vector <double> TC;
224 : : vector <double> newValue;
225 : : vector <double> OriginalValue;
226 : : vector <double> ValueSpan;
227 : : vector <bool> Transiting;
228 : : vector <FGFunction*> Functions;
229 : :
230 : 9 : event() {
231 : 9 : Triggered = false;
232 : 9 : Persistent = false;
233 : 9 : Continuous = false;
234 : 9 : Delay = 0.0;
235 : 9 : Notify = Notified = false;
236 : 9 : Name = "";
237 : 9 : StartTime = 0.0;
238 : 9 : TimeSpan = 0.0;
239 : 0 : }
240 : :
241 : : void reset(void) {
242 : 0 : Triggered = false;
243 : 0 : Notified = false;
244 : 0 : StartTime = 0.0;
245 : : }
246 : : };
247 : :
248 : 7 : struct LocalProps {
249 : : double *value;
250 : : string title;
251 : 7 : LocalProps(double initial_value=0) {
252 : 7 : value = new double(initial_value);
253 : 7 : title = "";
254 : 0 : }
255 : : };
256 : :
257 : : string ScriptName;
258 : : double StartTime;
259 : : double EndTime;
260 : : vector <struct event> Events;
261 : : vector <LocalProps*> local_properties;
262 : :
263 : : FGFDMExec* FDMExec;
264 : : FGPropertyManager* PropertyManager;
265 : : void Debug(int from);
266 : : };
267 : : }
268 : : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269 : : #endif
270 : :
|