Python Tutorial for Aerowinx PSX

Hoppie Home


Download
psx-print-pushover.py
File size 5.9 KiB
22-Mar-2020 16:57Z

Download
psx-python-demo.py
File size 6.8 KiB
22-Mar-2020 17:54Z

This is a work in progress. For discussion and suggestions, please use the Aerowinx Forum.

Python Resources

  • Main Python Web site where you can download the interpreter and find everything else.
  • Python 3 documentation is your go-to resource for nearly all questions on what Python is and how to use it to write programs.

PSX Resources

  • PSX Networking is the official reference to all PSX network details and the list of available variables.

Explanation of the Python Code

I wrote this code with two goals in mind:
  1. Demonstrate how to work with the PSX network model.
  2. Demonstrate how the relatively new asyncio library can be used to write robust, understandable, and maintainabe network code without using threads and other artifacts that tend to make things complicated.

I do not claim that this approach is the best or that there are no other ways, and if you know what you are doing threads certainly are okay, but for many applications, keeping things asynchronous with an event loop is a very clean solution. If you are not trying to squeeze the very last bit of performance out of your multicore CPU, which needs work that is inherently parallelizable, you don't need threads. When done properly, both methods use zero CPU if there is no incoming data.

The psx-print-pushover.py file is not part of the tutorial and an earlier item that performs a useful function. For more info, see the PSX forum thread about it.

This is not a plain Python tutorial. If you are not sufficiently fluent in the language yet, I recommend you to use the excellent Python tutorial referenced above (part of the Python documentation) to figure out what the various language constructs do. For questions, either search the Web, or post in the appropriate thread on the Aerowinx Forum.

What does psx-python-demo.py do?

Not a lot!

This script connects to PSX, and listens to the MCP Altitude Window. It shows you the value in that window every time it changes, for example when you turn the MCP Altitude knob.

If the altitude is below 37,000 ft, the Left FMC is selected (that selector knob is just below the MCP Altitude knob). If the altitude is 37,000 ft or higher, the Right FMC is selected. That's it.

However these minimal things demonstrate how to listen to simulator variables and how to kick physical controls around. You can easily add new functions while not touching the networking and database code at all.

class PSX

This structure contains the networking part of the script. It attempts to maintain a link to PSX all the time and will retry if something went amiss. When a connection has been established, the received variables are sent to the database so that they can be processed.

more will follow

class Db

Each subscribed variable is stored in the database, so that the script can get its last known value whenever it needs it. Additionally, variables may have an associated callback function that is called immediately after a change. This avoids the script having to poll variables all the time.

more will follow

function McpWdoAlt

A very simple processing callback. It displays the new value shown in the MCP Altitude Window, and depending on its value, flips the FMC selector left or right.

function Main

Python likes it best when the main event loop is set up as a separate co-routine, but I'll explain that in more detail.

more will follow

Executable part

This is the part of the Python script that actually does things, instead of defining tooling like the classes and functions/coroutines. It begins with a simple check for Python 3.8 or better. Then it processes command line arguments, if any.

Before a PSX connection is opened, the script subscribes to all PSX variables it is interested in. Some of these are not related to the simulated aircraft, such as id and version. For the others, I personally prefer to use their nickname, such as FmcEicas, instead of their Q-code. The PSX Lexicon hands you the translation and this demonstration shows you how to work with it. Note that you need to subscribe to both the input and the output variables to get the lexicon mapping. The Db functions accept both nicknames and Q-codes.

Some subscriptions have an associated function that is called when a new value of that variable has been received. The McpWdoAlt() function is a good example. For the version key, I used a nameless function built with the Python lambda feature. A nice thing to study but by no means important; I could have declared a normal function as well.

Lastly, the script starts the Event Loop and will remain in there until something blows up or you hit the Ctrl-C key to stop the script.


© 2020 Jeroen Hoppenbrouwers For more information, mail to hoppie@hoppie.nl