|
@@ -1,11 +1,14 @@
|
|
|
-/*****************************************************************
|
|
|
- * Copyright:2016-2020 www.corvin.cn ROS小课堂
|
|
|
- * Description:使用串口方式读取IMU模块信息.
|
|
|
+/*******************************************************************
|
|
|
+ * Copyright:2016-2021 www.corvin.cn ROS小课堂
|
|
|
+ * Description:使用串口方式读取和控制IMU模块信息.
|
|
|
* Author: corvin
|
|
|
* History:
|
|
|
* 20200401:init this file.
|
|
|
* 20200702:将读取到的各数据的高低自己合成后要转换为short类型,
|
|
|
* 这样才能进行/32768.0*16.0运算.
|
|
|
+ * 20210318:增加可以将yaw角度归零的函数yawZeroCmd().
|
|
|
+ * 20210319:增加可以修改串口通信波特率的函数setIMUBaudRate().
|
|
|
+ * 20210321:增加控制引脚输出数字高低电平的函数setIMUPinOutHL().
|
|
|
******************************************************************/
|
|
|
#include<ros/ros.h>
|
|
|
#include<stdio.h>
|
|
@@ -18,15 +21,18 @@
|
|
|
#include<sys/types.h>
|
|
|
|
|
|
|
|
|
-char r_buf[512];
|
|
|
-int port_fd = 0;
|
|
|
-float acce[3],angular[3],angle[3],quater[4];
|
|
|
+static unsigned char r_buf[88]; //一次从串口中读取的数据存储缓冲区
|
|
|
+static int port_fd = -1; //串口打开时的文件描述符
|
|
|
+static float acce[3],angular[3],angle[3],quater[4];
|
|
|
|
|
|
-/**********************************************
|
|
|
- * Description:打开串口,输入各参数即可.
|
|
|
- *********************************************/
|
|
|
-int openSerialPort(const char *path, int baud, int dataBits,
|
|
|
- const char* parity, int stopBit)
|
|
|
+/******************************************************
|
|
|
+ * Description:打开IMU模块串口,输入两个参数即可连接.
|
|
|
+ * open()打开失败时,返回-1,成功打开时返回文件描述符.
|
|
|
+ * 各参数意义如下:
|
|
|
+ * const char *path:IMU设备的挂载地址;
|
|
|
+ * const int baud:串口通讯的波特率;
|
|
|
+ *****************************************************/
|
|
|
+int openSerialPort(const char *path, const int baud)
|
|
|
{
|
|
|
int fd = 0;
|
|
|
struct termios terminfo;
|
|
@@ -35,11 +41,12 @@ int openSerialPort(const char *path, int baud, int dataBits,
|
|
|
fd = open(path, O_RDWR|O_NOCTTY);
|
|
|
if (-1 == fd)
|
|
|
{
|
|
|
- ROS_ERROR("Open imu dev error:%s", path);
|
|
|
+ ROS_ERROR("Open IMU dev error:%s, baud:%d", path, baud);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- if(isatty(STDIN_FILENO) == 0)
|
|
|
+ //判断当前连接的设备是否为终端设备
|
|
|
+ if (isatty(STDIN_FILENO) == 0)
|
|
|
{
|
|
|
ROS_ERROR("IMU dev isatty error !");
|
|
|
return -2;
|
|
@@ -53,16 +60,6 @@ int openSerialPort(const char *path, int baud, int dataBits,
|
|
|
cfsetospeed(&terminfo, B9600); //设置输出速度
|
|
|
break;
|
|
|
|
|
|
- case 19200:
|
|
|
- cfsetispeed(&terminfo, B19200);
|
|
|
- cfsetospeed(&terminfo, B19200);
|
|
|
- break;
|
|
|
-
|
|
|
- case 38400:
|
|
|
- cfsetispeed(&terminfo, B38400);
|
|
|
- cfsetospeed(&terminfo, B38400);
|
|
|
- break;
|
|
|
-
|
|
|
case 57600:
|
|
|
cfsetispeed(&terminfo, B57600);
|
|
|
cfsetospeed(&terminfo, B57600);
|
|
@@ -73,81 +70,34 @@ int openSerialPort(const char *path, int baud, int dataBits,
|
|
|
cfsetospeed(&terminfo, B115200);
|
|
|
break;
|
|
|
|
|
|
- default://设置默认波特率9600
|
|
|
+ default: //设置默认波特率9600
|
|
|
cfsetispeed(&terminfo, B9600);
|
|
|
cfsetospeed(&terminfo, B9600);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- //设置数据位
|
|
|
+ //设置数据位 - 8 bit
|
|
|
terminfo.c_cflag |= CLOCAL|CREAD;
|
|
|
terminfo.c_cflag &= ~CSIZE;
|
|
|
- switch(dataBits)
|
|
|
- {
|
|
|
- case 7:
|
|
|
- terminfo.c_cflag |= CS7;
|
|
|
- break;
|
|
|
-
|
|
|
- case 8:
|
|
|
- terminfo.c_cflag |= CS8;
|
|
|
- break;
|
|
|
-
|
|
|
- default:
|
|
|
- ROS_ERROR("set dataBit error !");
|
|
|
- return -3;
|
|
|
- }
|
|
|
-
|
|
|
- //设置奇偶校验位
|
|
|
- switch(*parity)
|
|
|
- {
|
|
|
- case 'o': //设置奇校验
|
|
|
- case 'O':
|
|
|
- terminfo.c_cflag |= PARENB;
|
|
|
- terminfo.c_cflag |= PARODD;
|
|
|
- terminfo.c_iflag |= (INPCK|ISTRIP);
|
|
|
- break;
|
|
|
-
|
|
|
- case 'e': //设置偶校验
|
|
|
- case 'E':
|
|
|
- terminfo.c_iflag |= (INPCK|ISTRIP);
|
|
|
- terminfo.c_cflag |= PARENB;
|
|
|
- terminfo.c_cflag &= ~PARODD;
|
|
|
- break;
|
|
|
-
|
|
|
- case 'n': //不设置奇偶校验位
|
|
|
- case 'N':
|
|
|
- terminfo.c_cflag &= ~PARENB;
|
|
|
- break;
|
|
|
+ terminfo.c_cflag |= CS8;
|
|
|
|
|
|
- default:
|
|
|
- ROS_ERROR("set parity error !");
|
|
|
- return -4;
|
|
|
- }
|
|
|
-
|
|
|
- //设置停止位
|
|
|
- switch(stopBit)
|
|
|
- {
|
|
|
- case 1:
|
|
|
- terminfo.c_cflag &= ~CSTOPB;
|
|
|
- break;
|
|
|
+ //不设置奇偶校验位 - N
|
|
|
+ terminfo.c_cflag &= ~PARENB;
|
|
|
|
|
|
- case 2:
|
|
|
- terminfo.c_cflag |= CSTOPB;
|
|
|
- break;
|
|
|
+ //设置停止位 - 1
|
|
|
+ terminfo.c_cflag &= ~CSTOPB;
|
|
|
|
|
|
- default:
|
|
|
- ROS_ERROR("set stopBit error !");
|
|
|
- return -5;
|
|
|
- }
|
|
|
+ //设置等待时间和最小接收字符
|
|
|
+ terminfo.c_cc[VTIME] = 0;
|
|
|
+ terminfo.c_cc[VMIN] = 1;
|
|
|
|
|
|
- terminfo.c_cc[VTIME] = 10;
|
|
|
- terminfo.c_cc[VMIN] = 0;
|
|
|
+ //清除串口缓存的数据
|
|
|
tcflush(fd, TCIFLUSH);
|
|
|
|
|
|
if((tcsetattr(fd, TCSANOW, &terminfo)) != 0)
|
|
|
{
|
|
|
ROS_ERROR("set imu serial port attr error !");
|
|
|
- return -6;
|
|
|
+ return -3;
|
|
|
}
|
|
|
|
|
|
return fd;
|
|
@@ -162,37 +112,70 @@ int closeSerialPort()
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-/*****************************************************
|
|
|
- * Description:向串口中发送数据.
|
|
|
- ****************************************************/
|
|
|
-int send_data(int fd, char *send_buffer, int length)
|
|
|
+/*****************************************************************
|
|
|
+ * Description:向IMU模块的串口中发送数据,第一步就是先解锁,其次才能
|
|
|
+ * 发送控制命令,最后就是需要发送命令保存刚才的操作.解锁命令是固
|
|
|
+ * 定的0xFF 0xAA 0x69 0x88 0xB5,保存命令也是固定的0xFF 0xAA 0x00
|
|
|
+ * 0x00 0x00.
|
|
|
+ *****************************************************************/
|
|
|
+int send_data(int fd, unsigned char *send_buffer, int length)
|
|
|
{
|
|
|
- length = write(fd, send_buffer, length*sizeof(unsigned char));
|
|
|
+ int ret = -1;
|
|
|
+ unsigned char unLockCmd[5] = {0xFF, 0xAA, 0x69, 0x88, 0xB5};
|
|
|
+ unsigned char saveCmd[5] = {0xFF, 0xAA, 0x00, 0x00, 0x00};
|
|
|
|
|
|
- return length;
|
|
|
-}
|
|
|
+ ret = write(fd, unLockCmd, sizeof(unLockCmd));
|
|
|
+ if(ret != sizeof(unLockCmd))
|
|
|
+ {
|
|
|
+ ROS_ERROR("Send IMU module unlock cmd error !");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ ros::Duration(0.01).sleep(); //delay 10ms才能发送其他配置命令
|
|
|
|
|
|
-/*******************************************************
|
|
|
- * Description:从串口中接收数据.
|
|
|
- *******************************************************/
|
|
|
-int recv_data(int fd, char* recv_buffer, int len)
|
|
|
-{
|
|
|
- int length = read(fd, recv_buffer, len);
|
|
|
- return length;
|
|
|
+ #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
|
|
|
+
|
|
|
+ //发送控制命令
|
|
|
+ ret = write(fd, send_buffer, length*sizeof(unsigned char));
|
|
|
+ if(ret != length)
|
|
|
+ {
|
|
|
+ ROS_ERROR("Send IMU module control cmd error !");
|
|
|
+ return -2;
|
|
|
+ }
|
|
|
+
|
|
|
+ //发送保存命令
|
|
|
+ ret = write(fd, saveCmd, sizeof(saveCmd));
|
|
|
+ if(ret != sizeof(saveCmd))
|
|
|
+ {
|
|
|
+ ROS_ERROR("Send IMU module save cmd error !");
|
|
|
+ return -3;
|
|
|
+ }
|
|
|
+ ros::Duration(0.1).sleep(); //delay 100ms才能进行其他操作
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-/************************************************************
|
|
|
- * Description:根据串口数据协议来解析有效数据,
|
|
|
- ***********************************************************/
|
|
|
-void parse_serialData(char chr)
|
|
|
+/**************************************************************
|
|
|
+ * Description:根据串口数据协议来解析有效数据,这里共有4种数据.
|
|
|
+ * 解析读取其中的44个字节,正好是4帧数据,每一帧数据是11个字节.
|
|
|
+ * 有效数据包括加速度输出(0x55 0x51),角速度输出(0x55 0x52)
|
|
|
+ * 角度输出(0x55 0x53),四元素输出(0x55 0x59).
|
|
|
+ *************************************************************/
|
|
|
+void parse_serialData(unsigned char chr)
|
|
|
{
|
|
|
static unsigned char chrBuf[100];
|
|
|
static unsigned char chrCnt = 0;
|
|
|
+
|
|
|
signed short sData[4]; //save 8 Byte valid info
|
|
|
unsigned char i = 0;
|
|
|
unsigned char frameSum = 0;
|
|
|
|
|
|
- //printf("%d \n",chrCnt);
|
|
|
chrBuf[chrCnt++] = chr; //保存当前字节,字节计数加1
|
|
|
|
|
|
//判断是否满一个完整数据帧11个字节
|
|
@@ -205,12 +188,6 @@ void parse_serialData(char chr)
|
|
|
frameSum += chrBuf[i];
|
|
|
}
|
|
|
|
|
|
- //for(i=0;i<11; i++)
|
|
|
- //ROS_INFO("0x%02x ",chrBuf[i]);
|
|
|
- //printf("0x%02x ",chrBuf[i]);
|
|
|
- //printf("\n");
|
|
|
- //printf("sum is 0x%02x\n",frameSum);
|
|
|
-
|
|
|
//找到数据帧第一个字节是0x55,校验和是否正确,若两者有一个不正确,
|
|
|
//都需要移动到最开始的字节,再读取新的字节进行判断数据帧完整性
|
|
|
if ((chrBuf[0] != 0x55)||(frameSum != chrBuf[10]))
|
|
@@ -220,9 +197,11 @@ void parse_serialData(char chr)
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- //for(i=0;i<11; i++)
|
|
|
- // printf("0x%02x ",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])
|
|
@@ -250,85 +229,211 @@ void parse_serialData(char chr)
|
|
|
quater[1] = ((short)(((short)chrBuf[5]<<8)|chrBuf[4]))/32768.0;
|
|
|
quater[2] = ((short)(((short)chrBuf[7]<<8)|chrBuf[6]))/32768.0;
|
|
|
quater[3] = ((short)(((short)chrBuf[9]<<8)|chrBuf[8]))/32768.0;
|
|
|
+ //printf("%f %f %f %f\n", quater[0], quater[1], quater[2], quater[3]);
|
|
|
break;
|
|
|
}
|
|
|
chrCnt = 0;
|
|
|
}
|
|
|
|
|
|
-/*****************************************************************
|
|
|
- * Description:根据串口配置信息来配置打开串口,准备进行数据通信
|
|
|
- ****************************************************************/
|
|
|
-int initSerialPort(const char* path, const int baud, const int dataBits,
|
|
|
- const char* parity, const int stopBit)
|
|
|
+/********************************************************************
|
|
|
+ * Description:将yaw角度归零,这里需要通过串口发送的命令如下所示,
|
|
|
+ * 发送z轴角度归零的固定命令:0xFF 0xAA 0x01 0x04 0x00;
|
|
|
+ *******************************************************************/
|
|
|
+int makeYawZero(void)
|
|
|
{
|
|
|
- bzero(r_buf, 512);
|
|
|
- port_fd = openSerialPort(path, baud, dataBits, parity, stopBit);
|
|
|
- if(port_fd < 0)
|
|
|
+ int ret = 0;
|
|
|
+ unsigned char yawZeroCmd[5] = {0xFF, 0xAA, 0x01, 0x04, 0x00};
|
|
|
+
|
|
|
+ ret = send_data(port_fd, yawZeroCmd, sizeof(yawZeroCmd));
|
|
|
+ if(ret != 0)
|
|
|
{
|
|
|
- ROS_ERROR("openSerialPort error !");
|
|
|
+ ROS_ERROR("Send yaw zero control cmd error !");
|
|
|
return -1;
|
|
|
}
|
|
|
-
|
|
|
+ ROS_INFO("set yaw to zero radian successfully !");
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-float getAccX()
|
|
|
-{
|
|
|
- return acce[0];
|
|
|
-}
|
|
|
-float getAccY()
|
|
|
-{
|
|
|
- return acce[1];
|
|
|
-}
|
|
|
-float getAccZ()
|
|
|
+/******************************************************************
|
|
|
+ * Description:更新串口通信波特率,根据输入参数的不同,修改为不同的
|
|
|
+ * 波特率,1修改为9600,2修改为57600,3修改为115200.修改波特率就是
|
|
|
+ * 发送3条指令,第一条命令是解除锁定,然后是发送串口波特率更新命令,
|
|
|
+ * 最后是保存操作的指令.
|
|
|
+ ******************************************************************/
|
|
|
+int setIMUBaudRate(const int flag)
|
|
|
{
|
|
|
- return acce[2];
|
|
|
+ int ret = 0;
|
|
|
+ int baud = 0;
|
|
|
+ unsigned char baudRate[5] = {0xFF, 0xAA, 0x04, 0x00, 0x00};
|
|
|
+
|
|
|
+ switch(flag)
|
|
|
+ {
|
|
|
+ case 1: //set baud 9600 bps
|
|
|
+ baudRate[3] = 0x02;
|
|
|
+ baud = 9600;
|
|
|
+ break;
|
|
|
+ case 2: //set baud 57600 bps
|
|
|
+ baudRate[3] = 0x05;
|
|
|
+ baud = 57600;
|
|
|
+ break;
|
|
|
+ case 3: //set baud 115200 bps
|
|
|
+ baudRate[3] = 0x06;
|
|
|
+ baud = 115200;
|
|
|
+ break;
|
|
|
+ default: //default set baud 57600 bps
|
|
|
+ baudRate[3] = 0x05;
|
|
|
+ baud = 57600;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ ret = send_data(port_fd, baudRate, sizeof(baudRate));
|
|
|
+ if(ret != 0)
|
|
|
+ {
|
|
|
+ ROS_ERROR("Send baud rate update cmd error !");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ ROS_INFO("Set new baud rate:[ %d bps] successfully !", baud);
|
|
|
+ ROS_INFO("Please reconnect IMU module with new baud !");
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-float getAngularX()
|
|
|
+/*********************************************************************
|
|
|
+ * Description:要想控制引脚输出数字高低电平,其实就是发送固定的控制命令,
|
|
|
+ * 这里的高低电平的控制字节是命令中的第4个字节,0x03表明是输出低电平,
|
|
|
+ * 要想输出高电平就是将该字节改为0x02即可.剩下就是依次发送这4个引脚
|
|
|
+ * 的控制命令就可以了.
|
|
|
+ ********************************************************************/
|
|
|
+int setIMUPinOutHL(const int d0,const int d1,const int d2,const int d3)
|
|
|
{
|
|
|
- return angular[0];
|
|
|
+ int ret = -1;
|
|
|
+ unsigned char pinD0CtlCmd[5] = {0xFF, 0xAA, 0x0E, 0x03, 0x00};
|
|
|
+ unsigned char pinD1CtlCmd[5] = {0xFF, 0xAA, 0x0F, 0x03, 0x00};
|
|
|
+ unsigned char pinD2CtlCmd[5] = {0xFF, 0xAA, 0x10, 0x03, 0x00};
|
|
|
+ unsigned char pinD3CtlCmd[5] = {0xFF, 0xAA, 0x11, 0x03, 0x00};
|
|
|
+
|
|
|
+ if(d0 == 1)
|
|
|
+ pinD0CtlCmd[3] = 0x02;
|
|
|
+ if(d1 == 1)
|
|
|
+ pinD1CtlCmd[3] = 0x02;
|
|
|
+ if(d2 == 1)
|
|
|
+ pinD2CtlCmd[3] = 0x02;
|
|
|
+ if(d3 == 1)
|
|
|
+ pinD3CtlCmd[3] = 0x02;
|
|
|
+
|
|
|
+ ret = send_data(port_fd, pinD0CtlCmd, sizeof(pinD0CtlCmd));
|
|
|
+ if(ret != 0)
|
|
|
+ {
|
|
|
+ ROS_ERROR("Send pin D0 control cmd error !");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ ret = send_data(port_fd, pinD1CtlCmd, sizeof(pinD1CtlCmd));
|
|
|
+ if(ret != 0)
|
|
|
+ {
|
|
|
+ ROS_ERROR("Send pin D1 control cmd error !");
|
|
|
+ return -2;
|
|
|
+ }
|
|
|
+ ret = send_data(port_fd, pinD2CtlCmd, sizeof(pinD2CtlCmd));
|
|
|
+ if(ret != 0)
|
|
|
+ {
|
|
|
+ ROS_ERROR("Send pin D2 control cmd error !");
|
|
|
+ return -3;
|
|
|
+ }
|
|
|
+ ret = send_data(port_fd, pinD3CtlCmd, sizeof(pinD3CtlCmd));
|
|
|
+ if(ret != 0)
|
|
|
+ {
|
|
|
+ ROS_ERROR("Send pin D3 control cmd error !");
|
|
|
+ return -4;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-float getAngularY()
|
|
|
+/*****************************************************************
|
|
|
+ * Description:根据串口配置信息来连接IMU串口,准备进行数据通信,
|
|
|
+ * 两个输入参数的意义如下:
|
|
|
+ * const char* path:IMU设备的挂载地址;
|
|
|
+ * const int baud:IMU模块的串口通信波特率;
|
|
|
+ ****************************************************************/
|
|
|
+int initSerialPort(const char* path, const int baud)
|
|
|
{
|
|
|
- return angular[1];
|
|
|
-}
|
|
|
+ port_fd = openSerialPort(path, baud);
|
|
|
+ if(port_fd < 0)
|
|
|
+ {
|
|
|
+ ROS_ERROR("openSerialPort error !");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
|
|
|
-float getAngularZ()
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+/*****************************************
|
|
|
+ * Description:得到三轴加速度信息,输入
|
|
|
+ * 参数可以为0,1,2分别代表x,y,z轴的
|
|
|
+ * 加速度信息.
|
|
|
+ *****************************************/
|
|
|
+float getAcc(int flag)
|
|
|
{
|
|
|
- return angular[2];
|
|
|
+ return acce[flag];
|
|
|
}
|
|
|
|
|
|
-float getAngleX()
|
|
|
+/******************************************
|
|
|
+ * Description:得到角速度信息,输入参数可
|
|
|
+ * 以为0,1,2,分别代表x,y,z三轴的角速度
|
|
|
+ * 信息.
|
|
|
+ *****************************************/
|
|
|
+float getAngular(int flag)
|
|
|
{
|
|
|
- return angle[0];
|
|
|
+ return angular[flag];
|
|
|
}
|
|
|
-float getAngleY()
|
|
|
+
|
|
|
+/*******************************************
|
|
|
+ * Description:获取yaw,pitch,roll,输入参数0,
|
|
|
+ * 1,2可以分别获取到roll,pitch,yaw的数据.
|
|
|
+ *******************************************/
|
|
|
+float getAngle(int flag)
|
|
|
{
|
|
|
- return angle[1];
|
|
|
+ return angle[flag];
|
|
|
}
|
|
|
-float getAngleZ()
|
|
|
+
|
|
|
+/********************************************
|
|
|
+ * Description:输入参数0,1,2,3可以分别获取到
|
|
|
+ * 四元素的q0,q1,q2,q3.但是这里对应的ros中
|
|
|
+ * 的imu_msg.orientation.w,x,y,z的顺序,
|
|
|
+ * 这里的q0是对应w参数,1,2,3对应x,y,z.
|
|
|
+ ********************************************/
|
|
|
+float getQuat(int flag)
|
|
|
{
|
|
|
- return angle[2];
|
|
|
+ return quater[flag];
|
|
|
}
|
|
|
|
|
|
-int getImuData()
|
|
|
+/*************************************************************
|
|
|
+ * Description:从串口读取数据,然后解析出各有效数据段,这里
|
|
|
+ * 一次性从串口中读取88个字节,然后需要从这些字节中进行
|
|
|
+ * 解析读取44个字节,正好是4帧数据,每一帧数据是11个字节.
|
|
|
+ * 有效数据包括加速度输出(0x55 0x51),角速度输出(0x55 0x52)
|
|
|
+ * 角度输出(0x55 0x53),四元素输出(0x55 0x59).
|
|
|
+ *************************************************************/
|
|
|
+int getImuData(void)
|
|
|
{
|
|
|
- int ret = 0;
|
|
|
- ret = recv_data(port_fd, r_buf, 44);
|
|
|
- if(ret == -1)
|
|
|
+ int data_len = 0;
|
|
|
+ bzero(r_buf, sizeof(r_buf)); //存储新数据前,将缓冲区清零
|
|
|
+
|
|
|
+ //开始从串口中读取数据,并将其保存到r_buf缓冲区中
|
|
|
+ data_len = read(port_fd, r_buf, sizeof(r_buf));
|
|
|
+ if(data_len <= 0)
|
|
|
{
|
|
|
ROS_ERROR("read serial port data failed !\n");
|
|
|
closeSerialPort();
|
|
|
-
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- for (int i=0; i<ret; i++)
|
|
|
+ //printf("recv data len:%d\n", data_len);
|
|
|
+ for (int i=0; i<data_len; i++)
|
|
|
{
|
|
|
+ //printf("0x%02x ", r_buf[i]);
|
|
|
parse_serialData(r_buf[i]);
|
|
|
}
|
|
|
+ //printf("\n\n");
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
+
|