Python program to turn until reaching an angle with LEGO Education SPIKE Prime Gyro Sensor

Python program to turn until reaching an angle with LEGO Education SPIKE Prime Gyro Sensor.

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(30) # 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'.

wait_for_seconds(1) # Wait for one second.
hub.motion_sensor.reset_yaw_angle() # Reset the Gyro sensor. The current yaw angle value is equal to 0.

motor_pair.start(steering = 100) # Turn left around the center of the wheelbase. Leftward because of steering=-100 parameter.

# To program the robot to wait until the robot has turned, we need to define a fuction that checks if the robot has turned.
def left_turn_end(): # Define the function
    return hub.motion_sensor.get_yaw_angle() > 90 # Return true or false depending on the yaw angle value.

wait_until(left_turn_end) # Wait until the left turn end. left_turn_end is a function that defines the left turn

motor_pair.stop() # Stop moving. Hit the brakes! Remember the motor_pair.set_stop_action('brake') statement/setting?

raise SystemExit # Close the program.

How to open?

Download
  • #18zxih
  • 13 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 Turn precisely with the Motion Sensor
  • 1
  • 0
  • 1
  • 3d_rotation 1