dronekit / dronekit-python

DroneKit-Python library for communicating with Drones via MAVLink.
https://readthedocs.org/projects/dronekit-python/
Apache License 2.0
1.63k stars 1.45k forks source link

How to get mavlink system messages on my program? #1165

Closed ghost closed 2 years ago

ghost commented 2 years ago

Hello, I want to make great and simple drone delivery GCS for my drone delivery apparatus(it is just for hobby yet.)

I found mavlink critical message is printed on pycharm console like below :

"CRITICAL:autopilot:PreArm: Fence enabled, need position estimate" (I don't know how to add a picture so I copied the message here.)

I want this message to printed in my PYQT5 text label.

I dig in the web and only found this way : https://dronekit-python.readthedocs.io/en/latest/guide/mavlink_messages.html

in above link, I guess this code is only solution :

@vehicle.on_message('*')
def listener(self, name, message):
    print 'message: %s' % message

but when I try this code in my class it does not work.

global mw

class DroneWorker(QThread):
    def __init__(self, _mw):
        super().__init__()
        global mw
        self.mw = _mw
        self.connection_string = "COM9"
        self.vehicle = connect(self.connection_string, baud=57600, wait_ready=True)
        print(" Connected, Autopilot Firmware version: %s" % self.vehicle.version) 
        self.alive = True

   @vehicle.on_message('*')
   def listener(self, name, message):
       print 'message: %s' % message

    def run(self):
        while self.alive:
            self.mw.drone_heading.setText(str(self.vehicle.heading))
            self.mw.num_sat.setText(str(self.vehicle.gps_0.satellites_visible))
            self.mw.batt_voltage.setText(str(self.vehicle.battery.voltage))
            self.mw.flight_mode.setText(str(self.vehicle.mode))
            self.mw.mavlink_message.setText(str(self.vehicle.system_status))

I added "self" in front of vehicle, like this :

@self.vehicle.on_message('*')
def listener(self, name, message):
       print 'message: %s' % message

it also didn't work.

I also tried to make "vehicle" to "global" variables but it also failed

ghost commented 2 years ago

https://dronekit-python.readthedocs.io/en/latest/automodule.html#dronekit.Vehicle.add_message_listener

this link helped me. I added message listener by the way on above link