#include "MPU6050.h" #include "I2C.h" #include "usart.h" #define PRINT_ACCEL (0x01) #define PRINT_GYRO (0x02) #define PRINT_QUAT (0x04) #define ACCEL_ON (0x01) #define GYRO_ON (0x02) #define MOTION (0) #define NO_MOTION (1) #define DEFAULT_MPU_HZ (200) #define FLASH_SIZE (512) #define FLASH_MEM_START ((void*)0x1800) #define q30 1073741824.0f short gyro[3], accel[3], sensors; //零点漂移计数 int Deviation_Count; short sum_gyro[3]; short sum_accel[3]; // Gyro static error, raw data //陀螺仪静差,原始数据 short Deviation_gyro[3],Original_gyro[3]; short Deviation_accel[3],Original_accel[3]; float q0=1.0f,q1=0.0f,q2=0.0f,q3=0.0f; //static signed char gyro_orientation[9] = {-1, 0, 0, // 0,-1, 0, // 0, 0, 1}; //static unsigned short inv_row_2_scale(const signed char *row) //{ // unsigned short b; // if (row[0] > 0) // b = 0; // else if (row[0] < 0) // b = 4; // else if (row[1] > 0) // b = 1; // else if (row[1] < 0) // b = 5; // else if (row[2] > 0) // b = 2; // else if (row[2] < 0) // b = 6; // else // b = 7; // error // return b; //} void MPU6050_task(void *pvParameters) { u32 lastWakeTime = getSysTickCnt(); while(1) { //This task runs at 100Hz //此任务以100Hz的频率运行 vTaskDelayUntil(&lastWakeTime, F2T(RATE_100_HZ)); //Read the gyroscope zero before starting //开机前,读取陀螺仪零点 if(Deviation_Count32768) Temp-=65536; //数据类型转换 Temp=(36.53f+Temp/340)*10; //温度放大十倍存放 return (int)Temp; } /************************************************************************** Function: Initialize TIM2 as the encoder interface mode Input : LPF: Digital low-pass filtering frequency (Hz) Output : 0: Settings successful, others: Settings failed 函数功能:设置MPUrobot_select_init.h的数字低通滤波器 入口参数:lpf:数字低通滤波频率(Hz) 返回 值:0:设置成功, 其他:设置失败 **************************************************************************/ unsigned char MPU6050_Set_LPF(u16 lpf) { u8 data=0; if(lpf>=188)data=1; else if(lpf>=98)data=2; else if(lpf>=42)data=3; else if(lpf>=20)data=4; else if(lpf>=10)data=5; else data=6; return I2C_WriteOneByte(devAddr,MPU6050_RA_CONFIG,data); //Set the digital lowpass filter//设置数字低通滤波器 } /************************************************************************** Function: Initialize TIM2 as the encoder interface mode Input : rate:4~1000(Hz) Output : 0: Settings successful, others: Settings failed 函数功能:设置MPUrobot_select_init.h的采样率(假定Fs=1KHz) 入口参数:rate:4~1000(Hz) 返回 值:0:设置成功, 其他:设置失败 **************************************************************************/ unsigned char MPU6050_Set_Rate(u16 rate) { u8 data; if(rate>1000)rate=1000; if(rate<4)rate=4; data=1000/rate-1; data=I2C_WriteOneByte(devAddr,MPU6050_RA_SMPLRT_DIV,data); //Set the digital lowpass filter//设置数字低通滤波器 return MPU6050_Set_LPF(rate/2); //Automatically sets LPF to half of the sampling rate //自动设置LPF为采样率的一半 } /************************************************************************** Function: Initialize TIM2 as the encoder interface mode Input : Gx, Gy, Gz: raw readings (plus or minus) of the x,y, and z axes of the gyroscope Output : 0: success, others: error code 函数功能:获得陀螺仪值(原始值) **************************************************************************/ void MPU_Get_Gyroscope(void) { gyro[0]=(I2C_ReadOneByte(devAddr,MPU6050_RA_GYRO_XOUT_H)<<8)+I2C_ReadOneByte(devAddr,MPU6050_RA_GYRO_XOUT_L); //读取X轴陀螺仪 gyro[1]=(I2C_ReadOneByte(devAddr,MPU6050_RA_GYRO_YOUT_H)<<8)+I2C_ReadOneByte(devAddr,MPU6050_RA_GYRO_YOUT_L); //读取Y轴陀螺仪 gyro[2]=(I2C_ReadOneByte(devAddr,MPU6050_RA_GYRO_ZOUT_H)<<8)+I2C_ReadOneByte(devAddr,MPU6050_RA_GYRO_ZOUT_L); //读取Z轴陀螺仪 if(Deviation_Count