Board index FlightGear Support

Run FlighGear with python  Topic is solved

All general support: help on flying, installation, hardware, getting online etc. There are lots of users and developers to help you out.
Forum rules
In order to help you, we need to know a lot of information. Make sure to include answers to at least the following questions in your initial post.

- what OS (Windows Xp/Vista, Mac etc.) are you running?
- what FlightGear version do you use?
- what graphics card do you have?
- does the problem occur with any aircraft, at any airport?
- where did you download your aircraft/scenery from?
- is there any output printed to the console (black window)?
- copy&paste your commandline (tick the "Show commandline box on the last page of FGRun or the "Others" section on the Mac launcher).

Please report any bugs not specific to an aircraft on the issue tracker.
To run FlightGear on old computers with bad OpenGL support, please take a look at this wiki article.

Note: If you did not get a reponse, even after 7 days, you may want to check out the FlightGear mailing lists to ask your question there.

Run FlighGear with python  

Postby sadegh1377 » Sat Feb 13, 2021 12:47 pm

Hi
I tried to run Flight Gear with python and almost I did it but my problem is that I can not set time and series of parameters
My code in python is this
Code (): Select all
from telnetlib import Telnet
import sys
import socket
import re
import time

__all__ = ["FlightGear"]

CRLF = '\r\n'

class FGTelnet(Telnet):
def __init__(self,host,port):
Telnet.__init__(self,host,port)
self.prompt = [re.compile('/[^>]*> '.encode('utf-8'))]
self.timeout = 5
#Telnet.set_debuglevel(self,2)

def help(self):
return

def ls(self,dir=None):
"""
Returns a list of properties.
"""
if dir is None:
self._putcmd('ls')
else:
self._putcmd('ls %s' % dir )
return self._getresp()

def dump(self):
"""Dump current state as XML."""
self._putcmd('dump')
return self._getresp()

def cd(self, dir):
"""Change directory."""
self._putcmd('cd ' + dir)
self._getresp()
return

def pwd(self):
"""Display current path."""
self._putcmd('pwd')
return self._getresp()

def get(self,var):
"""Retrieve the value of a parameter."""
self._putcmd('get %s' % var )
return self._getresp()

def set(self,var,value):
"""Set variable to a new value"""
self._putcmd('set %s %s' % (var,value))
self._getresp() # Discard response

def quit(self):
"""Terminate connection"""
self._putcmd('quit')
self.close()
return

# Internal: send one command to FlightGear
def _putcmd(self,cmd):
cmd = cmd + CRLF
Telnet.write(self, cmd.encode('utf-8'))
return

# Internal: get a response from FlightGear
def _getresp(self):
(_i,_match,resp) = Telnet.expect(self, self.prompt, self.timeout)
# Remove the terminating prompt.
# Everything preceding it is the response.
return resp.decode('utf-8').split('\n')[:-1]

class FlightGear:
"""FlightGear interface class.

An instance of this class represents a connection to a FlightGear telnet
server.

Properties are accessed using a dictionary style interface:
For example:

# Connect to flightgear telnet server.
fg = FlightGear('myhost', 5500)
# parking brake on
fg['/controls/gear/brake-parking'] = 1
# Get current heading
heading = fg['/orientation/heading-deg']

Other non-property related methods
"""

def __init__( self, host = 'localhost', port = 5502 ):
try:
self.telnet = FGTelnet(host,port)
except socket.error as msg:
self.telnet = None
raise msg

def __del__(self):
# Ensure telnet connection is closed cleanly.
self.quit()

def __getitem__(self,key):
"""Get a FlightGear property value.
Where possible the value is converted to the equivalent Python type.
"""
s = self.telnet.get(key)[0]
match = re.compile( r'[^=]*=\s*\'([^\']*)\'\s*([^\r]*)\r').match( s )
if not match:
return None
value,type = match.groups()
#value = match.group(1)
#type = match.group(2)
if value == '':
return None

if type == '(double)':
return float(value)
elif type == '(int)':
return int(value)
elif type == '(bool)':
if value == 'true':
return 1
else:
return 0
else:
return value

def __setitem__(self, key, value):
"""Set a FlightGear property value."""
self.telnet.set( key, value )

def quit(self):
"""Close the telnet connection to FlightGear."""
if self.telnet:
self.telnet.quit()
self.telnet = None

def view_next(self):
"""Move to next view."""
self.telnet.set( "/command/view/next", "true")

def view_prev(self):
"""Move to next view."""
self.telnet.set( "/command/view/prev", "true")


And when I run this code orientation will change
Code (): Select all
flightgear_server = 'localhost'
flightgear_server_port = 5502
fg = FlightGear(flightgear_server, flightgear_server_port)
fg['/orientation/heading-deg'] = 1

but my Problem is I want to set series of property with respect to time
sadegh1377
 
Posts: 1
Joined: Sat Feb 13, 2021 12:36 pm

Re: Run FlighGear with python

Postby cgdae » Sat Feb 13, 2021 8:27 pm

In the flightgear repository, there's a python module scripts/python/FlightGear.py that handles all the telnet details for you, giving a useful higher level interface onto properties etc.

I used it to write a test script for record/replay recently, and it seems to work pretty well; i pushed it to next a few hours ago - see: flightgear/scripts/python/recordreplay.py.

Hope that helps,

- Jules
cgdae
 
Posts: 117
Joined: Tue May 31, 2016 8:35 pm


Return to Support

Who is online

Users browsing this forum: No registered users and 8 guests