LEGO MINDSTORMS Education EV3 + MicroPython: we program a children's constructor in an adult language

Hello, Habr! We already talked about the LEGO MINDSTORMS Education EV3 platform. The main objectives of this platform are training on practical examples, development of STEAM skills and the formation of engineering thinking. It can carry out laboratory work to study mechanics and dynamics. Laboratory stands made of LEGO blocks and utilities for recording and processing data make the experiments even more interesting and visual and help children better understand physics. For example, schoolchildren can collect data on the melting temperature and use the application to organize them and present them in a graph. But this is only the beginning: today we will tell you how to complement this set with the MicroPython programming environment and use it to teach robotics.







Learn programming with EV3



Modern students want to see a colorful result. Yes, they are bored if the program displays numbers in the console, and they want to look at color charts, diagrams and create real robots that move and execute commands. Regular code also seems too complicated for children, so it’s better to start with something easier.



The basic programming environment EV3 is based on the graphic language LabVIEW and allows you to set algorithms for the robot visually: the commands are presented in the form of blocks that can be dragged and joined.







This method works well when you need to show how the algorithms are built, but it is not suitable for programs with a large number of blocks. When complicating the scripts, it is necessary to switch to programming using code, but it is difficult for children to take this step.



There are a few tricks here, one of which is to show that the code performs the same tasks as the blocks. In the EV3 environment, this can be done through integration with MicroPython: children create the same program in the base programming environment using blocks and Python in Microsoft Visual Studio Code. They see that both methods work the same, but it’s more convenient to solve complex problems with code.



Going to MicroPython



The EV3 environment is based on the ARM9 processor, and the developers intentionally left the architecture open. This solution allowed us to roll up alternative firmware, one of which was the image for working with MicroPython. It allows you to use Python to program EV3, which makes typing even closer to real-life tasks.



To get started, you need to download the EV3 MicroPython image to any microSD card, install it in the EV3 microcomputer and turn it on. Then you need to install the free extension for Visual Studio. And you can get to work.



We program the first robot on MycroPython









There are several lessons on our site for mastering the basic concepts of robotics. Models on EV3 introduce children to the basics that are used in self-driving cars, factory-assembled robots, and CNC machines.



We take for example a drawing machine that can be taught to draw patterns and geometric shapes. This case is a simplified version of adult welding robots or milling machines and shows how you can use EV3 in conjunction with MicroPython for teaching students. And the drawing machine can mark the holes in the printed circuit board for dad, but this is another level that requires mathematical calculations.



For work, we need:





The assembly of the robot itself is in the instructions , and we will consider an example of programming.



First, initialize the EV3 module library:



#!/usr/bin/env pybricks-micropython from pybricks import ev3brick as brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, GyroSensor) from pybricks.parameters import Port, Stop, Direction, Color, ImageFile from pybricks.tools import wait
      
      





We set up a platform that rotates the handle like a motor in port B. We set the gear ratio of the two-stage gear transmission with the number of teeth 20-12-28, respectively.



 turntable_motor = Motor(Port.B, Direction.CLOCKWISE, [20, 12, 28])
      
      





We configure the lifting mechanism for the handle as a motor in port C:



 seesaw_motor = Motor(Port.C)
      
      





We set up a gyroscope that measures the angle of the handle, in port 2:



 gyro_sensor = GyroSensor(Port.S2)
      
      





Configure the color sensor in port 3. The sensor is used to detect white paper under the drawing machine:



 color_sensor = ColorSensor(Port.S3)
      
      





Configure the touch sensor in port 4. The robot starts to draw when the sensor is pressed:



 touch_sensor = TouchSensor(Port.S4)
      
      





Define the functions that raise and lower the handle:



 def pen_holder_raise():  seesaw_motor.run_target(50, 25, Stop.HOLD)  wait(1000) def pen_holder_lower():  seesaw_motor.run_target(50, 0, Stop.HOLD)  wait(1000)
      
      





We determine the function for turning the handle by a given angle or to a certain angle:



 def pen_holder_turn_to(target_angle):  if target_angle > gyro_sensor.angle():
      
      





If the target angle is larger than the current angle of the gyroscopic sensor, continue moving clockwise at a positive speed:



     turntable_motor.run(70) while gyro_sensor.angle() < target_angle: pass  elif target_angle < gyro_sensor.angle():
      
      





If the target angle is less than the current gyro sensor, then move counterclockwise:



     turntable_motor.run(-70) while gyro_sensor.angle() > target_angle: pass
      
      





We stop the rotating platform when the target angle is reached:



   turntable_motor.stop(Stop.BRAKE)
      
      





Set the initial position of the handle in the upper position:



 pen_holder_raise()
      
      





Now comes the main part of the program - an endless loop. At first, EV3 waits for the color sensor to detect white paper or a blue starting cell, and the touch sensor will be pressed. Then he draws a pattern, returns to its original position and repeats everything again.



When the device is not ready, the LEDs on the controller turn red and the finger-down image is displayed on the LCD screen:



 while True:  brick.light(Color.RED)  brick.display.image(ImageFile.THUMBS_DOWN)
      
      





We wait when the color sensor considers blue or white, set the color of the LEDs to green, display the thumb up image on the LCD screen and inform that the device is ready for use:



   while color_sensor.color() not in (Color.BLUE, Color.WHITE): wait(10)  brick.light(Color.GREEN)  brick.display.image(ImageFile.THUMBS_UP)
      
      





We wait for the touch sensor to be pressed, assign the value of the angle 0 to the gyro sensor and start drawing:



   while not touch_sensor.pressed(): wait(10)  gyro_sensor.reset_angle(0)  pen_holder_turn_to(15)  pen_holder_lower()  pen_holder_turn_to(30)  pen_holder_raise()  pen_holder_turn_to(45)  pen_holder_lower()  pen_holder_turn_to(60)
      
      





Raise the pen holder and return it to its original position:



   pen_holder_raise()  pen_holder_turn_to(0)</i>
      
      





Here we have such a simple program. And now we launch it and look at the robot draftsman in action.



What do such examples give?







EV3 is a career guidance tool for STEM professions and an entry point into engineering specialties. Since it is possible to solve practical problems on it, children gain experience in technical development and the creation of industrial robots, learn to simulate real situations, understand programs and analyze algorithms, master the basic programming constructs.



MicroPython support makes the EV3 platform suitable for high school. Students can try themselves as programmers in one of the most popular modern languages, get acquainted with professions related to programming and engineering design. EV3 kits show that writing code is not scary, they prepare for serious engineering problems and help to take the first step towards mastering technical specialties. And for those who work in school and are associated with education, we have prepared training programs and training materials. They describe in detail what skills are formed when performing certain tasks, and how the acquired skills are related to training standards.



All Articles