Re: [MAVLINK] Asynchronous MAVLink in Python -- Observer Pattern

430 views
Skip to first unread message

Kevin Hester

unread,
Nov 8, 2014, 8:37:07 AM11/8/14
to drones-discuss
Hi Paul,

If you use the DroneAPI module in mavproxy you can receive messages in a non blocking fashion.  It will do what you want if you call set_mavlink_callback (see small_demo.py for an example).  I'm happy to help if you have questions.

Kevin



On Thu, Nov 6, 2014 at 12:06 AM, Paul <paulb...@gmail.com> wrote:
I'm currently working with MAVLink in Python (pymavlink) to control multiple drones. So far I am able to connect to the drones and communicate, change modes, etc. What I'm doing is essentially instantiating multiple objects from the class with a connection, etc -- so the basic stuff.

I'm working on creating a simple GUI (using web-based Flask, not TkInter) and need to devise a way to simultaneously update certain messages (GPS, mode, etc,) while at the same type being able to send commands, for each drone. Now I'm fairly new to Python but so far I've figured out that I'm going to need to use threads.

The problem I've found is that listening to the MAVLink messages is blocking. I can read them continuously, for example, using this function from the pymavlink example:

    def show_messages(self):
        m = self.vehicle
        '''show incoming mavlink messages'''
        while True:
            msg = m.recv_match(blocking=True)
            if not msg:
                return
            if msg.get_type() == "BAD_DATA":
                if mavutil.all_printable(msg.data):
                    sys.stdout.write(msg.data)
                    sys.stdout.flush()
            else:
                print(msg)

But as you can see it's running in a while loop so it just stays there. What I want is to be able to get these messages asynchronously outside of a while loop so that I can then send messages. 


So if my drone class is something like this (this is just a sample -- not the actual thing):

class Drone():
   
def __init__(self, port):
       
self.mav = mavutil.mavlink_connection(device=port, baud=57600)
       
self.lat = None
       
self.lon = None
       
self.alt = None

   
def getMessages(self):
       
self.lat = something
       
self.lon = something2
       
self.alt = something3

   
def update(self):
        updateMode
()



I want my variables (lat, lon, alt) to be kept up to date so that they can refresh in the GUI and at the same type be able to call any update functions to send messages. 

I know I probably need to use the observer pattern but I need a bit of help implementing that in Python with MAVLink.

Thanks!

--
Sie erhalten diese Nachricht, weil Sie in Google Groups E-Mails von der Gruppe "MAVLink" abonniert haben.
Wenn Sie sich von dieser Gruppe abmelden und keine E-Mails mehr von dieser Gruppe erhalten möchten, senden Sie eine E-Mail an mavlink+u...@googlegroups.com.
Weitere Optionen finden Sie unter https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages