Lego Mindstorms for the programmer

    Mindstorms setI really enjoy playing Lego. I 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 the information and transfers control commands to the motors. Motors drive the structure.

    Sensors


    The standard set of Mindstorms 2.0 sensors includes:
    light sensor
    Color sensor
    This sensor can do quite a lot. Firstly, 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.

    touch sensor
    Two touch sensors
    These sensors can record three events: they pressed the button, the button was released and the button was pressed and then released.

    sonic sensor
    Ultrasonic sensor
    This sensor can determine the distance to the obstacle in front of the sensor. Works in a radius of 2 meters.

    Motors


    servo motorThe kit includes 3 motors. Each can be twisted by a given angle (promise accuracy of one degree), and also 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


    servo motorThis is a small computer. 48 MHz processor, 64 KB RAM, 256 KB flash memory, USB 1.1 port and bluetooth radio. Of course, whist cannot be raised on it, but applications for controlling robots work very smartly.

    Programs


    Here the fun begins. Along with the kit comes a disc with the program Mindstorms NXT. This is a graphical shell for writing simple programs. She seemed very uncomfortable to me. Here, for example, looks like a program with one loop and ifth:

    program


    Fortunately, there are a number of projects that allow you to program in normal languages. I settled on the lejos project . Programs are written in Java, compiled into classes, and then translated into a binary format, which the flashed control module understands.

    Java is quite real: there are quite a few standard libraries, there are threads, some of 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, blinks a light 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 much more. Unfortunately, no more than four sensors can be connected to one programmable module. So for complex projects often use several modules working together.

    Total


    All in all, Mindstorms is a great way to spend time. If you like Lego and want to feel like a crazy professor, I highly recommend it.

    I will be glad to answer questions about this set.

    Also popular now: