Board index FlightGear Development

Fast access to property tree via Python

FlightGear is opensource, so you can be the developer. In the need for help on anything? We are here to help you.
Forum rules
Core development is discussed on the official FlightGear-Devel development mailing list.

Bugs can be reported in the bug tracker.

Fast access to property tree via Python

Postby lsimons » Tue Jun 25, 2013 2:37 pm

I have been working on some custom gauges for a glass cockpit simulator in Python and wxWidgets. My plan was to then poll the property tree for the appropriate values and set the gauges.

I was originally using the file FlightGear interface class at https://github.com/flightgearonandroid/ ... ghtGear.py to access property tree values, but found it very slow, around 500 ms to get a response. The wiki has some mentions that interacting via Telnet may be slow, so I wrote a custom class (very heavily based on viewtopic.php?f=36&t=16291) using sockets to see if I could improve the speed.

Code: Select all
# By tuxklok
# http://www.flightgear.org/forums/viewtopic.php?f=36&t=16291

import socket

class FlightGearInterface:

    def __init__(self):
        self.fgfs_sock = None
        self.fgfs_file = None

    def connect(self, host, port):
        # Fix a Windows 7 problem by manually looking up hostname
        if host == "":
            host = socket.gethostname()

        self.fgfs_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.fgfs_sock.connect((host, port))
        self.fgfs_file = self.fgfs_sock.makefile()
        self.fgfs_sock.sendall("data\r\n")

    def setprop(self, prop, value):
        self.fgfs_sock.sendall("set {0} {1}\r\n".format(prop, value))

    def getprop(self, prop):
        self.fgfs_sock.sendall("get {0}\r\n".format(prop))
        return self.fgfs_file.readline().strip()

if __name__ == "__main__":
    fgfs = FlightGearInterface()
    fgfs.connect("", 5401)
    fgfs.setprop("/some/property", "1")
    some_value = fgfs.getprop("/some/property")
    print some_value


This doesn't seem to be any faster. I have been testing using the /orientation/alpha-deg property. Is there a trick to getting property tree data? The wiki sounded like getting data up to the framerate should be possible. I'm open to different ideas, but would really like to keep it in Python as much as possible.

Thanks,
Louis
lsimons
 
Posts: 14
Joined: Thu Aug 30, 2012 12:53 pm

Re: Fast access to property tree via Python

Postby Hooray » Fri Jul 12, 2013 4:58 pm

I think 5hz is the default, but can be changed - if you need even higher rate, you need to adjust simtime-per-frame usually.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11377
Joined: Tue Mar 25, 2008 8:40 am

Re: Fast access to property tree via Python

Postby lsimons » Fri Jul 12, 2013 5:30 pm

I actually found a work around using python's SocketServer.UDPServer and the generic protocol (http://wiki.flightgear.org/Generic_protocol). It's a little more complicated, but it lets you select which properties flightgear should present on a specific port and at what rate. My protocol xml file is very simple:

Code: Select all
<?xml version="1.0"?>

<PropertyList>
 <generic>

  <output>
   <line_separator>newline</line_separator>
   <var_separator>tab</var_separator>


   <chunk>
    <name>alpha (deg)</name>
    <type>float</type>
    <format>%f</format>
    <factor>1.0</factor>
    <node>/orientation[0]/alpha-deg</node>
   </chunk>

  </output>

 </generic>
</PropertyList>


I had missed it in the wiki since it's called the "Generic Protocol," and I was reading it in my head as a generic protocol to address the property tree and thought it was the same as the other socket methods. It really means that it's generic as in non-prescriptive, with regards to what data is being output, not generic like not specifying an interface.
lsimons
 
Posts: 14
Joined: Thu Aug 30, 2012 12:53 pm

Re: Fast access to property tree via Python

Postby Hooray » Fri Jul 12, 2013 5:34 pm

you could also check if it's any faster when using binary mode - but your protocol seems simple enough for now, so that speed should not be an issue, or is it ?
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11377
Joined: Tue Mar 25, 2008 8:40 am

Re: Fast access to property tree via Python

Postby lsimons » Fri Jul 12, 2013 5:37 pm

I think i'm only going to need 2 or 3 properties tops, so ASCII should be fast enough. It seems to be spitting it out at 30 Hz no problem.
lsimons
 
Posts: 14
Joined: Thu Aug 30, 2012 12:53 pm


Return to Development

Who is online

Users browsing this forum: No registered users and 2 guests