In the previous part, I managed to bring my LEGO Mindstorms NXT back to life. Now that the device could boot up and had a working firmware installed, the next step was to figure out how to program it.
Controlling the NXT with Python
My first attempt was to use Python. With the help of AI, I generated a simple Python script using an existing NXT library. By connecting the NXT to my computer through a USB Type-B cable, I could send commands directly to the device and control its motors. For example, I could specify how much a motor should rotate and simply run the script to make it happen. The same approach could also be used to read sensor data.
However, this was not exactly what I wanted.
The Python library essentially provides a set of APIs that communicate with the NXT firmware through USB. The program itself still runs on my computer, while the NXT receives and executes the commands.
What I really wanted was to upload a program to the device itself, disconnect the USB cable, and let the NXT execute the program independently. So I decided to explore a different approach.
Bringing Back Visual Programming
I eventually came across an open-source project that provides a Blockly-based graphical programming interface for the NXT.
Instead of writing code directly, users can drag and drop blocks to construct a program. These blocks represent operations such as if-else conditions, loops, motor rotations, and sensor inputs. The concept is quite similar to Scratch.
Although I am comfortable writing code myself, I wanted to make this project more accessible so that my friends could also get involved and have fun with it without necessarily knowing how to program.
It also reminded me of the graphical programming environment I used when I was a kid. There is something particularly enjoyable about putting a few blocks together and watching a physical robot respond to them.
Building a Complete Programming Workflow
The existing project was a good starting point, but a few things were missing or did not work properly in my setup. I started adding features and fixing bugs, particularly around compiling, uploading, and executing programs.
Another problem was that my NXT’s LCD was still broken. Even if I successfully uploaded a program, I could not navigate through the original menu to select and execute it.
To solve this, I modified the firmware so that pressing a physical button would directly execute a predefined program. This allowed me to operate the device without relying on the LCD.
One interesting thing I learned during this process was how the NXT executes programs. The graphical blocks are translated into NXC (Not eXactly C), a C-like programming language designed for the NXT. The code is then compiled into an executable file (.rxe) containing bytecode, which is executed by a virtual machine inside the NXT firmware.
The complete programming flow now looks like this:
Blockly Frontend
|
v
NXC Code
|
v
Compiler
|
v
.rxe File
|
v
USB Upload
|
v
NXT Firmware / Virtual Machine
Once the program is uploaded, the USB cable is no longer needed. I can simply press a button on the NXT to start the program.
A Quick Look at the Hardware Architecture
While working on the firmware, I also started exploring the underlying hardware architecture.
The NXT has two main processors:
- AT91SAM7S256: A 32-bit ARM7-based microcontroller that serves as the main processor, running the firmware and executing user programs.
- ATmega48: An 8-bit AVR microcontroller that handles several low-level hardware operations, including motor PWM control and analog sensor readings.
I found this architecture quite interesting, but I will leave a more detailed discussion of the hardware and the communication between these two processors for a future post.
Bringing Everything Together with AI
What I found particularly interesting about this project was how quickly I could build a complete development environment with the help of AI.
Once I understood the interfaces between different components and had a clear idea of how I wanted the system to work, I could use AI to help implement the missing pieces and debug the integration.
Of course, the bring-up process was not entirely smooth. There were times when the device became unresponsive, the motors did not move, or pressing a button did nothing. But after investigating and fixing these issues, I finally had a working end-to-end system.
I can now drag and drop programming blocks in a graphical interface, define how the robot should respond to its sensors, compile the program, upload it to the NXT, disconnect the USB cable, and execute it with a simple button press.
It is quite satisfying to see a device I played with around 15 years ago running again, this time with a modern programming environment that I have adapted to my own needs.
Back then, I was learning how to program a robot. This time, I am also learning how the robot itself works.