Python program to move forward, turn left, move forwad, turn right with LEGO Education SPIKE Prime

This program makes the robot move forwards, turn left, move forwards, and then turn right using LEGO Education SPIKE App Python.

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() 

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.move(50,'cm', 0, 50) # Start moving for 50cm with NO Steering '0' at 50% of the maximum speed.
motor_pair.move(8.8,'cm', -100, 50) # Start moving for 8.8cm with steering '-100' at 50% of the maximum speed. This makes the robot turn left 90deg
                                    # at 50% of the maximum speed of the motors. The value 8.8 represents the angle of rotation. This is the distance every 
                                    # wheel needs to travel to rotate the robot 90deg. This value is calculated in the following way: The wheel base
                                    # (the distance between the points where the wheels touch the ground) is the
                                    # diameter of the turn (in this case it is 11,2cm). Since we want to turn 90deg (1/4 of the turn) we multiply
                                    # the diameter (11,2) by "π" (3.14) and divide it to 4 as 90 degrees are 1/4 of the circle.
motor_pair.move(40,'cm', 0, 50) # Start moving for 50cm with NO Steering '0' at 50% of the maximum speed.
motor_pair.move(8.8,'cm', 100, 50) # Start moving for 8.8cm with steering '100' at -50% of the maximum speed. This makes the robot turn right 90deg
                                    # at 50% of the maximum speed of the motors. Turn right because the steering is positive 100%.

raise SystemExit # Close the program.

How to open?

Download
  • #2xvw1d
  • 12 Mar 2021

Courses and lessons with this Program

This Program is used in the following courses and lessons

Image for FIRST LEGO League with LEGO Education SPIKE Prime. "Challenge" competition for 9-16 years old
  • 40
  • 127:22
  • 24
Image for FIRST LEGO League with LEGO Education SPIKE Prime. "Challenge" competition for 9-16 years old
  • 40
  • 127:22
  • 24
Image for Turn with SPIKE Prime
  • 1
  • 0
  • 2
  • 3d_rotation 1