
I enjoy playing Lego. Like to collect robots and cars.
And I really like to program. I like to write programs and frameworks.
And not so long ago, I found a way to combine these two hobbies.
Lego Mindstorms is a set that includes several motors, sensors and a programmable module that can read information from sensors and control motors.
In this post I will talk more about Mindstorms and how to program.
')
The architecture of any project looks like this: sensors transmit information to the control module. The program executed on the module processes information and transfers control commands to the motors. The motors set the design in motion.
Sensors
The standard set of sensors Mindstorms 2.0 includes:

Color sensor
This sensor can do quite a lot. First, it can read the current color (and return its RGB values). Secondly, he is able to return the current illumination. Well, and thirdly, he knows how to work like a light bulb.

Two touch sensors
These sensors are able to register three events: the button was pressed, the button was released and the button was pressed and released.

Ultrasonic sensor
This sensor is able to determine the distance to the barrier in front of the sensor. Works in a radius of 2 meters.
Little motors

The kit includes 3 motors. Each can be rotated at a given angle (they promise an accuracy of up to one degree), as well as read the current angle of rotation. The latter is useful if a wheel is attached to the motor, which the user turns himself.
Programmable module

This is a small computer. 48 MHz processor, 64 KB RAM, 256 KB flash memory, USB 1.1 port and bluetooth radio. Of course, whist on it does not raise, but applications for controlling robots work very well even.
Programs
The fun begins here. Along with the set comes a disk with the program Mindstorms NXT. This is a graphical shell for writing simple programs. It seemed to me very uncomfortable. Here, for example, looks like a program with one cycle and if-ohm:
Fortunately, there are a number of projects that allow you to program in normal languages. I stopped at the project
lejos . Programs are written in Java, compiled into classes, and then translated into a binary format that is understood by the flash control module.
Java is quite real: there are quite a few standard libraries, there are threads, some IO. There is even an API for working with bluetooth. Moreover, there is a plugin for eclipse. In general, everything that a developer can dream of.
I will give an example of a simple program:
public static void main( String [] aArg) throws Exception {
ColorLightSensor cs = new ColorLightSensor(SensorPort.PORTS[0],
ColorLightSensor.TYPE_COLORNONE);
for ( int i = 0; i < 3; i++) {
cs.setFloodlight( true );
LCD.drawString( "Hello Habr" , 3, 4);
Thread.sleep(500);
Motor.A.rotate(i % 2 == 0 ? 90 : -90);
cs.setFloodlight( false );
}
}
* This source code was highlighted with Source Code Highlighter .
As you understand, the robot rolls back and forth, flashes a light bulb and says “Hello Habr”.
More about sensors
It's great that third-party developers also produce sensors for Mindstorms. Rummaging on the Internet you can find an accelerometer, a compass, a GPS receiver, a thermometer, an IR sensor, an RFID reader, and more. Unfortunately, no more than four sensors can be connected to one programmable module. So for complex projects often use several modules that work together.
Total
In general, Mindstorms is a great way to spend time. If you like Lego and you want to feel like a crazy professor, I highly recommend it.
I will be glad to answer questions about this set.