极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 25145|回复: 6

I2C通信之Arduino与陀螺仪传感器ITG3205

[复制链接]
发表于 2014-2-3 12:43:03 | 显示全部楼层 |阅读模式
本帖最后由 leicheng 于 2014-2-4 22:10 编辑



  1. #include <Wire.h>
  2. //陀螺仪传感器ITG3205
  3. #define ITGAddress   0x68    //ITG3205的I2C地址(AD0接地)
  4. #define G_SMPLRT_DIV 0x15    //设置采样率的寄存器
  5. #define G_DLPF_FS 0x16     //设置量程、低通滤波带宽、时钟频率的寄存器
  6. #define G_INT_CFG 0x17     //设置中断的寄存器
  7. #define G_PWR_MGM 0x3E    //设置电源管理的寄存器

  8. int xGyro, yGyro, zGyro;      //存放角速度值,温度
  9. int buff[6];                  //存放寄存器高低位值,X、Y、Z轴共6个

  10. // 陀螺仪传感器误差修正的偏移量
  11. int g_offx = 0;
  12. int g_offy = 0;
  13. int g_offz = 0;

  14. void writeRegister(int deviceAddress, byte address, byte val)
  15. {
  16.   Wire.beginTransmission(deviceAddress);
  17.   Wire.write(address);      
  18.   Wire.write(val);        
  19.   Wire.endTransmission();
  20. }

  21. void readRegister(int deviceAddress, byte address)
  22. {
  23.   Wire.beginTransmission(deviceAddress);  
  24.   Wire.write(address);        
  25.   Wire.endTransmission();
  26.   Wire.beginTransmission(deviceAddress);
  27.   Wire.requestFrom(deviceAddress, 6);   

  28.   int i = 0;
  29.   while(Wire.available())   
  30.   { buff[i++] = Wire.read(); }
  31.   Wire.endTransmission();
  32. }

  33. void initGyro()
  34. {
  35.   /*****************************************
  36.    * ITG3205
  37.    * G_SMPLRT_DIV:采样率 = 125Hz
  38.    * G_DLPF_FS:+ - 2000度/秒、低通滤波器5HZ、内部采样率1kHz
  39.    * G_INT_CFG:没有中断
  40.    * G_PWR_MGM:电源管理设定:无复位、无睡眠模式、无待机模式、内部振荡器
  41.    ******************************************/
  42. writeRegister(ITGAddress, G_SMPLRT_DIV, 0x07); //设置采样率
  43. writeRegister(ITGAddress, G_DLPF_FS, 0x1E); //设置量程、低通滤波带宽、内部采样率
  44. writeRegister(ITGAddress, G_INT_CFG, 0x00); //设置中断(默认值)
  45. writeRegister(ITGAddress, G_PWR_MGM, 0x00);    //设置电源管理(默认值)
  46. }

  47. void getGyroValues()
  48. {
  49.   readRegister(ITGAddress, 0x1D); //读取陀螺仪ITG3205的数据
  50.   xGyro = ((buff[0] << 8) | buff[1]) + g_offx;
  51.   yGyro = ((buff[2] << 8) | buff[3]) + g_offy;
  52.   zGyro = ((buff[4] << 8) | buff[5]) + g_offz;
  53. }

  54. void setup()
  55. {
  56.   Serial.begin(9600);
  57.   Wire.begin();
  58.   initGyro();
  59.   delay(50);
  60. }

  61. void loop()
  62. {
  63.     getGyroValues();
  64.     Serial.print("xGyro=");
  65.     Serial.print(xGyro);
  66.     Serial.print("  yGyro=");
  67.     Serial.print(yGyro);
  68.     Serial.print("  zGyro=");
  69.     Serial.println(zGyro);
  70.     delay(200);
  71. }
复制代码


将陀螺仪静止放置,运行后截图如下,然后再修正陀螺仪传感器误差偏移量g_offx、g_offy、g_offz。



最后,如果需要把角速度原始值的数字单位换算成度每秒,需要原始值除以14.375。

其它相关帖子:
加速度计和陀螺仪指南
I2C通信之Arduino与加速度传感器ADXL345

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2014-2-4 13:57:19 | 显示全部楼层
貌似滤波效果不错,不知道代码开发原理,
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-4 17:59:08 | 显示全部楼层
本帖最后由 leicheng 于 2014-2-4 21:20 编辑
学慧放弃 发表于 2014-2-4 13:57
貌似滤波效果不错,不知道代码开发原理,


本程序没有添加滤波算法,只是起个头,好让大家快速入门使用I2C连接Arduino和传感器。
滤波算法见henhaiyu的:十大滤波算法程序大全
回复 支持 反对

使用道具 举报

发表于 2014-2-4 21:13:53 | 显示全部楼层
leicheng 发表于 2014-2-4 17:59
本程序没有滤波,只是起个头,好让大家快速入门使用I2C和传感器。论坛里面一大把滤波算法。

但是其他传感器也有现成的库阿 ,为啥还要用这种??
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-4 21:17:34 | 显示全部楼层
学慧放弃 发表于 2014-2-4 21:13
但是其他传感器也有现成的库阿 ,为啥还要用这种??

传感器选型是自己的事。
回复 支持 反对

使用道具 举报

发表于 2014-2-8 16:27:33 | 显示全部楼层
{:soso_e179:}{:soso_e179:}
回复 支持 反对

使用道具 举报

发表于 2015-7-11 23:29:46 | 显示全部楼层
你好,请问读取传感器数据的子程序“getGyroValues()”中的“(buff[0] << 8) | buff[1]”为何和另外一个帖子“I2C通信之Arduino与加速度传感器ADXL345”中读取传感器数据的语句“(buff[1] << 8) | buff[0]”不一致?哪个是对的呢?谢谢。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-10-6 13:14 , Processed in 0.037932 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表