Advertisement
Guest User

pytho

a guest
May 21st, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. # Simple two DC motor robot class. Exposes a simple LOGO turtle-like API for
  2. # moving a robot forward, backward, and turning. See RobotTest.py for an
  3. # example of using this class.
  4. # Author: Tony DiCola
  5. # License: MIT License https://opensource.org/licenses/MIT
  6. import time
  7. import atexit
  8.  
  9. from Adafruit_MotorHAT import Adafruit_MotorHAT
  10.  
  11.  
  12. class Robot(object):
  13. def __init__(self, addr=0x60, left_id=1, right_id=2, left_trim=0, right_trim=0,
  14. stop_at_exit=True):
  15. """Create an instance of the robot. Can specify the following optional
  16. parameters:
  17. - addr: The I2C address of the motor HAT, default is 0x60.
  18. - left_id: The ID of the left motor, default is 1.
  19. - right_id: The ID of the right motor, default is 2.
  20. - left_trim: Amount to offset the speed of the left motor, can be positive
  21. or negative and use useful for matching the speed of both
  22. motors. Default is 0.
  23. - right_trim: Amount to offset the speed of the right motor (see above).
  24. - stop_at_exit: Boolean to indicate if the motors should stop on program
  25. exit. Default is True (highly recommended to keep this
  26. value to prevent damage to the bot on program crash!).
  27. """
  28. # Initialize motor HAT and left, right motor.
  29. self._mh = Adafruit_MotorHAT(addr)
  30. self._left = self._mh.getMotor(left_id)
  31. self._right = self._mh.getMotor(right_id)
  32. self._left_trim = left_trim
  33. self._right_trim = right_trim
  34. # Start with motors turned off.
  35. self._left.run(Adafruit_MotorHAT.RELEASE)
  36. self._right.run(Adafruit_MotorHAT.RELEASE)
  37. # Configure all motors to stop at program exit if desired.
  38. if stop_at_exit:
  39. atexit.register(self.stop)
  40.  
  41. def _left_speed(self, speed):
  42. """Set the speed of the left motor, taking into account its trim offset.
  43. """
  44. assert 0 <= speed <= 255, 'Speed must be a value between 0 to 255 inclusive!'
  45. speed += self._left_trim
  46. speed = max(0, min(255, speed)) # Constrain speed to 0-255 after trimming.
  47. self._left.setSpeed(speed)
  48.  
  49. def _right_speed(self, speed):
  50. """Set the speed of the right motor, taking into account its trim offset.
  51. """
  52. assert 0 <= speed <= 255, 'Speed must be a value between 0 to 255 inclusive!'
  53. speed += self._right_trim
  54. speed = max(0, min(255, speed)) # Constrain speed to 0-255 after trimming.
  55. self._right.setSpeed(speed)
  56.  
  57. def stop(self):
  58. """Stop all movement."""
  59. self._left.run(Adafruit_MotorHAT.RELEASE)
  60. self._right.run(Adafruit_MotorHAT.RELEASE)
  61.  
  62. def forward(self, speed, seconds=None):
  63. """Move forward at the specified speed (0-255). Will start moving
  64. forward and return unless a seconds value is specified, in which
  65. case the robot will move forward for that amount of time and then stop.
  66. """
  67. # Set motor speed and move both forward.
  68. self._left_speed(speed)
  69. self._right_speed(speed)
  70. self._left.run(Adafruit_MotorHAT.FORWARD)
  71. self._right.run(Adafruit_MotorHAT.FORWARD)
  72. # If an amount of time is specified, move for that time and then stop.
  73. if seconds is not None:
  74. time.sleep(seconds)
  75. self.stop()
  76.  
  77. def backward(self, speed, seconds=None):
  78. """Move backward at the specified speed (0-255). Will start moving
  79. backward and return unless a seconds value is specified, in which
  80. case the robot will move backward for that amount of time and then stop.
  81. """
  82. # Set motor speed and move both backward.
  83. self._left_speed(speed)
  84. self._right_speed(speed)
  85. self._left.run(Adafruit_MotorHAT.BACKWARD)
  86. self._right.run(Adafruit_MotorHAT.BACKWARD)
  87. # If an amount of time is specified, move for that time and then stop.
  88. if seconds is not None:
  89. time.sleep(seconds)
  90. self.stop()
  91.  
  92. def right(self, speed, seconds=None):
  93. """Spin to the right at the specified speed. Will start spinning and
  94. return unless a seconds value is specified, in which case the robot will
  95. spin for that amount of time and then stop.
  96. """
  97. # Set motor speed and move both forward.
  98. self._left_speed(speed)
  99. self._right_speed(speed)
  100. self._left.run(Adafruit_MotorHAT.FORWARD)
  101. self._right.run(Adafruit_MotorHAT.BACKWARD)
  102. # If an amount of time is specified, move for that time and then stop.
  103. if seconds is not None:
  104. time.sleep(seconds)
  105. self.stop()
  106.  
  107. def left(self, speed, seconds=None):
  108. """Spin to the left at the specified speed. Will start spinning and
  109. return unless a seconds value is specified, in which case the robot will
  110. spin for that amount of time and then stop.
  111. """
  112. # Set motor speed and move both forward.
  113. self._left_speed(speed)
  114. self._right_speed(speed)
  115. self._left.run(Adafruit_MotorHAT.BACKWARD)
  116. self._right.run(Adafruit_MotorHAT.FORWARD)
  117. # If an amount of time is specified, move for that time and then stop.
  118. if seconds is not None:
  119. time.sleep(seconds)
  120. self.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement