Driving along the ev3 line with one sensor. Advanced line tracking algorithm

In order to make the robot move smoothly along the black line, you need to force it to calculate the speed of movement itself.

A person sees a black line and its clear border. The light sensor works a little differently.

It is this property of the light sensor - the inability to clearly distinguish between the border of white and black - and we will use it to calculate the speed of movement.

First, we introduce the concept of “Ideal trajectory point”.

The light sensor readings range from 20 to 80, most often on white the readings are about 65, on black about 40.

Ideal point is a conditional point approximately in the middle of white and black colors, following which the robot will move along the black line.

Here, in principle, the location of the point is between white and black. It will not be possible to ask it exactly on white or black for mathematical reasons, why - it will be clear later.

Empirically, we have calculated that the ideal point can be calculated using the following formula:

The robot must move strictly along the ideal point. If a deviation occurs in any direction, the robot must return to this point.

Let's compose mathematical description of the problem.

Initial data.

Perfect point.

The current reading of the light sensor.

Result.

Motor rotation power V.

Motor rotation power C.

Solution.

Let's consider two situations. First: the robot has deviated from the black line towards the white one.

In this case, the robot must increase the rotational power of motor B and decrease the power of motor C.

In a situation where the robot enters the black line, the opposite is true.

The more the robot deviates from the ideal point, the faster it needs to return to it.

But the creation of such a regulator is a rather difficult task, and it is not always required as a whole.

Therefore, we decided to limit ourselves only to the P-controller, which adequately responds to deviations from the black line.

In the language of mathematics, it will be written like this:

where Hb and Hc are the final powers of motors B and C, respectively,

Hbase - a certain basic power of the motors, which determines the speed of the robot. It is selected experimentally, depending on the design of the robot and the sharpness of the turns.

Itek - current readings of the light sensor.

I id - calculated ideal point.

k - coefficient of proportionality, is selected experimentally.

In the third part, we will look at how to program this in the NXT-G environment.

This is how a person sees the line:

This is how the robot sees her:


It is this feature that we will use when designing and programming a robot for the "Trajectory" competition category.

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

I want to tell you about a programming method that even children of 2-3 grades will 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 for any route of the track in 15-30 minutes (taking into account the step-by-step verification and adjustment of some of the trajectory features).

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. In the same place, I became convinced that this topic is very relevant for many teams.

Well, let's get started.

In preparation for this type of competition, programming is only part of the solution to the problem. You need to start by designing a robot for a specific track. In the next article, I'll show you how to do this. Well, since movement along a line is very common, I'll start with programming.

Let's consider a variant of a robot with two light sensors, since it is more understandable for primary school students.

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

The program will consist of a small number of blocks:


1. Two blocks of light sensor;
2. Four blocks of "Mathematics";
3. Two blocks of motors.

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

Now let's figure out how to connect all the blocks, set up the program and what will happen in it.
Let's set up two light sensors and assign them ports 2 and 3.
Take the math block and choose Subtraction.
Let's connect the light sensors from the "Intensity" outputs with buses to the mathematics block to the "A" and "B" inputs.
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 subtracting, 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 ratio, you need to measure the "white" and "black" levels with the NXT unit.
Suppose white is -70, black is -50.
Then we count: 70-50 = 20 (the difference between white and black), 50/20 = 2.5 (we set the average value of the power when moving in a straight line in the math blocks at 50. This value plus the added power when correcting the movement should be equal to 100)
Try to set the value to 2.5 at the "A" input, and then select it more accurately.
Connect the "Result" output of the previous "Subtraction" math block to the "B" input of the "Multiplication" math block.
Next comes a pair - a block of mathematics (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 the motor B.
Next, steam is a block of mathematics (Subtraction) and motor C.
Setting up a math block:
Input "A" is set to 50.
Input "B" is connected by a bus with the output "Result" of the "Multiplication" math block.
The output of the "Result" block is connected by a bus to the "Power" input of the motor C.

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

Since all this will work in a loop, we add a “Loop”, select it and transfer it all to a “Loop”.

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


While the robot is traveling in a straight line, the sensor values ​​coincide, which means that the output of the Subtraction block will have a value of 0. The output of the Multiplication block also gives the value 0. This value is fed 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 run at the same power of 50, and the robot rolls in a straight line.

Suppose the track makes a turn or the robot deviates from the 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, since it moves to a white field, and the illumination of sensor 3 decreases. Suppose 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 the two sensors (10) and submit it to the correction block (multiplication by a factor (10 * 2.5 = 25)) and then to the control units
motors.
In the block of mathematics (Addition) control of motor B to the value of the average speed 50
25 will be added and the power value 75 will be supplied to motor B.
In the math block (Subtraction) of motor C control, 25 will be subtracted from the average speed value 50 and the power value 25 will be supplied to motor C.
Thus, the deviation from the straight line will be corrected.

If the track turns sharply to the side and sensor 2 is on white, and sensor 3 is on 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 the two sensors (20) and submit it to the correction block (20 * 2.5 = 50) and then to the motor control units.
Now, in the mathematics block (Addition) of motor B control, the power value 50 +50 = 100 will be fed to motor B.
In the mathematics block (Subtraction) of motor C control, the power value 50 - 50 = 0 will be fed 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 matching sensors with the same values.

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

In the "Block Constructor" dialog box, give a name to our block, for example, "Go", select an icon for the block and click "DONE".

We now have a block that can be used in cases where we need line movement.

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

There are many approaches to solving 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 to each other.

In our example, we will analyze three examples of a robot based on the basic training model Robot Educator.

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

Also, for examples we need EV3 light-color sensors. These light sensors, like no others, are the best suited for our task, when working with them, we do not have to worry about the intensity of the ambient light. For this sensor, in the programs we will use the reflected light mode, in which the amount of reflected light of 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.

For 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 controller.

· Two sensors.

Example 1. One sensor, with a P controller.

Design

The light sensor is mounted 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 backlight beam with a black line, the readings returned by the sensor vary with a gradient. The robot keeps the position of the light sensor on the border of the black line. By converting the input data from the light sensor, the control system generates a value for the rotation speed of the robot.


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

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

An example of how the algorithm works

Example 2. One sensor, with a PC controller.

This example builds on the same construction.

You may have 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 also add a simple cube controller that will add flex to the function of the controller. This will reduce the rocking of the robot near the desired boundary of the trajectory, as well as make stronger jerks at a great distance from it.

The text of the work is placed without images and formulas.
The full version of the work is available in the "Work files" tab in PDF format

Lego Mindstorms EV3 construction set

Preparatory stage

Creating and calibrating a program

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.

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

Mindstorms EV3 Constructor invites us to enter the fascinating world of robots, plunge into the complex environment of information technology.

Objective: Learn to program the movement of the robot in a straight line.

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

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

    Mindstorms EV3 Constructor.

Construction set parts - 601 pcs., Servo motor - 3 pcs., Color sensor, touch motion sensor, infrared sensor and touch sensor. The EV3 microprocessor block is the brain of the LEGO Mindstorms constructor.

A large servo motor is responsible for the movement of the robot, which is connected to the EV3 microcomputer and makes the robot move: go back and forth, turn and drive along a given trajectory. This servo motor has a built-in rotation sensor, which allows very precise control of the movement of the robot and its speed.

You can use the EV3 computer software to make your robot perform an action. The program consists of various control units. We will work with a motion block.

The movement block controls the motors of the robot, turns it on, turns it off, makes it work according to the tasks. You can program the movement for a certain number of revolutions, or degrees.

    Preparatory stage.

    Creation of a technical field.

On the field of work of the robot, we will apply a marking, using electrical tape and a ruler, create three lines 30 cm long - a green line, 1 m 15 cm - a red line and 2 m 17 cm - a 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 circumference with a diameter of 5.7 cm.The circumference is found by 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. in one wheel revolution, 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 according to the following algorithm:

Algorithm:

    Select motion block in Mindstorms EV3 software.

    Switch on both motors in the specified direction.

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

    Switch off motors.

We load the finished program into the robot's control unit. We put the robot on the field and press the start button. The EV3 drives across the field and stops at the end of the set line. But in order to achieve an accurate finish, it is necessary to calibrate, since external factors influence the movement.

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

    The surface of the field is smooth, therefore, poor adhesion of the wheels of the robot to the field is possible.

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

5. Conclusion.

Learning how to program the robot to move in a straight line will come in handy for creating more complex programs. As a rule, all movement sizes are indicated in the terms of reference for robotics competitions. They are necessary so that the program is not overloaded by logic conditions, cycles and other complex control blocks.

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

It is very interesting to work with the constructor. 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.

    D. Koposov "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 /

15.01.2012, 18:51

Until now, in articles about algorithms used when moving along a line, a method was considered when the light sensor seemed to follow its left or right border: as soon as the robot moves to the white part of the field, the controller returned the robot to the border, the sensor starts moving deeper into the black lines - the regulator straightened it back.
Despite the fact that the picture above is for a relay controller, the general principle of movement of the proportional (P-controller) will be the same. As already mentioned, the average speed of such movement is not very high and several attempts were made to increase it due to a slight complication of the algorithm: in one case, "soft" braking was used, in the other, in addition to turns, forward movement was introduced.
In order to allow the robot to move forward in some areas, a narrow area was allocated in the range of values ​​given by the light sensor, which could be conventionally called "the sensor is on the line border".
This approach has a small drawback - if the robot "follows" the left border of the line, then on the right turns it does not immediately detect the curvature of the trajectory and, as a result, spends more time searching for the line and turning. Moreover, it is safe to say that the steeper the turn, the longer this search takes place.
The next figure shows that if the sensor was not on the left side of the border, but on the right, then it has already detected the curvature of the trajectory and would begin to make turn maneuvers.

Therefore, it is a good idea to equip the robot with two sensors at once, which were located on opposite sides of the line and, accordingly, would help the robot more quickly respond to changes in the direction of movement.
The next step is to determine how this design change will affect the program. For simplicity, you should again start with the simplest relay controller and therefore, first of all, you are interested in the possible positions of the sensors relative to the line:

In fact, one more permissible state can be distinguished - on difficult routes it will be the intersection of an intersection or some kind of thickening on the way.
Other positions of the sensors will not be considered, because either they are derived from those shown above, or these are the positions of the robot when it left the line and will no longer be able to return itself to it using information from the sensors. As a result, all of the above provisions can be reduced to the following classification:
  • the left sensor, as well as the right one - above the light surface
  • left sensor above a light surface, right sensor above a dark surface
  • left sensor above dark surface, right sensor above light
  • both sensors are located above a dark surface
If at a certain moment in time the program on the robot detects one of these positions, it will have to react accordingly:
    If both sensors are above the white surface, then this is a normal situation in which the line is between the sensors, so the robot should go straight. If the left sensor is still above the light surface, and the right sensor is already above the dark surface, then the robot has driven its right side onto the line. and that means he needs to turn to the right so that the line is again between the sensors. If the left sensor is above the dark surface, and the right one is still above the light one, then to align the robot needs to turn left. If both sensors are above the dark surface, then in general, the robot is again continues to move straight.

The diagram above immediately shows how exactly the behavior of the motors should change in the program. Now, writing the program should not be difficult. You should start by choosing which sensor will be polled first. It doesn't really matter, so let's keep it on the left. It is necessary to determine whether it is above a light or above a dark surface:
This action does not yet allow you to tell which direction the robot should go. But it will divide the states listed above into two groups: (I, II) for the upper branch and (III, IV) for the lower one. Each of the groups now has two states, so you need to select one of them. If you look closely at the first two states I and II, they differ in the position of the right sensor - in one case it is above a light surface, in the other - above a dark one. This is what will determine the choice of what action to take:
Now you can insert blocks that define the behavior of the motors according to the tables above: the upper branch of the nested condition defines the combination "both sensors on light", the top one - "left on light, right on dark":
The lower branch of the main condition is responsible for another group of states III and IV. These two conditions also differ from each other in the level of illumination that the right sensor picks up. Hence, it will determine the choice of each of them:
The resulting two branches are filled with movement blocks. The upper branch is responsible for the "left on dark, right on light" state, and the bottom one - for "both sensors on dark".
It should be noted that this design only determines how to turn on the motors depending on the readings of the sensors in a certain place in the field, naturally after a moment the program should check whether the readings have changed in order to correct the behavior of the motors, and after a moment again, again, and so on. .d. Therefore, it must be placed in a loop that will provide this iterative check:

Such a fairly simple program will provide a fairly high speed of movement of the robot along the line without flying out of its limits, if you correctly set the maximum speed when driving in states I and IV, and also set the optimal method of braking in states II and III - the steeper the turns on the track , the "harder" the braking should be - the speed should drop faster, and vice versa - with smooth turns it is quite possible to apply braking through the power off or even generally through a slight drop in speed.

A few separate words should also be said about the placement of the sensors on the robot. Obviously, for the location of these two sensors relative to the wheels, the same recommendations will apply as for one sensor, only for the apex of the triangle, in this case, the middle of the segment connecting the two sensors is taken. The very same distance between the sensors should also be selected from the characteristics of the track: the closer the sensors are located to each other, the more often the robot will align (perform relatively slow turns), but if the sensors are spread wide enough, there is a risk of flying off the track, so you will have to perform tighter turns and slower travel speeds on straight sections.