International robot competitions - Rules - Examples of robots - Trajectory robot based on LEGO EV3. International robot competitions - Rules - Examples of robots - Trajectory robot based on LEGO EV3 Line driving pid controller ev3

Robotics is an exciting new area that is likely to be further developed in school computer science and technology courses. The boom in robotics is largely due to the fact that it allows us to answer the question: “Why do we actually learn programming?” In addition, in the course of robotics you can get acquainted with the elementary concepts of the theory of automatic control.

This page presents programming simulators and Arduino boards developed by the author. They can help in cases where for some reason it is not possible to use real hardware.

The simulators use HTML5 capabilities, so they will only work in modern browsers (it is best to use Google Chrome or Mozilla Firefox).

News now also in the Telegram channel

November 27, 2015
The “embryo” track has been added to the simulators ( M.V. Lazarev, Orekhovo-Zuevo).

October 13, 2015
Now in the LEGO robot simulators you can load your own tracks (fields for the robot). How to do this? See.
New simulators have been added - LEGO robots with two, three, four light sensors.

Robot control language

To control robots in simulators, a simple programming language is used, which received the working name SiRoP (Simple Robot Programming).

Robot control with light sensor

The light sensor allows the robot to navigate on the table surface, for example, to move along the border between white and black areas (along the edge black line). A photodiode illuminates the surface, a photodetector “catches” the reflected rays and measures their intensity.

The most popular task of this type is moving along a line. Using the simulator, you can study various control laws - relay, proportional, and even PID control (proportional-integral-derivative).

Examples of programs for a robot with a light sensor

While 1 (if sensor > 128 (motor = 100 motor = 0) otherwise (motor = 0 motor = 100) wait(10))

KP = 0.2 while 1 ( u = kP*(sensor-128) motor = 50 + u motor = 50 - u wait(20) )

Main ( while 1 ( while sensor > 128 ( motor = 100 motor = 100 wait(10) ) back() turn() ) ) back ( motor = -100 motor = -100 wait(260) ) turn ( motor = -50 motor = 50 wait(50) )

Robot control with two light sensors

Two light sensors allow the robot to better navigate and drive along a thin line. They are brought forward a little and spread to the sides. As with single-sensor problems, this simulator can be used to study various control laws.

Examples of programs for a robot with three light sensors

Robot control with four light sensors

Four light sensors allow the robot to better detect sharp turns. Internal sensors are used for fine adjustment; proportional control is used for them. Two external sensors are placed slightly forward and apart. They are used when a sharp turn is encountered. The gain for control based on the readings of the sensors of the external pair is selected greater than for the internal pair (see. L.Yu. Ovsyanitskaya et al., Algorithms and programs for the movement of the Lego Mindstorms EV3 robot along the line, M.: “Pero”, 2015).

Examples of programs for a robot with four light sensors

While 1 ( d0 = sensor > 128 d1 = sensor > 128 d2 = sensor > 128 d3 = sensor > 128 if d1 & !d2 ( motor = 100 motor = 0 ) if! d1 & d2 ( motor = 0 motor = 100 ) if d1 == d2 ( motor = 100 motor = 100 ) if d0 & !d3 ( motor = 30 motor = 0 ) if!d0 & d3 ( motor = 0 motor = 30 ) wait(10) )

K1 = 0.2 k2 = 0.4 while 1 ( u1 = sensor - sensor u2 = sensor - sensor motor = 50+k1*u1+k2*u2 motor = 50-k1*u1-k2*u2 wait(10) )

Controlling a robot with a distance sensor (sonar)

The distance sensor (sonar) allows you to determine the distance to the nearest obstacle while the robot is moving. It emits an ultrasonic signal and receives the reflected signal. The longer the time between the emitted and received signals, the greater the distance.

Using a distance sensor, the robot can be programmed to automatically navigate a maze of known shape but unknown size.

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 educational robot Robot Educator, for this you can use the instructions in software MINDSTORMS EV3.

Also, for examples, we will need EV3 light-color sensors. These light sensors are like no other in the best possible way suitable 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 border of the 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.

One of the basic movements in light construction is following the black line.

The general theory and specific examples of creating a program are described on the website wroboto.ru

I will describe how we implement this in the EV3 environment, since there are differences.

The first thing the robot needs to know is the meaning of the “ideal point” located on the border of black and white.

The location of the red dot in the figure corresponds exactly to this position.

The ideal calculation option is to measure the black and white values ​​and take the arithmetic average.

You can do this manually. But the disadvantages are immediately visible: over even a short period of time, the illumination may change, and the calculated value will be incorrect.

So, you can get a robot to do it.

During the experiments, we found out that it is not necessary to measure both black and white. Only white can be measured. And the ideal point value is calculated as the white value divided by 1.2 (1.15), depending on the width of the black line and the speed of the robot.

The calculated value must be written to a variable in order to access it later.

Calculation of the “ideal point”

The next parameter involved in movement is the rotation coefficient. The larger it is, the more sharply the robot reacts to changes in illumination. But too much great value will cause the robot to wobble. The value is selected experimentally individually for each robot design.

The last parameter is the base power of the motors. It affects the speed of the robot. An increase in movement speed leads to an increase in the robot's response time to changes in illumination, which can lead to departure from the trajectory. The value is also selected experimentally.

For convenience, these parameters can also be written into variables.

Turn Ratio and Base Power

The logic of moving along the black line is as follows: the deviation from the ideal point is measured. The larger it is, the stronger the robot should strive to return to it.

To do this, we calculate two numbers - the power value of each of the motors B and C separately.

In formula form it looks like this:

Where Isens is the value of the light sensor readings.

Finally, the implementation in EV3. It is most convenient to arrange it in the form of a separate block.

Implementation of the algorithm

This is exactly the algorithm that was implemented in the robot for the middle category of WRO 2015

Details Author: Konovalov Igor     The proportional controller is an improvement. The main disadvantage of the relay is that it does not care how the current values ​​differ from the normal value of the sensor. It has only two states - either try to increase the sensor values ​​by a certain constant number if they are less than the normal value, or increase it. Because of this, oscillations occur with a constant amplitude, which is very inefficient.
    It is much more logical to determine how “far” the current readings are from normal, and change the amplitude depending on this. To make it more clear, let's look at an example. The example, as in the previous article, is the same: a robot from Lego Mindstorms EV3 drives along a black line using one color sensor in light mode.

The robot tries to drive along the border between white and black, and there the sensor shows approximately 50% of the illumination. And the further it is from the normal position, the more effort the robot makes to return to 50%.
    To write a program, we will use the terms “error” and “control action”. Error is the difference between the current sensor reading and the normal one. In our case, if the robot now sees 20% of the illumination, then the error is 20-50 = -30%. The error sign indicates which direction the robot should turn to get rid of the error. Now we must tell the motors which direction the robot should turn, at what speed and how sharply. It is necessary to exert a control effect on the motors, which means how quickly it should return to its normal position. The control action (UP) is calculated as the error (error) multiplied by the proportionality factor (k). This coefficient is used to enhance or reduce the influence of the error on the control action. The control action is supplied to steering, where the average speed of the robot is set.
    How to adjust the proportionality factor? Select the values ​​experimentally; for traveling the trajectory it can be, for example, from 0.2 to 1.5, depending on the speed and design of the robot. If the coefficient is too large, then the robot will wobble a lot; if it is small, it will drive smoothly, but at some point it will slide off when turning due to insufficient control input. Let's write two versions of the program - with variables (for those who have already studied them) and without.


    But this regulator can also be strengthened by introducing a proportional and integral component; the description will be in the following articles. See you soon!

A proportional controller is a device that exerts a control action u(t) on an object in proportion to its linear deviation e(t) from a given state x0(t);

e(t)=x0(t)-x(t), where x(t) is the state in at the moment time;

u(t)=ke(t), where k is the amplification factor.
That is, the further the robot deviates from the given course, the more actively the motors must work to level it.

Line movement with one light sensor using a P-controller

Movement along the border of black and white can also be built on the P-regulator. Although outwardly the problem seems to be solvable only with the help of a relay controller, since the system has only two states visible to the human eye: black and white. But the robot sees everything differently; for it there is no sharp boundary between these colors. We can say that he is nearsighted and sees a gradient transition of shades of gray.

This is what will help you build a P-regulator.
Defining the work state as readings from the light sensor, we will learn to exert a proportional control effect on the motors according to the following law:
e=s1-grey, where s1 is the current sensor readings, and gray is the set value.

The coefficient k (equal to 2 in this example) should be quite small (from 1 to 3). Such a regulator works effectively only for small deflection angles, so the robot must be placed in the direction of movement so that the sensor is aligned left side from the black line. It is easy to notice that the movement along the line on the P-regulator is smooth. and in some areas of work it moves almost linearly or exactly following the bends of the line.

Sensor calibration

Let's look at the number 48 used in the formula. This is the arithmetic average of the light sensor reading on black and white, for example (40+56)/2=48. However, sensor readings often change for various reasons: a different surface, a change in the overall illumination in the room, a slight modification of the design, etc. Therefore, we will calibrate the robot manually by determining the light sensor readings on white and black.

Line movement with two light sensors using a P-controller
It is quite difficult to navigate an intersection correctly with one light sensor. If you want to do this at a high enough speed, you need at least two sensors placed at a distance of two line widths (or wider).
When driving, four sensor states are possible:

  • both on white - moving straight;
  • left (s1) not on black, right (s2) on white - movement to the left;
  • left on white, right on black - movement to the right;
  • both on black - straight ahead.
That. with equal sensor readings (both white or both black), the robot drives straight. Before starting the robot, we will auto-calibrate both sensors. Then the algorithm for moving along a line with a P-regulator will look like this:

The coefficient k can vary in a fairly wide range (from 1 to 20 or more) depending on the curvature of the line, the maneuverability of the robot and the difference between black and white on the field.
Important condition. Auto-calibration should be carried out on a single-color surface and preferably at the illumination that will occupy the largest part of the path. For example, if the robot moves along a black line on a white field, then it must be calibrated on a white field. Those. The position of the robot at start should be like this:


And one more note. There are sensors whose readings differ by 10-20%. It is advisable not to pair them with a regulator with a large coefficient, since with a sharp change in the overall illumination, even on a uniform white field, the deviations may turn out to be different, which will lead to unexpected consequences.