MAVLink and Arduino: step by step

Hello Juan,

Do you know what the upper limit of the number of new samples of the euler angles to arduino is for this mavlink communication method of yours?

My application was to use the euler angle computations from the pixhawk (using the airspeed sensor with it) and feed them to my drone for use in a stabilization algorithm done by the arduino.

Any information would be much appreciated, thank you for writing this article!

Hello @jplopezll ,

I would like to ask some questions. I am currently interested in drone collision avoidance project using ultrasonic sensors. So if I use arduino to receive the data from sensors and have it make a decision to avoid obstacles on its own , let’s say the drone is going forward but then it detect obstacle in the front then arduino decide that the drone need to stop immediately instead of continuing forward. So that’s mean arduino is streaming/sending data to pixhawk right? so how do you sending data to pixhawk? I mean the code structure needed to make that happen.

Hi, @mattskoiiz:

Have you read any of the literature on the matter? I have no experience on the topic, but there are many articles on this topic. Just an example here.

Good luck!

KR,
Juan Pedro

Hello Pedro, did you see my previous message?

My question was: Have you been able to identify an upper limit of the number of new samples of euler angles/ quaternions you can send between the pixhawk and arduino (uno for instance)

probably 50 Hz I guess

I think you may be interested to that:

In the example above it is sending control directly to RCIn but you could add RC override through Mavlink.

Hi, @Shef:

Sorry for the belated answer, Christmas was here…

I do not know what is the limit, but I guess it is very much hardware dependent. MAVLink protocol allows requesting for rates up to 0xFF Hz, this means up to 255 times per second, but angles calculations are always very cycle-hungry processes for any microcontroller and I am sure you will never get that frequency back. Also I am sure it will depend on the priority of the calculation/reporting of the firmware.

I am afraid you will have to experiment it by yourself.

KR,
Juan Pedro

Your estimate has been noted, thank you very much amilcarlucas. I’m kind of asking multiple people the same question to try and get a range of possible max sample transfer rates. You’re very helpful, thanks also for being a pillar of this community.

Mr. Lopez,

Thank you for your response.

Yes, what you say makes sense. I believe this communication method of yours will be my best avenue if another sensor I have in mind comes up short. Have a good one.

Hi everybody,

I tried to use the original code and the simplified code (msg 94 of this thread).
My final aim is to get on Mavlink Global_position_int and channels_output_raw to send on wifi beacon some drone identification data related to the law of my country (to integrate in a code that already exist) and also to trigger camera.

I never got any result with both of these code. Can it be related to the board I use which is a wemos D1 R1 based on ESP8266 (later I’ll use D1 mini)? Maybe related to the 3.3v logic wich is however same than pixhawk I think?

Thanks for advices!

Hi, @BL08FR:

I do not think it is a problem with the logic levels. Most probably has to do with libraries or connections. Can you please post your schematics here? (Only to know what is connected to what and the pins you are using)

KR,
Juan Pedro

@jplopezll
Libraries it’s possible, I downloaded the one of your first message of this thread.
Was it updated?

Connection I don’t believe, I’m quite familliar to it, it’s on telem2 protocol1 baud rate 57.
If i remember well, the pin nearer to the edge of the pixhawk is +5v, then rx (go to tx of the D1R1) then tx (go to rx of D1R1) and last is GND. As you can see on the picture, few messages from pixhawk appear in the serial window so I believe this is fine about connexion.

Any other idea?
Anyway thanks a lot for your help!

[EDIT] I’m sorry to have made you waste your time, it work and you was right!
First I wanted to use pin TX/RX of the board that are connected on the same serial than USB.
Second pin number written on wemos board don’t match GPIO number.
Third, despite I care about it seriously, I reversed rx/tx.
I feel stupid that I spent 2 evenings for this and disturbed you uselessly.

Lesson for all, always check pinout of “exotic” boards!

Hello,
Does anyone experience the latency situation?
I have observed that each message seems to come 2s - 3s late. I test with message ATTITUDE (and many other messages but not simultaneously), parse and print out yaw information. The change of yaw is 2s - 3s later than my action to the autopilot.
If you face the same cases and the solution is known, please help.
Thanks in advance.
Vu.

Hi Juan, thanks a lot for this!

I managed to get some serial response from the Arduino UNO by adding

pxSerial.begin(57600);

to your script. Is this correct that this had to be done? I also uncommented the lines 299 and 300:
Serial.println(“PX ATTITUDE”);
Serial.println(attitude.roll);

The serial monitor looks as following:
Arduino

Which I suppose is fine.

I would like to show the battery voltage (or preferably percentage), GPS height and arm status as well in the serial output. However, I do not manage to output them…

I suppose I have to switch the stream, for some reason it seems to loop continiously in the Case #30:
MAVLINK_MSG_ID_ATTITUDE, which is shown in the serial monitor.

Could you give me advise on how to output other stats from the PX4?

Thanks again for the great work!

Hi, @Menno_Jorna:

You output looks promising. I think you need to read slowly the 5th step or the post. You are receiving several messages but the decoding is not implemented. In step 5 you will see how to decode those messages with message #30 as an example.

The full list of messages and their meaning can be found in the MAVLink library itself: https://mavlink.io/en/messages/common.html#messages.

I can see you are also getting:

As the decoding is not implemented, they are captured by the default case clasue and it just shows the ID of the message and the sequence number.

In case you need a different message, you will have to request the adequate stream. For that, read step 4. Take into account that not all flight controllers implement all streams or messages.

Have a look and revert in case you need further help, kind regards,
Juan Pedro

Hi Juan,

Thanks for the quick reply.
I managed to get the relative altitude of data stream #30, and I understand how to decode the message.

I am however not managing to change/ get new message ID’s/different data streams. It seems to keep getting the same messages shown in the figure (24/30/36/42/74). I would however like to get data stream 1, to obtain the battery voltage.

I would expect that by setting the data stream to all:

It would show all message ID’s in the serial monitor, however it keeps giving me 24/30/36/42/74.

I also tried setting the steams to MAV_DATA_STREAM_RC_CHANNELS and others, but still I recieve the same message ID’s.

Might there be something off with the data stream selection? I see in the Mavlink doc that the stream is replaced in 2015 already by Message_interval?

Hi, @Menno_Jorna:

We have three components to look for carefully:

  1. The library you have installed. What have you configured in your Arduino IDE? There are some versions and sources with slight differences. Some things could be or could be not implemented in your particular version.
  2. The flight controller. Same as above, the manufacturer might have implemented part or all of a specific version of the protocol.
  3. The documentation itself. It usually refers to the last full version, but this might or might not be your case.

In my case, for instance, I have not needed any update and I am using an old flight controller. Thus, even if the documentation says “deprecated” for me it is the cutting-edge-of-technology. :wink:

If you have a well documented flight controller, I would suggest investigating what has been the implementation of the MAVLink protocol.

If you tell me a bit more about your project/layout, I could try to help you with the investigation. Please be patient, as I am now very busy and cannot devote too much time for my hobby.

Kind regards,
Juan Pedro

Hi @jplopezll,

Considering the library: I am using the .zip file as provided by you above.

I am using a PX4 flightcontroller, with the Holybro V3 telemetry set. As microcontroller I am using an Arduino UNO connected with the Rx/Tx to the telemetry receiver.

Please let me know if you have experience with this particular setup and might know the cause of the issue.

I will do further research myself as well. Thank you.

Hi there!

In my application I am trying to obtain simple parameters such as the roll, pitch and yaw angles (mavlink_msg_attitude), and the speeds in each direction (x, y and z - mavlink_msg_global_position_int).
If for the first ones I am able to do it without a problem (using the MAV_DATA_STREAM_EXTENDED_STATUS stream), for the speeds I have not been so lucky.
Whether using MAV_DATA_STREAM_POSITION or MAV_DATA_STREAM_ALL, I always get 2 values for each speed, even using a frequency of 0x01.
The worst of all is that, although the second value seems correct, the first is always absurd, for example, with the drone being stopped it gives me speeds in X of 30000.
What is this and how can I solve it?

Best regards

image

Hi, @Menno_Jorna:

The Holybro is just the modem. It just transmitts the signal and has nothing to do here. Regarding the PX4, what version are you using? (Model, version, manufacturer, firmware).

I can advance you that in my PX4 I had not all the messages implemented. I guess it is (human) time and (microcontroller) memory consuming. If you try to get something very specific, you will probably have to implement it yourself, but it is a tricky way I have never tried.

Kind regards,
Juan Pedro