|
@@ -37,6 +37,7 @@
|
|
|
#include "crc16.h"
|
|
|
#include "uart.h"
|
|
|
#include "string.h"
|
|
|
+#include <math.h>
|
|
|
|
|
|
#define UARTx UART2
|
|
|
#define UARTx_IRQn UART2_IRQn
|
|
@@ -56,7 +57,11 @@ static uint8_t init_finished = 0;
|
|
|
static float roll_angle = 0;
|
|
|
static float pitch_angle = 0;
|
|
|
static float yaw_angle = 0;
|
|
|
-//static int16_t tmp =0;
|
|
|
+
|
|
|
+static uint8_t imu_dataError_count = 0;
|
|
|
+
|
|
|
+
|
|
|
+//static int16_t print_times =0;
|
|
|
|
|
|
//static uint32_t printfBuff[16];
|
|
|
//static uint16_t printfLen = 0;
|
|
@@ -76,6 +81,8 @@ static int TransmitData(uint8_t *pdata, uint16_t length);
|
|
|
static void UartEventCallback(void *device, uint32_t wpara, uint32_t lpara)
|
|
|
{
|
|
|
int16_t tmp = 0;
|
|
|
+ uint8_t sum = 0;
|
|
|
+ uint8_t i =0;
|
|
|
UART_Type *uart_Device = (UART_Type *)device;
|
|
|
/*rx interrupt*/
|
|
|
if ((uart_Device->IER & UART_IER_ERXNE_Msk) && (wpara & UART_LSR0_DR_Msk))
|
|
@@ -86,24 +93,50 @@ static void UartEventCallback(void *device, uint32_t wpara, uint32_t lpara)
|
|
|
if (g_imuRxDataIndex >= 11)
|
|
|
{
|
|
|
g_imuRxDataIndex = 0;
|
|
|
+
|
|
|
+ for( i=0; i<10; i++){
|
|
|
+ sum += g_imuRxDataBuff[i];
|
|
|
+ }
|
|
|
+
|
|
|
if(1 == init_finished){
|
|
|
|
|
|
- tmp = g_imuRxDataBuff[3];
|
|
|
- tmp = (tmp<<8)|g_imuRxDataBuff[2];
|
|
|
- roll_angle = 1.0*tmp/32768*180;
|
|
|
+ if((sum == g_imuRxDataBuff[10]) && (0x55 == g_imuRxDataBuff[0]) && (0x53 == g_imuRxDataBuff[1])){
|
|
|
+
|
|
|
+ tmp = g_imuRxDataBuff[3];
|
|
|
+ tmp = (tmp<<8)|g_imuRxDataBuff[2];
|
|
|
+ roll_angle = 1.0*tmp/32768*180;
|
|
|
+
|
|
|
+
|
|
|
+ tmp = g_imuRxDataBuff[5];
|
|
|
+ tmp = (tmp<<8)|g_imuRxDataBuff[4];
|
|
|
+ pitch_angle = 1.0*tmp/32768*180;
|
|
|
+
|
|
|
|
|
|
- tmp = g_imuRxDataBuff[5];
|
|
|
- tmp = (tmp<<8)|g_imuRxDataBuff[4];
|
|
|
- pitch_angle = 1.0*tmp/32768*180;
|
|
|
+ tmp = g_imuRxDataBuff[7];
|
|
|
+ tmp = (tmp<<8)|g_imuRxDataBuff[6];
|
|
|
+ yaw_angle = 1.0*tmp/32768*180;
|
|
|
+
|
|
|
+
|
|
|
+ imu_dataError_count = 0;
|
|
|
+
|
|
|
+
|
|
|
+ }else{
|
|
|
+
|
|
|
+ imu_dataError_count++;
|
|
|
+ if(imu_dataError_count >= 5){
|
|
|
+ imu_dataError_count = 3;
|
|
|
+
|
|
|
+ roll_angle = -300;
|
|
|
+ pitch_angle = -300;
|
|
|
+ yaw_angle = -300;
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- tmp = g_imuRxDataBuff[7];
|
|
|
- tmp = (tmp<<8)|g_imuRxDataBuff[6];
|
|
|
- yaw_angle = 1.0*tmp/32768*180;
|
|
|
+ }
|
|
|
|
|
|
-// printfLen = snprintf((char*)printfBuff, 64, "Imu roll: %f, pitch:%f, yaw:%f \r\n", roll_angle, pitch_angle, yaw_angle);
|
|
|
-// rs485_TransmitData((uint8_t*)printfBuff, printfLen);
|
|
|
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -132,8 +165,8 @@ static void uart_Initialize(void)
|
|
|
UART_ConfigType uartConfig = {0};
|
|
|
|
|
|
//set pin mux
|
|
|
-// GPIO_SetFunc(GPIOB, GPIO_PIN9, GPIO_FUN3);
|
|
|
-// GPIO_SetFunc(GPIOB, GPIO_PIN10, GPIO_FUN3);
|
|
|
+ //GPIO_SetFunc(GPIOB, GPIO_PIN9, GPIO_FUN3);
|
|
|
+ //GPIO_SetFunc(GPIOB, GPIO_PIN10, GPIO_FUN3);
|
|
|
|
|
|
|
|
|
uartConfig.baudrate = 9600;
|
|
@@ -179,6 +212,8 @@ void Imu_Init(void)
|
|
|
{
|
|
|
uint16_t crc;
|
|
|
uart_Initialize();
|
|
|
+
|
|
|
+ mdelay(30);
|
|
|
|
|
|
//设置参数
|
|
|
imucmd_buffer[0] = 0xFF;
|
|
@@ -192,8 +227,9 @@ void Imu_Init(void)
|
|
|
imucmd_buffer[5] = (uint8_t)((crc>>8) & 0x00ff);
|
|
|
imucmd_buffer[6] = (uint8_t)(crc & 0x00ff);
|
|
|
TransmitData(imucmd_buffer, 7);
|
|
|
+ mdelay(100);
|
|
|
|
|
|
- //step2 设置输出内容
|
|
|
+ //step2 设置输出内容 ,仅输出角度
|
|
|
imucmd_buffer[2] = 0x02;
|
|
|
imucmd_buffer[3] = 0x08;
|
|
|
imucmd_buffer[4] = 0x00;
|
|
@@ -202,6 +238,7 @@ void Imu_Init(void)
|
|
|
imucmd_buffer[5] = (uint8_t)((crc>>8) & 0x00ff);
|
|
|
imucmd_buffer[6] = (uint8_t)(crc & 0x00ff);
|
|
|
TransmitData(imucmd_buffer, 7);
|
|
|
+ mdelay(100);
|
|
|
|
|
|
//step3 设置单次回传
|
|
|
imucmd_buffer[2] = 0x03;
|
|
@@ -230,9 +267,9 @@ uint8_t Imu_MR(void)
|
|
|
crc = crc16(imucmd_buffer, 5);
|
|
|
imucmd_buffer[5] = (uint8_t)((crc>>8) & 0x00ff);
|
|
|
imucmd_buffer[6] = (uint8_t)(crc & 0x00ff);
|
|
|
- TransmitData(imucmd_buffer, 7);
|
|
|
|
|
|
g_imuRxDataIndex = 0;
|
|
|
+ TransmitData(imucmd_buffer, 7);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
@@ -240,10 +277,14 @@ uint8_t Imu_MR(void)
|
|
|
|
|
|
uint8_t Imu_GetAngle(float* roll, float* pitch, float* yaw)
|
|
|
{
|
|
|
+ UART_SetRXNEInterrupt(UARTx, DISABLE);
|
|
|
+
|
|
|
*roll = roll_angle;
|
|
|
*pitch = pitch_angle;
|
|
|
*yaw = yaw_angle;
|
|
|
|
|
|
+ UART_SetRXNEInterrupt(UARTx, ENABLE);
|
|
|
+
|
|
|
return 0;
|
|
|
}
|
|
|
|