dsuarezv / mavlink.net

A better MavLink object generation for C#. Richer message classes are generated from the object definitions.
38 stars 35 forks source link

MavLink.NET

A better MAVLink object generation for C#. Richer message classes are generated from the object definitions.

What is MAVLink

MAVLink is a very lightweight, header-only message marshalling library for micro air vehicles. It is used by several autopilots and ground station software. More details can be found on the homepage http://qgroundcontrol.org/mavlink/start.

Why is it needed?

MAVLink already provides a generator for C# classes. So, why another implementation?

Here is a sample of code that uses the original MAVLink generator:

    Msg_heartbeat msg = new Msg_heartbeat()
    {
        type = (byte)MAV_TYPE.MAV_TYPE_HELICOPTER,
        autopilot = (byte)MAV_AUTOPILOT.MAV_AUTOPILOT_ARDUPILOTMEGA,
        base_mode = (byte)(MAV_MODE_FLAG.MAV_MODE_FLAG_GUIDED_ENABLED | MAV_MODE_FLAG.MAV_MODE_FLAG_SAFETY_ARMED)
    };

Of course, nothing prevents you from assigning an enum field a value from the wrong enumeration:

        type = (byte)MAV_MODE_FLAG.MAV_MODE_FLAG_TEST_ENABLED 

Now, here is how the same code looks with the MavLink.NET classes:

    UasHeartbeat msg = new UasHeartbeat()
    {
        Type = MavType.Helicopter,
        Autopilot = MavAutopilot.Ardupilotmega,
        BaseMode = MavModeFlag.GuidedEnabled | MavModeFlag.SafetyArmed
    };

With the added benefit of safe types and code completion.

MavLink.NET tries to solve those problems, at the expense of not being API compatible with the original MAVLink generated classes. This is intended, as a way to make the code that uses the protocol easier to read. If this isn't a reason good enough for you, that's fine, go ahead and use the generated classes from the python Mavlink generator. These classes are intended for new code more than changing existing code.

The generated classes contain extended metadata that can be used to retrieve detailed descriptions in runtime, accessible through the method GetMetadata() on every message.

Usage

To generate the classes, first download the Mavlink definitions from https://github.com/mavlink/mavlink. Build the solution and run mavlinkgen like this (choose the XML definition file that you prefer):

mavlinkgen --output="mavlink.net\GeneratedMessages.cs" "c:\path to the mavlink repo\message_definitions\v1.0\ardupilotmega.xml"

Build again and the mavlink.net.dll assembly has everything you need. Enjoy!

License

This software is licensed under the MIT license (http://opensource.org/licenses/MIT).