MAVLink and Arduino: step by step

Hi Peter,

Thanks for your reply. Appreciated.

As I am new to this mavlink arduino interface :roll_eyes: can you please elaborate more on how to send statustext message and how it is directed to system id?

Also, I want to know how can I create a sysid & compid for Arduino.

Thank you

Hi, @fawwazichsanul98:

I think APM has good support of MAVLink. I have never worked with it, but I would say that it is perfectly possible.

Dear @Rajakumar:

Please re-read carefully the steps 4 and 5 of the post. You must write your code to implement the message you want. In your case, as you want to send information to the Pixhawk, you must first pack the message and then send it.

The functions needed are detailed in step 4 for the mav_request_data, you must adapt it to the mavlink_msg_statustext. First use:
static inline uint16_t mavlink_msg_statustext_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg, uint8_t severity, const char *text)

And then:
static inline void mavlink_msg_statustext_send_buf(mavlink_message_t *msgbuf, mavlink_channel_t chan, uint8_t severity, const char *text)

You can see in step 4 how to define the variables for the buffer to finaly use Serial.write(buf, len) to start the actual comms.

Hope it is clearer now.

I am having difficulty getting heartbeats from Pixhawk (Arduplane 3.9.6). I am using a modified set of minimal.xml header files from ones used to compile Arduplane 3.9.6 and a slightly modified version of the c/c++ file you posted in #94. Code can be seen at TunaLobster/ArduinoMAVLink. I am compiling for an Arduino Nano using 90% of the dynamic memory (I have a mega 2560 on the way so I will be able to use the full data output.) Serial2 is set to baud 57600 and MAVLinkv2 output. I have swapped RX and TX pins to check my sanity. I am powering from a powerbrick to a Pixhawk 2.1. I have tried 3 different Pixhawk 2.1 boards. I am not sure what else to try at this point.

Hi @TunaLobster:

Try to set your Serial2 to use v1. V2 was not working for me.

I can try to help with troubleshooting, better PM me. I will be probably needing more info on schematics and wiring.

Kind regards.

I got it working for the Mega 2560 on MAVLinkV1. Arduino code is here. Hopefully this helps others out. The Nano might just be quirky enough for it to not like it anymore. SAMD21 might be a better microcontroller for this workload.

I did not have to power the pixhawk 2.1 from a powerbrick. Just USB power.

Hi @jplopezll,

Thanks for the post to help me to set up the mavlink and Arduino.

I ended up transmitting data from pixhawk to Arduino and able to see the data output at the serial monitor. That’s a good sign for me. However, I am wondering if there is any chance for me to send command from Arduino to Pixhawk. For example, I obtain a pitch rate from my own control system and would like to send that pitch rate to the pixhawk so pixhawk can help me to fly the drone with the pitch rate I obtained from my control system.

Is that possible to do it by some ways? I would appreciate if you give me an answer on this question.

Hi @zimin:

Thanks for your kind words!

I am not fully sure, but if you read this article on mission command messages, I would dare to say that it is not possible modifying the pitch rate via MAVLink.

In my opinion, the pitch rate is something you calibrate, send to your flight controller, and then never touch again unless you modify the layout of your drone. But I do not know about your setup and maybe you need to do so.

Kind regards.

1 Like

Hi @jplopezll

Thanks for your reply. I would definitely read through the article.Also, basically, my project team is working on a gnc system which would calculate the control (rotation rate) needed for the drone to fly to a specific location. We use the pixhawk to fly the drone and hope the pixhawk would act like the manual mode whenever they receive the command generated from our gnc system. So, try to think that the taranis transmitter is our gnc system and the pixhawk will fly the drone with the input from the taranis transmitter. Same concept for our gnc system.

If you do have any recommendation, please feel free to give me your opinion. I am also thinking what if my command input change to attitude instead of attitude rate.

Is there ONE source that has all the code? Putting together all the snippets is a pain. I am looking for code that doesn’t run LEDs - it simply outputs requested parameters and allows the user to send Mavlink messages to change parameters inside a PixHawk.

I would greatly appreciate if someone posted a .ZIP file or some other source that was directly compilable. I need to start from this code and add my own functionality.

I figured out how to get things working to a basic degree. I’m using a TEENSY 3.2, and the PixHawk is connected to Serial1. My computer is connected to the USB port (which is Serial0). This thread is so long maybe I missed it but I would like to be able to read the current MODE (STABILIZE, AUTO etc) of the controller, and I would like to be able to change the MODE of the controller, as well as change the direction of movement.

Can anyone give me a few pointers on how to accomplish this?

Hi @jplopezll,

I having some doubts and need help from everyone. I am building an obstacle avoidance system for F450 quadcopter by using hc-sr04 ultrasonic sensor. The arduino UNO TX is connect to RX telem2 pixhawk 2.4.8. It seem like there is no function to the pitch and roll. I was wondering there are some problem in my coding. Below are the code to connect arduino with MAVlink. Thanks.

void FHeartBeat() {
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
uint16_t len;
// System ID = 255 = GCS
mavlink_msg_heartbeat_pack(1, 0, &msg, MAV_TYPE_QUADROTOR, MAV_AUTOPILOT_GENERIC, 0, 1, 0);

// Copy the message to send buffer
len = mavlink_msg_to_send_buffer(buf, &msg);

// Send the message (.write sends as bytes)
Serial.write(buf, len);

//Serial.write("\n\rHeartBeat\n\r");
}

void RCOverride(mavlink_message_t *msg, uint16_t len, uint8_t *buf, uint16_t PitchOut, uint16_t RollOut) {
//Pack and send the calculated Pitch and Roll data. Only send if the data is new
mavlink_msg_rc_channels_override_pack(1, 0 , msg, 1, 0, RollOut, PitchOut, 0, 0, 0, 0, 0, 0);
len = mavlink_msg_to_send_buffer(buf, msg);
Serial.write(buf, len);
Serial.print("\n\rPitch: “);
Serial.print(PitchOut);
Serial.print(”,");
Serial.print(" Roll: ");
Serial.print(RollOut);
}

Dear @jplopezll:
Your way of encode MAVlink is so readable and clear .Thank you so much! I got the msgids from PIXhawk,but the msgids I got are imperfect.
In short, the msgid I want to get is 141.
MAVLINK_MSG_ID_ALTITUDE 141.
(Define in MAVLINK_MSG_ID_ALTITUDE.h).
I have been tried to change the MAVStreams,but it no useful.

>     MAV_DATA_STREAM_ALL=0, // Enable all data streams | 
>     MAV_DATA_STREAM_RAW_SENSORS=1, // Enable IMU_RAW, GPS_RAW, GPS_STATUS packets. | 
>     MAV_DATA_STREAM_EXTENDED_STATUS=2, //Enable GPS_STATUS, CONTROL_STATUS, AUX_STATUS | 
>     MAV_DATA_STREAM_RC_CHANNELS=3, // Enable RC_CHANNELS_SCALED, RC_CHANNELS_RAW, SERVO_OUTPUT_RAW | 
>     MAV_DATA_STREAM_RAW_CONTROLLER=4, // Enable ATTITUDE_CONTROLLER_OUTPUT, POSITION_CONTROLLER_OUTPUT, NAV_CONTROLLER_OUTPUT. | 
>     MAV_DATA_STREAM_POSITION=6, // Enable LOCAL_POSITION, GLOBAL_POSITION/GLOBAL_POSITION_INT messages. |
>     MAV_DATA_STREAM_EXTRA1=10, // Dependent on the autopilot | 
>     MAV_DATA_STREAM_EXTRA2=11, // Dependent on the autopilot |
>     MAV_DATA_STREAM_EXTRA3=12, //Dependent on the autopilot | 
>     MAV_DATA_STREAM_ENUM_END=13,

I have tried each MAVStreams.But the msgids don’t seem to have changed.
It will be grateful if you could answer my dump questions.I will try my best to answer asap.By the way,there may be jet lag,because I am in China.
best wishes for you!:grinning:

1 Like

Hi, @Agent_of_Fooyou:

Sorry for the belated reply, I have not much spare time nowadays…

I canno’t find any stream that include 141 as a reply message, I am almost sure it is not implemented. I don’t know what is your final need, at first sight you have two choices:

  1. Implement the function: write the code, compile, upload to the Pixhawk. Complex task and error prone.

  2. Calculate the altitude yourself based on the pressure reported by the barometer, the GPS and the initial offset. Sounds complex but it is more or less trivial if you do not need extreme precission in the calculations. You need to request for MAV_DATA_STREAM_RAW_SENSORS.

If you need further help, better send me a PM.

Regards.

Hi, @charles_linquist:

Sorry for the belated answer.

As far as I know, you cannot request for the mode via streams. But you can read the value of the RC channel where you send the mode and calculate in which mode you are based on your configuration.

You can also override the value of that channel to manually force a mode on the flight controller. Be careful when doing this, as you may lose control of the aircraft.

For more focused recommendations I would need to know more about your setup and objectives. Send me a PM if you need further assistance.

Kind regards.

Hi, Juan. Your post is the most helpful to understand how to receive data from Mavlink.

I’m having one problem right now, probably you can help.
My code is working but somehow the code -> mavlink_parse_char(MAVLINK_COMM_0, c, &msg, &status) always return the value as zero. Do you have any idea why?

Solved, I did twice Mav_Request_Data() that’s why it doens’t work.

1 Like

Dear @Fazlur_Rahman:

Thank you very much for your kind words. Good to see that your problem is already solved!

Do not hesitate to ask if you need further help.

Kind regards.

Dear @jplopezll
I’m working on a connection between Pixhawk and Arduino and I want to control the movement of the drone from the Arduino according to the ultra sound sensor. I can reicive pixhawk packets on the Arduino and see them on the Arduino’s serial monitor, but my objectif is to write an Arduino code that allows me to send commands to the Pixhawk to control it (Land if there is an obstacle). and also see it on QGroundControl.
My problem now is that I do not know what functions I should use and how to use it.
Please I need HELP.
And Thank you soo much.

Hi, @BARGAZ:

Yours is not an easy question and has many potential answers depending on your setup and your objectives.

Have a look at the definitions and descriptions of the common part of the MAVLink library and revert with what could be your proposal. Either me or anybody else will be willing to help!

Cheers!