|
@@ -6,6 +6,7 @@
|
|
|
* 20200401:init this file.
|
|
|
* 20200702:将读取到的各数据的高低自己合成后要转换为short类型,
|
|
|
* 这样才能进行/32768.0*16.0运算.
|
|
|
+ * 20210318:增加可以将yaw角度归零的函数yawZeroCmd().
|
|
|
******************************************************************/
|
|
|
#include<ros/ros.h>
|
|
|
#include<stdio.h>
|
|
@@ -22,13 +23,13 @@ unsigned char r_buf[88]; //一次从串口中读取的数据存储缓冲区
|
|
|
static int port_fd = -1; //串口打开时的文件描述符
|
|
|
float acce[3],angular[3],angle[3],quater[4];
|
|
|
|
|
|
-/****************************************************
|
|
|
+/******************************************************
|
|
|
* Description:打开IMU模块串口,输入两个参数即可连接.
|
|
|
* open()打开失败时,返回-1,成功打开时返回文件描述符.
|
|
|
* 各参数意义如下:
|
|
|
* const char *path:IMU设备的挂载地址;
|
|
|
* const int baud:串口通讯的波特率;
|
|
|
- ***************************************************/
|
|
|
+ *****************************************************/
|
|
|
int openSerialPort(const char *path, const int baud)
|
|
|
{
|
|
|
int fd = 0;
|
|
@@ -110,12 +111,23 @@ int closeSerialPort()
|
|
|
}
|
|
|
|
|
|
/*****************************************************
|
|
|
- * Description:向串口中发送数据.
|
|
|
+ * Description:向IMU模块的串口中发送数据.
|
|
|
****************************************************/
|
|
|
-int send_data(int fd, char *send_buffer, int length)
|
|
|
+int send_data(int fd, unsigned char *send_buffer, int length)
|
|
|
{
|
|
|
- length = write(fd, send_buffer, length*sizeof(unsigned char));
|
|
|
- return length;
|
|
|
+ int len = -1;
|
|
|
+
|
|
|
+ #if 0 //打印发送给IMU模块的数据,用于调试
|
|
|
+ printf("Send %d byte to IMU:", length);
|
|
|
+ for(int i=0; i<length; i++)
|
|
|
+ {
|
|
|
+ printf("0x%02x ", send_buffer[i]);
|
|
|
+ }
|
|
|
+ printf("\n");
|
|
|
+ #endif
|
|
|
+
|
|
|
+ len = write(fd, send_buffer, length*sizeof(unsigned char));
|
|
|
+ return len;
|
|
|
}
|
|
|
|
|
|
/*******************************************************
|
|
@@ -127,12 +139,12 @@ int recv_data(int fd, unsigned char* recv_buffer, int len)
|
|
|
return length;
|
|
|
}
|
|
|
|
|
|
-/************************************************************
|
|
|
+/**************************************************************
|
|
|
* Description:根据串口数据协议来解析有效数据,这里共有4种数据.
|
|
|
* 解析读取其中的44个字节,正好是4帧数据,每一帧数据是11个字节.
|
|
|
* 有效数据包括加速度输出(0x55 0x51),角速度输出(0x55 0x52)
|
|
|
* 角度输出(0x55 0x53),四元素输出(0x55 0x59).
|
|
|
- ***********************************************************/
|
|
|
+ *************************************************************/
|
|
|
void parse_serialData(unsigned char chr)
|
|
|
{
|
|
|
static unsigned char chrBuf[100];
|
|
@@ -163,9 +175,11 @@ void parse_serialData(unsigned char chr)
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- //for(i=0;i<11; i++)
|
|
|
- // printf("0x%x ",chrBuf[i]);
|
|
|
- //printf("\n");
|
|
|
+ #if 0 //打印出完整的带帧头尾的数据帧
|
|
|
+ for(i=0;i<11; i++)
|
|
|
+ printf("0x%02x ",chrBuf[i]);
|
|
|
+ printf("\n");
|
|
|
+ #endif
|
|
|
|
|
|
memcpy(&sData[0], &chrBuf[2], 8);
|
|
|
switch(chrBuf[1])
|
|
@@ -198,6 +212,46 @@ void parse_serialData(unsigned char chr)
|
|
|
chrCnt = 0;
|
|
|
}
|
|
|
|
|
|
+/********************************************************************
|
|
|
+ * Description:将yaw角度归零,这里需要通过串口发送的命令分为3个步骤,
|
|
|
+ * 首先需要进行解锁操作,就是固定的命令:0xFF,0xAA,0x69,0x88,0xB5;
|
|
|
+ * 其次就是发送z轴角度归零的固定命令:0xFF 0xAA 0x01 0x04 0x00;
|
|
|
+ * 最后就是保存操作,也是固定命令:0xFF 0xAA 0x00 0x00 0x00;
|
|
|
+ *******************************************************************/
|
|
|
+int makeYawZero(void)
|
|
|
+{
|
|
|
+ int ret = 0;
|
|
|
+ unsigned char unLockCmd[5] = {0xFF, 0xAA, 0x69, 0x88, 0xB5};
|
|
|
+ unsigned char yawZeroCmd[5]= {0xFF, 0xAA, 0x01, 0x04, 0x00};
|
|
|
+ unsigned char saveCmd[5] = {0xFF, 0xAA, 0x00, 0x00, 0x00};
|
|
|
+
|
|
|
+ ret = send_data(port_fd, unLockCmd, sizeof(unLockCmd));
|
|
|
+ if(ret != sizeof(unLockCmd))
|
|
|
+ {
|
|
|
+ ROS_ERROR("Send yaw zero unlock cmd error !");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ ros::Duration(0.01).sleep(); //delay 10ms才能发送其他配置命令
|
|
|
+
|
|
|
+ ret = send_data(port_fd, yawZeroCmd, sizeof(yawZeroCmd));
|
|
|
+ if(ret != sizeof(yawZeroCmd))
|
|
|
+ {
|
|
|
+ ROS_ERROR("Send yaw zero control cmd error !");
|
|
|
+ return -2;
|
|
|
+ }
|
|
|
+
|
|
|
+ ret = send_data(port_fd, saveCmd, sizeof(saveCmd));
|
|
|
+ if(ret != sizeof(saveCmd))
|
|
|
+ {
|
|
|
+ ROS_ERROR("Send yaw zero save cmd error !");
|
|
|
+ return -3;
|
|
|
+ }
|
|
|
+ ros::Duration(0.1).sleep(); //delay 100ms才能进行其他操作
|
|
|
+ ROS_INFO("set yaw to zero radian successfully !");
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
/*****************************************************************
|
|
|
* Description:根据串口配置信息来连接IMU串口,准备进行数据通信,
|
|
|
* 两个输入参数的意义如下:
|