mavlink協議移植問題

mavlink協議移植問題

mavlink源代碼是一個代碼庫,使用的時候只須要將mavlink.h頭文件包含到工程項目中便可。app

mavlink通訊協議是無狀態的鏈接,通常採用心跳消息跟蹤系統是否存在。請確保每60、30、10或1秒發送心跳(建議使用1HZ),一旦心跳到達則視爲系統已經鏈接。函數

 

快速整合方法: 發送數據例程(這個例子須要的代碼比較多)

 /* The default UART header for your MCU */ #include "uart.h" #include <mavlink/v1.0/common/mavlink.h>  mavlink_system_t mavlink_system;  mavlink_system.sysid = 20;                   ///< ID 20 for this airplane mavlink_system.compid = MAV_COMP_ID_IMU;     ///< The component sending the message is the IMU, it could be also a Linux process  // Define the system type, in this case an airplane uint8_t system_type = MAV_TYPE_FIXED_WING; uint8_t autopilot_type = MAV_AUTOPILOT_GENERIC;  uint8_t system_mode = MAV_MODE_PREFLIGHT; ///< Booting up uint32_t custom_mode = 0;                 ///< Custom mode, can be defined by user/adopter uint8_t system_state = MAV_STATE_STANDBY; ///< System ready for flight  // Initialize the required buffers mavlink_message_t msg; uint8_t buf[MAVLINK_MAX_PACKET_LEN];  // Pack the message mavlink_msg_heartbeat_pack(mavlink_system.sysid, mavlink_system.compid, &msg, system_type, autopilot_type, system_mode, custom_mode, system_state);  // Copy the message to the send buffer uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);  // Send the message with the standard UART send function // uart0_send might be named differently depending on // the individual microcontroller / library in use. uart0_send(buf, len);

接收數據例程:此段代碼運行效率比較低,因此建議在主循環中運行該函數,而且儘快清空USART緩衝區。

 #include <mavlink/v1.0/common/mavlink.h>  // Example variable, by declaring them static they're persistent // and will thus track the system state static int packet_drops = 0; static int mode = MAV_MODE_UNINIT; /* Defined in mavlink_types.h, which is included by mavlink.h */  /** * @brief Receive communication packets and handle them * * This function decodes packets on the protocol level and also handles * their value by calling the appropriate functions. */ static void communication_receive(void) {  mavlink_message_t msg;  mavlink_status_t status;   // COMMUNICATION THROUGH EXTERNAL UART PORT (XBee serial)   while(uart0_char_available())  {   uint8_t c = uart0_get_char();   // Try to get a new message   if(mavlink_parse_char(MAVLINK_COMM_0, c, &msg, &status)) {    // Handle message     switch(msg.msgid)    {          case MAVLINK_MSG_ID_HEARTBEAT:          {     // E.g. read GCS heartbeat and go into                                  // comm lost mode if timer times out          }          break;    case MAVLINK_MSG_ID_COMMAND_LONG:     // EXECUTE ACTION     break;    default:     //Do nothing     break;    }   }    // And get the next one  }   // Update global packet drops counter  packet_drops += status.packet_rx_drop_count; }

利用適配器函數減小代碼ui

 #include "your_mavlink_bridge_header.h" /* You have to #define MAVLINK_USE_CONVENIENCE_FUNCTIONS in your_mavlink_bridge_header,    and you have to declare: mavlink_system_t mavlink_system;    these two variables will be used internally by the mavlink_msg_xx_send() functions.    Please see the section below for an example of such a bridge header. */ #include <mavlink.h>  // Define the system type, in this case an airplane int system_type = MAV_FIXED_WING; // Send a heartbeat over UART0 including the system type mavlink_msg_heartbeat_send(MAVLINK_COMM_0, system_type, MAV_AUTOPILOT_GENERIC, MAV_MODE_MANUAL_DISARMED, MAV_STATE_STANDBY);
your_mavlink_bridge_header.h /* MAVLink adapter header */ #ifndef YOUR_MAVLINK_BRIDGE_HEADER_H #define YOUR_MAVLINK_BRIDGE_HEADER_H  #define MAVLINK_USE_CONVENIENCE_FUNCTIONS  #include <mavlink/v1.0/mavlink_types.h>  /* Struct that stores the communication settings of this system.    you can also define / alter these settings elsewhere, as long    as they're included BEFORE mavlink.h.    So you can set the     mavlink_system.sysid = 100; // System ID, 1-255    mavlink_system.compid = 50; // Component/Subsystem ID, 1-255     Lines also in your main.c, e.g. by reading these parameter from EEPROM.  */ mavlink_system_t mavlink_system; mavlink_system.sysid = 100; // System ID, 1-255 mavlink_system.compid = 50; // Component/Subsystem ID, 1-255  /**  * @brief Send one char (uint8_t) over a comm channel  *  * @param chan MAVLink channel to use, usually MAVLINK_COMM_0 = UART0  * @param ch Character to send  */ static inline void comm_send_ch(mavlink_channel_t chan, uint8_t ch) {    if (chan == MAVLINK_COMM_0)    {        uart0_transmit(ch);    }    if (chan == MAVLINK_COMM_1)    {     uart1_transmit(ch);    } }  #endif /* YOUR_MAVLINK_BRIDGE_HEADER_H */
相關文章
相關標籤/搜索