0

I am trying to figure out how to create a basic GUI to display altitude telemetry of an RC plane in real time. So far I have successfully written code that prints out the live data in the terminal. My question now is how do i take that live data from the terminal and instead display it in a GUI in real time. I took the listen.py code and turned it into a def planeoutput(): and used a generator to display values in the GUI window but it wasn't displaying.

Here is a video that I found that shows what I want to do: https://www.youtube.com/watch?v=P4GBUcvMrDA

This is the code I have that listens for the altitude data and displays it in the terminal when it is compiled:

# Purpose: to get live telemetry from vehicle and display it as the output  
#def planeoutput():
    # https://mavlink.io/en/mavgen_python/
from pymavlink import mavutil
    #x = 0

    # Start a connection listening on a UDP port
the_connection = mavutil.mavlink_connection('udpin:localhost:14551') #make a user prompt to ask for COM port later on

    # Wait for the first heartbeat 
    # This sets the system and component ID of remote system for the link
the_connection.wait_heartbeat()
print("Heartbeat from system (system %u component %u)" % (the_connection.target_system, the_connection.target_component)) # tells us if we are connected to vehicle

    # Once connected, use 'the_connection' to get and send messages
while 1: 
        altitude = the_connection.recv_match(type='GLOBAL_POSITION_INT',blocking=True) # locates GPS data and streams it to altitude variable
        print("Planes Altitude:",round(altitude.relative_alt/304.8),"ft") #This is alt object not a dict

This is the output when I run the above code: output of listen.py

This is the basic GUI code that I have so far:

import wx
 
class Window(wx.Frame):
    def __init__(self, title):
        super().__init__(parent = None, title = title, size = (420, 300))
        self.panel = wx.Panel(self)
         
        wx.StaticLine(self.panel, pos=(20, 240), size=(360,1), style = wx.HORIZONTAL)
 
        content1 = "Altitude:" #live data would display here
 
        text1 = wx.StaticText(self.panel, label = content1, pos = (60, 100))
     
        wx.StaticLine(self.panel, pos=(20, 20), size=(360,1), style = wx.LI_HORIZONTAL)
       
        self.Centre()
        self.Show()
 
         
app = wx.App()
window = Window("Ground Control Station")
app.MainLoop()
ce-ET
  • 11
  • 1

1 Answers1

2

The easiest way, is to use a multi-line wx.TextCtrl and simply write the results in.

I suspect that you'll want to investigate running your routine as a separate thread and have a method of stopping it, mid-simulation but those are different questions.

import wx
import time

def LongSimulation(self,_input):
    # Simulate the behaviour of the simulation
    for i in range(0, 1000, 100):
        self.log.write("Planes Altitude: "+str(i)+" ft\n")
        time.sleep(1)
        wx.GetApp().Yield()
    for i in range(i, 0, -100):
        self.log.write("Planes Altitude: "+str(i)+" ft\n")
        time.sleep(1)
        wx.GetApp().Yield()
    return 'Landed\n'

class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None)

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)
        style = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL

        sizer = wx.BoxSizer(wx.VERTICAL)

        self.log = wx.TextCtrl(panel, wx.ID_ANY, size=(300,100), style=style)
        sizer.Add(self.log, 1, wx.ALL|wx.EXPAND, 5)

        btn = wx.Button(panel, wx.ID_ANY, 'Start')
        self.Bind(wx.EVT_BUTTON, self.onButton, btn)
        sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
        panel.SetSizer(sizer)
        self.Bind(wx.EVT_CLOSE, self.onQuit)

    def onButton(self, event):
        _input = 0
        self.log.write("Takeoff!\n")
        res = LongSimulation(self,_input)
        self.log.write(res)

    def onQuit(self, event):
        self.Destroy()

# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

enter image description here

Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60