说实话 恒久力行 这个我也看到很多次了
https://blog.csdn.net/xiaoxilang/article/details/80221065?utm_source=blogxgwz2
https://www.cnblogs.com/daxuezhidao/p/6064802.html
https://blog.csdn.net/u011919377/article/details/78415037
摘自:https://blog.csdn.net/u011919377/article/details/78415037
STM32控制APM飞控(二)MAVLINK源码集成到stm32工程中
JIN金 2017-11-01 15:56:23 3900 收藏 3
分类专栏: APM飞控 文章标签: APM MAVLINK STM32 无人机
MAVLINK协议源码集成到32工程中
一.MAVLINK代码转化为C语言源码文件
主要根据 http://www.cnblogs.com/lovechen/p/5801679.html (作者:恒久力行)的方式,我进行归纳简要说明!
1.首先进入 MAVLINK官网( http://qgroundcontrol.org/mavlink/start ),下拉到MAVLINK Code and Generator处,如下图,得到 mavlink源码有多种途径,这里选取用python生成。即点击MAVLink Generator (C/C++, Python)
后
2.然后进入如下界面,读懂英文解释,说的是生成的方法和步骤
3. 然后点击clone and downed里面的download zip 即可下载 下载好后解压,然后直接运行mavlink-master 里 mavgenerate.py(python环境自己安装,这里不讲)
然后进入如下界面
然后,
点击第一个browse选择message_definitions文件夹里面的v1.0里面的common.xml
然后自己选择输出路径我这里选的新建文件夹
language选择C
protocol选择1.0
然后直接点generate生成
到此材料准备完毕。
二.MAVLINK源码移植到32工程中
1.找到正点原子带有串口例子的工程或者UCOSIII系统工程或者其他RTOS实时操作系统都可以,然后将刚才生成的文件拷贝过来,如图:
然后在KEIL软件中,将所有拷贝过来的文件和头文件目录添加好,如图:
2.在main函数中添加#include "mavlink.h"如下:
然后编译
"..\OBJ\USART.axf" - 30 Error(s), 17 Warning(s).
一个一个解决。
错误一:
..\MAVLINK\common\../mavlink_types.h(53): error: #20: identifier "pack" is undefined
解决方法:
将mavlink_types.h中
#define MAVPACKED( __Declaration__ ) __pragma( pack(push,1)) __Declaration__ __pragma( pack(pop))
改为
#define MAVPACKED( __Declaration__ ) __Declaration__
这里不使用对齐字节了,直接用也是一样的。
编译后:
"..\OBJ\USART.axf" - 30 Error(s), 7 Warning(s).
错误二:
..\MAVLINK\common\../mavlink_types.h(54): error: #3092: anonymous unions are only supported in --gnu mode, or when enabled with #pragma anon_unions
解决方法:
根据提示信息在mavlink_types.h的前面加入#pragma anon_unions
编译后:
"..\OBJ\USART.axf" - 30 Error(s), 8 Warning(s).
错误三:
..\MAVLINK\common\../checksum.h(34): warning: #260-D: explicit type is missing ("int" assumed)
包括后面很多错误都有共同点,指向有inline的行,这里是因为mdk中无法识别inline
解决方法:
在checksum.h加入一行代码定义inline
#defineinline __INLINE
编译后:
"..\OBJ\USART.axf" - 4 Error(s), 195 Warning(s).
错误四:
..\MAVLINK\common\../mavlink_conversions.h(167): error: #268: declaration may not appear after executable statement in block
解决方法:
指向定义的变量,将变量的定义放到函数的最前端(四个错误都是这种错误)
以前mavlink_conversions.h的代码
MAVLINK_HELPER void mavlink_dcm_to_quaternion(constfloat dcm[3][3],float quaternion[4])
{
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;
}
}
int dcm_j =(dcm_i +1)%3;
int dcm_k =(dcm_i +2)%3;
float 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_conversions.h 的代码
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_helpers.h的代码
MAVLINK_HELPER uint16_t mavlink_msg_to_send_buffer(uint8_t*buffer,constmavlink_message_t*msg)
{
memcpy(buffer,(constuint8_t*)&msg->magic, MAVLINK_NUM_HEADER_BYTES +(uint16_t)msg->len);
uint8_t*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;
}
更改后 mavlink_helpers.h 的代码
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;
}
编译后:
"..\OBJ\USART.axf" - 0 Error(s), 195 Warning(s).
解决所有警告的方法:
将protocol.h里面的旧代码
#define _MAV_RETURN_char(msg, wire_offset)(constchar)_MAV_PAYLOAD(msg)[wire_offset]
#define_MAV_RETURN_int8_t(msg, wire_offset)(constint8_t)_MAV_PAYLOAD(msg)[wire_offset]
#define_MAV_RETURN_uint8_t(msg, wire_offset)(constuint8_t)_MAV_PAYLOAD(msg)[wire_offset]
去掉const,改为
#define _MAV_RETURN_char(msg, wire_offset)(constchar)_MAV_PAYLOAD(msg)[wire_offset]
#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]
编译后:
"..\OBJ\USART.axf" - 0 Error(s), 0 Warning(s).