一:準備材料: mavlink源碼 stm32串口程序python
// Macro to define packed structures
//#ifdef __GNUC__
// #define MAVPACKED( __Declaration__ ) __Declaration__ __attribute__((packed))
//#else
// #define MAVPACKED( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop) )
//#endif
#define MAVPACKED( __Declaration__ ) __Declaration__
#pragma anon_unions
#defineinline __INLINE
MAVLINK_HELPER void mavlink_dcm_to_quaternion(constfloat dcm[3][3],float quaternion[4])
{
int dcm_j,dcm_k;
float s;
float tr = dcm[0][0]+ dcm[1][1]+ dcm[2][2];
if(tr >0.0f){
float s = sqrtf(tr +1.0f);
quaternion[0]= s *0.5f;
s =0.5f/ s;
quaternion[1]=(dcm[2][1]- dcm[1][2])* s;
quaternion[2]=(dcm[0][2]- dcm[2][0])* s;
quaternion[3]=(dcm[1][0]- dcm[0][1])* s;
}else{
/* Find maximum diagonal element in dcm
* store index in dcm_i */
int dcm_i =0;
int i;
for(i =1; i <3; i++){
if(dcm[i][i]> dcm[dcm_i][dcm_i]){
dcm_i = i;
}
}
dcm_j =(dcm_i +1)%3;
dcm_k =(dcm_i +2)%3;
s = sqrtf((dcm[dcm_i][dcm_i]- dcm[dcm_j][dcm_j]-
dcm[dcm_k][dcm_k])+1.0f);
quaternion[dcm_i +1]= s *0.5f;
s =0.5f/ s;
quaternion[dcm_j +1]=(dcm[dcm_i][dcm_j]+ dcm[dcm_j][dcm_i])* s;
quaternion[dcm_k +1]=(dcm[dcm_k][dcm_i]+ dcm[dcm_i][dcm_k])* s;
quaternion[0]=(dcm[dcm_k][dcm_j]- dcm[dcm_j][dcm_k])* s;
}
}
MAVLINK_HELPER uint16_t mavlink_msg_to_send_buffer(uint8_t*buffer,constmavlink_message_t*msg)
{
uint8_t*ck;
memcpy(buffer,(constuint8_t*)&msg->magic, MAVLINK_NUM_HEADER_BYTES +(uint16_t)msg->len);
ck = buffer +(MAVLINK_NUM_HEADER_BYTES +(uint16_t)msg->len);
ck[0]=(uint8_t)(msg->checksum &0xFF);
ck[1]=(uint8_t)(msg->checksum >>8);
return MAVLINK_NUM_NON_PAYLOAD_BYTES +(uint16_t)msg->len;
}
#define_MAV_RETURN_int8_t(msg, wire_offset)(int8_t)_MAV_PAYLOAD(msg)[wire_offset]
#define_MAV_RETURN_uint8_t(msg, wire_offset)(uint8_t)_MAV_PAYLOAD(msg)[wire_offset]
/** @file mavlink_avoid_errors.h
* @簡介:本文件是由624668529添加,用來統一解決mavlink報錯信息
* @see QQ624668529
*/
#ifndef MAVLINK_AVOID_ERRORS_H
#define MAVLINK_AVOID_ERRORS_H
/*解決..\MAVLINK\common\../mavlink_types.h(53): error: #20: identifier "pack" is undefined*/
#define MAVPACKED( __Declaration__ ) __Declaration__
/*解決..\MAVLINK\common\../mavlink_types.h(53): error: #3092: anonymous unions are only supported in --gnu mode, or when enabled with #pragma anon_unions*/
#pragma anon_unions
#defineinline __INLINE
#ifndef memset//由624668529添加 2018-08-24
staticinlinevoid*memset(void*dest,int data,size_t length){
uint32_t i;
int*point = dest;
for(i=0; i<length; i++) point[i]= data;
return dest;
}
#endif
#ifndef memcpy//由624668529添加 2018-08-24
void*memcpy(void*dest,constvoid*src,size_t n)
{
unsignedchar*pout =(unsignedchar*)dest;
unsignedchar*pin =(unsignedchar*)src;
while(n-->0)*pout++=*pin++;
return dest;
}
#include"mavlink_types.h"
#define MAVLINK_USE_CONVENIENCE_FUNCTIONS
#define MAVLINK_SEPARATE_HELPERS
//mavlink_system_t mavlink_system = {0,0};
mavlink_system_t mavlink_system ={
1,
1
};// System ID, 1-255, Component/Subsystem ID, 1-255
void comm_send_ch(mavlink_channel_t chan,uint8_t buf)
{
chan=chan;
USART_SendData(USART1, buf);//向串口1發送數據
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待發送結束
}
#endif
#include"mavlink.h"
#include"mavlink_helpers.h"
#endif//AVLINK_AVOID_ERRORS_H
#include"sys.h"
#include"delay.h"
#include"usart.h"
#include"led.h"
#include"beep.h"
#include"key.h"
#include"mavlink_avoid_errors.h"
//ALIENTEK 探索者STM32F407開發板 實驗4
//串口通訊實驗 -庫函數版本
//技術支持:www.openedv.com
//淘寶店鋪:http://eboard.taobao.com
//廣州市星翼電子科技有限公司
//做者:正點原子 @ALIENTEK
int main(void)
{
mavlink_heartbeat_t packet_in ={
963497464,17,84,151,218,3
};
mavlink_heartbeat_t packet1;
memset(&packet1,0,sizeof(packet1));
packet1.custom_mode = packet_in.custom_mode;
packet1.type = packet_in.type;
packet1.autopilot = packet_in.autopilot;
packet1.base_mode = packet_in.base_mode;
packet1.system_status = packet_in.system_status;
packet1.mavlink_version = packet_in.mavlink_version;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設置系統中斷優先級分組2
delay_init(168);//延時初始化
uart_init(115200);//串口初始化波特率爲115200
LED_Init();//初始化與LED鏈接的硬件接口
while(1)
{
mavlink_msg_heartbeat_send(MAVLINK_COMM_1 , packet1.type , packet1.autopilot , packet1.base_mode , packet1.custom_mode , packet1.system_status );
delay_ms(1000);
}
}
MAVLINK_HELPER void mavlink_dcm_to_quaternion(constfloat dcm[3][3],float quaternion[4])
{
int dcm_j,dcm_k;
float s;
float tr = dcm[0][0]+ dcm[1][1]+ dcm[2][2];
if(tr >0.0f){
float s = sqrtf(tr +1.0f);
quaternion[0]= s *0.5f;
s =0.5f/ s;
quaternion[1]=(dcm[2][1]- dcm[1][2])* s;
quaternion[2]=(dcm[0][2]- dcm[2][0])* s;
quaternion[3]=(dcm[1][0]- dcm[0][1])* s;
}else{
/* Find maximum diagonal element in dcm
* store index in dcm_i */
int dcm_i =0;
int i;
for(i =1; i <3; i++){
if(dcm[i][i]> dcm[dcm_i][dcm_i]){
dcm_i = i;
}
}
dcm_j =(dcm_i +1)%3;
dcm_k =(dcm_i +2)%3;
s = sqrtf((dcm[dcm_i][dcm_i]- dcm[dcm_j][dcm_j]-
dcm[dcm_k][dcm_k])+1.0f);
quaternion[dcm_i +1]= s *0.5f;
s =0.5f/ s;
quaternion[dcm_j +1]=(dcm[dcm_i][dcm_j]+ dcm[dcm_j][dcm_i])* s;
quaternion[dcm_k +1]=(dcm[dcm_k][dcm_i]+ dcm[dcm_i][dcm_k])* s;
quaternion[0]=(dcm[dcm_k][dcm_j]- dcm[dcm_j][dcm_k])* s;
}
}
MAVLINK_HELPER uint16_t mavlink_msg_to_send_buffer(uint8_t*buffer,constmavlink_message_t*msg)
{
uint8_t*ck;
memcpy(buffer,(constuint8_t*)&msg->magic, MAVLINK_NUM_HEADER_BYTES +(uint16_t)msg->len);
ck = buffer +(MAVLINK_NUM_HEADER_BYTES +(uint16_t)msg->len);
ck[0]=(uint8_t)(msg->checksum &0xFF);
ck[1]=(uint8_t)(msg->checksum >>8);
return MAVLINK_NUM_NON_PAYLOAD_BYTES +(uint16_t)msg->len;
}
// Macro to define packed structures
//#ifdef __GNUC__
// #define MAVPACKED( __Declaration__ ) __Declaration__ __attribute__((packed))
//#else
// #define MAVPACKED( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop) )
//#endif
#define_MAV_RETURN_int8_t(msg, wire_offset)(int8_t)_MAV_PAYLOAD(msg)[wire_offset]
#define_MAV_RETURN_uint8_t(msg, wire_offset)(uint8_t)_MAV_PAYLOAD(msg)[wire_offset]