Review of Using Generated C# Code

disclaimer: I am new to the world of UAVs as well as C# but I am learning pretty quick. I feel much more comfortable working in the embedded world with C but I have started working with a group of people who use C# extensively.

Currently I am working on moving over a uav platform that uses a different commercial autopilot to the APM stack and all of the applications are written in C#. So what I want to do is write a C# library that can drop into the existing on board computer program to make the transition as seamless as possible.

I figured the code generator provided through the MavLink repository would be a good place to start. I am currently just testing out this concept by connecting my program to a `sim_vehicle.sh`/MavProxy instance via UDP on my desktop Linux machine.

I couldn't find many examples using the actual code generated from the `mavgenerate.py` tool. So my question here is: Would anyone mind looking at this minimum working example and telling me if this is the proper way to interface with the generated MavLink libraries or if there is a more idiomatic way?

Explanation: Like I said I was able to successfully generate the C# code via the `mavgenerate.py` tool and I have included that dll in my solution. I have written two files Program.cs and PacketEventMethods.cs . I initialize a UDP client to just listen to the correct port. Then I instantiate my class that holds contains the methods I want to add to the packet receive handler. I instantiate a MavLink object and a byte array. I also add a function to the packet receive handler. I feel fine up to this point but if there is anything that could be written a different way or the libraries used differently I would appreciate some pointers here too.

Where I am unsure is in my function that is in the PacketEventMethods.cs. The way that I have to check the type of the Message field seems odd to me for some reason as well as accessing fields of the particular Message field of the packet. If there is something I skimmed over in the library I would appreciate a quick pointer. 

This code does work I just feel like it is messy for some reason and I don't want to get too far into writing an application to find out later that major refactoring is in order. I will add threads and more complexity to it later. I'm just trying to get the basics of interfacing with MavLink down right now.

Program.cs

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using MavLink;

namespace MAVtester
{

    class MainClass
    {
        public static void Main (string[] args)
        {
            Console.WriteLine ("Testing Mavlink Protocol using offically generated code");
            int listenPort = 14551;
            UdpClient listener = new UdpClient (listenPort);
            IPEndPoint groupEP = new IPEndPoint (IPAddress.Any, listenPort);

            // Instatiate object containing methods to be called upon packet receive event
            PacketEventMethods packetEventMethods = new PacketEventMethods ();
            // Mavlink message object
            Mavlink receivedMsg = new Mavlink();
            // Add my packet receive methods to the event handler
            receivedMsg.PacketReceived += new PacketReceivedEventHandler (packetEventMethods.PrintRecievedPackets);
            byte[] receiveByteArray;
            bool done = false;

            while (!done) 
            {
                receiveByteArray = listener.Receive (ref groupEP);
                receivedMsg.ParseBytes (receiveByteArray);
            }
        }
    }
}

PacketEventMethods.cs:


using System;
namespace MavLink
{
    class PacketEventMethods
    {
        public void PrintRecievedPackets(object sender, MavlinkPacket e)
        {
            // Print Message Basic Info
//            Console.WriteLine("System ID: {0}", e.SystemId);
//            Console.WriteLine("Message: {0}", e.Message.ToString ());
//            Console.WriteLine ("Time Stamp: {0}", e.TimeStamp);
                        
            if (e.Message.GetType() == typeof(MavLink.Msg_local_position_ned)) 
            {
                Console.WriteLine ("-----------------------------");
                Console.WriteLine ("Position X: {0}", (e.Message as MavLink.Msg_local_position_ned).x);
                Console.WriteLine ("Position Y: {0}", (e.Message as MavLink.Msg_local_position_ned).y);
                Console.WriteLine ("Position Z: {0}", (e.Message as MavLink.Msg_local_position_ned).z);
                Console.WriteLine ("-----------------------------");
            }
        }
    }
}

Thanks for any pointers,

Jesse

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

Join diydrones

Email me when people reply –

Replies

  • Hi Jesse,

    I have a tracking system where I get x,y,z position dates from a C# programm and with this dates I would controle a AR.Drone about the MAVLink protocol. Can I use your coude and maybe can I get your dll? 

    Nice regards Lexx

This reply was deleted.

Activity