Play Your LEGO MindStorms EV3 – Switch Between Block Language And MicroPython

In April 2019, LEGO released its first Python version programming tool for LEGO MindStorms EV3. This is an initial version 1.0 which is based on EV3Dev’s image file. The fans of EV3 could write Python code in VS Code environment to drive LEGO MindStorms EV3.

Today, Let me illustrate how to switch between EV3 block language and the Python code, so that the project written in Lego block language could be renewed.

LEGO EV3 Block Language

I will show two sample programs written in EV3 block language. They are used in the beginner lessons of The Coding Fun.

Program 1 – The first program will monitor continuously if any brick button is pressed. If it is, the program will show the button number on the screen, and then speak out the button name. It uses the the Loop, Switch, Display, Brick Button Sensor and Wait block.

Program 2 – this code segment makes the vehicle turn around and monitors if any brick button is pressed. If it is, the vehicle will stop running and exit the program.

It uses the Motor Tank, Loop, Loop Interrupt and Switch blocks. Meanwhile, it uses the parallel method to run code lines. Therefore, the turning of motor and the monitoring of brick button are done simultaneously. They will not interfere with each other.

Micro Python

To implement the above functions of block language, we first need to create a new Python project by using LEGO MindStorms EV3 MicroPython extension, as shown below.

At the beginning of the Python program, you need to import some predefined modules. Those code has become the default part at the head of file main.py of new EV3 project. If you are new to MiroPython programming, you could just keep those code there and then begin adding your new code.

default head of main.py

Not only keep those imported modules in the place, you could also import other modules when necessary. Here in this project, I imported three other modules “os”, “sys” and “multiprocessing”. I will introduce their usage in the following sections.

Program 1 – As mentioned above, in this program, when any brick button is pressed, brick button number will display on the screen. However, the default font size in MicroPython is super mini. You could notice this when smart hub is launching and there are lots of mini letters scrolling up the screen.

Here we could use “os.system()” function to choose a bigger font size for the screen. Please note LEGO EV3-MicroPython Documentation did not list all of the usable functions and modules. If you are keen in knowing more functions, you could refer to http://www.ev3dev.org website for more information and coding techniques.

“brick.buttons()” function returns a list, of which each item represents the pressed button’s ID. Wrapping “brick.buttons()” function in while statement ensures that the program monitors the button pressing continuously.

Program 2 – In the block language, the program uses a parallel mechanism and uses Loop Interrupt block to stop the running of loop and finally stop the execution.

In Python code, I did not use parallel mechanism. I will introduce how to implement parallel running in the next section. To implement the same function as the above block language, the program calls “motor.run_time()” function to set motor speed, running time, stopping mode and waiting behavior in one shot. Please note the last argument – “wait”. Its default value is True, meaning that the program will wait for this function till it finishes execution. The program calls this function by passing False to the “wait”. Therefore, the program will continue running forward when the motor is running.

After setting the motor to run, the program uses a “while” statement to wrap the code of monitoring brick buttons.

There is a global variable “finish_sign”, which is initialized as False, and when any brick button is pressed, it changes to True. I will introduce its usage in the next example.

Another Example – Multi-processing

In the previous block language, we uses parallel programming to make two code lines run simultaneously, so that the vehicle could do two different things at the same time. In the above Python’s version, we do not use parallel mechanism but realize the same effect. In this example, I will introduce Python’s way of parallel running in case you want to use it in other scenarios.

There are several ways to implement the parallel running. One way is through using multiprocessing module.

To demonstrate this, I write a function called “play_music” which will make the vehicle plays sound repeatedly.

I call multiprocessing.Process() function to create two concurrent processes and let them start and join. I use the global variable “finish_sign” to communicate between function “moveforward” and “play_music”. When the variable “finish_sign” changes to true, both function wisll execute “sys.exit” and stop running.

Now when you run the program, the vehicle will move forward and play the sound. When any brick button is pressed, it will stop running and exit the program.

That is all for the Python code conversion. Stay tuned and I will explain more Python code examples for LEGO MindStorms EV3. Don’t forget to Enjoy the Coding and Have Fun!

Note: All the analysis articles are copyright products of http://www.thecodingfun.com. Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.