|
@@ -6,7 +6,8 @@
|
|
* 20200401:init this file.
|
|
* 20200401:init this file.
|
|
* 20200702:将读取到的各数据的高低自己合成后要转换为short类型,
|
|
* 20200702:将读取到的各数据的高低自己合成后要转换为short类型,
|
|
* 这样才能进行/32768.0*16.0运算.
|
|
* 这样才能进行/32768.0*16.0运算.
|
|
- * 20210318:增加可以将yaw角度归零的函数yawZeroCmd().
|
|
|
|
|
|
+ * 20210318:增加可以将yaw角度归零的函数yawZeroCmd().
|
|
|
|
+ * 20210319:增加可以修改串口通信波特率的函数setIMUBaudRate().
|
|
******************************************************************/
|
|
******************************************************************/
|
|
#include<ros/ros.h>
|
|
#include<ros/ros.h>
|
|
#include<stdio.h>
|
|
#include<stdio.h>
|
|
@@ -19,7 +20,7 @@
|
|
#include<sys/types.h>
|
|
#include<sys/types.h>
|
|
|
|
|
|
|
|
|
|
-unsigned char r_buf[88]; //一次从串口中读取的数据存储缓冲区
|
|
|
|
|
|
+static unsigned char r_buf[88]; //一次从串口中读取的数据存储缓冲区
|
|
static int port_fd = -1; //串口打开时的文件描述符
|
|
static int port_fd = -1; //串口打开时的文件描述符
|
|
float acce[3],angular[3],angle[3],quater[4];
|
|
float acce[3],angular[3],angle[3],quater[4];
|
|
|
|
|
|
@@ -39,7 +40,7 @@ int openSerialPort(const char *path, const int baud)
|
|
fd = open(path, O_RDWR|O_NOCTTY);
|
|
fd = open(path, O_RDWR|O_NOCTTY);
|
|
if (-1 == fd)
|
|
if (-1 == fd)
|
|
{
|
|
{
|
|
- ROS_ERROR("Open imu dev error:%s, baud:%d", path, baud);
|
|
|
|
|
|
+ ROS_ERROR("Open IMU dev error:%s, baud:%d", path, baud);
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -68,7 +69,7 @@ int openSerialPort(const char *path, const int baud)
|
|
cfsetospeed(&terminfo, B115200);
|
|
cfsetospeed(&terminfo, B115200);
|
|
break;
|
|
break;
|
|
|
|
|
|
- default://设置默认波特率9600
|
|
|
|
|
|
+ default: //设置默认波特率9600
|
|
cfsetispeed(&terminfo, B9600);
|
|
cfsetispeed(&terminfo, B9600);
|
|
cfsetospeed(&terminfo, B9600);
|
|
cfsetospeed(&terminfo, B9600);
|
|
break;
|
|
break;
|
|
@@ -110,12 +111,25 @@ int closeSerialPort()
|
|
return ret;
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
|
|
-/*****************************************************
|
|
|
|
- * Description:向IMU模块的串口中发送数据.
|
|
|
|
- ****************************************************/
|
|
|
|
|
|
+/*****************************************************************
|
|
|
|
+ * Description:向IMU模块的串口中发送数据,第一步就是先解锁,其次才能
|
|
|
|
+ * 发送控制命令,最后就是需要发送命令保存刚才的操作.解锁命令是固
|
|
|
|
+ * 定的0xFF 0xAA 0x69 0x88 0xB5,保存命令也是固定的0xFF 0xAA 0x00
|
|
|
|
+ * 0x00 0x00.
|
|
|
|
+ *****************************************************************/
|
|
int send_data(int fd, unsigned char *send_buffer, int length)
|
|
int send_data(int fd, unsigned char *send_buffer, int length)
|
|
{
|
|
{
|
|
- int len = -1;
|
|
|
|
|
|
+ int ret = -1;
|
|
|
|
+ unsigned char unLockCmd[5] = {0xFF, 0xAA, 0x69, 0x88, 0xB5};
|
|
|
|
+ unsigned char saveCmd[5] = {0xFF, 0xAA, 0x00, 0x00, 0x00};
|
|
|
|
+
|
|
|
|
+ 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才能发送其他配置命令
|
|
|
|
|
|
#if 0 //打印发送给IMU模块的数据,用于调试
|
|
#if 0 //打印发送给IMU模块的数据,用于调试
|
|
printf("Send %d byte to IMU:", length);
|
|
printf("Send %d byte to IMU:", length);
|
|
@@ -126,8 +140,24 @@ int send_data(int fd, unsigned char *send_buffer, int length)
|
|
printf("\n");
|
|
printf("\n");
|
|
#endif
|
|
#endif
|
|
|
|
|
|
- len = write(fd, send_buffer, length*sizeof(unsigned char));
|
|
|
|
- return len;
|
|
|
|
|
|
+ //发送控制命令
|
|
|
|
+ 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;
|
|
}
|
|
}
|
|
|
|
|
|
/*******************************************************
|
|
/*******************************************************
|
|
@@ -213,42 +243,57 @@ void parse_serialData(unsigned char chr)
|
|
}
|
|
}
|
|
|
|
|
|
/********************************************************************
|
|
/********************************************************************
|
|
- * Description:将yaw角度归零,这里需要通过串口发送的命令分为3个步骤,
|
|
|
|
- * 首先需要进行解锁操作,就是固定的命令:0xFF,0xAA,0x69,0x88,0xB5;
|
|
|
|
- * 其次就是发送z轴角度归零的固定命令:0xFF 0xAA 0x01 0x04 0x00;
|
|
|
|
- * 最后就是保存操作,也是固定命令:0xFF 0xAA 0x00 0x00 0x00;
|
|
|
|
|
|
+ * Description:将yaw角度归零,这里需要通过串口发送的命令如下所示,
|
|
|
|
+ * 发送z轴角度归零的固定命令:0xFF 0xAA 0x01 0x04 0x00;
|
|
*******************************************************************/
|
|
*******************************************************************/
|
|
int makeYawZero(void)
|
|
int makeYawZero(void)
|
|
{
|
|
{
|
|
int ret = 0;
|
|
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};
|
|
|
|
|
|
+ unsigned char yawZeroCmd[5] = {0xFF, 0xAA, 0x01, 0x04, 0x00};
|
|
|
|
|
|
- ret = send_data(port_fd, unLockCmd, sizeof(unLockCmd));
|
|
|
|
- if(ret != sizeof(unLockCmd))
|
|
|
|
|
|
+ ret = send_data(port_fd, yawZeroCmd, sizeof(yawZeroCmd));
|
|
|
|
+ if(ret != 0)
|
|
{
|
|
{
|
|
- ROS_ERROR("Send yaw zero unlock cmd error !");
|
|
|
|
|
|
+ ROS_ERROR("Send yaw zero control cmd error !");
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
- ros::Duration(0.01).sleep(); //delay 10ms才能发送其他配置命令
|
|
|
|
|
|
+ ROS_INFO("set yaw to zero radian successfully !");
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
|
|
- ret = send_data(port_fd, yawZeroCmd, sizeof(yawZeroCmd));
|
|
|
|
- if(ret != sizeof(yawZeroCmd))
|
|
|
|
|
|
+/******************************************************************
|
|
|
|
+ * Description:更新串口通信波特率,根据输入参数的不同,修改为不同的
|
|
|
|
+ * 波特率,1修改为9600,2修改为57600,3修改为115200.修改波特率就是
|
|
|
|
+ * 发送3条指令,第一条命令是解除锁定,然后是发送串口波特率更新命令,
|
|
|
|
+ * 最后是保存操作的指令.
|
|
|
|
+ ******************************************************************/
|
|
|
|
+int setIMUBaudRate(const int flag)
|
|
|
|
+{
|
|
|
|
+ int ret = 0;
|
|
|
|
+ unsigned char baudRate[5] = {0xFF, 0xAA, 0x04, 0x00, 0x00};
|
|
|
|
+
|
|
|
|
+ switch(flag)
|
|
{
|
|
{
|
|
- ROS_ERROR("Send yaw zero control cmd error !");
|
|
|
|
- return -2;
|
|
|
|
|
|
+ case 1: //set baud 9600 bps
|
|
|
|
+ baudRate[3] = 0x02;
|
|
|
|
+ break;
|
|
|
|
+ case 2: //set baud 57600 bps
|
|
|
|
+ baudRate[3] = 0x05;
|
|
|
|
+ break;
|
|
|
|
+ case 3: //set baud 115200 bps
|
|
|
|
+ baudRate[3] = 0x06;
|
|
|
|
+ break;
|
|
|
|
+ default: //set baud 57600 bps
|
|
|
|
+ baudRate[3] = 0x05;
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
-
|
|
|
|
- ret = send_data(port_fd, saveCmd, sizeof(saveCmd));
|
|
|
|
- if(ret != sizeof(saveCmd))
|
|
|
|
|
|
+ ret = send_data(port_fd, baudRate, sizeof(baudRate));
|
|
|
|
+ if(ret != 0)
|
|
{
|
|
{
|
|
- ROS_ERROR("Send yaw zero save cmd error !");
|
|
|
|
- return -3;
|
|
|
|
|
|
+ ROS_ERROR("Send baud rate update cmd error !");
|
|
|
|
+ return -1;
|
|
}
|
|
}
|
|
- ros::Duration(0.1).sleep(); //delay 100ms才能进行其他操作
|
|
|
|
- ROS_INFO("set yaw to zero radian successfully !");
|
|
|
|
-
|
|
|
|
|
|
+ ROS_INFO("set new baud rate successfully !");
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|