I2C通信之Arduino与陀螺仪传感器ITG3205
本帖最后由 leicheng 于 2014-2-4 22:10 编辑#include <Wire.h>
//陀螺仪传感器ITG3205
#define ITGAddress 0x68 //ITG3205的I2C地址(AD0接地)
#define G_SMPLRT_DIV 0x15 //设置采样率的寄存器
#define G_DLPF_FS 0x16 //设置量程、低通滤波带宽、时钟频率的寄存器
#define G_INT_CFG 0x17 //设置中断的寄存器
#define G_PWR_MGM 0x3E //设置电源管理的寄存器
int xGyro, yGyro, zGyro; //存放角速度值,温度
int buff; //存放寄存器高低位值,X、Y、Z轴共6个
// 陀螺仪传感器误差修正的偏移量
int g_offx = 0;
int g_offy = 0;
int g_offz = 0;
void writeRegister(int deviceAddress, byte address, byte val)
{
Wire.beginTransmission(deviceAddress);
Wire.write(address);
Wire.write(val);
Wire.endTransmission();
}
void readRegister(int deviceAddress, byte address)
{
Wire.beginTransmission(deviceAddress);
Wire.write(address);
Wire.endTransmission();
Wire.beginTransmission(deviceAddress);
Wire.requestFrom(deviceAddress, 6);
int i = 0;
while(Wire.available())
{ buff = Wire.read(); }
Wire.endTransmission();
}
void initGyro()
{
/*****************************************
* ITG3205
* G_SMPLRT_DIV:采样率 = 125Hz
* G_DLPF_FS:+ - 2000度/秒、低通滤波器5HZ、内部采样率1kHz
* G_INT_CFG:没有中断
* G_PWR_MGM:电源管理设定:无复位、无睡眠模式、无待机模式、内部振荡器
******************************************/
writeRegister(ITGAddress, G_SMPLRT_DIV, 0x07); //设置采样率
writeRegister(ITGAddress, G_DLPF_FS, 0x1E); //设置量程、低通滤波带宽、内部采样率
writeRegister(ITGAddress, G_INT_CFG, 0x00); //设置中断(默认值)
writeRegister(ITGAddress, G_PWR_MGM, 0x00); //设置电源管理(默认值)
}
void getGyroValues()
{
readRegister(ITGAddress, 0x1D); //读取陀螺仪ITG3205的数据
xGyro = ((buff << 8) | buff) + g_offx;
yGyro = ((buff << 8) | buff) + g_offy;
zGyro = ((buff << 8) | buff) + g_offz;
}
void setup()
{
Serial.begin(9600);
Wire.begin();
initGyro();
delay(50);
}
void loop()
{
getGyroValues();
Serial.print("xGyro=");
Serial.print(xGyro);
Serial.print("yGyro=");
Serial.print(yGyro);
Serial.print("zGyro=");
Serial.println(zGyro);
delay(200);
}
将陀螺仪静止放置,运行后截图如下,然后再修正陀螺仪传感器误差偏移量g_offx、g_offy、g_offz。
最后,如果需要把角速度原始值的数字单位换算成度每秒,需要原始值除以14.375。
其它相关帖子:
加速度计和陀螺仪指南
I2C通信之Arduino与加速度传感器ADXL345
貌似滤波效果不错,不知道代码开发原理, 本帖最后由 leicheng 于 2014-2-4 21:20 编辑
学慧放弃 发表于 2014-2-4 13:57 static/image/common/back.gif
貌似滤波效果不错,不知道代码开发原理,
本程序没有添加滤波算法,只是起个头,好让大家快速入门使用I2C连接Arduino和传感器。
滤波算法见henhaiyu的:十大滤波算法程序大全 leicheng 发表于 2014-2-4 17:59 static/image/common/back.gif
本程序没有滤波,只是起个头,好让大家快速入门使用I2C和传感器。论坛里面一大把滤波算法。
但是其他传感器也有现成的库阿 ,为啥还要用这种?? 学慧放弃 发表于 2014-2-4 21:13 static/image/common/back.gif
但是其他传感器也有现成的库阿 ,为啥还要用这种??
传感器选型是自己的事。 {:soso_e179:}{:soso_e179:} 你好,请问读取传感器数据的子程序“getGyroValues()”中的“(buff << 8) | buff”为何和另外一个帖子“I2C通信之Arduino与加速度传感器ADXL345”中读取传感器数据的语句“(buff << 8) | buff”不一致?哪个是对的呢?谢谢。
页:
[1]