Mavlink communications

Hi,

So I am trying to retrieve sensor information from my ArduCopter to my c# application and as such need to use Mavlink.

From the Mission Planner source I have found functions to create the packets with check-sums and all. Once connected I can see my ArduCopter streaming heart beat messages. As I am a newbie to Mavlink, how do I get it to stream sensor info or how can I poll the ArduCopter? Send messages with no payload?

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

Join diydrones

Email me when people reply –

Replies

  • Developer

    This is a very oldd post try here http://discuss.ardupilot.org/c/ground-control-software/mavlink and ask the question

    and this post http://discuss.ardupilot.org/t/mavlink-step-by-step/9629

    Hint: pocketed is actually packet sequence so declaring it as a global byte starting at zero and make the line  Packet[2] = packetid++; means the sequence goes up each packet


    endrie said:

    Hi Jan, could you show me how you declare variable "packetid" ???

    is that integer or byte or what ??

    thanks

    Jan Swanepoel said:

    Hi Ryan,

    Here is what I have done and the functions that handle the sending of messages.

           public void requestDatastream(byte id, byte rate)
            {
                mavlink_request_data_stream_t rqstDataStream = new mavlink_request_data_stream_t
                {
                    req_message_rate = rate,
                    req_stream_id = id,
                    start_stop = 1,
                    target_system = 1,
                    target_component = 0,
                };

                byte[] packet = GeneratePacket(rqstDataStream, MAVLINK_MSG_ID_REQUEST_DATA_STREAM);

                SendPacket(packet);
            }

           public byte[] GeneratePacket(byte[] packetData, byte msgID)
            {
                byte[] Packet = new byte[packetData.Length + 6 + 2];

                Packet[0] = (byte)254;
                Packet[1] = (byte)packetData.Length;
                Packet[2] = (byte)packetid;
                Packet[3] = (byte)255;
                Packet[4] = (byte)1;
                Packet[5] = msgID;

                for (int n = 0; n < packetData.Length; n++)
                    Packet[6 + n] = packetData[n];

                ushort checksum = MavlinkCRC.crc_calculate(Packet, Packet[1] + 6);

                checksum = MavlinkCRC.crc_accumulate(MAVLINK_MESSAGE_CRCS[msgID], checksum);
                
                byte ck_a = (byte)(checksum & 0xFF); ///< High byte
                byte ck_b = (byte)(checksum >> 8); ///< Low byte

                Packet[Packet.Length - 2] = ck_a;
                Packet[Packet.Length - 1] = ck_b;

                return Packet;
            }

    //Overloaded function
            public byte[] GeneratePacket(object packetData, byte msgID)
            {
                int len = System.Runtime.InteropServices.Marshal.SizeOf(packetData);
                byte[] arr = new byte[len];
                IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocHGlobal(len);
                System.Runtime.InteropServices.Marshal.StructureToPtr(packetData, ptr, true);
                System.Runtime.InteropServices.Marshal.Copy(ptr, arr, 0, len);
                System.Runtime.InteropServices.Marshal.FreeHGlobal(ptr);

                return GeneratePacket(arr, msgID);

            }


            public bool SendPacket(byte[] packetData)
            {
                try
                {
                    if (MAVlinkPort.IsOpen)
                    {
                        MAVlinkPort.Write(packetData, 0, packetData.Length);
                        return true;
                    }
                }
                catch
                {
                    return false;
                }
                return false;
            }

    The serial port is created as:

            public bool Connect(string COMPort, int BaudRate)
            {
                try
                {
                    MAVlinkPort = new System.IO.Ports.SerialPort(COMPort, BaudRate);
                    MAVlinkPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(MAVlinkPort_DataReceived);
                    MAVlinkPort.DtrEnable = true;   //Required to communicate with ArduPilot
                    MAVlinkPort.RtsEnable = true;
                    MAVlinkPort.Open();
                    MAVlinkPort.DiscardInBuffer();
                    IsConnected = true;
                    return true;
                }
                catch
                {
                    return false;
                }
            }

    I am also very new to Mavlink but this has worked for me. I usually also wait a few seconds after the autopilot has booted before sending my first messages.

    This is how the packet for requesting to stream all data looks:

    254 6 0 255 1 66 20 0 1 0 0 1 183 129

    Hope this can help.

  • Hi Jan, could you show me how you declare variable "packetid" ???

    is that integer or byte or what ??

    thanks

    Jan Swanepoel said:

    Hi Ryan,

    Here is what I have done and the functions that handle the sending of messages.

           public void requestDatastream(byte id, byte rate)
            {
                mavlink_request_data_stream_t rqstDataStream = new mavlink_request_data_stream_t
                {
                    req_message_rate = rate,
                    req_stream_id = id,
                    start_stop = 1,
                    target_system = 1,
                    target_component = 0,
                };

                byte[] packet = GeneratePacket(rqstDataStream, MAVLINK_MSG_ID_REQUEST_DATA_STREAM);

                SendPacket(packet);
            }

           public byte[] GeneratePacket(byte[] packetData, byte msgID)
            {
                byte[] Packet = new byte[packetData.Length + 6 + 2];

                Packet[0] = (byte)254;
                Packet[1] = (byte)packetData.Length;
                Packet[2] = (byte)packetid;
                Packet[3] = (byte)255;
                Packet[4] = (byte)1;
                Packet[5] = msgID;

                for (int n = 0; n < packetData.Length; n++)
                    Packet[6 + n] = packetData[n];

                ushort checksum = MavlinkCRC.crc_calculate(Packet, Packet[1] + 6);

                checksum = MavlinkCRC.crc_accumulate(MAVLINK_MESSAGE_CRCS[msgID], checksum);
                
                byte ck_a = (byte)(checksum & 0xFF); ///< High byte
                byte ck_b = (byte)(checksum >> 8); ///< Low byte

                Packet[Packet.Length - 2] = ck_a;
                Packet[Packet.Length - 1] = ck_b;

                return Packet;
            }

    //Overloaded function
            public byte[] GeneratePacket(object packetData, byte msgID)
            {
                int len = System.Runtime.InteropServices.Marshal.SizeOf(packetData);
                byte[] arr = new byte[len];
                IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocHGlobal(len);
                System.Runtime.InteropServices.Marshal.StructureToPtr(packetData, ptr, true);
                System.Runtime.InteropServices.Marshal.Copy(ptr, arr, 0, len);
                System.Runtime.InteropServices.Marshal.FreeHGlobal(ptr);

                return GeneratePacket(arr, msgID);

            }


            public bool SendPacket(byte[] packetData)
            {
                try
                {
                    if (MAVlinkPort.IsOpen)
                    {
                        MAVlinkPort.Write(packetData, 0, packetData.Length);
                        return true;
                    }
                }
                catch
                {
                    return false;
                }
                return false;
            }

    The serial port is created as:

            public bool Connect(string COMPort, int BaudRate)
            {
                try
                {
                    MAVlinkPort = new System.IO.Ports.SerialPort(COMPort, BaudRate);
                    MAVlinkPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(MAVlinkPort_DataReceived);
                    MAVlinkPort.DtrEnable = true;   //Required to communicate with ArduPilot
                    MAVlinkPort.RtsEnable = true;
                    MAVlinkPort.Open();
                    MAVlinkPort.DiscardInBuffer();
                    IsConnected = true;
                    return true;
                }
                catch
                {
                    return false;
                }
            }

    I am also very new to Mavlink but this has worked for me. I usually also wait a few seconds after the autopilot has booted before sending my first messages.

    This is how the packet for requesting to stream all data looks:

    254 6 0 255 1 66 20 0 1 0 0 1 183 129

    Hope this can help.

  • Hello folks,

     

    you can find the last version of my MAVLinkJava project here :

    https://code.google.com/p/mavlinkjava/

     

    Best

     

    Guillaume

  • Hi

    This is cool! But does anyone have a simple vb.net example of send and recieve like this ?

    Tommy

    VB.NET Shop
This reply was deleted.

Activity

Jose Araujo liked Jose Araujo's profile
Aug 29
spencer harvey liked spencer harvey's profile
Jul 9
More…