Python demo program using a button to control our car LED
Part of the perfect STEM course, this program needs a Raspberry Pi, a button connected on GPIO 26 and 3.3V, and an LED connected to GPIO 18.
- #fn5xil
- 06 Apr 2018
Part of the perfect STEM course, this program needs a Raspberry Pi, a button connected on GPIO 26 and 3.3V, and an LED connected to GPIO 18.
Part of the Perfect STEM course, you should have a Raspberry Pi and a HC-SR04 distance sensor connected to 5V, GPIO 5, GPIO 6 and GND.
Part of the Perfect STEM course, you should have a Raspberry Pi, a button connected on GPIO 26 and 3.3V, and two motors connected to GPIO 27, GPIO 22, GPIO 23 and GPIO 24.
This project works with the Chronos - LEGO Mindstorms EV3 clock robot. It makes the hands of the clock start moving like they would on a real clock. There are two programs in the project, one that controls hours and minutes hands, and second that controls seconds and minutes hands of the clock.
This project works with the Chronos - LEGO Mindstorms EV3 clock robot. It aims to teach you to read the clock by showing a randomly selected time and giving you 5 seconds to guess what it is. It then shows you the correct answer and returns to its starting position.
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.
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.
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() motorA = Motor('A') motorB = Motor('B') motorA.set_default_speed(-30) motorB.set_default_speed(30) 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. while True: # Repeat forever. if hub.motion_sensor.get_yaw_angle() < 0: motorA.start() motorB.stop() else: motorB.start() motorA.stop()
This program makes the robot move forwards and then backwards 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 the motor_pair. motor_pair.set_motor_rotation(17.6, 'cm') # Set the distance that the robot travel 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(40,'cm', 0, 50) # Start moving for 40cm with NO Steering '0' with 50% speed. motor_pair.move(40,'cm', 0, -50) # Start moving backward for 40cm with NO Steering '0' with 50% speed. -50 for speed means: rotate with 50% seed backward. raise SystemExit # Close the program.
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 align to a line with two color sensor. It's a mechanism we've used with EV3, NXT and now SPIKE.
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() color_sensor_E = ColorSensor('E') color_sensor_F = ColorSensor('F') motor_A = Motor('A') # Set the motor port to the motor. motor_B = Motor('B') # Set the motor port to the motor. motor_A.set_default_speed(-30) # Set the default speed of the motor. motor_B.set_default_speed(30) # Set the default speed of the motor. motor_A.set_stop_action('brake') # Activate the brakes when the motor stops. The other conditions are 'hold' and 'coast'. motor_B.set_stop_action('brake') # Activate the brakes when the motor stops. motor_A_flag = 0 # Create a flag for motor A and set it to OFF. motor_B_flag = 0 # Create a flag for motor B and set it to OFF. def stop_at(colour): motor_A_flag = 0 # Reset the flag for motor A to OFF. motor_B_flag = 0 # Reset the flag for motor B to OFF. # Move forward. motor_A.start() motor_B.start() while (motor_A_flag == 0) or (motor_B_flag == 0): # Repeat while both sensors ever detect black color. if color_sensor_E.get_color() == colour: # If the color sensor on port E detect the desired color motor_A.stop() # stop the motor an port A and motor_A_flag = 1 # set the flag for motor A to ON. if color_sensor_F.get_color() == colour: # If the color sensor on port F detect the desired color motor_B.stop() # stop the motor an port B and motor_B_flag = 1 # set the flag for motor B to ON. stop_at('black') stop_at('white') raise SystemExit # Close the program.
This program is developed with LEGO Education SPIKE App Python and is used to accomplish the Power Switch mission model with Luly, small LEGO Education SPIKE Prime competition robot with 3D building instructions.
This program is developed with LEGO Education SPIKE App Python and is used to accomplish the Power Switch mission model with a reusable geared attachment placed on Luly, small LEGO Education SPIKE Prime competition robot with 3D building instructions.
Developed with LEGO Education SPIKE App Python this program is used to accomplish the Flip mission model with a Flip attachment placed on Luly, small LEGO Education SPIKE Prime competition robot with 3D building instructions.
This program is developed with LEGO Education SPIKE App Python and is used to accomplish a Power Switch mission model with a heavy lifting attachment attachment placed on Luly, small LEGO Education SPIKE Prime competition robot with 3D building instructions.
Developed with LEGO Education SPIKE App Python this program is used to accomplish a Power Switch mission model with a vertical heavy lifting attachment placed on Luly, small LEGO Education SPIKE Prime competition robot with 3D building instructions.
Developed with LEGO Education SPIKE App Python this program is used to accomplish the Power Switch mission model with a Front attachment placed on Luly, small LEGO Education SPIKE Prime competition robot with 3D building instructions.
The course introduces students to the programming language Python. We use LEGO Mindstorms EV3 Robots. Python is a popular programming language. It could be used for introducing students to programming, for academic studies, for developing machine learning algorithms and as a general-purpose language.
During the course, students learn how to read and how to develop Python programs. They use an Integrated Development Environment called Visual Studio Code. Robots are programmed to perform interesting and funny tasks like "bringing you water". The level ends with competition on a playing field with boxes.
Part of the Perfect STEM course, you should have a Raspberry Pi and an LED connected on GPIO 18 for this example to work.
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.
In the second level of Python for EV3 robots, students learn in-depth the touch sensor. The sensor is used as an input device for manual control of machines, as well as a sensor for autonomous robots. In a pair of lessons, students build a control panel for the grabber and the movement of a crane. Programming wise, students learn how to fork code with "if-else" constructions, how to create conditional and forever loops with "while" and how to negate conditions with "not" operator. In the end of the lesson, robots can detect obstacles and avoid them, so that they traverse a simple labyrinth.