EV3 robot programming course in Lego Mindstorms EV3 environment. Bookshelf robotics Robot programming course lego mindstorms ev3

L.Yu. Ovsyanitskaya, D.N. Ovsyanitsky, A.D. Ovsyanitsky

EV3 Robot Programming Course

in the environment Lego Mindstorms EV3

Second edition, revised and expanded

UDC 004.42+004.896

Ovsyanitskaya, L.Yu. Lego robot programming course

Mindstorms EV3 in the EV3 environment: ed. second, revised and additional /

L.Yu. Ovsyanitskaya, D.N. Ovsyanitsky, A.D. Ovsyanitsky. – M.:

“Pero”, 2016. – 296 p.

ISBN 978-5-906862-76-1

The book is devoted to programming the EV3 robot in the Lego Mindstorms EV3 environment. The work is the result of many years of experience of the authors’ direct participation in regional, all-Russian and international competitions in robotics and pedagogical activities aimed at training teachers, lecturers and trainers on this topic.

The book will be useful to teachers of primary, secondary, higher and additional education, students, students and everyone interested in robotics issues.

Reviewer:

Doctor of Physical and Mathematical Sciences, Professor A.F. Shorikov.

ISBN 978-5-906862-76-1 © L.Yu. Ovsyanitskaya, D.N. Ovsyanitsky, A.D. Ovsyanitsky, 2016 Contents Introduction

Chapter 1. Characteristics of the robot.

Creation and launch of the first project 7

1.1. a brief description of robotic platforms. Overview of the Lego Mindstorms EV3 programming environment

1.2. Ways to connect the robot to a computer. EV3 Brick firmware update. Uploading Programs to the EV3 Brick



Chapter 2. Programming the robot

2.1. Motors. Programming movements along various trajectories

2.2. Working with backlight, screen and sound

2.2.1. Working with the screen

2.2.2. Working with backlit buttons on the EV3 Brick

2.2.3. Working with sound

2.3. Program structures

2.3.1. Structure Expectation

2.3.2. Structure Cycle

2.3.3. Switch Structure

2.4. Working with data

2.4.1. Data types. Conductors

2.4.2. Variables and Constants

2.4.3. Mathematical operations with data

2.4.5. Working with Arrays

2.4.6. Logical operations with data

2.5. Working with sensors

2.5.1. Touch sensor

2.5.2. Color sensor

2.5.3 Gyro sensor

2.5.4. Ultrasonic sensor

2.5.5. Infrared sensor and beacon

2.5.6. Motor rotation sensor (determining the angle/number of revolutions and motor power)

2.5.7. Module control buttons

2.6. Working with files

Lego Mindstorms EV3 robot programming course in EV3 environment

2.7. Collaboration of several robots

2.7.1. Connecting robots with a USB cable

2.7.2. Robot communication using Bluetooth connection.................................. 207

2.8. Useful blocks and tools

2.8.1. Block “Keep active”

2.8.2. Block “Stop program”

2.8.3. Creating Routines

2.8.4. Recording comments

2.8.5. Using a wired input port

Chapter 3. Main types of competitions and elements of tasks.

3.1. Sumo competition

3.2. Kegelring

3.3. Slalom (avoiding obstacles)

3.4. Line movement programming

3.4.1. Algorithm for moving along a Zigzag line with one and two color sensors

3.4.2. Algorithm "Wave"

3.4.3. Algorithm for automatic calibration of the color sensor..... 258

3.5. Proportional Linear Control

3.5.1. Line movement based on proportional control

3.5.2. Finding and counting intersections with proportional line control

3.5.3. Travel inversion

3.5.4. Robot movement along the wall

3.6. Finding the goal in the maze

Chapter 4: Updating Firmware and Restarting the EV3 Brick.

286 Chapter 5. Using third-party sensors

5.1. Working with HiTech color sensor

5.2. Using other sensors

Conclusion

List of projects Project “Faithful Dog” 90 Project “Sports Scoreboard” 98 Project “Auto Finish” 102 Project “60 Seconds” 109 Project “Recording and Reading a Color Barcode” 120 Project “Sorting an Array Using the Bubble Method” 123 Project “ Smart House» 153 Project “Stubborn Robot” 160 Project “Robot with remote control» 182 Project Animated game on the screen of the EV3 block “Catch a snowball” 191 Project “Construction of a 3D surface map” 197 Project “EV3 - music synthesizer” 203 Course of programming the Lego Mindstorms EV3 robot in the EV3 environment image of a coil (Fig. 1.1.11b). The block will become active (bright) (Fig. 1.1.11c).

–  –  –

Figure 1.1.

12. Parallel programs To scale images (Fig. 1.1.13 a, b), standard MSWindows combinations are used Ctrl keys and mouse scroll wheels or icons in the upper right corner of the window:. Scaling is used when navigating large programs, copying specific blocks, and much more.

Lego Mindstorms EV3 robot programming course in the EV3 environment Blocks Large motor and Medium motor The first block of the palette is called Medium motor, the second is called Large motor. The blocks are used to control one motor and have the same functionality.

Let's consider the structure of blocks using the example of a large motor block (Fig. 2.1.4).

–  –  –

Figure 2.1.

4. Large Motor Control Unit First of all, click on the letter representing the port name and select the name of the port to which the motor is connected.

Let's take a closer look at each control element.

1. Select operating mode:

a) turn on (Fig. 2.1.5);

–  –  –

Figure 2.4.

3.4. Program for the implementation of the “60 seconds” project

DIY activities Add a picture of a clock face to the screen.

Add the minute and hour hands by analogy.

2.4.4. Other data processing blocks

–  –  –

Before you start working with arrays, you need to initialize them, i.e. specify the type (numeric or logical) and assign a name.

Data can be entered into the array manually or automatic mode(reading readings from sensors). To create an array, you must use the Variable block.

Creating an array. Writing an array to a variable

In order to create and fill an array, you must:

(a) move the Variable block to the workspace and determine its mode (Write) and type (numeric or logical array);

Formation Formation of a numeric array of a logical array

–  –  –

Figure 2.5.

3.3. Operating modes of the gyroscopic sensor Important!

Sometimes (quite often!) when working with a gyroscopic sensor, you can observe the following: when running program the robot is stationary, and the angle value is constantly increasing (drift), the rate of increase can be more than 1 degree per second!

Increasing sensor values ​​Chapter 2. Programming the robot 177 If the beacon is very far away (further than 1 m), the measurement value will be 100, if very close (minimum 1 cm) - 0. Intermediate results also do not correspond to centimeters.

In the case when the beacon is located directly in front of the sensor, the relative result of measuring the angle will be equal to 0, the maximum location of the beacon on the left, counterclockwise -25 (the maximum detectable angle of deviation is approximately 100 degrees), on the right, clockwise 25 (Fig. 2.5. 5.5).

Figure 2.5.

5.5. Position of the IR beacon relative to the IR sensor Let's look at example programs. Place the beacon in front of the robot, turn it on and point it towards the robot.

The LED indicator will turn on and stay lit. The beacon will continuously transmit a signal. On the infrared sensor unit, install the same channel that was installed on the beacon. The sensor will only detect the beacon on the channel you specify in your program.

The beacon turns off if not used for an hour.

In Fig. 2.5.5.6 shows the choice of operating mode with the beacon.

Lego Mindstorms EV3 robot programming course in the EV3 environment using MS Excel.

The robot rotates and at each moment of time records the value of the rotation angle and the distance to the surface.

Solution:

1. Reset the gyroscope sensor values.

We insert cycle 01, the termination condition is time (3 seconds).

2. During the cycle, the robot rotates and reads information from ultrasonic and gyroscopic sensors. The readings are combined in the Text program block, separated by a comma.

3. The measurement result at each step of the cycle is written to the Map file.

4. Set a pause of 0.25 seconds. After the end of the cycle, close the file.

Attention! When using a gyroscopic sensor, pay attention to the presence of reading drift when turning it on (see.

section 2.5.3 to remove drift).

Chapter 2. Programming the robot 213 move and stop, feed each trailer sequentially.

2. “Vocal and instrumental ensemble”

The task is to perform a piece of music with an ensemble. The first EV3 robot is a conductor that gives commands via Bluetooth to the other robot musicians and robot singers when to play their musical parts. The robot conductor can be equipped with a conductor's baton, which moves up and down and turns towards the robot, which starts playing. A robot conductor can, for example, act as a soloist part-time.

3. "Dance Ensemble"

The task is to create a robotic ensemble. The first robot issuing commands via Bluetooth is the soloist. The rest of the robots follow the commands. Program different types of dances - round dance of robots ("Locomotive"), slow and fast dances.

4. “Morning exercises”

The task is to simultaneously perform gymnastic exercises at the command of the first robot.

2.8. Useful blocks and tools 2.8.1. Block “Keep active”

After a certain time has passed and in the case when we do not contact the robot and the robot does not perform any operations, it turns off (in EV3 terms, it goes into sleep mode). This causes inconvenience when working with programs designed to wait long periods of time for some processes. We can set the time for switching to sleep mode directly on the block (it is possible to set the time before switching off: 2 minutes, Lego Mindstorms EV3 robot programming course in the EV3 environment, create several program options and choose combat tactics. For example, if we have a powerful but slow robot , we can run a program in which our robot will attack the opponent quickly and from the side; if the opponent’s robot always turns to the right in search of our robot, we need to run a program that goes around and attacks it from the left.

The robot can have one or two ultrasonic sensors to determine the position of the enemy without unnecessary turns. Particularly interesting are the rounds in which robots of approximately equal strength or speed compete, in this case the outcome is decided by millimeters and seconds!

The winner is the participant who was able to assemble a strong and reliable structure, wrote a competent program (or programs) and chose the right strategy. It is the combination of these factors that makes the process of preparing for competitions exciting, and the competitions themselves very entertaining and exciting!

Let's give an example of a program algorithm for a robot sumo wrestler.

Stopping the robot.

2. The robot turns until it sees the opponent’s robot with the ultrasound sensor (until the sensor value becomes less than 100 cm), which corresponds to a rotation angle of 120-180 degrees. Stopping the robot.

3. Create a cycle with termination condition 01, – Unlimited.

4. In cycle 01 we insert cycle 02, the termination condition of which is a logical value: the cycle will be executed until the value True is supplied to the Termination Condition input.

Chapter 3. Main types of competitions and elements of tasks 245 100 25 + 18 =.

Let's find the speed of the left wheel: V1=58.

To implement the algorithm, install an ultrasonic sensor in front of the robot in the center and connect it to port 4.

Point the color sensor down, position it to the left of the line and connect it to port 2. In Fig. 3.3.3 presents a program for avoiding obstacles. Please note that after detecting an obstacle, the robot stops and turns sharply to the right in order to move off a line perpendicular to the obstacle and go around the object along a given radius.

Tasks for independent work

Program the paths:

a) avoiding several obstacles with the same radius;

b) avoiding obstacles with different radii;

c) riding in a figure eight.

Chapter 3. Main types of competitions and elements of tasks 275 Figure 3.

5.3.2. Program for driving an inverse trajectory of movement Chapter 3. Main types of competitions and elements of tasks 285 Figure 3.6.5. A program for finding a goal in a maze. A course in programming the Lego Mindstorms EV3 robot in the EV3 environment.

CHAPTER 5. USE OF THIRD PARTY

SENSORS

In addition to the sensors included in the Lego Mindstorms EV3 home or educational kits, it is possible additional acquisition Lego sensors and Lego certified sensors from HiTechnic.

HiTechnic produces a large number of sensors for LEGO Mindstorms, most of them are certified by LEGO, confirming full compatibility, high quality and safety standards.

Also an important factor allowing the use of these sensors when working with children is the presence of a RoHS certificate (Restriction of Hazardous Substances), confirming the absence of the use of substances in electrical and electronic equipment: lead, mercury, cadmium, tin, hexavalent chromium, and some bromide compounds. For a current list of Lego certified sensors, visit www.hitechnic.com/sensors.

Currently available: rotation angle sensor;

force applied transversely to the axis; compass; accelerometer;

gyroscope; magnetic field detector; infrared sensor;

infrared motion sensor, which allows you to determine the presence of people or animals in the room, similar to sensors used in security systems; barometer, which determines atmospheric pressure and temperature;

an electro-optical distance sensor that accurately detects small objects and small changes in the distance to them, but at a distance of no more than ~20 cm; color sensor

In the second lesson, we will become more familiar with the programming environment and study in detail the commands that set the movement of our robot cart, assembled in the first lesson. So, let's launch the Lego mindstorms EV3 programming environment, load our lessons.ev3 project created earlier and add it to the project new program- lesson-2-1. You can add a program in two ways:

  • Select team "File" - "Add program" (Ctrl+N).
  • Click "+" on the programs tab.

Rice. 1

2.1. Programming palettes and program blocks

Let's now turn our attention to the lower section of the programming environment. From the material of the first lesson, we already know that here are the commands for programming the robot. The developers used an original technique and, by grouping program blocks, assigned each group its own color, calling the groups palettes.

The green palette is called: "Action":

Rice. 2

This palette contains software blocks for controlling motors, a display block, and a control block for the module status indicator. Now we will begin to study these program blocks.

2.2. Green palette - action blocks

The first program block of the green palette is designed to control the medium motor, the second block is to control the large motor. Since the parameters of these blocks are identical, let's consider the setting using the example of a block - a large motor.

Rice. 3

For correct settings control unit for a large motor we must:

  1. Select the port to which the motor is connected (A, B, C or D) (Fig. 3 item 1)
  2. Select motor operating mode (Fig. 3 item 2)
  3. Configure the parameters of the selected mode (Fig. 3 item 3)

How do the modes differ? Mode: "Turn on" turns on the motor with a given parameter "Power" and after that control is transferred to the next program block of the program. The motor will continue to rotate until it is stopped by the next block "Big Motor" with the regime "Switch off" or next block "Big Motor" will not contain other execution parameters. Mode "Enable for number of seconds" turns on a large motor with the set power for a specified number of seconds, and only after the time has expired will the motor stop and control in the program will move to the next program block. The motor will behave similarly in modes "Turn on by number of degrees" And "Enable by number of revolutions": only after the set rotation of the motor has been completed, it will stop and control in the program will move to the next block.

The power parameter (in Fig. 3 the power is set to 75) can take values ​​from -100 to 100. Positive power values ​​set the motor to rotate clockwise, negative values ​​set the motor to rotate counterclockwise. At a power value of 0, the motor will not rotate; the “higher” the power value, the faster the motor rotates.

The power parameter is specified only in integer values; the parameters: seconds, degrees, revolutions can take values ​​with a decimal fraction. But remember that the minimum motor rotation step is one degree.

Special mention should be made about the parameter "Brake at the end". This parameter, if set to "To brake" causes the motor to slow down after executing the command, and if set to "Overrun", then the motor will rotate by inertia until it stops.

The next two program blocks "Steering" and implement control of a pair of large motors. By default, the left big motor is connected to the port "IN", and the right one - to the port "WITH". But you can change the connection ports in the unit settings in accordance with the requirements of your design ( Rice. 4 pos. 1).

Rice. 4

Parameter "Steering" (Rice. 4 pos. 2) can take values ​​from -100 to 100. Negative values ​​of the parameter cause the robot to turn left, a value of 0 causes the robot to move straight, and positive values ​​cause the robot to turn right. The arrow above the numerical parameter changes its orientation depending on the value, thereby indicating the direction of movement of the robot ( Rice. 5).

Rice. 5

Program block "Independent motor control" looks like a software block "Steering". It also controls two large motors, but instead of "Steering" it becomes possible to independently control the power of each motor. With equal parameter value "Power" for left and right motor, the robot will move in a straight line. If you apply a negative power value to one motor (for example -50), and a positive value to the second (for example 50), then the robot will turn in place ( Rice. 6).

Rice. 6

The operating modes of these units are similar to those of a single motor control unit, therefore additional description don't require...

2.3. Straight-line movement, turns, turning around and stopping

So, now we can write a program for the robot to move along any route.

Task 1: Drive straight ahead for 4 engine revolutions. Turn around. Drive 720 degrees.

Solution ( Rice. 7):

  1. Using the "Steering" software block, drive forward 4 turns.
  2. Using the “Independent Motor Control” software block, turn around on the spot (the degree value will have to be selected experimentally).
  3. Using the "Steering" software block, drive forward 720 degrees.

Note: Why did I have to select the degree value when turning? block 2?. Is not 360 degrees - the desired value? Not if we set the parameter value "Degrees" equal 360 , then we will thereby force the shafts of the left and right motors of our robot to rotate by the required amount. The angle at which the robot turns around its axis depends on the size (diameter) of the wheels and the distance between them. On Rice. 7 parameter value "Degrees" equals 385 . This value allows a robot assembled according to instructions small-robot 45544 turn around its axis. If you have a different robot, then you will have to choose a different value. Can this value be found mathematically? It is possible, but we will talk about this later.


Rice. 7

Task 2: Place some obstacle (can, cube, small box) on a flat surface and mark the starting point for your robot. Create a new program in the project: lesson-2-2, which allows the robot to drive around an obstacle and return to the starting point.

How many software blocks have you used? Share your success in the comments to the lesson...

2.4. Screen, sound, module status indicator

Program block "Screen" allows you to display text or graphic information on the LCD screen of the EV3 Brick. What could this mean? practical use? Firstly, at the stage of programming and debugging the program, you can display the current sensor readings while the robot is operating. Secondly, you can display the name of the intermediate stages of program execution. Well, thirdly, with the help of graphic images you can “revive” the robot’s screen, for example, using animation.

Rice. 8

Program block "Screen" has four operating modes: mode "Text" allows you to display a text string on the screen, mode "Shapes" allows you to display one of four geometric shapes on the screen (straight line, circle, rectangle, point), mode "Image" can display one image on the screen. You can choose an image from a rich collection of images or draw your own using an image editor. Mode "Reset settings window" Resets the EV3 Brick screen to the standard information screen shown while the program is running.

Rice. 9

Let's look at the parameters of the program block "Screen" in mode "Text" (Fig. 9 item 1). The string intended to be displayed on the screen is entered into a special field (Fig. 9 item 2). Unfortunately, you can only enter Latin letters, numbers and punctuation marks in the text input field. If mode "Clear screen" set to value "True", the screen will be cleared before displaying information. Therefore, if you need to combine the current output with information already on the screen, then set this mode to "Lie". Modes "X" And "Y" determine the point on the screen from which information output begins. The EV3 Brick screen is 178 pixels (dots) wide and 128 pixels high. Mode "X" can take values ​​from 0 to 177, mode "Y" can take values ​​from 0 to 127. The upper left point has coordinates (0, 0), the lower right (177, 127)

Rice. 10

While setting up a program block "Screen" you can turn on the mode preview (Fig. 9 item 3) and visually evaluate the result of the information output settings.

In mode "Shapes" (Fig. 11 item 1) settings of the program block vary depending on the type of figure. So, when displaying a circle, you will need to specify the coordinates "X" And "Y" the center of the circle, as well as the value "Radius". Parameter "Fill" (Fig. 11 item 2) is responsible for the fact that either the outline of the figure will be displayed, or the internal area of ​​the figure will be filled with the color specified in the parameter "Color" (Fig. 11 item 3).

Rice. eleven

To display a straight line, you need to specify the coordinates of the two extreme points between which the straight line is located.

Rice. 12

To display a rectangle, you need to specify the coordinates "X" And "Y" the upper left corner of the rectangle, as well as its "Width" And "Height".

Rice. 13

Displaying a point is the easiest way! Just indicate its coordinates "X" And "Y".

Mode "Image", probably the most interesting and most used mode. It allows you to display images on the screen. The programming environment contains a huge library of images, sorted by category. In addition to the existing images, you can always create your own drawing and, by inserting it into the project, display it on the screen. ("Main menu of the programming environment" - "Tools" - "Image editor"). When creating your image, you can also display characters of the Russian alphabet.

Rice. 14

As you can see, the programming environment attaches great importance to the display of information on the screen of the EV3 Main Brick. Let's look at the following important program block "Sound". Using this block, we can output sound files, tones of arbitrary duration and frequency, as well as musical notes to the built-in speaker of the EV3 block. Let's look at the settings of the program block in mode "Play Tone" (Fig. 15). In this mode you need to set "Frequency" tones (Fig. 15 item 1), "Duration" sound in seconds (Fig. 15 item 2), as well as sound volume (Fig. 15 item 3).

Rice. 15

In mode "Play note" instead of tone frequency, you need to select a note on virtual keyboard, and also set the sound duration and volume (Fig. 16).

Rice. 16

In mode "Play file" you can choose one of the sound files from the library (Fig. 17 item 1), or by connecting a microphone to your computer using the Sound Editor ("Main menu of the programming environment" - "Tools" - "Sound Editor") record your own audio file and include it in the project.

Rice. 17

Let's look at the parameter separately "Playback type" (Fig. 17 item 2), common to all modes of the program block "Sound". If this parameter set to value "Wait for completion", then control will be transferred to the next program block only after complete playback of the sound or sound file. If you set one of the following two values, the sound will start playing and control in the program will move to the next program block, only the sound or sound file will be played once or will be repeated until it is stopped by another program block "Sound".

We just have to get acquainted with the last program block of the green palette - the block. There is a color indicator mounted around the EV3 module control buttons, which can glow in one of three colors: green, orange or red. The corresponding mode is responsible for turning on and off the color indication (Fig. 18 item 1). Parameter "Color" sets the color design of the indication (Fig. 18 item 2). Parameter "Impulse" responsible for turning on/off the color indication flickering mode (Fig. 18 item 3). How can you use color indication? For example, you can use different color signals during different modes of operation of the robot. This will help us understand whether the program is being executed as we planned.

Rice. 18

Let's put this knowledge into practice and colorize our program from Task 1 a little.

Task 3:

  1. Play signal "Start"
  2. Enable green non-blinking color indication
  3. "Forward"
  4. Drive straight ahead for 4 engine revolutions.
  5. Enable orange flashing color indication
  6. Turn around
  7. Enable green flashing color indication
  8. Display image on screen "Backward"
  9. Drive 720 degrees
  10. Play signal "Stop"

Try to solve problem 3 yourself without looking at the solution! Good luck!

The program blocks of the "Actions" panel were discussed in the previous parts of the review, and in this article I will talk about the blocks from the "Operator Management" tab.

These blocks can be represented as “regulators” of the program: they will order to stop and continue the movement of the program, move to the next branch or go in a circle.

Compared to NXT, 2 new blocks were added:

  • Beginning - in NXT, the beginning of the program was uniform and was set immediately when the program was opened.
  • Interrupting the cycle - there was simply no such block in NXT. If it was necessary to implement similar functionality, then we had to use variables.
The general list of operator control units looks like this:
  • Start
  • Expectation
  • Switch
  • Interrupting a cycle


Block "Start"

You may have noticed that the first block of all EV3 programs is the block with a green arrow. This block is "Start". Not a single program can do without it - this is where command execution begins. If you do not put “Start” in front of the sequence of blocks, then such a program will not be executed.
For example, according to the program presented below, the robot will spin around an axis (the upper sequence of actions will be performed), but will not play audio files and light up the buttons (the lower sequence without the “Start” block is not active):

EV3 supports multitasking, i.e. a program can contain more than one sequence of commands. Moreover, these sequences can have their own “Start” block or come out of one “Start”:


All such sequences will be executed simultaneously.

Please note that the green arrow on the block is not a decorative element. If the unit is connected to a computer (no matter how: via usb, wi-fi or bluetooth), then clicking on the arrow will launch this sequence for execution.

Block "Waiting"

This block is also one of the most used. The program “freezes” on it - subsequent blocks of the program are not executed - and waits for a certain amount of time or a certain sensor value.
"Waiting" has a large number of modes, which can be scary:


But, in fact, everything is quite simple. All modes can be divided into categories:
  • by time - the block waits the specified number of seconds before starting to execute the next blocks
  • according to the sensor:
    • comparison - the block expects a specific sensor reading specified in the block
    • change - the block waits for the sensor reading to change by a specified amount compared to the initial value. Moreover, you can choose not only the size of the value, but also its direction - decreasing value, increasing it, or in any direction.
Let's look at examples of programs with each block mode.
In the first program, the backlighting of the buttons changes. The backlight will be green for one second, then red for 1 second and then switch to the standard mode - flashing green:

The second program is the beginning of a classic solution in the Kegelring competition: the robot spins around its axis until it sees a can in front of it:

The following program turns on motor A, and after it has made 5 revolutions, turns it off:

The operation of this unit with bluetooth is no different from working with any sensor. For example, the following program waits for a "HI" message and then turns the backlight green and plays a sound file:

Block "Cycle"

This is a special block - other blocks can be inserted inside it. The blocks inside will be repeated. The Loop block modes specify the method that determines when the loop should end. We already know most of these modes from the previous waiting block, but several new ones have been added:

  • Unlimited - this loop will run until the program is forced to terminate
  • Counting - the loop will repeat a specified number of times
  • Boolean value - the loop will repeat until the given value is true
  • Time - the cycle will repeat for the specified time
  • Sensor reading:
    • comparison - the cycle will be repeated until the sensor accepts the specified value
    • change - the cycle will be repeated until the sensor reading changes by the specified amount compared to the initial value.
The name of the loop is written above the block - 01, 02, .... This name can be used in the loop interrupt block, which will be described later.
Let's look at examples. According to this program, the robot will drive straight and turn until the program is stopped (an endless loop is used):

The following program uses a touch sensor. Until it is pressed, the robot rotates the middle motor, first clockwise, then counterclockwise. After pressing the sensor, the motor stops:

A counter loop allows you to play a note 10 times:

With the logical value mode, you will have to use yet unexplored sensor polling blocks. This program forces the robot to move forward until it sees an object at a distance of less than 20 cm (first block of the cycle) or hits an obstacle with the touch sensor (second block of the cycle). Result logical operation"OR" gives the third block of the loop:


"Switch" block

There is a similar block in NXT; it allows, depending on the value of a variable or sensor reading, to perform different sequences actions.
After completing this sequence of actions, the program executes the blocks following the “Switch”.
The mode of this block determines which sensor or variable value will be used. All the same modes are used as the “Cycle” block: you can use any sensor (color, gyroscopic, infrared, ultrasonic, motor rotation and others), numerical or text value, bluetooth message.
For example, according to this program, the robot will first rotate around its axis for 5 motor revolutions, and then, depending on the readings of the gyroscope sensor, move forward or backward. If the angle detected by the gyroscope is less than 90 degrees, then the robot will move forward. If the angle is less than 90 degrees, then the robot will go backwards.

The following program is an implementation of a simple relay controller for driving along a black line:

In both previous examples, the "Switch" block contained only 2 options for the development of events. But in fact, this block may have more options. For example, if a robot determines the color of an object, it can choose from a much larger number of options:


In this program, the robot detects red, blue, green and no color. It will name the colors, and also turn on the backlight of the buttons for red and green colors, and will move forward in the blue color. You can add the remaining colors that the sensor can detect - black, yellow, white, brown - using the small "+" button located at the top option (circled in red).
With a large number of options (as in the example above), the “Switch” block becomes quite cumbersome and inconvenient to work with. In this case, the block can be switched to “flat mode”:

The button that switches between flat/detail modes is circled in red.

Block "Cycle Interrupt"

This block is new, there was no similar block in NXT. It allows you to exit the loop - the remaining blocks of the loop will not be executed, and the program will move on to the blocks after the loop. The "header" of the block specifies the name of the cycle that must be completed.
For example, the cycle in the program should be repeated 5 times, but if the distance to the object becomes more than 50 centimeters, the cycle will exit early and the robot will play a sound tone:

The peculiarity of this block is that it does not have to be located inside an interruptible loop. For example, the following program prints the number of times the loop will repeat as long as the reflected light brightness is greater than 50. But if the touch sensor is pressed while the program is running, the loop will stop and the program will stop:

Hello. In my articles, I want to introduce you to the basics of programming the LEGO NXT Mindstorms 2.0 microcomputer. For application development I will use Microsoft platforms Robotics Developer Studio 4 (MRDS 4) and National Instruments LabVIEW (NI LabVIEW). The tasks of automatic and automated control of mobile robots will be considered and implemented. We will move from simple to complex.

Anticipating some questions and comments from readers.

Why NXT Mindstorms 2.0? Because for their projects this set It seemed to me the most suitable, because The NXT microcomputer is fully compatible with the MRDS 4 and NI LabVIEW platforms, and this kit is also very flexible in terms of assembling various robot configurations - a minimum of time is spent on assembling the robot.

Why MRDS 4 and NI LabVIEW platforms? This is how it happened historically. While studying in senior years at the university, the task was to develop training courses using these platforms. In addition, the platforms are quite easy to learn and functional; using them, you can write a program directly to control the robot, develop a user interface and conduct testing in a virtual environment (in the case of MRDS 4).

Who needs these lessons of yours anyway, there are already a lot of robotics projects on the Internet! There are practically no educational articles using this combination (NXT+MRDS 4/NI LabVIEW), mostly the native programming environment is used, and everything in it is completely trivial. Anyone who is interested in robotics, programming and who has an NXT set (and there are quite a few of them), any age audience.

Graphical programming languages ​​are evil, and those who program in them are heretics! Graphical programming languages, which are MRDS 4 and NI LabVIEW, undoubtedly have their drawbacks, for example, being focused on narrow tasks, but still in functionality they are not much inferior to text languages, especially since NI LabVIEW was originally developed as easy language in mastering for solving scientific and engineering problems, for this it contains many necessary libraries and tools. Therefore, these graphic languages ​​are the most suitable for solving our problems. And you don’t need to burn us at the stake for this.

All this looks childish and not serious at all! When the task is to implement algorithms, to teach the basics and principles of programming, robotics, real-time systems without delving into circuitry and protocols, then this is a very suitable tool, although not cheap (regarding the NXT set). Although for the same purposes, kits for Arduino based, but this controller has almost no compatibility with MRDS 4 and NI LabVIEW, and these platforms have their own charms.

The technologies that are used are the product of decaying capitalist countries, and the author is an enemy of the people and an accomplice of Western conspirators! Unfortunately, most technologies in the field of electronics and computer technology I come from the West, I will be very glad if they point out to me similar technologies of original domestic production. In the meantime, we will use what we have. And there is no need to report this to the special services and hold a grudge against me.

A brief overview of the MRDS 4 and NI LabVIEW platforms.

Let me clarify some terminology. By platform, in this case, we mean a set of various tools, for example, the VPL language in MRDS, as well as the application execution environment, i.e. There is no direct compilation of applications into executable (*.exe) files.

In 2006, Microsoft announced the creation of a platform Microsoft Robotics Developer Studio(more details in the Wikipedia article). MRDS is a Windows-based application development environment for robotics and simulation. Currently relevant is Microsoft version Robotics Developer Studio 4. Features include: VPL graphical programming language, Web and Windows-based interfaces, VSE simulation environment, simplified access to sensors, microcontroller and robot actuators, support for the C# programming language, libraries for multi-threaded programming and distributed execution of CCR applications and DSS, support for many robotic platforms (Eddie, Boe - Bot, CoroBot, iRobot, LEGO NXT, etc.).

LabVIEW (Laboratory Virtual Instrumentation Engineering Workbench) is a development environment and platform for executing programs created in the graphical programming language “G” from National Instruments (for more details, see the Wikipedia article). LabVIEW is used in data acquisition and processing systems, as well as for managing technical objects and technological processes. Ideologically, LabVIEW is very close to SCADA systems, but unlike them, it is more focused on solving problems not so much in the field of automated process control systems (automated control systems technological process), how many in the ASNI region ( automated systems scientific research). The graphical programming language "G" used in LabVIEW is based on a data flow architecture. The execution sequence of operators in such languages ​​is determined not by the order in which they appear (as in imperative programming languages), but by the presence of data at the inputs of these operators. Operators that are not data-related are executed in parallel in random order. The LabVIEW program is called and is a virtual instrument (English: Virtual Instrument) and consists of two parts:

  • a block diagram describing the logic of the virtual instrument;
  • front panel describing the user interface of the virtual instrument.

A quick look at the LEGO NXT Mindstorms 2.0 set.

The NXT kit consists of a control unit, four sensors and three servos. The control block contains:
  • 32-bit AVR microcontroller 7 with 256 KB FLASH memory and 64 KB RAM memory;
  • 8-bit AVR microcontroller with 4 KB FLASH memory and 512 Bytes RAM memory;
  • Bluetooth V 2.0 radio module;
  • USB port;
  • 3 connectors for connecting servos;
  • 4 connectors for connecting sensors;
  • LCD display with a resolution of 99x63 pixels;
  • speaker;
  • connector for 6 AA batteries.
Sensors (different sets of sensors available in different configurations):
  • ultrasonic sensor;
  • two tactile sensors (touch sensors);
  • color detection sensor.


Figure 1 - NXT microcomputer with connected sensors and actuators

And of course, the set contains a variety of LEGO parts in the LEGO Technic form factor from which the actuators and supporting structure will be assembled.


Figure 2 - Details in the LEGO Technic form factor

We are writing the first application.

Let's write the first application. Let, classically, this application displays the text “Hello, World!”. The implementation will take place alternately in MRDS 4 and NI LabVIEW, in the process we will consider the specifics of each platform.

We first install the MRDS 4 and NI LabVIEW platforms; in the case of MRDS 4, the installation should be carried out in a folder whose path does not consist of Cyrillic (Russian letters), Account username should also consist only of Latin letters.

1. MRDS 4 platform.
Launch the VPL environment (Start Menu - All Programs - Microsoft Robotics Developer Studio 4 - Visual Programming Language). This environment allows you to develop applications in the VPL language and conduct testing in the VSE virtual environment. A VPL program is a diagram consisting of interconnected blocks. In the window that opens, in addition to the standard command bar and menu, there are 5 main windows:
  1. Basic Activities – contains basic blocks that implement operators such as constant, variable, condition, etc.;
  2. Services – contains blocks that provide access to the functionality of the MRDS platform, for example, blocks for interacting with any hardware component of the robot, or blocks for calling a dialog box;
  3. Project – combines diagrams included in the project, as well as various configuration files;
  4. Properties – contains the properties of the selected block;
  5. Diagrams window – contains directly the diagram ( source) applications.

Figure 3 - VPL programming environment

Let's perform the following sequence of actions:

2. NI LabVIEW platform.
On this platform, everything is implemented almost identically. Let's launch the LabVIEW environment. Two windows will appear in front of us, the first is Front Panel, designed to implement the user interface ( appearance virtual device), the second is Block Diagram, for implementing the program logic.


Figure 8 - LabVIEW Environment Windows

We will use the Block Diagram window. Let's perform the following steps:

Summary

  • We made a review software platforms for developing NXT microcomputer applications.
  • We looked at the basic principles of application development in the MRDS 4 and NI LabVIEW platforms.
  • We got acquainted with the interface of the environment.
In the following articles we will deal directly with NXT programming. There are a lot of training materials online for the LabVIEW environment, but much less for VPL. I highly recommend studying the reference manual of both platforms (knowledge of English is required), in these manuals there are a lot of examples that can be implemented without having NXT, as well as the following books:
  • Programming the NXT microcomputer in LabVIEW - Lidiya Beliovskaya, Alexander Beliovsky,
  • Microsoft Robotics Developer Studio. Programming robot control algorithms - Vasily Gai.
In my articles I will describe only my projects, because... I don’t see the point in rewriting information from one source to another. I will accept any constructive criticism and answer any questions regarding the platforms reviewed. Thank you in advance!

The pinnacle of Lego's creation was the release of programmable construction sets LEGO Mindstorms Ev3. The toy is intended for children over ten years of age.

Now mindstorms ev3 can be bought without any problems in special stores or on the Internet. They are easily programmed to perform certain actions.

Setting up the programming environment

Before you start writing commands for the robot, you need to install the software.

PC system requirements for working with lego mindstorms ev3:

  • OS Windows XP, 7, 8 or MacOs (10.6-10.8);
  • 2GB of RAM and 750 MB of disk space.

When installing the environment using USB, select the teacher or student version.

After installation, we create a project, which is displayed as a folder. In the control panel, we select what we want to create, programs or conduct an experiment. It is recommended to create an experiment to study the performance of sensors.

The robot control program consists of blocks, sequential operations that it performs, in turn, each individual block has its own mode. For example, the mode of the motor control unit is the ability to stop. Study in detail all the symbols that are printed on the screen.

There is a menu on the screen, which includes tabs:

  • action;
  • operator management;
  • sensor;
  • data operations;
  • my blocks, etc.

Using this menu, you can program the robot to perform different actions. For example, in the tab that is responsible for the operation of various mechanisms, you can set the motor mode to move, stop, or start. There you can set the time, quantity and rotation angle.

In the “sound” block you can program the robot to play sound signals. These signals can be uploaded or recorded using a microphone. An important element of program control is the part of the menu that controls the operators. In it you can control the action of the program itself.

Here you can give the following commands to the program:

  • start expecting;
  • repetition of the cycle;
  • switch between blocks;
  • end the cycle.

Lego mindstorms ev3 is multitasking, it accommodates multiple command sequences. You can program not only your actions into the program, but also the sequence of their execution.

By combining all possible commands of the corresponding menu, you can create complex trajectories and types of constructor behavior.

LEGO Education Mindstorms EV3: Programming Robohand (Robohand H25):