Next Steps with Robotics Kits – LEGO Mindstorms

LEGO Mindstorms NXT : Beta-Rex Photograph

General Thoughts on Robotics Kits

Before I begin, a disclaimer. My thoughts on robotics kits are limited and dated. Limited in the sense that I started without kits building simple robots and then quickly moved on to building my own robots. Dated because it has been a while since I have built from a kit. Regardless of that, I want to share my thoughts on two robot kits that I did use early in my robotics journey. Both the LEGO Mindstorms kit and the BOE-Bot kit helped me immensely as I took my first steps into programming.

Also checkout my only BEAM robot kit thus far – Solar Roller – in my BEAM robotics post.

Checkout my writeup on Parallax BOE-Bot.

LEGO Mindstorms

When I first started robotics I was, understandably, intimidated by programming. At that point in my life, I only had three points of reference: pop culture, my father’s Commadore64 stories, and my mother’s dislike of it (that’s an oversimplification – she didn’t mind programming itself, just the math). I had already started tinkering with building PCs, but getting myself over that initial hurdle of starting to program was a challenge. 

Enter LEGO Mindstorms, a robot kit that is designed for easy introduction to robotics. These robot’s are used in education to teach basic STEM / programming skills and in competitions like FIRST LEGO League.

I purchased mine, a NXT 2.0, kit from Target years ago. LEGO is currently selling the Robot Inventor kit. It looks like they have changed the visual programming language and integrated some really cool new features. Alas, I will be discussing my experience with their programming environment on the NXT 2.0. It originally came on disc but you can download it here. I am not going to go into detail on how to program in this environment. There are already good introduction article here If you want to deep-dive into LEGO Mindstorms then I recommend you checkout LEGO Engineering.

Example of NXT-G environment. Code shown is for my robot ~ Beta Rex.

Beta Rex

I started by building some small wheeled robots with the NXT but eventually I settled on the Alpha Rex kit. After building the kit, I made a few modifications (e.g., moved the light sensor to the head), hence I incremented it to Beta Rex. He is a small humanoid walking robot that navigates under a simple wall-banger routine. It achieves locomotion by shifting weight between each leg then shifting the foot forward before falling back onto it.

You can find my code on Github within my Simple Hobby Robot Projects repository. Specifically under LEGO Mindstorms (here). Note, its just an archive of the Beta Rex Code.

Beta Rex in all his glory.

Photoshoot

Here are some additional photos I have taken of Beta Rex.

Code Review

The primary goal for getting a LEGO Mindstorms kit for me was to learn programming. So lets review Beta Rex’s code to see what knowledge we can glean. Probably best to have reviewed NXT-G code before this point.

1) At a high level, Beta Rex has a primary behavior tract and a secondary track to toggle LEDs.

Overview of Beta Rex code. Note the two tracks.

2) In the primary track, startup is indicated by a music/sound pattern. This isn’t strictly necessary for further behaviors to execute but is a nice way of indicating start state to the user.

Sound generation at start of program to indicate behavior routine has began.

3) Then we enter the main loop. the purpose of this loop is to run forever (or until I turn the robot off / batteries die). This loop walks forward until the ultrasonic sensor detects a distance less than its minimum threshold. Then Beta Rex turns his head left and right; each time reading a distance from the ultrasonic sensor. Beta Rex then decides on which direction to turn and then acts on it. Repeat ad nauseum. We call this move forward until collision, turn, and repeat a wall-banger behavior.

Main wall-banger behavior loop.

3.a)  Walking forward inner loop. Performs a left step and right step. Loop ends when ultrasonic sensor detects an impending collision. 

Inner walking forward loop.

3.b) Left and right head movement followed by ultrasonic readings. The distances are piped along the main track to a comparison block. On decision, we always want to choose the larger distance.

Left and right distance scanning.

3.c) A decision block in NXT-G with nested inner turn loops. these are calibrated to twist the robot in a particular direction. Loops run for a pre-specified number of iterations.

Left and right turn loops based on distance decision.

4) The other, secondary, track simply toggles between LED color states. This is merely for aesthetic effect.

LED blinking loop.

There are a few useful structural points. Firstly, I like to structure a primary behavior track to be the main interface with hardware. This track is responsible for interacting with motors and sensors. It should be tuned to provide solid behavior and not be a UI interface loop. The second, albeit trivial, loop is focused on the user interface. Though if we are being critical, the sound generation should be moved to this loop under my description (its not because its serving as an indicator of the main tract starting).

Second, the main loop has an inner general case (move forward) and then more complex logic to subsume that behavior. This is classic subsumption architecture. You can envision that if we added an additional level of logic on top of these two layers it would be quite the long program track. This could be better solved with a state machine (will touch on this in a later post).

More generally, the NXT LEGO Mindstorms kit is good at exposing programming concepts and clean hardware interfaces. The next step in complexity would be to have lower-level access to the hardware. I cover this in my BOE-Bot kit writeup here. The strength of NXT-G is the simplicity of the visual programming language, affording easy scripting of simple routines. However, as complexity grows, the visual program becomes much more difficult to manage.

Other potentially useful tidbits

1. Years later during my undergrad at MSOE, I made use of the NXT 2.0 again. This time it was for a group assignment where we had to program the robot to do some basic tasks. Though instead of using the NXT G environment we had to write it in Java and follow the principles of good software engineering. That is definitely a story for another time if I can find my old code.

2. There is a decent book for targeted at the LEGO Mindstorms for Java programming. Its called Intelligence Unleashed: Creating LEGO NXT Robots with Java by Brian Bagnall. I used this book as an introduction to LeJOS though it has some good general robotics knowledge as well.

Leave a Reply

Your email address will not be published. Required fields are marked *