Program for robot ev3 to drive along a line. Lego EV3. Movement along the black line. Color sensor - "Ambient light brightness" mode

Let's consider simplest algorithm movement along the black line on one color sensor on EV3.

This algorithm is the slowest, but the most stable.

The robot will not move strictly along the black line, but along its border, turning left and right and gradually moving forward.

The algorithm is very simple: if the sensor sees black, then the robot turns in one direction, if white, in the other.

Implementation in Lego Mindstorms EV3 environment

In both motion blocks, select the “enable” mode. We set the switch to color sensor - measurement - color. At the bottom, don't forget to change "no color" to white. Also, you must specify all ports correctly.

Don't forget to add a cycle, the robot won't go anywhere without it.

Check it out. To achieve best result try changing the steering and power values.

Movement with two sensors:

You already know the algorithm for moving a robot along a black line using one sensor. Today we will look at moving along a line using two color sensors.
The sensors must be installed so that the black line runs between them.


The algorithm will be as follows:
If both sensors see white– moving forward;
If one of the sensors sees white and the other black, turn towards black;
If both sensors see black, we are at an intersection (for example, we are stopping).

To implement the algorithm, we will need to monitor the readings of both sensors, and only after that set the robot to move. To do this, we will use switches nested within another switch. Thus, we will first poll the first sensor, and then, regardless of the readings of the first, we will poll the second sensor, after which we will set the action.
Let's connect the left sensor to port No. 1, the right one to port No. 4.

Program with comments:

Don’t forget that we start the motors in the “On” mode so that they work as long as necessary based on the sensor readings. Also, people often forget about the need for a loop - without it, the program will immediately end.

http://studrobots.ru/

The same program for the NXT model:

Study the movement program. Program the robot. Send video of model testing

The text of the work is posted without images and formulas.
Full version work is available in the "Work Files" tab in PDF format

Lego Mindstorms EV3

Preparatory stage

Program creation and calibration

Conclusion

Literature

1.Introduction.

Robotics is one of the most important areas of scientific and technological progress, in which the problems of mechanics and new technologies come into contact with the problems of artificial intelligence.

For recent years Advances in robotics and automated systems have changed the personal and business areas of our lives. Robots are widely used in transportation, earth and space exploration, surgery, the military industry, laboratory research, security, and mass production of industrial and consumer goods. Many devices that make decisions based on data received from sensors can also be considered robots - such as, for example, elevators, without which our life is already unthinkable.

The Mindstorms EV3 designer invites us to enter the fascinating world of robots and immerse ourselves in the complex environment of information technology.

Goal: Learn to program the robot to move in a straight line.

    Get acquainted with the Mindstorms EV3 designer and its programming environment.

    Write programs for the robot to move in a straight line at 30 cm, 1 m 30 cm and 2 m 17 cm.

    Mindstorms EV3 constructor.

Designer parts - 601 pcs., servomotor - 3 pcs., color sensor, touch motion sensor, infrared sensor and a touch sensor. The EV3 microprocessor unit is the brain of the LEGO Mindstorms constructor.

A large servomotor is responsible for the movement of the robot, which is connected to the EV3 microcomputer and makes the robot move: go forward and backward, turn and drive along a given path. This servomotor has a built-in rotation sensor, which allows you to very accurately control the robot's movement and speed.

You can make the robot perform an action using the EV3 computer program. The program consists of various control blocks. We will work with the motion block.

The movement block controls the robot’s engines, turns it on, off, and makes it work in accordance with the assigned tasks. You can program the movement to a certain number of revolutions, or degrees.

    Preparatory stage.

    Creation of a technical field.

Let's apply markings to the robot's work area, using electrical tape and a ruler to create three lines 30 cm long - green line, 1 m 15 cm - red and 2 m 17 cm - black line.

    Necessary calculations:

The diameter of the robot wheel is 5 cm 7 mm = 5.7 cm.

One revolution of the robot wheel is equal to the length of a circle with a diameter of 5.7 cm. We find the circumference using the formula

Where r is the radius of the wheel, d is the diameter, π = 3.14

l = 5,7 * 3,14 = 17,898 = 17,9.

Those. For one revolution of the wheel, the robot travels 17.9 cm.

Let's calculate the number of revolutions required to drive:

N = 30: 17.9 = 1.68.

    1 m 30 cm = 130 cm

N = 130: 17.9 = 7.26.

    2 m 17 cm = 217 cm.

N = 217: 17.9 = 12.12.

    Creation and calibration of the program.

We will create the program using the following algorithm:

Algorithm:

    Select a movement block in the Mindstorms EV3 program.

    Turn on both motors in the given direction.

    Wait for the reading of the rotation sensor of one of the motors to change to the specified value.

    Turn off the motors.

We load the finished program into the robot control unit. We place the robot on the field and press the start button. EV3 drives across the field and stops at the end of a given line. But in order to achieve an accurate finish, you have to perform calibration, since the movement is influenced by external factors.

    The field is installed on student desks, so a slight deflection of the surface is possible.

    The surface of the field is smooth, so poor adhesion of the robot’s wheels to the field is possible.

    In calculating the number of revolutions, we had to round the numbers, and therefore, by changing hundredths in revolutions, we achieved the required result.

5. Conclusion.

The ability to program a robot to move in a straight line will be useful for creating more complex programs. As a rule, the technical specifications for robotics competitions indicate all dimensions of movement. They are necessary so that the program is not overloaded with logical conditions, loops and other complex control blocks.

At the next stage of getting to know the Lego Mindstorms EV3 robot, you will have to learn how to program turns at a certain angle, movement in a circle, and spirals.

Working with the designer is very interesting. By learning more about its capabilities, you can solve any technical problem. And in the future, perhaps, create your own interesting models of the Lego Mindstorms EV3 robot.

Literature.

    Koposov D. G. “The first step into robotics for grades 5-6.” - M.: Binom. Knowledge Laboratory, 2012 - 286 p.

    Filippov S. A. “Robotics for children and parents” - “Science” 2010

    Internet resources

    http://lego. rkc-74.ru/

    http://www.9151394.ru/projects/lego/lego6/beliovskaya/

    http://www. lego com/education/

Algorithms for controlling a mobile LEGO robot. Line movement with two light sensors

Additional education teacher

Kazakova Lyubov Alexandrovna


Movement along the line

  • Two light sensors
  • Proportional controller (P-controller)

Algorithm for moving along the black line without a proportional controller

  • Both motors spin with the same power
  • If the right light sensor hits the black line, then the power of the left motor (for example B) decreases or stops
  • If the left light sensor hits the black line, then the power of another of the motors (for example C) decreases (returns to the line), decreases or stops
  • If both sensors are on white or black, then linear motion occurs

The movement is organized by changing the power of one of the motors


Example of a program for driving along a black line without a P-controller

Movement is organized by changing the angle of rotation


  • A proportional controller (P-controller) allows you to adjust the behavior of the robot depending on how much its behavior differs from the desired one.
  • The more the robot deviates from the goal, the more effort it needs to put in to return to it.

  • The P-controller is used to keep the robot in a certain state:
  • Holding the position of the manipulator Moving along a line (light sensor) Moving along a wall (distance sensor)
  • Holding the position of the manipulator
  • Line movement (light sensor)
  • Movement along the wall (distance sensor)

Line movement with one sensor

  • The goal is to move along the “white-black” border
  • A person can distinguish the border between white and black. A robot can't.
  • The goal for the robot is in gray

Driving through intersections

When using two light sensors, it is possible to organize movement along more complex routes



Algorithm for driving along a highway with intersections

  • Both sensors are on white - the robot is driving straight (both motors are spinning with the same power)
  • If the right light sensor hits the black line and the left one hits the white line, then a right turn occurs
  • If the left light sensor hits the black line and the right one hits the white line, then it turns left
  • If both sensors are black, then linear motion occurs. You can count intersections or perform any actions


Operating principle of the P-regulator

Sensor position

O=O1-O2


Algorithm for moving along the black line with a proportional controller

HC = K*(C-T)

  • Ts - target values ​​(take readings from the light sensor on white and black, calculate the average)
  • T - current value - obtained from the sensor
  • K - sensitivity coefficient. The more, the higher the sensitivity


This problem is classic, ideologically simple, it can be solved many times, and each time you will discover something new for yourself.

There are many approaches to solve the line following problem. The choice of one of them depends on the specific design of the robot, on the number of sensors, their location relative to the wheels and each other.

In our example, three examples of a robot will be analyzed based on the main educational model of Robot Educator.

To begin with, we assemble the basic model of the Robot Educator educational robot; for this you can use the instructions in the MINDSTORMS EV3 software.

Also, for examples, we will need EV3 light-color sensors. These light sensors, like no other, are best suited for our task; when working with them, we do not have to worry about the intensity of the surrounding light. For this sensor, in programs we will use the reflected light mode, in which the amount of reflected light from the red backlight of the sensor is estimated. The limits of the sensor readings are 0 - 100 units, for “no reflection” and “total reflection”, respectively.

As an example, we will analyze 3 examples of programs for moving along a black trajectory depicted on a flat, light background:

· One sensor, with P regulator.

· One sensor, with PC regulator.

· Two sensors.

Example 1. One sensor, with P regulator.

Design

The light sensor is installed on a beam conveniently located on the model.


Algorithm

The operation of the algorithm is based on the fact that, depending on the degree of overlap of the sensor illumination beam with a black line, the readings returned by the sensor vary gradiently. The robot maintains the position of the light sensor on the boundary black line. By converting input data from the light sensor, the control system generates a value for the robot's turning speed.


Since on a real trajectory the sensor generates values ​​throughout its entire operating range (0-100), 50 is selected as the value to which the robot strives. In this case, the values ​​transmitted to the rotation functions are generated in the range -50 - 50, but these values ​​are not enough for a steep turning the trajectory. Therefore, the range should be expanded one and a half times to -75 - 75.

As a result, in the program, the calculator function is a simple proportional controller. The function of which ( (a-50)*1.5 ) in the operating range of the light sensor generates rotation values ​​in accordance with the graph:

Example of how the algorithm works

Example 2. One sensor, with PK regulator.

This example is based on the same construction.

You probably noticed that in the previous example the robot swayed excessively, which did not allow it to accelerate enough. Now we will try to improve this situation a little.

To our proportional controller we are also adding a simple cube controller, which will add some bending to the controller function. This will reduce the swaying of the robot near the desired boundary of the trajectory, as well as make stronger jerks when far away from it.

This is how a person sees the line:

This is how the robot sees it:


We will use this feature when designing and programming a robot for the “Trajectory” competition category.

There are many ways to teach a robot to see a line and move along it. There are complex programs and very simple ones.

I want to talk about a programming method that even children in grades 2-3 can master. At this age, it is much easier for them to assemble structures according to instructions, and programming a robot is a difficult task for them. But this method will allow the child to program the robot to any route of the track in 15-30 minutes (taking into account step-by-step testing and adjustment of some features of the trajectory).

This method was tested at municipal and regional robotics competitions in the Surgut region and Khanty-Mansi Autonomous Okrug-Yugra and brought our school first places. There I became convinced that this topic is very relevant for many teams.

Well, let's get started.

When preparing for this type of competition, programming is only part of the solution to the task. You need to start by designing a robot for a specific route. In the next article I will tell you how to do this. Well, since movement along a line occurs very often, I’ll start with programming.

Let's consider the option of a robot with two light sensors, as it is more understandable to primary school students.

Light sensors are connected to ports 2 and 3. Motors to ports B and C.
The sensors are placed at the edges of the line (try experimenting with placing the sensors at different distances from each other and at different heights).
Important point. For better work For such a scheme, it is advisable to select a pair of sensors according to the parameters. Otherwise, it will be necessary to introduce a block for adjusting sensor values.
Installation of sensors on the chassis according to the classic pattern (triangle), approximately as in the figure.

The program will consist of a small number of blocks:


1. Two light sensor units;
2. Four blocks of “Mathematics”;
3. Two motor blocks.

Two motors are used to control the robot. The power of each is 100 units. For our scheme, we will take the average value of motor power equal to 50. That is, the average speed when moving in a straight line will be equal to 50 units. When deviating from straight-line motion, the power of the motors will proportionally increase or decrease, depending on the angle of deviation.

Now let's figure out how to connect all the blocks, configure the program and what will happen in it.
Let's set up two light sensors and assign ports 2 and 3 to them.
Take the math block and select “Subtraction.”
Let's connect the light sensors from the "Intensity" outputs by buses to the mathematics block to the inputs "A" and "B".
If the robot's sensors are installed symmetrically from the center of the track line, then the values ​​of both sensors will be equal. After subtraction we get the value – 0.
The next block of mathematics will be used as a coefficient and you need to set “Multiplication” in it.
To calculate the coefficient, you need to measure the “white” and “black” levels using the NXT block.
Let's assume: white -70, black -50.
Next we calculate: 70-50=20 (the difference between white and black), 50/20=2.5 ​​(we set the average power value when moving in a straight line in the math blocks to 50. This value plus the added power when adjusting the movement should be equal to 100)
Try setting the value to 2.5 at input “A”, and then select it more accurately.
To the input “B” of the mathematics block “Multiplication”, connect the output “Result” of the previous mathematics block “Subtraction”.
Next comes a pair - a math block (Addition) and motor B.
Setting up a math block:
Input “A” is set to 50 (half the motor power).
The output of the “Result” block is connected by a bus to the “Power” input of motor B.
The next pair is a math block (Subtraction) and motor C.
Setting up a math block:
Input “A” is set to 50.
Input “B” is connected by a bus to the “Result” output of the “Multiplication” mathematics block.
The output of the “Result” block is connected by a bus to the “Power” input of motor C.

As a result of all these actions you will get the following program:

Since all this will work in a cycle, we add “Cycle”, select it and move it all to “Cycle”.

Now let's try to figure out how the program will work and how to configure it.


While the robot is moving in a straight line, the sensor values ​​coincide, which means that the output of the “Subtraction” block will be 0. The output of the “Multiplication” block also gives the value 0. This value is supplied in parallel to the motor control pair. Since these blocks are set to 50, adding or subtracting 0 does not affect the power of the motors. Both motors operate at the same power of 50, and the robot rolls in a straight line.

Let's assume that the track makes a turn or the robot deviates from a straight line. What will happen?

The figure shows that the illumination of the sensor connected to port 2 (hereinafter referred to as sensors 2 and 3) increases as it moves onto the white field, and the illumination of sensor 3 decreases. Let's assume that the values ​​of these sensors become: sensor 2 – 55 units, and sensor 3 – 45 units.
The “Subtraction” block will determine the difference between the values ​​of two sensors (10) and feed it to the correction block (multiplying by a coefficient (10*2.5=25)) and then to the control blocks
motors.
In the mathematics block (Addition) of motor control B to the average speed value of 50
25 will be added and a power value of 75 will be supplied to motor B.
In the mathematics block (Subtraction) of motor C control, 25 will be subtracted from the average speed value of 50 and a power value of 25 will be supplied to motor C.
In this way, the deviation from the straight line will be corrected.

If the track turns sharply to the side, sensor 2 turns out to be white, and sensor 3 is black. The illumination values ​​of these sensors become: sensor 2 - 70 units, and sensor 3 - 50 units.
The “Subtraction” block will determine the difference between the values ​​of two sensors (20) and feed it to the correction block (20*2.5=50) and then to the motor control units.
Now in the mathematics (Addition) block of motor B control, a power value of 50 +50 =100 will be supplied to motor B.
In the mathematics block (Subtraction) of motor C control, a power value of 50 – 50 = 0 will be supplied to motor C.
And the robot will make a sharp turn.

On white and black fields, the robot must drive in a straight line. If this does not happen, try to select sensors with the same values.

Now let's create a new block and use it to move the robot along any route.
Select the cycle, then in the “Edit” menu select the “Create my block” command.

In the “Block Designer” dialog box, give our block a name, for example, “Go”, select an icon for the block and click “DONE”.

Now we have a block that can be used in cases where we need to move along a line.