LEGO Mindstorms: Gateway to Robotics

Luka Androjna
Geek Culture
Published in
4 min readJun 1, 2021

--

The Background

Currently, I am a data scientist and software developer, but I was fascinated by robots when I was younger. I even built a small robot that follows lines and competed with it. But then I got sidetracked and landed in the software niche. Since I don’t know too much about hardware and figured that learning about it upfront might be a big-time investment, I decided to go with the new LEGO Mindstorms 51515 set. Since it seemed like one of the simplest ways of getting back into robotics.

Tricky 2.0, a slightly upgraded version of the original Tricky model.

The Code

I decided to start with something simple and make a program that drives around Tricky (a base robot from the set), does collision detection and aversion. By default, the Hub (the brain of the bot) can be either programmed in Scratch or a variation of Python called MicroPython.

MicroPython

Due to my current occupation I opted for MicroPython, I felt like it gave me more control and I really like coding so it was a better pick for me. But if you aren’t as well versed in programming or simply prefer the visual coding style you should go the Scratch route, to be honest, the software provided by LEGO for coding bots seems to be made more for Scratch users.

MicroPython is a special version of Python implemented in C to run on microcontrollers. It is mostly compatible with Python 3 and the experience using it is mostly the same. But there are some differences, the most notable being the standard library. A lot of modules you might expect or are used to using like datetime are missing. And those that are present are adapted and might be missing some functions or have them replaced with something else. So you might have to do a bit more googling than you expected at first.

The Bot Class

Let’s start with the class definition. And if you wondered, you could just write a simple script and run it, but I prefer the OOP way of coding. First, we need to initialize the components we are using. The hub, both motors, and the distance sensor. I also set up the distance to activate collision aversion, measured in centimeters, the motor speed, and the duration for which the program will run in seconds. We then set the default motor speed and start the clock using `time.ticks_ms() `, this is one of those cases where I’d usually use `datetime ` but it isn’t available in MicroPython. And since the duration of the program is measured in milliseconds the argument needs to be multiplied by 1000.

Collision Detection

The collision detection itself is very simple, we just periodically call this function that takes the distance measurement and informs whether we are closer than the set distance or not.

Collision Aversion

When Tricky gets close to the wall we want to make him go some other way. I opted for a 180-degree turn with 1:3 odds. Otherwise, he will randomly turn left or right at 90 degrees. Notice that when we make the turn, the switch that runs the motors gets interrupted and we have to start it up again.

The Whole Code

This is the whole program I wrote for Tricky. Feel free to use it and play around a bit. It has a few additions to the code shown above but nothing too fancy, like I said this is only the beginning of this journey.

Next Steps

So far Tricky doesn’t really know how to do anything interesting or can be interacted with in any way other than putting something in front of the distance sensor. So we will have to upgrade the bot and the software it runs.

Deciding Which Way to Turn

An issue I found with the current implementation was running into corners. Tricky randomly turns when he gets close to a wall. Well if he gets into a corner there is a 1/3 chance that he will turn into the other wall. Not running into the wall can be mitigated by increasing the collision aversion distance, but Tricky would still turn into the wall often. A simple solution I will try is to mount a motor beneath the distance sensor and make Tricky look left and right before deciding which way to turn.

Color-based Controls

Another way to improve the playability of the robot is to add more ways of interacting with it. The set comes with a color sensor. It can be used to add a way of controlling the robot. Some examples would be, if red is detected Tricky stops driving, yellow adds an extra 10 seconds, etc.

Closing Thoughts

Overall the 51515 set is a great way to get into robotics and programming. Assembling the different robots is a breeze and they are pretty fun out of the gate. And upgrading most base models is very simple. However, as a programmer, it is a great feeling to code up something that moves and affects the physical world. At least in my line of work that is a rare occurrence. I would recommend this experience to anyone who is thinking about getting into robotics or programming, it’s a fun way to do it with a low barrier of entry. I hope I will explore this system further and write a couple more blog posts on the topic. All of the code for this project and further projects will be available here: here.

Thank you for reading my post. If you have any comments, ideas or feedback, I’d be glad to read about it in the comments below.

--

--