My LEGO MINDSTORMS projects (using set 51515 electronics)
Projects • My programming setup • Additional resources • Questions/Feedback
All of the projects use the electronics of the 51515 set and are programmed using (Micro)Python (although some of them might include their Scratch equivalent). I've grouped them in three categories:
* Pun absolutely intended
Although technically the 51515 hub can be programmed in Python, its support is quite limited. You are restricted to writing code in Python within the LEGO Mindstorms app (either in your laptop, tablet, or mobile), locking you out of using your preferred (and probably more powerful) IDE. Moreover, each project is saved as an .lms
file, regardless if it is written in Scratch or Python. .lms
files are a bit tricky and cannot be put under version control easily.
What is within an
.lms
file?The structure within an
.lms
file is quite peculiar. When you open the.lms
file in a text editor, you can see that the first 2 characters are PK. This is a remnant from the old DOS "pkzip" - the mother of all zip programs. You can rename the.lms
file to.zip
and double click it. Inside, you will findscratch.sb3
. This is supposedly a scratch file. You can also rename this file to.zip
and double click it. Inside, you will find aproject.json
. This is an ugly formatted json file.As you can see, there are a lot of things going on within an
.lms
file, making putting them under version control not very easy.Credit and kudos go to
@hacky97
for shedding some light into this matter.
Given these restrictions, this is how I've configured my setup for the development and documentation of my LEGO Mindstorms projects using Python:
The LEGO Mindstorms Python interface
.ipynb
file) to be paired with a "percent Script". This generates a .py
file which is linked to the original notebook.Configuring a Jupyter Notebook using Jupytext
.py
script. There, I can define text cells with %% [markdown]
and code cells with %%
.Working on Spyder
.py
file, I can refresh the .ipynb
file and see the changes there. This is a very elegant way to work on notebooks in a simple script, which as you can imagine is incredibly handy, since it allows me to create notebooks to explain the different steps of the programs in a nice format. For more details on how Jupytext works, I suggest you take a look at its documentation.The Jupyter Notebook reflects the changes of the .py
file.
.py
file into the LEGO Mindstorms app and it will work, since all the extra stuff (headers, cell definition) is defined as comments.The LEGO Mindstorms Python interface with code from the .py
file. It works!
After this, I just get into the coding and documentation cycle (with a lot of copy-pasting in between). This setup isn't ideal, but at least it works. Anyhow, I hope that in the future the LEGO Mindstorms app works with .py
files. Technically, Python support is still on beta, so it might still happen!
Recently, jamesbiederbeck pointed out that there is an extension for Visual Studio Code, which you can download here. It seems to have some nice features, but I haven't had the chance to check it out. If you do, please share your experience in the discussion!
.py
file into the Mindstorms Python project.If you need some pointers on where to start learning Python to program your first LEGO Mindstorms robots or if you want to go deep into the rabbit hole with your creations, I recommend taking a look at this resources:
Python programming lessons by Sanjay and Arvind Seshan
Amazing resource for LEGO Mindstorms robot programming. A bit focused towards FIRST LEGO League, but useless nevertheless. Content is structured as classroom lessons. It is actively being developed.
LEGO Mindstorms 51515 Micropython programming tutorial by Coder Shah
Handy playlist with some YouTube videos that should get you started quite quickly on the basics of Python for LEGO Mindstorms.
LEGO Mindstorms FAQ by Maarten Pennings
More than a FAQ, I'd say this is a guide well worth reading from start to bottom. Even if you already know the basics, I am sure you will learn something useful
(Unofficial) Documentation for LEGO Mindstorms Python API by Laid Back Koala
Very handy resource if you are sick of the Knowledge Base closing every time you switch windows wrapped. It comes in a cohesive, nice PDF format. It also has a PDF for Scratch!
Undocumented Python for LEGO Mindstorms by Anton's Mindstorms
from mindstorms import MSHub
and then hub = MSHub()
, right? Well, actually import hub
has way many more options. Spoiler alert: it is cool, but it is a tad less user friendly.
Pybricks by the Pybricks team
If you feel that the LEGO Mindstorms vanilla (Micro)Python starts being insufficient for your creations, you might want to take a look at Pybricks. Basically, you download a new firmware to your hub where you run Pybrick's version of (Micro)Python. However, note that support for the 51515 set is still on alpha at the time of writing.
How can I run two motors asynchronously?
The moment you start building more ellaborate robots, you will come across this issue sooner or later. So far, I've found two different ways to tackle this:
drum_solo
activity and it works quite well.If you have any questions, comments, or feedback, please open a discussion. If there is a problem with the code (e.g., mistake), please open an issue. Moreover, you can always drop me a line on Twitter (@amoncadatorres).