Bought a mpu9250 began to toss, find a lot of information, read a lot of articles ah, mpu9250 information is not many.
Use the I²c link to the SCL of the Raspberry Pi, the SDA interface VCC to the 3v pin, gnd on the Raspberry Pi gnd is ok.
Start tossing:
To operate the MPU must use the MPU register to implement the parameters of the set and read, take the official download to see a bit, on GitHub to find a Python code, can not run too many bugs, and then streamlined a bit.
Finally can read the data, read out the data are 6 bytes, and later found that the man with Python read mpu did not do byte merge, rewrite, and later found that the data are integers, no matter how I rotate the numbers are positive, read a piece of the article on the Internet that register degree is an unsigned integer.
Later thought for a long time, with a mobile phone under a gyroscope app found that there will be negative situation, just to the opposite direction of the axis to do the movement will appear negative, so think of cTYPES, cast to int this time the data appears to be positive and negative, But in the gyroscope flat when reading the advantages are too large to reach 3000, obviously not, it seems that the number needs to be converted to use, I found n more information, finally found a personal mpu6050 code, there is a fixed steady, he used acc_x times this fixed constant, Then I tested it and it worked, so just multiply the accelerometer value by 16.4 to get the right value!
Gyro reading is multiplied by 13. I searched for half a day and did not find why to multiply 16.4, do not find, if someone saw this article know why multiplied by 16.4 please leave a message or email me [email protected] Thank you
Use the following code to install the SMBus library, the Raspberry Pi directly sudo apt-get install Py-smbus is OK
The Python code is as follows:
Import SMBus
Import Sched, Time
Import Binascii
From threadingImport Timer, Thread, Event
from structImport *
Import cTYPES
From MathImport ACOs
Import Sched, Time
Import Binascii
from structImport *
I²c = SMBus. SMBus (1)
Addr =0x68
T0 = Time.time ()
# ====== Initial Zone ======
Try
device_id = I2c.read_byte_data (addr,0X75)
Print"Device ID:" +StrHex (device_id))
Print"MPU9250 i²c Connected."
Print""
Except
Print"Connect failed"
Print""
I2c.write_byte_data (0x680x6a0x00
Time.sleep (0.05);
I2c.write_byte_data (0x680x370x02
I2c.write_byte_data (0x0c0x0a0x16
# Set Frequence for accelerator
I2c.write_byte_data (0x6829,9)
# Enable FIFO and DMP
# I2c.write_byte_data (0x68, 106, 32+64);
# ====== intial done ======
Def mpu9250_data_get_and_write ():
# Global T_a_g
# Keep AKM pointer on continue measuring
I2c.write_byte_data (0x0c0x0a0x16
# get MPU9250 SMBus block data
# Xyz_g_offset = I2c.read_i2c_block_data (addr, 0x13, 6)
Xyz_a_out = I2c.read_i2c_block_data (addr,0x3B,6)
Print"Xyz_a_out" +StrList2word (Xyz_a_out, Calc_accelerator_value)))
# print ("xyz_a_out_org#:" +str (Xyz_a_out))
Xyz_g_out = I2c.read_i2c_block_data (addr,0x436)
Print"Xyz_g_out" +StrList2word (Xyz_g_out, Calc_gyro_value)))
# Xyz_a_offset = I2c.read_i2c_block_data (addr, 0x77, 6)
# Get AK8963 smb#us data (by pass-through)
Xyz_mag = I2c.read_i2c_block_data (0x0c0x036)
# print ("Xyz_mag" +str (List2word (XYZ_MAG)))
Xyz_mag_adj = I2c.read_i2c_block_data (0x0c0x103)
DefList2word (data_list=[], callback=‘‘):
data = data_list[:]
If notLen (data):
return [];
result = []
For IInchRange3):
High_byte = Data.pop (0)
Low_byte = Data.pop (0)
Result.append (Callback (Float (ctypes.c_int16 ((High_byte <<8) | Low_byte)))
return result
def calc_accelerator_value (value):
ReturnRound (Value/16.4)
def calc_gyro_value (value):
ReturnRound (Value/131)
Def clear_i2c_and_close_file ():
I2c.write_byte_data (addr,0x6A,0x07
# solution 1:while True
Def while_true_method ():
# Max_count = raw_input ("Enter How many count of you want.")
Max_count =100;
If Max_count <1:max_count =1000
Print"Data Counts:" +STR (MAX_COUNT)
Max_count =Int (max_count)
Count =1
Print""
Print"MPU9250 9axis DATA recording ..."
while True:
# if Count <= max_count:
Mpu9250_data_get_and_write ()
Count + = 1
Time.sleep (0.5)
# Else:
Pass
# break;
While_true_method ();
mpu9250 Gyroscope Toss Little Kee