Developer

Python MAVLink interface (version 0.2)

mavlink-logo.png?w=400

 

For those of you who prefer a scripting language for your MAVLink needs, you may like to have a look at pymavlink, a Python implementation of the MAVLink protocol.I've just released version 0.2, which fixes some bugs, and adds support for the latest changes in the upstream mavlink git repository.

I initially wrote pymavlink to make it easier to develop MAVProxy, a little command line tool which provides a proxy between a MAVLink master and some number of slaves. I use this for a HIL setup between FlightGear, APM and qgroundcontrol. I'm releasing it more widely as I'm hoping it will be more generally useful to the diydrones community. It can be used to write little scripts that manipulate and display MAVLink messages in interesting ways.

The way pymavlink works is that you generate a custom mavlink python module using mavgen.py, which reads a set of MAVLink message definition files in their XML format, and spits out a ready to use python module that implements all the messages in the XML files.

A good example of what can be done with a short script is mavlogdump.py, which formats a qgroundcontrol MAVLink log file in a human readable manner. This really shows the advantage of using a scripting language for MAVLink, as it can easily display structures without having to write a lot of formatting code for each of the messaging types.

For our UAV project we are planning on using sets of python scripts with pymavlink to do things like mission planning, log analysis, camera control, image processing etc.

If you want to ask questions about pymavlink, it is probably best to join the mavlink mailing list.

 

E-mail me when people leave their comments –

You need to be a member of diydrones to add comments!

Join diydrones

Comments

  • Developer

    Thanks for the tip on serial detection! I've added auto-detection in mavproxy now. Please let me know if it works OK for you

     

  • Developer

    Tridge, Thanks for the rapid fix and update. I will test here (UK) tomorrow.

    I saw theseexample programs for polling windows serial devices when looking at the pyserial documentation today. The windows one (scanwin32.py) works on my machine (Windows 7 64bit premium).

    Here is the output from my computer .... COM5 is my connected USB cable to the UAV DevBoard.

    ------------------------------------------------------------------------------
    Serial ports
    ------------------------------------------------------------------------------
    COM5      : USB Serial Port (COM5) (FTDIBUS\COMPORT&VID_0403&PID_6001) -> Ready

    ------------------------------------------------------------------------------
    All serial ports (registry)
    ------------------------------------------------------------------------------
    COM4      : USB Serial Port (COM4) (FTDIBUS\COMPORT&VID_0403&PID_6001)
    COM5      : USB Serial Port (COM5) (FTDIBUS\COMPORT&VID_0403&PID_6001)
    COM6      : USB Serial Port (COM6) (FTDIBUS\COMPORT&VID_0403&PID_6001)
    COM8      : USB Serial Port (COM8) (FTDIBUS\COMPORT&VID_0403&PID_6001)
    COM9      : USB Serial Port (COM9) (FTDIBUS\COMPORT&VID_0403&PID_6001)
    >>>

     

    Best wishes, Pete

     

     

  • Developer

    btw, although mavproxy does use file numbers in a select loop if available, it detects if fd is None and falls back to using port.inWaiting() and polling. Not as efficient as using select, but its OK for the sort of low speed communication involved in MAVLink.

    You might also like to look through some of the other examples in pymavlink/examples. The mavutil python interface is getting quite sophisticated now, and will get more so over time, so its possible to write very simple python scripts that do quite nice things with MAVLink. The mavutil.mavlink_connection() method works with serial or UDP MAVLink connections, and also works with logfiles. That means that all the utilities work with any MAVLink source.

    The apmsetrate.py and magtest.py examples will give you some idea of what is possible.

    Cheers, Tridge

     

  • Developer

    Hi Pete,

    I've fixed up mavproxy for windows. The problem is that I haven't been testing on windows regularly, and a small change I made recently broke it. I'll try to be more careful to keep it working on windows.

    To make it easier to use on windows I'd also like to add auto-detection of serial ports, so you don't need to go into the windows device manager to work out what serial port to use for --master. I haven't yet looked into how to do that, but I do think that the current system of guessing serial ports is not good. On linux/macos its easy, as you can easily find the device name in /dev, but on windows there doesn't seem to be any method but the device manager GUI.

    Maybe we need a pyQT UI for mavproxy?

    Cheers, Tridge

     

  • Developer

    Tridge,

     

    Can you give me any pointers as to the best way I might try to get MAVproxy working under Windows ?

     

    Mavproxy falls over at the moment in Windows with the error message:-

    Traceback (most recent call last):
      File "C:\Users\phollands\Desktop\MAVProxy\mavproxy.py", line 1221, in <module>
        mav_master = mavutil.mavserial(opts.master, baud=opts.baudrate)
      File "..\pymavlink\mavutil.py", line 104, in __init__
        fd = self.port.fileno()
    UnsupportedOperation: fileno

     

    And sure enough, when looking that up, the use of fileno in pyserial is not supported in Windows. fileno is used to return a file descriptor number for the serial port stream. The main loop of mavproxy is all about file descriptors, so it seems like quite a bit of work to change it. If you have any ideas on the best approach for Windows, then I would love to know.

     

    In the mean time, I got mavproxy working with the MatrixPilot_mavlink2 development branch under Ubuntu 10.04. After compiling up the latest QGroundControl, I was able to run QGroundControl via mavproxy  linked serially to the UAV DevBoard running MatrixPIlot. I was then able to change parameters (PID Gains) in the UAV Devboard using MAVproxy command lines, and immedately also see the results updating in QGroundControl. I could control the UAVDevBoard from either QgroundControl or MAVproxy. All cool stuff. Thanks for publishing.

     

    The main development environement for MatrixPilot is still in Windows, and for our Non Ubuntu / Mac users, I would like to explore whether there is an easy path by which I might get mavproxy running under Windows.

     

    Best wishes, Pete

     

  • Developer

    Hi again,

    To give you a bit more of an idea of what mavproxy does, this is the "help" menu of its command line interface when in quadcopter mode:

     


    MAV> help
    arm             : arm the motors
    d               : disarm the motors
    disarm          : disarm the motors
    param           : manage APM parameters
    pitch           : set fixed pitch PWM
    rc              : override a RC channel value
    reset           : reopen the connection to the MAVLink master
    roll            : set fixed roll PWM
    rudder          : set fixed rudder PWM
    setup           : go into setup mode
    switch          : set RC switch (1-5), 0 disables
    throttle        : set fixed throttle PWM
    wp              : waypoint management

     

    It can control all MAV parameters, and can load/save/list waypoints, plus fly the plane (if you type fast enough!)

    Cheers, Tridge

     

  • Developer

    Hi Mike,

    I'm doing a fair bit more than proxying in mavproxy.py now. For example, I have MAVLink messages to control RC input values (allowing me to fly the quadcopter with no RC transmitter), and I am doing FG proxying, which requires translating FG data into MAVLink attitude, GPS etc packets. This is similar to FGShim, but a bit more sophisticated (eg. it also can forward mavlink packets to a GCS like qgroundcontrol).

    I'm maintaining minimal state at the moment, but the MAVLink slave (qgroundcontrol) maintains a bit more. I am doing a little bit of filtering of control channels before sending to FG as well. mavproxy has a plane and quadcopter mode. I've been using the quadcopter mode to try to get it to fly Ron Jensens dflyer quad model in FG.

    I haven't looked at X-Plane. I run Linux on all my boxes, so FG is ideal for me :-)

    Cheers, Tridge

     

  • Developer

    I'm a bit curious why you need a parser if all you're doing is proxying.

     

    Are you maintaining state on e.g. stream requests/rates and filtering accordingly for the various downstream consumers?  Do you do request/reply matching?

     

    I built something similar in C (sadly, not Python-literate) to bridge for FG and QGCS; I'm assuming your FG shim is more involved.  Has anyone looked at an X-plane shim? (Released FG builds have been pretty remedial for the last few years...)

     

     = Mike

     

  • Cool, thanks for sharing!
This reply was deleted.