PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/dex/gyrotest.py

https://gitlab.com/MikeDabrowski/PiRobot
Python | 40 lines | 10 code | 3 blank | 27 comment | 2 complexity | c008570773f1d128466f112976055c6a MD5 | raw file
  1. #!/usr/bin/env python
  2. # BRICKPI LEGO EV3 GYRO SENSOR EXAMPLE.
  3. ############################################
  4. #
  5. # This example will show you how to use the LEGO EV3 Gyro sensor with the BrickPi.
  6. # Note you must have the latest firmware installed on the BrickPi or this example to work.
  7. # Connect the EV3 Gyro Sensor to Sensor port S4.
  8. ##
  9. # Select the mode of operation below. These are the modes of operation for the gyro.
  10. # TYPE_SENSOR_EV3_GYRO_M0 - Returns absolute angle turned from startup.
  11. # TYPE_SENSOR_EV3_GYRO_M1 - Rotational Speed
  12. # TYPE_SENSOR_EV3_GYRO_M2 - Raw sensor value ???
  13. # TYPE_SENSOR_EV3_GYRO_M3 - Angle and Rotational Speed?
  14. ##
  15. #
  16. # Original Author: John
  17. # Initial Date: Jun 13, 2014
  18. # Updated:Aug 19, 2015
  19. # Updated: Oct 28, 2016 Shoban
  20. #
  21. # These files have been made available online through a Creative Commons Attribution-ShareAlike 3.0 license.
  22. # (http://creativecommons.org/licenses/by-sa/3.0/)
  23. #
  24. # You can learn more about BrickPi here: http://www.dexterindustries.com/BrickPi
  25. # Have a question about this example? Ask on the forums here: http://forum.dexterindustries.com/c/brickpi
  26. from BrickPi import * #import BrickPi.py file to use BrickPi operations
  27. BrickPiSetup() # setup the serial port for communication
  28. BrickPi.SensorType[PORT_4] = TYPE_SENSOR_EV3_GYRO_M0 #Set the type of sensor at PORT_4. M0 is angle.
  29. BrickPiSetupSensors() #Send the properties of sensors to BrickPi. Set up the BrickPi.
  30. # There's often a long wait for setup with the EV3 sensors.
  31. # EV3 gyro works best (less drift and less error results) if you hold it still in setup.
  32. while True:
  33. result = BrickPiUpdateValues() # Ask BrickPi to update values for sensors/motors
  34. if not result :
  35. gyro = BrickPi.Sensor[PORT_4]
  36. print str(gyro)
  37. time.sleep(.01) # sleep for 10 ms