Debian Codegraph @

Nasal/track_target.nas

raw
1 # This is a small script that adjusts autopilot target values to track
2 # (fly in formation with) an AI or Multiplayer aircraft.
3
4 # Quick start instructions:
5 #
6 #
7 # 1. Copy this file into $FGROOT/data/Nasal (along with the other
8 # system nasal scripts.)
9 #
10 # 2. Start up FlightGear selecting an airplane with a reasonably configured
11 # autopilot that responds to and works with the standard autopilot
12 # dialog box (F11). The MiG 15 is one that works, the 777-200 works,
13 # the Citation Bravo does not work, the default c172 probably does not
14 # work, etc.
15 #
16 # 3. Take off and establish stable flight.
17 #
18 # 4. Open the property browser (File->Browse Internal Properties) and navigate
19 # to /ai/models/ Choose one of the available aircraft[] or multiplayer[]
20 # entries. You can look at all those subtrees to find the call sign you
21 # want. Also note that the subtree for each entity has a radar area that
22 # will show range and offset from your current heading.
23 #
24 # 5. Open a second property browser window (upper left click box in the first
25 # property browser window.) Navigate to /autopilot/target-tracking/
26 #
27 # 6. Set "/autopilot/target-tracking/target-root" to point to the entity
28 # path you discovered in step #4. For instance, this should be set to
29 # something like /ai/models/multiplayer[2] or /ai/models/aircraft[0]
30 #
31 # 7. Set "/autopilot/target-tracking/goal-range-nm" to the follow distance
32 # you want.
33 #
34 # 8. Set "/autopilot/target-tracking/enable" = 1, this will turn on the radar
35 # computation for each ai/multiplayer entity and will tell the tracking
36 # script to start updating the autopilot settings.
37 #
38 # 9. Open up the autopilot configuration window (F11) and activate any of the
39 # heading, pitch, and speed axes. The script will begin updating the heading
40 # bug angle, the "speed with throttle" value, and the "altitude hold" value.
41 #
42 # 10. You can choose to mix and match any of the autopilot modes you want, i.e.
43 # you could turn off the heading control and turn manually while the system
44 # holds speed and altitude for you.
45 #
46 # 11. It always helps to have a sensible target arcraft to chase. You are
47 # flying within the turn radius and climb rate limits of your autopilot.
48 #
49 # Don't forget you are pilot in command and at all times responsible for
50 # maintaining safe airspeed and altitude.
51 #
52 # Enjoy the ride!
53
54
55 # print("Target Tracking script loading ...");
56
57 # script defaults (configurable if you like)
58 var default_update_period = 0.05;
59 var default_goal_range_nm = 0.15;
60 var default_target_root = "/ai/models/aircraft[0]";
61 var default_min_speed_kt = 120;
62
63 # master enable switch
64 var target_tracking_enable = 0;
65
66 # update period
67 var update_period = default_update_period;
68
69 # goal range to acheive when following target
70 var goal_range_nm = 0;
71
72 # minimum speed so we don't drop out of the sky
73 var min_speed_kt = 0;
74
75 # Target property tree root
76 var target_root = "";
77
78 # Loop identifier
79 var tracker_loop_id = 0;
80
81 # Initialize target tracking
82 var TrackInit = func {
83 if (props.globals.getNode("autopilot") == nil)
84 return;
85
86 props.globals.initNode("/autopilot/target-tracking/enable", 0, "BOOL");
87 props.globals.initNode("/autopilot/target-tracking/update-period", default_update_period, "DOUBLE");
88 props.globals.initNode("/autopilot/target-tracking/goal-range-nm", default_goal_range_nm, "DOUBLE");
89 props.globals.initNode("/autopilot/target-tracking/min-speed-kt", default_min_speed_kt, "DOUBLE");
90 props.globals.initNode("/autopilot/target-tracking/target-root", default_target_root, "STRING");
91
92 setlistener("/autopilot/target-tracking/enable", func { startTimer();} );
93 }
94
95 # If enabled, update our AP target values based on the target range,
96 # bearing, and speed
97 var TrackUpdate = func(loop_id) {
98 # avoid running multiple concurrent timers
99 if (tracker_loop_id != loop_id)
100 return;
101
102 if (props.globals.getNode("autopilot") == nil)
103 return;
104
105 target_tracking_enable = getprop("/autopilot/target-tracking/enable");
106
107 if ( target_tracking_enable == 1 ) {
108 update_period = getprop("/autopilot/target-tracking/update-period");
109
110 # refresh user configurable values
111 goal_range_nm = getprop("/autopilot/target-tracking/goal-range-nm");
112 target_root = getprop("/autopilot/target-tracking/target-root");
113 min_speed_kt = getprop("/autopilot/target-tracking/min-speed-kt");
114
115 # force radar debug-mode on (forced radar calculations even if
116 # no radar instrument and ai aircraft are out of range
117 setprop("/instrumentation/radar/debug-mode", 1);
118
119 my_hdg_prop = sprintf("/orientation/heading-magnetic-deg" );
120 my_hdg = getprop(my_hdg_prop);
121
122 my_hdg_true_prop = sprintf("/orientation/heading-deg" );
123 my_hdg_true = getprop(my_hdg_true_prop);
124
125 var alt_prop = sprintf("%s/position/altitude-ft", target_root );
126 var alt = getprop(alt_prop);
127 if ( alt == nil ) {
128 print("bad property path: ", alt_prop);
129 return;
130 }
131
132 var speed_prop = sprintf("%s/velocities/true-airspeed-kt", target_root );
133 #correct by local IAS/TAS ratio, because autopilot uses IAS
134 #I need to calculate my TAS, not taking wind into account (MP velocities/true-airspeed-kt does not as well)
135 var northSpeed = getprop("/velocities/speed-north-fps");
136 var eastSpeed = getprop("/velocities/speed-east-fps");
137 var downSpeed = getprop("/velocities/speed-down-fps");
138 var true_airspeed = FPS2KT * math.sqrt(northSpeed*northSpeed + eastSpeed*eastSpeed + downSpeed*downSpeed);
139 #take target TAS and multiply it by my own IAS/TAS ratio to get target IAS
140 var speedTAS = getprop(speed_prop);
141 if ( speedTAS == nil ) {
142 print("bad property path: ", speed_prop);
143 return;
144 }
145 var speed = speedTAS * (getprop("/velocities/airspeed-kt") / true_airspeed);
146
147 var range_prop = sprintf("%s/radar/range-nm", target_root );
148 var range = getprop(range_prop);
149 if ( range == nil ) {
150 print("bad property path: ", range_prop);
151 return;
152 }
153
154 var h_offset_prop = sprintf("%s/radar/h-offset", target_root );
155 var h_offset = getprop(h_offset_prop);
156 if ( h_offset == nil ) {
157 print("bad property path: ", h_offset_prop);
158 return;
159 }
160
161 if ( h_offset > -90 and h_offset < 90 ) {
162 # in front of us
163 var range_error = range - goal_range_nm;
164 } else {
165 # behind us
166 var range_error = goal_range_nm - range;
167 }
168 var target_speed = speed + range_error * 100.0;
169 if ( !debug.isnan(target_speed) and target_speed < min_speed_kt ) {
170 target_speed = min_speed_kt;
171 }
172
173 setprop( "/autopilot/settings/target-altitude-ft", alt );
174 setprop( "/autopilot/settings/heading-bug-deg", my_hdg + h_offset );
175 setprop( "/autopilot/settings/true-heading-deg",
176 my_hdg_true + h_offset );
177 if( !debug.isnan(target_speed) ) setprop( "/autopilot/settings/target-speed-kt", target_speed ); #isnan check because I divide by TAS before
178
179 # only keep the timer running when the feature is really enabled
180 settimer(func() { TrackUpdate(loop_id); }, update_period );
181 }
182 }
183
184 # create and start a new timer to cause our update function to be called periodially
185 startTimer = func {
186 tracker_loop_id += 1;
187 TrackUpdate(tracker_loop_id);
188 }
189
190 settimer(TrackInit, 0);
191