Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import smbus
  4. import math
  5. import time
  6. import sys
  7. import curses
  8. import signal
  9.  
  10. screen = curses.initscr()
  11.  
  12. # Power management registers
  13. power_mgmt_1 = 0x6b
  14. power_mgmt_2 = 0x6c
  15.  
  16. def signal_handler(signal, frame):
  17. curses.endwin()
  18. sys.exit()
  19.  
  20. def read_byte(adr):
  21. return bus.read_byte_data(address, adr)
  22.  
  23. def read_word(adr):
  24. high = bus.read_byte_data(address, adr)
  25. low = bus.read_byte_data(address, adr+1)
  26. val = (high << 8) + low
  27. return val
  28.  
  29. def read_word_2c(adr):
  30. val = read_word(adr)
  31. if (val >= 0x8000):
  32. return -((65535 - val) + 1)
  33. else:
  34. return val
  35.  
  36. def dist(a,b):
  37. return math.sqrt((a*a)+(b*b))
  38.  
  39. def get_y_rotation(x,y,z):
  40. radians = math.atan2(x, dist(y,z))
  41. return -math.degrees(radians)
  42.  
  43. def get_x_rotation(x,y,z):
  44. radians = math.atan2(y, dist(x,z))
  45. return math.degrees(radians)
  46.  
  47. signal.signal(signal.SIGINT, signal_handler)
  48. bus = smbus.SMBus(1) # or bus = smbus.SMBus(1) for Revision 2 boards
  49. address = 0x68 # This is the address value read via the i2cdetect command
  50.  
  51. # Now wake the 6050 up as it starts in sleep mode
  52. bus.write_byte_data(address, power_mgmt_1, 0)
  53.  
  54. #Set up basic screen labels
  55. screen.addstr(0,0,"gyro data")
  56. screen.addstr(1,0,"---------"
  57. screen.addstr(2,0,"gyro_xout:")
  58. screen.addstr(2,0,"scaled:")
  59. screen.addstr(3,0,"gyro_yout:")
  60. screen.addstr(3,0,"scaled")
  61. screen.addstr(4,0,"gyro_zout:")
  62. screen.addstr(4,0,"scaled:")
  63.  
  64. while True:
  65. gyro_xout = read_word_2c(0x43)
  66. gyro_yout = read_word_2c(0x45)
  67. gyro_zout = read_word_2c(0x47)
  68.  
  69. screen.addstr(12,2,gyro_xout)
  70. screen.addstr(0,0,(gyro_xout / 131))
  71. screen.addstr(0,3,gyro_yout)
  72. screen.addstr(0,0,(gyro_yout / 131))
  73. screen.addstr(0,3,gyro_zout)
  74. screen.addstr(0,0,(gyro_zout / 131))
  75.  
  76. #print ""
  77. #print "accelerometer data"
  78. #print "------------------"
  79.  
  80. accel_xout = read_word_2c(0x3b)
  81. accel_yout = read_word_2c(0x3d)
  82. accel_zout = read_word_2c(0x3f)
  83.  
  84. accel_xout_scaled = accel_xout / 16384.0
  85. accel_yout_scaled = accel_yout / 16384.0
  86. accel_zout_scaled = accel_zout / 16384.0
  87.  
  88. #print "accel_xout: ", accel_xout, " scaled: ", accel_xout_scaled
  89. #print "accel_yout: ", accel_yout, " scaled: ", accel_yout_scaled
  90. #print "accel_zout: ", accel_zout, " scaled: ", accel_zout_scaled
  91.  
  92. #print "x rotation: " , get_x_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)
  93. #print "y rotation: " , get_y_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)
  94.  
  95. time.sleep(0.2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement