Establishing Mavlink-connection between Arduino and Pixhawk

Hi guys,

I try to establish a connection between my Pixhawk (Arducopter v3.3.3) and an Arduino Uno. The aim is to send custom waypoints via Mavlink from the Arduino to the TELEM2 - port of the Pixhawk.

Now I got some questions concerning the communication. I was looking through the internet for some libraries to set up the Mavlink-package correctly, and I found this page:

http://qgroundcontrol.org/dev/mavlink_arduino_integration_tutorial

I downloaded the file from step 1) and tried to update it with this: https://github.com/mavlink/c_library_v1 (simple copy+paste and overwrite if necessary). But when I try to compile, I just get a long error (a lot of "not declared"s and some more).
When I just use the file from step 1 without updating anything and use the contained example-file I can compile and upload it but I never receive a valid answer (i.e. heartbeat answer).

So to sumarize my problem:
Where can I find a working library for Arduino and Pixhawk for Mavlink?
And is it possible to have 2 active Mavlink-ports on Arducopter? As the TELEM1 port is connected to a wireless telemetry system, TELEM2 is my secondary Mavlink-connection.

Hopefully you can help me and kind regards,

Armin

1 Like

I suggest cloning the git repo: https://github.com/ArduPilot/mavlink.git.

Use git for this:
git clone https://github.com/ArduPilot/mavlink.git
git submodule update --init --recursive
This will copy the repository and update the submodules (such as pymavlink).

A combination of the instructions shown in the git readme, there and in pymavlink/ should help you install the required dependencies.

The mavgenerate script will allow you to select the language (C in your case) and the output folder. The result will be a .h file and .c file in that folder that you can include in your project. Within those files there will be functions that you can use for encoding and decoding mavlink packets. I suggest looking through those files to see what functions are available and what their inputs and outputs will be.

As far as mavlink ports are concerned. They both operate “simultaneously” for all intents and purposes. This means that you can send commands say, via radio to telem1, but also send commands via serial to telem2. This is how you can have an on-board computer update the mission, or otherwise instruct ArduPilot to take an action or store/send data. Likewise, all mavlink streams from the pixhawk are broadcast to both ports. This means that whatever messages you receive on your radio, or ground station, will be received by your on-board computer, you will just have to write the code that receives and decodes the messages.

All the best,
Mike

MinimOSD is based on Arduino and uses Mavlink. You can find some source code here

https://github.com/diydrones/MinimOSD-Extra/blob/master/MinimOsd-Extra/MAVLink.ino

Thanks for your quick response guys! :slight_smile:

Mike, I cloned the github - repository and then run the python-script “mavgenerate”. Then I have to choose, what message_definition I want to use. There I’m a little bit confused. As I’m using the Pixhawk - hardware I suggested to use a “pixhawk.xml” defintion but as you can see here:

there is no definition file for the Pixhawk. What for are these definition - files in detail? As “common.xml” contains the heartbeat for example (and as I see also the waypoint-commands) this one should suit my purposes, right?

I haven’t tried the header files yet but I give you some feedback as soon as I did.

MinimOSD is based on Arduino and uses Mavlink. You can find some source code here

Thanks for this hint! I tried this library used for the MinimOSD, but when I compile, the “mavlink_parse_char” reserves a lot more memory for the incoming message than other librarys I used so far so my Arduino complains that it has only 5% RAM left…unfortunately I need a lot of space for the rest of my program, so these headers wont fit within my project :confused:.

So I try the new compiled headers and then lets see whats happening!

Happy weekend and regards,
Armin

If you are talking to ArduPilot, you need the arupilot xml file. to generate your files. It’s dependent on the autopilot you are talking with. The pixhawk.xml is for the very old autopilot. The new PX4 autopilot only uses messages in the common.xml. (Both ArduPilot and Px4 autopilots runs on Pixhawk HW).

You can limit the space required by using a smaller message buffer length (see header). but you cannot use the longer message types

Hi Bill!

I’m using the Ardupilot-Software on a PX4 hardware. So if I get you right I have to use the common.xml to talk to the PX4? Sorry for the Noob-questions but I want to be sure that the wrong library is no error source (there will be a lot of others for sure :smile:)

Thanks for your help!

You will need to use the message_definitions/v1.0/ardupilotmega.xml file as the input. Common.xml includes the messages defined by the standard mavlink protocol as specified by qgroundcontrol. The ardupilotmega one is the one that has additional messages that are specific to ardupilot. Pymavgen will build for the messages in ardupilotmega.xml and common.xml. If you look at the top of the file you will see it contains some include tags for common to instruct the xml interpreter to include that file as well.

Mike

Hi guys,

I tried to send a heartbeat from one Arduino to another (just for testing) and on the RX-side I was able to receive a valid heartbeat - package and can read out its autopilot type, frame type etc. so it worked so far!
I builded the ardupilotmega and common xml file as you guys mentioned.

Now I got to test, how the Pixhawk PX4 responds to my packages to valid the connection between Arduino and Pixhawk.

Thanks so far for your assist and have a nice weekend!

Armin

You are using Ardupilot software on the Pixhawk HW* which requires the common.xml messages and the ardupilotmega.xml extension messages. PX4 refers to different autopilot software. Its uses only MAVLink messages that are specified in the common.xml file.

*It sound like your are using Pixhawk1 HW, there are other types like the Pixhawk2.2 (TheCube) and Pixracer available as well.

I’m using the Ardupilot software, right (Arducopter V3.3.3) on this Pixhawk hardware: https://pixhawk.org/_media/modules/pixhawk-logo-view.jpg. I thought its called “Pixhawk” but on its side it says “PX4” thats why I was a little bit confused. But mine looks like in the link.

I think the stuff with the definition files (XML) became a little bit clearer now.

1 Like

Great, it always seems so simple when you know! :wink:

Hi @billb, I am a student and working on similar problem for my project. could you tell me are you able to send mavlink packets from arduino to pixhawk and guide me through to do the same.

Hi,

I try to explain it to you as meanwhile, I managed to communicate with my Pixhawk from Arduino.

At first, visit GitHub - ArduPilot/mavlink: MAVLink micro air vehicle marshalling / communication library and follow the instructions below. You need python to generate the header-files. It may seems a little bit tricky, but if you follow every step you should manage to launch the GUI.

Seccond, generate the headerfile you need in the language you use. In my case it was C, as I’m using the Arduino.
Next, it should have generated the header-files you need.

In my case I set up a new Arduino - Codefile and included:

Arduino\libraries\mavlink\mavlink_types.h and Arduino\libraries\mavlink\common\mavlink.h
(I don’t know if both are required, but it works for me)

I connected my Pixhawk TELEM2 Port with my Arduino Serial Port (RX/TX). In addition, I’ve used a common ground, so I connected the TELME2 - GND with Arduino GND.

Pixhawk continiously sends a heartbeat so this is how you can check the connection.
To simply receive this message, you have to implement the code like this:

mavlink_message_t msg;
mavlink_status_t status;
while(Serial.available() > 0){
uint8_t c = Serial.read();
if(mavlink_parse_char(MAVLINK_COMM_0,c,&msg,&status))
{
//Do something with the received message
}

msg now contains your received message. With msg.imsgid you can read out the mavlink-ID of the message for example.

You now should receive some messages from your Pixhawk and can extend your program to send or receive as you need.

Thanks for the reply. There is a difference here I want to have mavlink connection between arduino and pixhawk over Telemetry radio. In my project I want to send mocap data to pixhawk over mavlink. my connection is following .

  1. ski telemetry radio(Air) connected on pixhawk – TELEM1
  2. ski telemetry radio(Ground) is connected to laptop — COM8
  3. Qgroundcontrol is connected to pixhawk using ski radio.
  4. Arduino is connected to laptop – COM3
  5. Serial connection between qgroundcontrol and COM3.

I use Nexonar infrared mocap system which gives me coordinates of my drone in my laptop. I want to send this mocap data to pixhawk. I am new to the drone community, I am not sure whether it is feasible to do with Arduino or not.

I don’t know if its possible to let QGroundControl bypass/forward a MAVLink message. But why do you need QGC at all? You can attach the SKI telemetry directly to your Arduino and build your MAVLink messages there.

If you need these information as well on your laptop then you can also connect the Arduino to your laptop.

What is the Nexonar providing? Is there already a MAVLink message for these information that ArduCopter can handle? If not, then you have to implement one at first. But I suggest you start with the heartbeat. And regarding my answer: there’s no difference weather you connect your Arduino via wire or wireless to the Pixhawk.

Hey im also working on that Project ( together with b-rohit)

Nexonar is providing the Position Information from the drone ( its a Motion Capture System) .
We want to use the internal GPS Mode from Pixhawk to fill in our Position Data manually . ( Pixhawk already has a System with a different Motion Capture System) (https://dev.px4.io/advanced-fake-gps.html

We were always thinking that we need that connection between QGround and Drone to get Mavlink working , but if thats not necessary than we will try your solution .

We will post more information later on .

edit: Why do we even need an arduino Device? Couldnt we just get acces to the drone directly via our Laptop?

Thanks for you help.

Edit2: https://i.ytimg.com/vi/ZooogJNdDE4/maxresdefault.jpg

We are using those Telemtry radios (3dr) . We dont have much experience with souldering so can we just put it on a deivce like that .
https://www.amazon.de/Adapter-FT232RL-Arduino-Christians-Technikshop/dp/B0178HVEH0/ref=sr_1_2?ie=UTF8&qid=1486557805&sr=8-2&keywords=FT232RL

or maybe directly with a UART to Female USB converter? ( http://www.al-kabelshop.de/epages/al-kabelshop.sf/?Locale=de_DE&ObjectPath=/Shops/al-kabelshop/Products/USBA-BA-SB&ViewAction=ViewProductViaPortal&gclid=CjwKEAiAoOvEBRDD25uyu9Lg9ycSJAD0cnBypf8t68y9eaai3qeyac7efGVfcwB5SFZeUxVE6mqmmhoCdBLw_wcB7

:innocent:

atx80 Did you get your arduino to read data from the pixhawk. I have been trying to do a similar thing but with no luck

Hi David,

sorry for the late response.

Yeah, I managed to read out the heartbeat-message and decode it (Autopilot-Type, Frame-Type etc). But I only was able to read it out with the new build Mavlink-libraries. You can create them if you follow the steps on the github-page (https://github.com/ArduPilot/mavlink) or in my post a little higher on this page.

With other libraries I wasn’t able to decode a valid message, even though my Pixhawk was sending something.

@atx80

I discovered this with the libraries I was using as the Arduino was running out of memory. I did see this GitHub link but I had no clue how to make the library. Would you be willing to share a library and a bit of sample code to get me started.

Thanks

Hi David,

I hope this doesn’t cause any trouble providing the generated code but I guess as its free to use for everyone, it should work.

mavlink.zip (388.9 KB)

I provided some example code some posts above here. Another example to lean on is posted here: http://qgroundcontrol.org/dev/mavlink_arduino_integration_tutorial