Exercise: Rescue

3.14. Exercise: Rescue#

  1. Navigate to the MicroPython editor https://python.microbit.org/v/3. If you already have it open, then use that existing window or tab.

  2. Connect your micro:bit using the instructions on the previous page

  3. Enter the following code into the editor

from microbit import *


def motor_left(speed, direction):
    buf = bytearray(3)
    buf[0] = 0x00
    buf[1] = direction
    buf[2] = speed
    i2c.write(0x10, buf)


def motor_right(speed, direction):
    buf = bytearray(3)
    buf[0] = 0x02
    buf[1] = direction
    buf[2] = speed
    i2c.write(0x10, buf)


def servo_one(angle=0):
    buf = bytearray(2)
    buf[0] = 0x14
    buf[1] = angle
    i2c.write(0x10, buf)


i2c.init()

while True:
    sleep(1000)
  1. Flash the code to your micro:bit.

If you get stuck ask a peer or your teacher for help!

Question 1

Extend the code so that:

  • the Maqueen repeatedly opens and closes the gripper

Tip

Make sure you check for the optimal open and close angles for your M aqueen. For the health of your servo and batteries its best that the servo doesn’t try to extend beyond the gripper limits.

Solution

Solution is locked

Question 2

Setup a line segment track that ends in a large white area. Place a few grippable objects in this area.

Extend the code so that the Maqueen performs a rescue operation by:

  • following the line to the end

  • then stopping when the end of the line is detected (more than 1 second has passed without seeing a line)

  • then actuating the gripper

Note

You will need to integrate your line following code with your gripper code.

You can measure the elapsed time with code like this:

start_time = running_time()

# do some tasks

elapsed_time = running_time() - start_time
Solution

Solution is locked