Advertisement
robathome

Gyro code - Python

Apr 6th, 2019
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. import time
  2. import board
  3. import busio
  4. import adafruit_l3gd20
  5.  
  6. # Hardware I2C setup:
  7. SerialObj = busio.I2C(board.SCL, board.SDA)
  8.  
  9. # When creating the L3GD20 instance, the second argument defines the range
  10. GyroSensor = adafruit_l3gd20.L3GD20_I2C(SerialObj, 2)
  11.  
  12. # Configure CTRLHz output data rate
  13. # - 800
  14. # Low-pass filter disabled
  15. GyroSensor.write_register(0x20, 0x5f)
  16.  
  17. # Configure CTRL2 register
  18. # - High-pass filter module disabled
  19. GyroSensor.write_register(0x21, 0x00)
  20.  
  21. # CTRL4 is already configured with
  22. # the L3GD20 instance!
  23. GyroSensor.write_register(0x23, 0x32)
  24.  
  25. # Begin sensor loop
  26. try:
  27.  
  28. while not SerialObj.try_lock():
  29. pass
  30.  
  31. StartTime = time.monotonic()
  32. while True:
  33.  
  34. LoopTime = time.monotonic()
  35. TimeStamp = LoopTime - StartTime
  36. GyroData = GyroSensor.gyro()
  37. DataPoint = [TimeStamp, GyroData(1), GyroData(2), GyroData(3)]
  38. print('{0:0.6f},{1:0.5f},{2:0.5f},{2:0.5f}'.format(DataPoint))
  39. time.sleep(0.001)
  40.  
  41. finally:
  42.  
  43. SerialObj.unlock()
  44. import sys
  45. import time
  46. import board
  47. import busio
  48. import adafruit_l3gd20
  49.  
  50. # Hardware I2C setup:
  51. SerialObj = busio.I2C(board.SCL, board.SDA)
  52.  
  53. # When creating the L3GD20 instance, the second argument defines the range
  54. GyroSensor = adafruit_l3gd20.L3GD20_I2C(SerialObj,2)
  55.  
  56. # Configure CTRLHz output data rate
  57. # 800 Hz ODR
  58. # Low-pass filter disabled
  59. GyroSensor.write_register(0x20,0x5f)
  60.  
  61. # Configure CTRL2 register
  62. # - High-pass filter module disabled
  63. GyroSensor.write_register(0x21,0x00)
  64.  
  65. # CTRL4 is already configured with
  66. # the L3GD20 instance!
  67. GyroSensor.write_register(0x23,0x32)
  68.  
  69. # Begin sensor loop
  70. try:
  71.  
  72. SerialObj.lock()
  73. StartTime = time.monotonic
  74. While True:
  75.  
  76. LoopTime = time.monotonic()
  77. TimeStamp = LoopTime - StartTime
  78. GyroData = GyroSensor.gyro()
  79. DataPoint = [TimeStamp, GyroData(1), GyroData(2), GyroData(3)]
  80. print('{0:0.6f},{1:0.5f},{2:0.5f},{2:0.5f}'.format(DataPoint))
  81. time.sleep(0.001)
  82.  
  83. finally:
  84.  
  85. SerialObj.unlock()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement