|
| 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 | +# Author2: Tony DiCola, Chris Anderson |
| 5 | +# License: MIT License https://opensource.org/licenses/MIT |
| 6 | + |
| 7 | + |
| 8 | +# This assumes the Left motor is on Motor 1 and the Right motor is on Motor 2 |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +import time |
| 13 | +import atexit |
| 14 | +from adafruit_motorkit import MotorKit |
| 15 | + |
| 16 | +kit = MotorKit() |
| 17 | + |
| 18 | +class Robot(object): |
| 19 | + def __init__(self, left_trim=0, right_trim=0, stop_at_exit=True): |
| 20 | + """Create an instance of the robot. Can specify the following optional |
| 21 | + parameter |
| 22 | + - left_trim: Amount to offset the speed of the left motor, can be positive |
| 23 | + or negative and use useful for matching the speed of both |
| 24 | + motors. Default is 0. |
| 25 | + - right_trim: Amount to offset the speed of the right motor (see above). |
| 26 | + - stop_at_exit: Boolean to indicate if the motors should stop on program |
| 27 | + exit. Default is True (highly recommended to keep this |
| 28 | + value to prevent damage to the bot on program crash!). |
| 29 | + """ |
| 30 | + |
| 31 | + self._left_trim = left_trim |
| 32 | + self._right_trim = right_trim |
| 33 | + if stop_at_exit: |
| 34 | + atexit.register(self.stop) |
| 35 | + |
| 36 | + def _left_speed(self, speed): |
| 37 | + """Set the speed of the left motor, taking into account its trim offset. |
| 38 | + """ |
| 39 | + assert -1 <= speed <= 1, 'Speed must be a value between -1 to 1 inclusive!' |
| 40 | + speed += self._left_trim |
| 41 | + speed = max(-1, min(1, speed)) # Constrain speed to 0-255 after trimming. |
| 42 | + kit.motor1.throttle = speed |
| 43 | + |
| 44 | + def _right_speed(self, speed): |
| 45 | + """Set the speed of the right motor, taking into account its trim offset. |
| 46 | + """ |
| 47 | + assert -1 <= speed <= 1, 'Speed must be a value between -1 to 1 inclusive!' |
| 48 | + speed += self._right_trim |
| 49 | + speed = max(-1, min(1, speed)) # Constrain speed to 0-255 after trimming. |
| 50 | + kit.motor2.throttle = speed |
| 51 | + |
| 52 | + @staticmethod |
| 53 | + def stop(): |
| 54 | + """Stop all movement.""" |
| 55 | + kit.motor1.throttle = 0 |
| 56 | + kit.motor2.throttle = 0 |
| 57 | + |
| 58 | + def forward(self, speed, seconds=None): |
| 59 | + """Move forward at the specified speed (0-255). Will start moving |
| 60 | + forward and return unless a seconds value is specified, in which |
| 61 | + case the robot will move forward for that amount of time and then stop. |
| 62 | + """ |
| 63 | + # Set motor speed and move both forward. |
| 64 | + self._left_speed(speed) |
| 65 | + self._right_speed(speed) |
| 66 | + # If an amount of time is specified, move for that time and then stop. |
| 67 | + if seconds is not None: |
| 68 | + time.sleep(seconds) |
| 69 | + self.stop() |
| 70 | + |
| 71 | + def steer(self, speed, direction): |
| 72 | + # Move forward at the specified speed (0- 1). Direction is +- 1. |
| 73 | + # Full left is -1, Full right is +1 |
| 74 | + if (speed + direction/2) > 1: |
| 75 | + speed = speed - direction/2 # calibrate so total motor output never goes above 1 |
| 76 | + left = speed + direction/2 |
| 77 | + right = speed - direction/2 |
| 78 | + self._left_speed(left) |
| 79 | + self._right_speed(right) |
| 80 | + |
| 81 | + def backward(self, speed, seconds=None): |
| 82 | + """Move backward at the specified speed (0-255). Will start moving |
| 83 | + backward and return unless a seconds value is specified, in which |
| 84 | + case the robot will move backward for that amount of time and then stop. |
| 85 | + """ |
| 86 | + # Set motor speed and move both backward. |
| 87 | + self._left_speed(-1*speed) |
| 88 | + self._right_speed(-1*speed) |
| 89 | + # If an amount of time is specified, move for that time and then stop. |
| 90 | + if seconds is not None: |
| 91 | + time.sleep(seconds) |
| 92 | + self.stop() |
| 93 | + |
| 94 | + |
| 95 | + def right(self, speed, seconds=None): |
| 96 | + """Spin to the right at the specified speed. Will start spinning and |
| 97 | + return unless a seconds value is specified, in which case the robot will |
| 98 | + spin for that amount of time and then stop. |
| 99 | + """ |
| 100 | + # Set motor speed and move both forward. |
| 101 | + self._left_speed(speed) |
| 102 | + self._right_speed(0) |
| 103 | + # If an amount of time is specified, move for that time and then stop. |
| 104 | + if seconds is not None: |
| 105 | + time.sleep(seconds) |
| 106 | + self.stop() |
| 107 | + |
| 108 | + def left(self, speed, seconds=None): |
| 109 | + """Spin to the left at the specified speed. Will start spinning and |
| 110 | + return unless a seconds value is specified, in which case the robot will |
| 111 | + spin for that amount of time and then stop. |
| 112 | + """ |
| 113 | + # Set motor speed and move both forward. |
| 114 | + self._left_speed(0) |
| 115 | + self._right_speed(speed) |
| 116 | + # If an amount of time is specified, move for that time and then stop. |
| 117 | + if seconds is not None: |
| 118 | + time.sleep(seconds) |
| 119 | + self.stop() |
0 commit comments