9轴姿态角度传感器,其中ky9250陀螺仪由于自带卡尔曼动态滤波算法方便用户使用。ky9250陀螺仪基本可以在各个平台上进行数据的读取(如stm32\arduino\C#\Matlab\树莓\Unity3d\python\ROS\英飞凌\Nvidia jetson linux 等)
1、树莓派和ky9250的接线方式
树莓派和ky92509250的模块(VCC对3V3,RX对TX,TX对RX,GND对GND)
2、安装树莓ch340驱动(下载包中有)
3、上传电脑的示例代码文件到树莓派中
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<assert.h>
#include<termios.h>
#include<string.h>
#include<sys/time.h>
#include<time.h>
#include<sys/types.h>
#include<errno.h>
static int ret;
static int fd;
#define BAUD 115200 //115200
int uart_open(int fd,const char *pathname)
{
fd = open(pathname, O_RDWR|O_NOCTTY);
if (-1 == fd)
{
perror("Can't Open Serial Port");
return(-1);
}
else
printf("open %s success!\n",pathname);
if(isatty(STDIN_FILENO)==0)
printf("standard input is not a terminal device\n");
else
printf("isatty success!\n");
return fd;
}
int uart_set(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio,oldtio;
if ( tcgetattr( fd,&oldtio) != 0) {
perror("SetupSerial 1");
printf("tcgetattr( fd,&oldtio) -> %d\n",tcgetattr( fd,&oldtio));
return -1;
}
bzero( &newtio, sizeof( newtio ) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch( nBits )
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch( nEvent )
{
case 'o':
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'e':
case 'E':
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'n':
case 'N':
newtio.c_cflag &= ~PARENB;
break;
default:
break;
}
/*设置波特率*/
switch( nSpeed )
{
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
case 460800:
cfsetispeed(&newtio, B460800);
cfsetospeed(&newtio, B460800);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if( nStop == 1 )
newtio.c_cflag &= ~CSTOPB;
else if ( nStop == 2 )
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return -1;
}
printf("set done!\n");
return 0;
}
int uart_close(int fd)
{
assert(fd);
close(fd);
return 0;
}
int send_data(int fd, char *send_buffer,int length)
{
length=write(fd,send_buffer,length*sizeof(unsigned char));
return length;
}
int recv_data(int fd, char* recv_buffer,int length)
{
length=read(fd,recv_buffer,length);
return length;
}
float a[17];
void ParseData(char chr)
{
static char chrBuf[100];
static unsigned char chrCnt=0;
static char sData[100];
unsigned char i;
time_t now;
chrBuf[chrCnt++]=chr;
if (chrCnt<52) return;
if (( (chrBuf[0]&0x50)!=0x50 ))
{
printf("Error:%x %x\r\n",chrBuf[0],chrBuf[1]);
memcpy(&chrBuf[0],&chrBuf[1],51);chrCnt--;
return;
}
memcpy(&sData[0],&chrBuf[0],52);
switch(chrBuf[0])
{
case 0x50:
if ((sData[49] * 256.0 * 256.0 + sData[50] * 256.0 + sData[51] - 1000000) * 0.001==128) //校验
{
a[0] = (sData[1] * 256.0 * 256.0 + sData[2] * 256.0 + sData[3] - 1000000) * 0.001; //gx 开始解析串口数据
a[1] = (sData[4] * 256.0 * 256.0 + sData[5] * 256.0 + sData[6] - 1000000) * 0.001;
a[2] = (sData[7] * 256.0 * 256.0 + sData[8] * 256.0 + sData[9] - 1000000) * 0.001;
a[3] = (sData[10] * 256.0 * 256.0 + sData[11] * 256.0 + sData[12] - 1000000) * 0.001; //ax
a[4] = (sData[13] * 256.0 * 256.0 + sData[14] * 256.0 + sData[15] - 1000000) * 0.001;
a[5] = (sData[16] * 256.0 * 256.0 + sData[17] * 256.0 + sData[18] - 1000000) * 0.001;
a[6] = (sData[19] * 256.0 * 256.0 + sData[20] * 256.0 + sData[21] - 1000000) * 0.001; //mx
a[7] = (sData[22] * 256.0 * 256.0 + sData[23] * 256.0 + sData[24] - 1000000) * 0.001;
a[8] = (sData[25] * 256.0 * 256.0 + sData[26] * 256.0 + sData[27] - 1000000) * 0.001;
a[9] = (sData[28] * 256.0 * 256.0 + sData[29] * 256.0 + sData[30] - 1000000) * 0.001; //roll
a[10] = (sData[31] * 256.0 * 256.0 + sData[32] * 256.0 + sData[33] - 1000000) * 0.001; //pitch
a[11] = (sData[34] * 256.0 * 256.0 + sData[35] * 256.0 + sData[36] - 1000000) * 0.001; //raw
a[12] = (sData[37] * 256.0 * 256.0 + sData[38] * 256.0 + sData[39] - 1000000) * 0.001; //q0
a[13] = (sData[40] * 256.0 * 256.0 + sData[41] * 256.0 + sData[42] - 1000000) * 0.001; //q1
a[14] = (sData[43] * 256.0 * 256.0 + sData[44] * 256.0 + sData[45] - 1000000) * 0.001; //q2
a[15] = (sData[46] * 256.0 * 256.0 + sData[47] * 256.0 + sData[48] - 1000000) * 0.001; //q3
}
time(&now);
printf("\r\nT:%s a:%6.3f %6.3f %6.3f ",asctime(localtime(&now)),a[0],a[1],a[2]); //输出加速度 可以自己照这行增加其他数据输出
printf("\r\nT:%s a:%6.3f %6.3f %6.3f ",asctime(localtime(&now)),a[9],a[10],a[11]); //输出欧拉角
break;
}
chrCnt=0;
}
int main(void)
{
char r_buf[1024];
bzero(r_buf,1024);
fd = uart_open(fd,"/dev/ttyUSB0");/*串口号/dev/ttySn,USB口号/dev/ttyUSBn */
if(fd == -1)
{
fprintf(stderr,"uart_open error\n");
exit(EXIT_FAILURE);
}
if(uart_set(fd,BAUD,8,'N',1) == -1)
{
fprintf(stderr,"uart set failed!\n");
exit(EXIT_FAILURE);
}
FILE *fp;
fp = fopen("Record.txt","w");
while(1)
{
ret = recv_data(fd,r_buf,44);
if(ret == -1)
{
fprintf(stderr,"uart read failed!\n");
exit(EXIT_FAILURE);
}
for (int i=0;i<ret;i++) {fprintf(fp,"%2X ",r_buf[i]);ParseData(r_buf[i]);}
usleep(1000);
}
ret = uart_close(fd);
if(ret == -1)
{
fprintf(stderr,"uart_close error\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
4、即可在树莓中取得陀螺仪数据
终端中的Desktop文件夹即为桌面
编辑:gcc Uart901Demo.cpp -o Uart901Demo
运行:./ Uart901Demo
17位ky9250软件包(内有stm32\arduino\C#\Matlab\树莓\Unity3d\python\ROS\英飞凌\Nvidia jetson linux 访问例程)
2024年3月3日驱动和上位机
链接:https://pan.baidu.com/s/1Mlp6Aa01mtP0IZ6bLTK_uA
提取码:abcd