Use Threading in Micro Python for Your LEGO EV3

In LEGO block language, we can drag out multiple lines from a block so that different lines could run simultaneously. We could also use Loop Interrupt Block to stop one loop in another code line. One example is shown in the below diagram.

Parallel Segment Lines

When switching to Micro Python, how could we achieve the above functionality? Let me show you two methods below.

Use Threading Module in Micro Python

Besides the default import modules, I also import another two modules: Thread and sys.

In the following method, it first calls brick.sound.file() to play a sound, and then calls motor.run_angle() method to run the motor up to a degree of 360. Please note that the left_motor.run_angle() method inputs False to the argument “wait”, while the right_motor.run_angle() method inputs True to the argument “wait”. Only in this way, we could create a MoveTank effect for the two motors to run forward. If you are not clear the reason, just refer to my another post of introducing MoveTank effect.

In another method “main_thread1()”, it creates a thread for the method “forward_sound()” and start that thread. After that, it uses a while loop to check the status of brick buttons. If any brick button is pressed, the main thread will call “sys.exit()” to exit the program. Done!

The Limitation of Multi-threading in Micro Python

However, please note that threading functionality in Micro Python is not fully implemented, as mentioned in the following documentation. For example, in the standard Python, thread.daemon(True) could kill the thread when main thread dies. However this method “thread.daemon()” is not implemented by Micro Python, yet.

Another Method of Implementation

we can use another method to implement the same functionality. In the below code block, instead of calling motor.run_angle() method, it calls motor.run method, so that the program will not wait for the motor to run up to a certain angle, instead, it will continue executing forward. In the following while loop, it will check the status of brick buttons. If any brick button is pressed, it calls sys.exit() to stop the whole program.

That is it for the implementation of thread. Which method do you prefer? Leave me a comment below. and most of all, 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.

One thought on “Use Threading in Micro Python for Your LEGO EV3

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 )

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.