Arduino Mavlink Library – Monitor the autopilot heartbeat
Monitoring the autopilot heartbeat helps to diagnose an unresponsive flight controller, determine system status, and closes the feedback loop for monitoring functions. Use the mavlink_msg_heartbeat.h file in the common directory to see the parameters which the module supports.
Values included in the heartbeat:
1 2 3 4 5 6 7 8 9 |
typedef struct __mavlink_heartbeat_t { uint32_t custom_mode; ///< A bitfield for use for autopilot-specific flags. uint8_t type; ///< Type of the MAV (quadrotor, helicopter, etc., up to 15 types, defined in MAV_TYPE ENUM) uint8_t autopilot; ///< Autopilot type / class. defined in MAV_AUTOPILOT ENUM uint8_t base_mode; ///< System mode bitfield, see MAV_MODE_FLAGS ENUM in mavlink/include/mavlink_types.h uint8_t system_status; ///< System status flag, see MAV_STATE ENUM uint8_t mavlink_version; ///< MAVLink version, not writable by user, gets added by protocol because of magic data type: uint8_t_mavlink_version } mavlink_heartbeat_t; |
Use the Arduino function below to receive and display the heartbeat variables. Run the function in a loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
MavLink_receive(); } //function called by Arduino to read any MAVlink messages sent by serial communication from flight controller to arduino void MavLink_receive() { mavlink_message_t msg; mavlink_status_t status; while(Serial1.available()) { uint8_t c= Serial1.read(); //Get new message if(mavlink_parse_char(MAVLINK_COMM_0, c, &msg, &status)) { //Handle new message from autopilot switch(msg.msgid) { //handle heartbeat message case MAVLINK_MSG_ID_HEARTBEAT: { mavlink_heartbeat_t hb; mavlink_msg_heartbeat_decode(&msg,&hb); Serial.print(millis()); Serial.print("\nFlight Mode: (10 is auto)");Serial.println(hb.custom_mode); Serial.print("Type: "); Serial.println(hb.type); Serial.print("Autopilot: "); Serial.println(hb.autopilot); Serial.print("Base Mode: "); Serial.println(hb.base_mode); Serial.print("System Status: "); Serial.println(hb.system_status); Serial.print("Mavlink Version: "); Serial.println(hb.mavlink_version) Serial.println(); } break; } } } } |
As you can see, monitoring the values which are included in the heartbeat can provide status updates to a companion computer to ensure system health. The heartbeat is especially handy for ensuring the autopilot is in the correct mode for navigating autonomously.
I just started working with mavlink on an Arduino to help control my pixhawk. I have seen other examples like this one but I don’t understand how they work. On line 13 there is a read off the serial bus for 1 character. That character is passed to mavlink_parse_char with we few other parameters. This method then returns a whole message? How did the code read the rest of the bytes off the serial bus? I ask because I have used a similar example and the code dies on mavlink_parse_char and I am stumped on how it is suppose to work.
@Vincent characters are passed to mavlink_parse_char one at a time and stored in a buffer inside the library (not great design imo). Once the characters resemble a valid message, mavlink_parse_char returns true and the body of the if statement is executed to handle that message