接线图:
这款姿态传感器,有四根引脚,分别是vcc gnd rx tx 分别与ardunio的5v gnd tx1 rx1相连接,实际接线图如下:
根据针脚就可以知道这是一款通过串口通信的芯片,该芯片包含了MPU6050和地磁传感器,并内置了卡尔曼融合滤波,是一款傻瓜式的姿态传感器,芯片将结算号的姿态角通过串口输出,说明书中介绍了数据输出的格式:
下面放出简单的代码(修改自网络上一个人提出问题中附带的代码):
unsigned char Re_buf[11],counter=0;
unsigned char sign=0;
float a[3],w[3],angle[3];
float aaa,bbb,ccc;
void setup() {
// initialize serial:
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
if(sign)
{
sign=0;
if(Re_buf[0]==0x55) //检查帧头
{
switch(Re_buf [1])
{
case 0x53:
aaa = angle[0] = (short(Re_buf [3]<<8| Re_buf [2]))/32768.0*180;
bbb = angle[1] = (short(Re_buf [5]<<8| Re_buf [4]))/32768.0*180;
ccc = angle[2] = (short(Re_buf [7]<<8| Re_buf [6]))/32768.0*180;
break;
}
//Serial.print("angle:");
Serial.print(aaa);Serial.print(",");
Serial.print(bbb);Serial.print(",");
Serial.print(ccc);Serial.println();
}
}
}
void serialEvent1() {
while (Serial1.available()) {
//char inChar = (char)Serial.read(); Serial.print(inChar); //Output Original Data, use this code
Re_buf[counter]=(unsigned char)Serial1.read();
if(counter==0&&Re_buf[0]!=0x55) return; //第0号数据不是帧头
counter++;
if(counter==11) //接收到11个数据
{
counter=0; //重新赋值,准备下一帧数据的接收
sign=1;
}
}
}