Mission 2  ·  Solution

The Left Hook

There is more than one right answer. Yours may look different — that is fine if it works.

The route
LLGREENFINISH START
Dashed line = the car’s path. Numbered circles are the decision points.
Turns in order: 1: Left2: Left then green → stop
First

Make these My Blocks

The program

Word Blocks

Same thing in Python
from hub import port, sound, light_matrix
import runloop, motor, motor_pair, distance_sensor, color_sensor, color

DRIVE      = motor_pair.PAIR_1
STEER      = port.D            # large motor, steers the back
EYES       = port.B            # distance sensor (rides on the turret)
BELLY      = port.F            # color sensor pointing down
STOP_MM    = 200               # wall closer than 20 cm ahead = stop and look
LANE_CM    = 36                # one lane + one wall
DEG_PER_CM = 20                # wheel degrees per cm: tune
TURN_CM    = 20                # roll while steering: tune

def wall_closer_than(mm):
    d = distance_sensor.distance(EYES)
    return 0 < d < mm

def on_color(c):
    return color_sensor.color(BELLY) == c

async def drive_cm(cm, speed=300):
    await motor_pair.move_for_degrees(DRIVE, int(cm * DEG_PER_CM), 0, velocity=speed)

async def drive_until_wall():
    motor_pair.move(DRIVE, 0, velocity=400)
    await runloop.until(lambda: wall_closer_than(STOP_MM))
    motor_pair.stop(DRIVE)

async def drive_until_green():
    motor_pair.move(DRIVE, 0, velocity=300)
    await runloop.until(lambda: on_color(color.GREEN))
    motor_pair.stop(DRIVE)

async def turn_left():
    await motor.run_to_absolute_position(STEER, -60, 500)
    await drive_cm(TURN_CM)
    await motor.run_to_absolute_position(STEER, 0, 500)

async def celebrate():
    light_matrix.show_image(light_matrix.IMAGE_HAPPY)
    await sound.beep(880, 300)
    await sound.beep(1175, 500)

async def main():
    motor_pair.pair(DRIVE, port.A, port.E)      # your drive motor ports
    await drive_until_wall()
    await turn_left()
    await drive_cm(LANE_CM)
    await turn_left()
    await drive_until_green()
    await celebrate()

runloop.run(main())

Starting values only. Steering angle, roll distances, the OPEN limit and the line target depend on your car and maze. Measure, then tune one at a time. I have not run these on a car — expect to adjust.

Level up

Ready for more?

← Hints All missions

Printable version: mission-2-solution.pdf