树莓派连接9轴IMU传感器(GY85 模块)

![GY-85](http://images.cnblogs.com/cnblogs_com/hangxin1940/466697/o_gy85.jpg "GY-85") 9轴IMU传感器(GY-85 模块) 其实也可以看作三个模块 `ITG3205 三轴陀螺仪传感器` `ADXL345 三轴加速度倾角传感器` `HMC5883L 电子罗盘` GY-85模块的接口是i2c, 可以方便的和arduino/树莓派链接. ## 准备工作 确保系统安装了quick2wire库,如果没有,移步这里 http://www.cnblogs.com/hangxin1940/archive/2013/04/04/2999015.html 在合适的目录下载thinkbowl的i2clibraries库 git clone https://bitbucket.org/thinkbowl/i2clibraries.git 此i2clibraries库恰好包含了`ITG3205` `ADXL345` `HMC5883L` 这三种传感器接口,很方便开发,要注意的是 `i2c_itg3205.py`接口中,默认对应 `ITG3205`设备地址为`69`,为了以后省事,将相关注释下面的代码做更改 # Address will always be either 0x68 (104) or 0x69 (105) def __init__(self, port, addr=0x68): #这里的addr改为68, 原值为69 ## 连接 ![rasp](http://images.cnblogs.com/cnblogs_com/hangxin1940/466697/o_rasp_gpio.png "rasp") 这张图指明了两排GPIO真脚中5V输出与3.3V输出的位置,用以指明方向 ![rasp](http://images.cnblogs.com/cnblogs_com/hangxin1940/466697/o_rasp_GPIOs_gy85.png "rasp") 这张图具体说明了GY-85的真脚与树莓派GPIO/I2C针角的连接方式 虽然GY-85有8个真脚,但用到的也只有四个 ## 测试 运行`i2cdetect`查看当前所连接的i2c设备 树莓派A型: sudo i2cdetect -y 0 树莓派B型: sudo i2cdetect -y 1 将会看到所有已经连接的i2c设备的地址 ![设备](http://images.cnblogs.com/cnblogs_com/hangxin1940/466697/o_rasp_i2c3.png "3个设备") 其中 地址`68`对应了`ITG3205`设备, `53`对应了`ADXL345`设备, `1e`对应了`HMC5883L`设备 到这里,GY-85模块已经顺利的介入到树莓派上 ## 获取 ITG3205 陀螺仪信息 更多信息请移步 http://think-bowl.com/raspberry-pi/i2c-python-library-3-axis-mems-gyro-itg-3205-with-the-raspberry-pi/ 新建脚本`i2c_itg3205.py`, 注意此脚本的位置,确保能引用之前下载的`i2clibraries`包, 或者将之前下载的python包添加至引用路径. from i2clibraries import i2c_itg3205 from time import * itg3205 = i2c_itg3205.i2c_itg3205(0) while True: (itgready, dataready) = itg3205.getInterruptStatus() if dataready: temp = itg3205.getDieTemperature() (x, y, z) = itg3205.getDegPerSecAxes() print("Temp: "+str(temp)) print("X: "+str(x)) print("Y: "+str(y)) print("Z: "+str(z)) print("") sleep(1) 运行此脚本 python3 i2c_itg3205.py 输出 Temp: 26.73 X: -8.278260869565218 Y: -12.869565217391305 Z: -28.034782608695654 Temp: 26.86 X: -1.808695652173913 Y: 3.4782608695652173 Z: -13.773913043478261 ## 获取 ADXL345 三轴/倾角信息 更多信息请移步 http://think-bowl.com/raspberry-pi/i2c-python-library-3-axis-digital-accelerometer-adxl345-with-the-raspberry-pi/ 新建脚本`i2c_adxl345.py` from i2clibraries import i2c_adxl345 from time import * adxl345 = i2c_adxl345.i2c_adxl345(0) while True: print(adxl345) sleep(1) 输出 X: -0.40625 Y: 0.15625 Z: -0.9375 X: -0.40625 Y: 0.15625 Z: -0.9375 ## 获取 HMC5883L 电子罗盘信息 更多信息请移步 http://think-bowl.com/raspberry-pi/i2c-python-library-3-axis-digital-compass-hmc5883l-with-the-raspberry-pi/ 新建脚本`i2c_hmc5883l.py` from i2clibraries import i2c_hmc5883l hmc5883l = i2c_hmc5883l.i2c_hmc5883l(0) hmc5883l.setContinuousMode() hmc5883l.setDeclination(9,54) print(hmc5883l) 输出 Axis X: -114.08 Axis Y: -345.92 Axis Z: -286.12 Declination: 9° 54' Heading: 261° 39' ## hack it! 上面示例代码很简单,但通过`i2clibraries`与上面示例代码,我们足以开发自己的应用了.
原文地址:https://www.cnblogs.com/hangxin1940/p/3000395.html