前言
MPL是Invensense公司推出的一个运动处理库,用于欧拉角解算的效果亲测很棒。不过目前网上貌似并没有什么教程讲这个库的移植,在此跟大家分享一下自己的移植过程。
环境及平台介绍
- STM32F407VE
- MPU6050
- STM32CUBEMX 4.26.1
- MDK5
- MPL 6.12
官网:https://www.invensense.com/ 真的特别慢,下了好久好久。
链接:https://pan.baidu.com/s/1vX4UFOOXi2n9awqI_DQVGg
提取码:putd
STM32CUBEMX配置
新建工程,选择MCU型号就不说了。
- 打开I2C2、RCC HSE选择Crystal。
- 如果需要的话,可以打开USB CDC用作调试
- 时钟配置
- 外设配置
- 生成代码配置,生成好之后打开工程即可。
文件移植
- 解压motion_driver_6.12.zip,打开motion_driver_6.12\arm\STM32F4_MD6\Projects\eMD6
这个是官方给的例程,将Core文件夹复制到刚才生成的工程的根目录下
- 再将motion_driver_6.12\mpl libraries\arm\Keil中的libmpllib_Keil_M4.zip解压至libmpllib_Keil_M4
- 复制libmpllib.lib到工程文件夹下的IMU_test\core\mpl文件夹里
- 在IMU_test\Drivers目录下新建文件夹Devices,在其中新建mpu_module.c与mpu_module.h文件
代码移植
- 打开IMU_test\MDK-ARM\IMU_test.uvprojx
- 在Options for Target里添加预定义,在原有的之后加上“,MPL_LOG_NDEBUG=1,EMPL,MPU6050,EMPL_TARGET_STM32F4”
3. 继续添加包含目录
要添加下面这些。
- 添加分组,命名为MPL
- 右键点击MPL,添加一些文件进来,这一步要把core下子目录的所有的.c文件都添加进来,包括刚才复制的lib文件。
- 把刚才新建的mpu_module.c添加到User组下面
- 开始撸代码!
在mpu_module.c下添加如下代码。
/* Includes ------------------------------------------------------------------*/
//#include "inv_mpu20608.h"
#include "invensense.h"
#include "invensense_adv.h"
#include "eMPL_outputs.h"
#include "mltypes.h"
#include "mpu.h"
#include "log.h"
#include "packet.h"
#include "inv_mpu.h"
#include "mpu_module.h"
/* Private define ------------------------------------------------------------*/
#ifdef MPU_DEBUG
/* Data read from MPL. */
#define PRINT_ACCEL (0x01)
#define PRINT_GYRO (0x02)
#define PRINT_QUAT (0x04)
#define PRINT_EULER (0x10)
#define PRINT_ROT_MAT (0x20)
#define PRINT_HEADING (0x40)
#define PRINT_PEDO (0x80)
#define PRINT_LINEAR_ACCEL (0x100)
#define PRINT_GRAVITY_VECTOR (0x200)
#endif
/* Switch */
#define ACCEL_ON (0x01)
#define GYRO_ON (0x02)
/* Starting sampling rate. */
#define DEFAULT_mpu_HZ (1000)
#define TEMP_READ_TICK (500)
/* Private typedef -----------------------------------------------------------*/
struct hal_s {
unsigned char lp_accel_mode;
unsigned char lp_6axis_mode;
unsigned char sensors;
unsigned char watermark;
//volatile unsigned char new_sensor;
unsigned char motion_int_mode;
unsigned long next_temp_tick;
unsigned int report;
};
/* Platform-specific information. Kinda like a boardfile. */
struct platform_data_s {
signed char orientation[9];
};
/* Private variables ---------------------------------------------------------*/
/* The sensors can be mounted onto the board in any orientation. The mounting
* matrix seen below tells the MPL how to rotate the raw data from the driver(s).
*/
static struct platform_data_s gyro_pdata = {
.orientation = {
1, 0, 0,
0, 1, 0,
0, 0, 1}
};
static struct hal_s hal = {
0};
unsigned char *mpl_key = (unsigned char*)"eMPL 5.1";
/* Private function prototypes -----------------------------------------------*/
/* Private functions