Lego Mindstorm NXT self balancing robot

Lego Mindstorm NXT are a great platform for many things such as games, education etc.

In the following I will described my adventures with the standard LEGO Mindstorm NXT package + a Gyro from hitechnic and MATLAB 2007b.
The quick guide is to download the wonderful package NXTway_GS and follow the instructions there. In what follows I am trying to explain the basic technologies used in NXTway_GS.

The long term goal of the project is to examine the possibilities of using LEGO Mindstorms as a way to teach Linear/Fuzzy Control. The short term goal is… to have fun.

So I need two or three “classical” case studies. The first thing that came in my mind is the inverted pendulum problem, or in this case the more interesting self-balancing robot problem. But first things first.

LEGO Mindstorm NXT – Specifications (wikipedia)

  • 32-bit AT91SAM7S256 main microprocessor (256 KB flash memory, 64 KB RAM)
  • 8-bit ATmega48 microcontroller @ 4 MHz (4 KB flash memory, 512 Bytes RAM)
  • 100×64 pixel LCD matrix display
  • A single USB 2.0 port
  • Bluetooth (Class II) wireless connectivity, to transfer programs to the NXT wirelessly or offer ways to control robots remotely (through mobile phones and possibly by PDA‘s)
  • 4 input ports, 6-wire cable digital platform (One port includes a IEC 61158 Fieldbus Type 4/EN 50 170 (P-NET) compliant expansion port for future use)
  • 3 output ports, 6-wire cable digital platform
  • Digital Wire Interface, allowing for third-party development of external devices

LEGO Mindstorm NXT – Programming

  • ROBOLAB – educational toolkit/graphical language
  • Next Byte Codes (NBC) is a simple language with an assembly language syntax that can be used to program the NXT brick.
  • Not eXactly C (NXC) is a high level language, similar to C, built on top of the NBC compiler. It can also be used to program the NXT brick. NXC is basically NQC for the NXT.[6]
  • leJOS NXJ is a high level language based on Java that uses custom firmware developed by the leJOS team.[7]
  • ………
  • LEJOS OSEK is an open source firmware for LEGO MINDSTORMS NXT that allows ANSI C/C++ programming. LEJOS OSEK consists of I/O driver part of leJOS NXJ platform C/Assembly source code, TOPPERS OSEK Real-Time Operating System source code that includes ARM7 (ATMEL AT91SAM7S256) specific porting part, and glue code to make them work together.

I used LEJOS OSEK so a few more expanations/definitions (and a picture) are in order.

Embedded Systems Programming – MATLAB

  • Simulink® is software for modeling, simulating, and analyzing dynamical systems. It provides a graphical user interface (GUI) for building models as block diagrams
  • Real-Time Workshop
    Real-Time Workshop® is an extension of capabilities of Simulink® and MATLAB® that automatically generates, packages and compiles source code from Simulink models to create real-time software
    applications on a variety of systems.
  • Real-Time Workshop Embedded Coder® is a separate, add-on product for use with Real-Time Workshop. It is intended for use in embedded systems development. Real-Time Workshop Embedded Coder generates code that is easy to read, trace, and customize for your production environment. Real-Time Workshop Embedded Coder generates optimized ANSI-C or ISO-C code for fixed-point and floating-point microprocessors. It extends the capabilities provided by Real-Time Workshop to support specification, integration, deployment, and testing of production applications on embedded targets. Real-Time Workshop Embedded Coder addresses targeting considerations such as RAM, ROM, and CPU constraints, code configuration, and code verification.

Self Balancing Robot using all of the above technologies

NXTway_GS is a wonderful demo that includes:

How to Build a Self-Balancing Two-Wheeled Robot called NXTway-GS
Mathematical Dynamics Model of NXTway-GS
Controller Design for Balance and Drive Control
NXTway-GS Model Illustration
Simulation and Experimental Results

You can simulate NXTway-GS model and generate controller program that can be executed on the NXT. Also, you can see the movie of NXTway-GS control experiment at the following URL.

NXTWay_GS uses Embedded Coder Robot NXT Demo.

What happens roughly? – A model based design approach

The equations that describe the robot have been inserted in Simulink, and so a simulation (with VRML) is available.

The plant is linearized and the controller to keep the robot standing up is found using LQR (Linear Quadratic Regulator) techniques.

The controller found is included in a Simulink diagram, and using Embedded Coder NXT Demo, Lejo OSEK C source code is produced.

The C code is then compiled using the GCC of GNU ARM toolchain that produces a binary file which can be uploaded (either in SRAM or in flash) using USB to the NXT.

More in a few days…



9 Responses

  1. Φαίνεται πολύ ενδιαφέρον. 🙂

  2. It is really very interesting! I have one question: In order to make visual animation which programs you must use? Simulink as mentioned before? (Sorry for my bad english)

  3. NXTway_GS contains a simulink model of the robot, and uses MATLAB’s VRML toolkit to create a 3d animation. Also check out to see the robot working.

  4. Hi excellent work!!… well, can you tell me how to make fuzzy control with it? You’ve mention it above, can you explain how to make it work? The project… Thx!
    Or can you help me?.. can’t make it work.

  5. Hi.
    Currently I am trying to code a lego robot to race round a marked track in Java. So far I have used leJOS RCX and the Lego MindStorm simulator for RCX (

    Can you recommend some NXT simulators that use Java?

    Btw, nice robots!

  6. where can I read about microcontroller in your blog?! I have searched about the word atmel microcontroller and you blog appeared to me

  7. hi…i hav started my final year undergrad project on ‘Lego Mindstorm NXT self balancing robot’.i want some help regarding it.

  8. i uploaded the code after makng NXTway_GS_OSEK.rxe in cygwin by ‘make all’ comand bt my program which was in samples_c/nxtway_gs directory is not balancing the segway and neither is gamepad controling its movements, my segway tries to balance itself in the begining if u put it on ground but then after a while its wheels start move in 1 direction im using standard tires, am i supose to make any changings in code? and need to upload extra code for gamepad…using dany gamepad and instructions on lejos site….any help plz?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: