Control with Python¶
FlightGear keeps its whole state in the property tree: altitude, switches, autopilot, view. Any program can read and change it through the built-in Telnet server. starten.sh enables the server on port 5411.
First script¶
Put the file next to fg_steuerung.py and run it while FlightGear is running:
from fg_steuerung import Fg
fg = Fg()
print("Altitude:", fg.get("/position/altitude-ft"))
fg.set("/sim/current-view/view-number", 1)
fg.nasal('setprop("/sim/messages/copilot","Hello");')
Good to know¶
Nasal without spaces:
run nasal script=…does not accept spaces outside of strings. Longer code is better placed in a function inside the aircraft or an add-on.Patience while waiting: FlightGear does not answer while it stalls. The first switch to an outside view takes several seconds. A 10-second timeout once ended one of my flight scripts in the middle of a launch, so
Fgnow waits up to 30 seconds.Check, don’t assume: Setting a value does not mean the action worked. After an engine start, read the thrust and stop with a clear message if it is zero.
Find property paths¶
The user interface shows the tree under Debug → Browse Internal Properties. From outside:
telnet 127.0.0.1 5411
There you can use ls, cd, get and set, and leave with quit.
Example: wait for the landing¶
import time
from fg_steuerung import Fg
fg = Fg()
while float(fg.get("/position/altitude-agl-ft") or 0) > 5:
time.sleep(0.5)
print("Down, ground speed:", fg.get("/velocities/groundspeed-kt"), "kt")