ADS

2013年4月26日 星期五

[Raspberry Pi] Read MPU6050 陀螺儀資料 use WiringPi libraries

接線由左至右是Vcc GND SCL SDA分別接至Raspberry的3.3V GND SCL SDA腳位可參考腳位圖






首先先進行WiringPi函式庫的安裝,如果你沒有git可以先進行git安裝再使用git去取得函式庫

sudo apt-get install git-core

下載wiringPi
git clone git://git.drogon.net/wiringPi            

更新
cd wiringPi
git pull origin

安裝
./build

要在Pi上使用I2C的話也必須經過一些設定
sudo apt-get install libi2c-dev

gpio load i2c

最後你應該就可以使用去抓MPU6050的位址 如果不行可以參考一下
i2cdetect -y 0  //版本1的raspberry
i2cdetect -y 1  //版本2的raspberry
最後找個資料夾來撰寫我們讀取MPU6050的程式,我設定在workspace/i2cReadReg底下
這個大家自訂吧。

在這邊先跟大家介紹一下幾個函式
wiringPiI2CSetup (0x68) 引數是輸入剛剛在i2cdetect找到的位址

 fd就是wiringPiI2CSetup的回傳值,第二個引數是reg位址,第三個是要寫入的值
wiringPiI2CWriteReg8 (fd,0x6B,0x00)
wiringPiI2CReadReg8 (fd,0x6B) 第二個引數是reg位址讀取8bit
wiringPiI2CReadReg16(fd, 0x43) 第二個引數是reg位址讀取16bit
關於wiringPiI2CReadReg16我感到相當的疑惑,因為我不知道他是讀reg 0x43 0x44還是0x43 0x42
所以我做了以下實驗,分別用8bit和16bit去做讀取然後再看結果,結果顯示他讀出的資料是0x44 0x43 在MPU6050的REG中MSB和LSB顛倒了,我以我們勢必沒有辦法用16bit去應用。

以下是我的程式碼(請手Key不要直接複製,會有編譯問題)
編譯時請輸入
gcc i2cMPU6050.c -lwiringPi
要輸入-lwiringPi這樣才找的到函式庫

#include<wiringPiI2C.h>
#include<stdio.h>

int main(){
int fd;
fd = wiringPiI2CSetup (0x68);
wiringPiI2CWriteReg8 (fd,0x6B,0x00);//set the sleep unenable
printf("set 0x6B=%X\n",wiringPiI2CReadReg8 (fd,0x6B));

while(1){
printf("My valueX:%X+%X\n",wiringPiI2CReadReg8(fd, 0x43)
,wiringPiI2CReadReg8(fd, 0x44));
printf("My valueY:%X+%X\n",wiringPiI2CReadReg8(fd, 0x45)
,wiringPiI2CReadReg8(fd, 0x46));
printf("My valueZ:%X+%X\n---------------------\n",wiringPiI2CReadReg8(fd, 0x47)
,wiringPiI2CReadReg8(fd, 0x48));

printf("My valueX:%X\n",wiringPiI2CReadReg16(fd, 0x43));
printf("My valueY:%X\n",wiringPiI2CReadReg16(fd, 0x45));
printf("My valueZ:%X\n------------------------\n",wiringPiI2CReadReg16(fd, 0x47);

delay(1000);
}
return 0;
}
可以對照上下暫存器的值MSB和LSB剛好是對掉的,因為這是讀取MPU6050陀螺儀的值,數值會飄,因為是靜止的所以MSB不太會飄就是了。


最後我有個不太明白的地方,希望知道的人可告訴我,就是關於CYCLE MODE到底是怎麼使用的?因為我一開始是在reg 0x6B中設定sleep 0 cycle 1 但是這樣值仍然讀不回來,最後都改成0才成功讀取值,我英文不太好...所以看datasheet真的很累。

This register allows the user to configure the power mode and clock source. It also provides a bit for resetting the entire device, and a bit for disabling the temperature sensor.
Description:
By setting SLEEP to 1, the MPU-60X0 can be put into low power sleep mode. When CYCLE is set to 1 while SLEEP is disabled, the MPU-60X0 will be put into Cycle Mode. In Cycle Mode, the device cycles between sleep mode and waking up to take a single sample of data from accelerometer at a rate determined by LP_WAKE_CTRL (register 108). To configure the wake frequency, use LP_WAKE_CTRL within the Power Management 2 register (Register 108).
An internal 8MHz oscillator, gyroscope based clock, or external sources can be selected as the MPU-60X0 clock source. When the internal 8 MHz oscillator or an external source is chosen as the clock source, the MPU-60X0 can operate in low power modes with the gyroscopes disabled.
Upon power up, the MPU-60X0 clock source defaults to the internal oscillator. However, it is highly recommended that the device be configured to use one of the gyroscopes (or an external clock source) as the clock reference for improved stability. The clock source can be selected according to the following table.



資料來源:
https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/ wiringPi
http://www.skpang.co.uk/blog/archives/575
http://invensense.com/mems/gyro/documents/RM-MPU-6000A.pdf MP6050 暫存器資料

1 則留言:

  1. 關於cycle mode, 字意上說的是SLEEP=0, CYCLE=1 的時候, 就是cycle mode,
    這個時候device會一下子醒來取一個sample, 然後就進入sleep, 然後又醒來取下一個sample, 如此周而復始. 至於每次醒來的頻率是由LP_WAKE_CTRL (register 108, bit7 & bit6)決定的.

    回覆刪除