Skip to content

Instantly share code, notes, and snippets.

@skfwMelonpan
Created July 28, 2015 06:44
Show Gist options
  • Save skfwMelonpan/9fec0db3e901ceae174e to your computer and use it in GitHub Desktop.
Save skfwMelonpan/9fec0db3e901ceae174e to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sys
import smbus
import math
import time
import datetime
# Power management registers
power_mgmt_1 = 0x6b
power_mgmt_2 = 0x6c
def read_byte(adr):
return bus.read_byte_data(address, adr)
def read_word(adr):
high = bus.read_byte_data(address, adr)
low = bus.read_byte_data(address, adr+1)
val = (high << 8) + low
return val
def read_word_2c(adr):
val = read_word(adr)
if (val >= 0x8000):
return -((65535 - val) + 1)
else:
return val
def dist(a,b):
return math.sqrt((a*a)+(b*b))
def get_y_rotation(x,y,z):
radians = math.atan2(x, dist(y,z))
return -math.degrees(radians)
def get_x_rotation(x,y,z):
radians = math.atan2(y, dist(x,z))
return math.degrees(radians)
bus = smbus.SMBus(1) # or bus = smbus.SMBus(1) for Revision 2 boards
address = 0x68 # This is the address value read via the i2cdetect command
# Now wake the 6050 up as it starts in sleep mode
bus.write_byte_data(address, power_mgmt_1, 0)
start = datetime.datetime.now()
end = start+datetime.timedelta(seconds=60)
print start
now = datetime.datetime.now()
while 1:
#now = datetime.now()
gyro_xout = read_word_2c(0x43)
gyro_yout = read_word_2c(0x45)
gyro_zout = read_word_2c(0x47)
accel_xout = read_word_2c(0x3b)
accel_yout = read_word_2c(0x3d)
accel_zout = read_word_2c(0x3f)
print "%s,%d,%d,%d,%d,%d,%d"%(datetime.datetime.now()-start,accel_xout,accel_yout,accel_zout,gyro_xout,gyro_yout,gyro_zout)
#time.sleep(0.5)
if end-datetime.datetime.now()<datetime.timedelta(seconds=0):
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment