Skip to content

arturomoncadatorres/lego-mindstorms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

81 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


ProjectsMy programming setupAdditional resourcesQuestions/Feedback


Projects

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:

  • The LEGO Mindstorms 51515 software already comes with several programs for the different robots. However, these are programmed using Scratch. In this directory you will find their counterparts in Python.

  • A few example scripts that I've created to test or experiment some functionality that I thought would be worth sharing.
  • Here I've documented my own creations (MOCs). For each of them, I've included the building instructions for the robot (as a PDF), as well as its corresponding (Python) program, so that anyone can reproduce them and, most importantly, build upon them!*

* Pun absolutely intended


My programming setup

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 find scratch.sb3. This is supposedly a scratch file. You can also rename this file to .zip and double click it. Inside, you will find a project.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:

  • First, I start coding on the LEGO Mindstorms app. I take advantage of pre-defined imports (which otherwise I would forget), and of the so-called "Knowledge Base" (which is nothing else than the documentation). When the hub is linked, I like how you can see in real-time the status of the motors and sensors connected to it. I always keep the console open (at the bottom of the screen).

The LEGO Mindstorms Python interface

  • Once I have something more or less functional, I bring the big guns. Namely, I open a Jupyter Notebook. Using Jupytext, I configure my notebook (i.e., a .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

  • Now, I can use Spyder (my preferred Python IDE) to indirectly work on the Jupyter Notebook. I copy the code from the LEGO Mindstorms app into the .py script. There, I can define text cells with %% [markdown] and code cells with %%.

Working on Spyder

  • This is where the magic happens. Every time that I save the .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.

  • Lastly, I can just copy and paste the contents of the .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!

VS Code

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!


TLDR: For each project, copy the contents of the .py file into the Mindstorms Python project.

Additional resources

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:

Basics

  • 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.

Intermediate

  • 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.

Advanced

  • 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:


Questions? Feedback?

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).