spacepaste

  1.  
  2. #!/usr/bin/python
  3. import smbus
  4. import math
  5. import time
  6. import sys
  7. import curses
  8. import signal
  9. from PID import PID
  10. screen = curses.initscr()
  11. # Power management registers
  12. power_mgmt_1 = 0x6b
  13. power_mgmt_2 = 0x6c
  14. def signal_handler(signal, frame):
  15. curses.endwin()
  16. sys.exit()
  17. def read_byte(adr):
  18. return bus.read_byte_data(address, adr)
  19. def read_word(adr):
  20. high = bus.read_byte_data(address, adr)
  21. low = bus.read_byte_data(address, adr+1)
  22. val = (high << 8) + low
  23. return val
  24. def read_word_2c(adr):
  25. val = read_word(adr)
  26. if (val >= 0x8000):
  27. return -((65535 - val) + 1)
  28. else:
  29. return val
  30. def dist(a,b):
  31. return math.sqrt((a*a)+(b*b))
  32. def get_y_rotation(x,y,z):
  33. radians = math.atan2(x, dist(y,z))
  34. return -math.degrees(radians)
  35. def get_x_rotation(x,y,z):
  36. radians = math.atan2(y, dist(x,z))
  37. return math.degrees(radians)
  38. signal.signal(signal.SIGINT, signal_handler)
  39. bus = smbus.SMBus(1) # or bus = smbus.SMBus(1) for Revision 2 boards
  40. address = 0x68 # This is the address value read via the i2cdetect command
  41. # Now wake the 6050 up as it starts in sleep mode
  42. bus.write_byte_data(address, power_mgmt_1, 0)
  43. #Set up basic screen labels
  44. screen.addstr(0,0,"gyro data")
  45. screen.addstr(1,0,"---------")
  46. screen.addstr(2,0,"gyro_xout:")
  47. screen.addstr(2,20,"scaled:")
  48. screen.addstr(3,0,"gyro_yout:")
  49. screen.addstr(3,20,"scaled:")
  50. screen.addstr(4,0,"gyro_zout:")
  51. screen.addstr(4,20,"scaled:")
  52. screen.addstr(6,0,"accelerometer data")
  53. screen.addstr(7,0,"------------------")
  54. screen.addstr(8,0,"accel_xout:")
  55. screen.addstr(8,20,"scaled:")
  56. screen.addstr(9,0,"accel_yout:")
  57. screen.addstr(9,20,"scaled:")
  58. screen.addstr(10,0,"accel_zout:")
  59. screen.addstr(10,20,"scaled:")
  60. screen.addstr(12,0,"x rotation:")
  61. screen.addstr(13,0,"y rotation:")
  62. #list of accel values for averaging
  63. accelvalues = []
  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. #clear existing values
  69. screen.addstr(2,12," ")
  70. screen.addstr(2,28," ")
  71. screen.addstr(3,12," ")
  72. screen.addstr(3,28," ")
  73. screen.addstr(4,12," ")
  74. screen.addstr(4,28," ")
  75. screen.addstr(8,12," ")
  76. screen.addstr(8,28," ")
  77. screen.addstr(9,12," ")
  78. screen.addstr(9,28," ")
  79. screen.addstr(10,12," ")
  80. screen.addstr(10,28," ")
  81. screen.addstr(12,12," ")
  82. screen.addstr(13,12," ")
  83. screen.addstr(2,12,str(gyro_xout))
  84. screen.addstr(2,28,str((gyro_xout / 131)))
  85. screen.addstr(3,12,str(gyro_yout))
  86. screen.addstr(3,28,str((gyro_yout / 131)))
  87. screen.addstr(4,12,str(gyro_zout))
  88. screen.addstr(4,28,str((gyro_zout / 131)))
  89. accel_xout = read_word_2c(0x3b)
  90. accel_yout = read_word_2c(0x3d)
  91. accel_zout = read_word_2c(0x3f)
  92. accel_xout_scaled = accel_xout / 16384.0
  93. accel_yout_scaled = accel_yout / 16384.0
  94. accel_zout_scaled = accel_zout / 16384.0
  95. screen.addstr(8,12,str(accel_xout))
  96. screen.addstr(8,28,str(accel_xout_scaled))
  97. screen.addstr(9,12,str(accel_yout))
  98. screen.addstr(9,28,str(accel_yout_scaled))
  99. screen.addstr(10,12,str(accel_zout))
  100. screen.addstr(10,28,str(accel_zout_scaled))
  101. screen.addstr(12,12,str(get_x_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)))
  102. screen.addstr(13,12,str(get_y_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)))
  103. #calculate average accel value
  104. accelvalues.append(get_y_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled))
  105. if len(accelvalues) > 10:
  106. del accelvalues[0]
  107. average = float(sum(accelvalues)) / len(accelvalues)
  108. screen.addstr(15,0,str(round(average,2)))
  109. screen.addstr(15,0,str(PID.GenOut(PID, 1)))
  110. screen.refresh()
  111.