This program is developed with LEGO Education SPIKE App Python and is used to make the Luly, small LEGO Education SPIKE Prime competition robot with 3D building instructions stop at a distance from the border field.
from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair
from spike.control import wait_for_seconds, wait_until, Timer
from math import *
hub = PrimeHub()
distance_sensor = DistanceSensor('C')
motor_pair = MotorPair('A', 'B') # Set the motor ports in the motor_pair.
motor_pair.set_default_speed(50) # Set the default speed of the motor_pair.
motor_pair.set_motor_rotation(17.6, 'cm') # Set the distance that the robot travels for one rotation of its wheels. The value 17.6 comes from
# the diameter of the wheel (5.6cm) multiplied by "π" (3.14).
motor_pair.set_stop_action('brake') # Activate the brakes when the robot stops. The other conditions are 'hold' and 'coast'.
distance_sensor.light_up_all() # Turn on the distance sensor lights.
motor_pair.start() # Move forward.
distance_sensor.wait_for_distance_closer_than(20, 'cm') # Wait until distance is closer than 20 centimeters.
motor_pair.stop() # Stop moving.
distance_sensor.light_up_all(0) # Turn off the distance sensor lights.
raise SystemExit # Close the program.