Mercurial > micro-bot
comparison bot.py @ 3:1dc1a2e5e642 default tip
add some comments
| author | Radomir Dopieralski <bitbucket@sheep.art.pl> |
|---|---|
| date | Wed, 20 Mar 2019 23:15:39 +0000 |
| parents | 6dcf45d13f35 |
| children |
comparison
equal
deleted
inserted
replaced
| 2:6dcf45d13f35 | 3:1dc1a2e5e642 |
|---|---|
| 1 import microbit | 1 import microbit |
| 2 | 2 |
| 3 | 3 |
| 4 class Servo: | 4 class Servo: |
| 5 """A class for controlling continuous-rotation servos.""" | |
| 6 | |
| 5 def __init__(self, pin, trim=0): | 7 def __init__(self, pin, trim=0): |
| 6 self.pin = pin | 8 self.pin = pin # The pin to which the servo is connected. |
| 7 self.trim = trim | 9 self.trim = trim # What value makes the servo stop. |
| 8 self.speed = 0 | 10 self.speed = 0 # Current speed. |
| 9 self.pin.set_analog_period(20) | 11 self.pin.set_analog_period(20) # Servo needs 20Hz signal. |
| 10 | 12 |
| 11 def set_speed(self, speed): | 13 def set_speed(self, speed): |
| 14 """Sets the rotation speed of the servo, range is -90 to +90.""" | |
| 15 # Value of 25 gives us around 1000us, which is the minimum duty cycle | |
| 16 # for the servo. 125 is the maximum. | |
| 12 self.pin.write_analog(int(25 + 100 * (90 + speed) / 180 + self.trim)) | 17 self.pin.write_analog(int(25 + 100 * (90 + speed) / 180 + self.trim)) |
| 13 self.speed = speed | 18 self.speed = speed |
| 14 | 19 |
| 15 | 20 |
| 16 class Robot: | 21 class Robot: |
| 17 def __init__(self): | 22 def __init__(self): |
| 23 # Initialize the servos. Adjust the trim as needed. | |
| 18 self.left_servo = Servo(microbit.pin0, 2) | 24 self.left_servo = Servo(microbit.pin0, 2) |
| 19 self.right_servo = Servo(microbit.pin1, 1) | 25 self.right_servo = Servo(microbit.pin1, 1) |
| 20 | 26 |
| 21 def go(self, distance): | 27 def go(self, distance): |
| 28 """Make the robot go straight for a given distance.""" | |
| 22 microbit.display.show(microbit.Image.ARROW_S) | 29 microbit.display.show(microbit.Image.ARROW_S) |
| 23 self.left_servo.set_speed(-90) | 30 self.left_servo.set_speed(-90) |
| 24 self.right_servo.set_speed(90) | 31 self.right_servo.set_speed(90) |
| 25 microbit.sleep(int(distance * 2000 / 17)) | 32 microbit.sleep(int(distance * 2000 / 17)) |
| 26 self.stop() | 33 self.stop() |
| 27 | 34 |
| 28 def turn(self, angle): | 35 def turn(self, angle): |
| 36 """Make the robot turn in place by given angle in degrees.""" | |
| 29 if angle > 0: | 37 if angle > 0: |
| 30 microbit.display.show(microbit.Image.ARROW_E) | 38 microbit.display.show(microbit.Image.ARROW_E) |
| 31 self.left_servo.set_speed(-90) | 39 self.left_servo.set_speed(-90) |
| 32 self.right_servo.set_speed(-90) | 40 self.right_servo.set_speed(-90) |
| 33 microbit.sleep(int(angle * 64 / 9)) | 41 microbit.sleep(int(angle * 64 / 9)) |
| 37 self.right_servo.set_speed(90) | 45 self.right_servo.set_speed(90) |
| 38 microbit.sleep(int(-angle * 64 / 9)) | 46 microbit.sleep(int(-angle * 64 / 9)) |
| 39 self.stop() | 47 self.stop() |
| 40 | 48 |
| 41 def stop(self): | 49 def stop(self): |
| 50 """Make the robot stop.""" | |
| 42 microbit.display.show(microbit.Image.DIAMOND) | 51 microbit.display.show(microbit.Image.DIAMOND) |
| 43 self.left_servo.set_speed(0) | 52 self.left_servo.set_speed(0) |
| 44 self.right_servo.set_speed(0) | 53 self.right_servo.set_speed(0) |
| 45 | 54 |
| 46 def get_distance(self): | 55 def get_distance(self): |
| 56 """Use the distance sensor on pin2.""" | |
| 47 return microbit.pin2.read_analog() | 57 return microbit.pin2.read_analog() |
| 48 | 58 |
| 49 | 59 |
| 50 robot = Robot() | 60 robot = Robot() # Create the robot object. |
| 51 while True: | 61 while True: |
| 52 robot.go(5) | 62 robot.go(5) # Go forward 5 units. |
| 53 if robot.get_distance() > 700: | 63 if robot.get_distance() > 700: # If the distance sensor returns something... |
| 54 robot.turn(20) | 64 robot.turn(20) # Turn 20 degrees one way. |
| 55 left_distance = robot.get_distance() | 65 left_distance = robot.get_distance() # Take a measurement. |
| 56 robot.turn(-40) | 66 robot.turn(-40) # Turn 20 degrees the other way (40 in total) |
| 57 right_distance = robot.get_distance() | 67 right_distance = robot.get_distance() # Take a measurement. |
| 58 robot.turn(20) | 68 robot.turn(20) # Turn back straight. |
| 59 if left_distance < right_distance: | 69 if left_distance < right_distance: # Pick whatever side was further away. |
| 60 robot.turn(60) | 70 robot.turn(60) |
| 61 else: | 71 else: |
| 62 robot.turn(-60) | 72 robot.turn(-60) |
