Lego ev3 proportional integral differential regulator. International Robot Competition - Rules - Robot Examples - LEGO EV3 Trajectory Robot

Robotics is an interesting new direction, which, apparently, will further develop within the framework of school computer science and technology courses. The boom in robotics is largely due to the fact that it allows you to answer the question: "Why are we, in fact, learning programming?" In addition, in the course of robotics, you can get acquainted with the elementary concepts of the theory automatic control.

This page presents the 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 the capabilities of HTML5, 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
You can now load your tracks (robot fields) in LEGO Robot Simulators. How to do it? Look.
Added new simulators - 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 orient itself on the table surface, for example, to drive along the border between the white and black areas (along the edge of the black line). The photodiode illuminates the surface, the photodetector "catches" the reflected rays and measures their intensity.

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

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 slightly and spread apart. As for the tasks with one sensor, 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 tight turns. Internal sensors are used for fine adjustment, proportional control is used for them. Two external sensor brought forward slightly and spread apart. They are used when a sharp turn is encountered. The gain for control according to the readings of the sensors of the outer pair is selected more than for the inner 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 = encoder> 128 d1 = encoder> 128 d2 = encoder> 128 d3 = encoder> 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 a reflected signal. The longer the time between the transmitted and received signals, the greater the distance.

Using a distance sensor, a robot can be programmed to automatically traverse a labyrinth of known shape but unknown dimensions.

Proportional controller

Description

With automatic control, the control action u (t) is usually a function of the dynamic error - the deviation e (t) of the controlled variable x (t) from its specified value x0 (t):

e (t) = x0 (t) - x (t).

This is the Polzunov-Watt principle of deviation regulation, or the principle of feedback. The mathematical expression of the functional dependence of the desired control action u0 (t) on the values ​​measured by the controller is called the law or the control algorithm, which was mentioned above.

A proportional controller is a device that exerts a control effect on an object in proportion to its deviation from a given state:

Here k is the gain of the controller.

The given state x0 is usually called the setpoint, and the deviation e from it is called the residual. In what follows, for definiteness, we will denote the residual by the abbreviation err (from english word"Error" - error).

Motor control

An experienced warrior will not swing a sword, as a robot does on a relay controller. It is necessary to come up with an algorithm that will hold the motor holding the sword in a strictly fixed position (Fig. 7.1). The P-controller will help with this.

Let e ​​1 - the readings of the speed sensor 1 on the motor A - is the controlled value. The setting is x0 = 45, and the residual is e = 45 - e 1. Then the control action on the motor is given by the formula

u = k ∙ (45 - e 1).

Here k is the gain, for example 5, which will enhance the motor's response even with small deviations from the setpoint.

1 Do not confuse the mathematical designation of the residual e (from error) with the readings of the encoder e 1 (from the encoder), the predefined Robolab environment variable.

When you deviate in positive side negative control will be applied to the motor, and vice versa. This control can be applied to the motor in a loop with a small delay of 1-10 ms to unload the controller (Figure 7.8).

Rice. 7.8. Algorithm for motor control on a proportional controller.

If the gain factor is increased from 5 to 100, our proportional regulator will begin to work as a relay, causing large fluctuations due to the occurrence of an overshoot effect.

In the RobotC language, there is no such convenient notation for the encoder readings as in Robolab, so the program looks a little longer:

int k = 5, u; nMotorEncoder = 0; while (true)

u = k * (45-nMotorEncoder); motor = u;

Further, in order to strike with the sword, it is enough, having a variable instead of the number 45, to change its value from the outside, for example, from a parallel task. This is covered in the section on robotic drummers in Chapter 8.

Now let's build a regulator that controls not only the static position of the motor, but also the speed of its movement. Following the logic of the algorithm, the setpoint, which until now has been constant and has not changed, should start moving in the direction of increasing or decreasing. Obeying the regulator, the motor will inevitably follow her. The simplest tool for continuously increasing the setpoint value is the timer.

The NXT controller has four built-in timers, each of which can measure time in tenths, hundredths, and thousandths of a second. Let us master the first timer, which completes 10 “ti-

kov ". In Robolab it is denoted as T1 or Timer100ms1, and in RobotC it is timer100.

The angle alpha of the motor deflection, given in the previous example by the value of 45, will depend on the readings of the timer with an accelerating factor k 2:

alpha = k2 ∙ T1.

The control action will remain the same with the amplifying coefficient k 1:

u = k 1 ∙ (alpha - e 1).

Briefly, in a Robolab program, we will immediately apply the control action to the motor, having previously initialized the timer

Rice. 7.9. Motor speed control is one revolution per second.

The coefficient k 2 = 36 determines that in a second the value of alpha ramps up to 360, which corresponds to one full revolution of the engine:

int k1 = 2, k2 = 36, u, alpha; nMotorEncoder = 0; ClearTimer (T1); while (true)

alpha = timer100 * k2; u = k1 * (alpha-nMotorEncoder); motor = u;

Using integer division, which is accepted in the C language (and in Robolab) for variables of an integer type, it is possible to achieve a discrete change in the angle, i.e. incrementing it once per second:

alpha = T 1/10 ∙ k 2.

With the coefficient k 2 = 60, the movement of the beam will correspond to the movement of the second hand on the watch dial. But this is not enough

noticeably. For clarity, you can set k2 = 30, then the arrow will make a full revolution in 12 "ticks" of 30 degrees each. Be careful with the sequence of operations of integer division and multiplication, changing their order or "reducing" will certainly change the result (Fig. 7.10).

Rice. 7.10. Accelerated imitation of the movement of the clock hand.

And finally, an example of a math drummer. Instead of constantly moving forward, the arrow will oscillate back and forth under the control of the P-controller. The remainder division, which is denoted by the% sign in C, will help with this. The remainder of dividing a non-negative integer by 2 will always be 0 or 1:

alpha = T 1% 2 ∙ k 2.

Strengthening the deviation by a factor of k 2 = 15 times, we get a fluctuating setpoint alpha, which will force the regulator to move the motor 5 times per second, alternately at 0º, then at 15 degrees. The changes in the program are minor. Let's look at an example on RobotC:

int k1 = 3, k2 = 15, u, alpha; nMotorEncoder = 0; ClearTimer (T1); while (true)

alpha = timer100% 2 * k2; u = k1 * (alpha-nMotorEncoder); motor = u;

This prototype drummer hits the table at regular intervals. The main thing is to start in the right position. Using integer mathematics, you can set a more complex rhythmic pattern, for example (Table 7.1):

alpha = T 1% 5% 2 ∙ k 2.

center = S3.

The coefficient is determined in the cycle:

k 1 = c + (S 3 - center) / k 2.

Rice. 7.36. Line movement on a proportional floating-factor controller.

The obtained law of control of the gains can be applied not only to the proportional component, but also to any other, as well as to the control action as a whole (Fig. 7.36).

PID controller

The proportional-integral-derivative (PID) controller is one of the most popular and is used in huge amount devices of the most different types that require responsiveness and positioning accuracy. As the name suggests, this regulator consists of the sum of three components and is graphically depicted in Fig. 7.37.

Rice. 7.37. PID controller circuit.

This is a simplified diagram. The value of the dynamic error e (t) is fed to the controller input, and the control action u (t) is generated at the output:

u (t) = p + i + d = k p ∙ e (t) + k i ∙ ò t

e (τ) d τ + k d ∙

de.

The proportional component, shown in the diagram as a triangle, is responsible for positioning the system in a given state. In some cases, it can cause overshoot with subsequent self-oscillations. That is, the P-controller can "overdo it" and the robot will start to move from side to side.

The integral component accumulates negative experience (sums up errors) and develops a compensating effect. With minimal deviations, the proportional component "weakens" and the integral, due to its rapid increase by summation, helps to "pull" the controlled value to the setpoint.

The differential component (D-component) monitors the rate of change in the state of the system and prevents possible overshoot. In some cases, the D-component is opposite to the proportional one in sign, and in some it coincides.

We are already familiar with the proportional component, the differential is described in the previous chapter 6. Let's take the integral. This component is determined dynamically, adding up with the previous value:

i = i + ki × e (t) × dt.

The physical meaning of the quantity e (t) × dt is that it is

proportional to the length of time that the system is in an error state. Since the coefficient k i is taken out of the parentheses, we can talk about the value of i as the sum of the error durations. So we find the integral by summation.

Let's consider the application of the PID controller on the example of a robot balancing on two wheels. This classic problem can be solved with various sensors in a variety of ways. In the proposed example, a light sensor and the simplest form of a PID controller are used. However, to achieve stabilization of the robot, more accurate sensor readings will need to be used.

RAW format

Sensor data is sent to the NXT controller in a raw, raw form. All sensors transmit operating system a digital value from 0 to 1023, which is then processed by the appropriate driver and reduced to a more understandable form (distance 0 ... 255, illumination 0 ... 100, touch 0 or 1, etc.). But the data can also be received, bypassing the driver, directly. This raw format is commonly referred to as RAW (from the English "raw"). In some cases, it can be used to obtain greater accuracy. So, for example, the range of values ​​of the light sensor can increase by about 10 times. It is this opportunity that is used further.

Both Robolab and RobotC can receive RAW data. For this, the sensor is initialized accordingly, and data is read from it using a special predefined variable.

Balancing robot

The design of a segway robot is shown in Fig. 7.38: Vertically positioned controller, closely spaced wheels and a downward facing light sensor. The algorithm will be somewhat more complicated.

The principle of stabilizing the Segway in the equilibrium position is as follows. If the robot leans forward, the reading on the light sensor is increased by the reflected light. In response to this, a control action is generated, forcing the robot to go forward and thereby again assume an upright position.

When leaning back, the sensor readings decrease and the robot starts moving backward. The proportional component is responsible for all this. The overshoot insurance is assigned to the role of the integral and differential components.

Rice. 7.38. Balancing robot segway.

In fig. 7.39 shows the algorithm in Robolab. Most of it is occupied by the initialization of variables. To improve accuracy, not only data from the sensor is read in RAW format, but most of the variables are declared in real float format. The PID algorithm itself is in a loop.

Rice. 7.39. The balancer algorithm is based on a PID controller.

Following the tradition of moving along the line, we use the variable gray as the setpoint - the average readings of the light sensor in the equilibrium position. The new parameter scale sets the scaling of the control. This is essentially an attenuation factor as the value produced by the regulator is too high for NXT motors. It would be possible to add it inside the existing coefficients, but for RobotC this parameter will be different, and the coefficients are the same.

With the given coefficients, the robot stabilizes well on plain light linoleum or school desk. That is, he does not need White color surface. To start, you need to accurately set the segway to the balance position. If the robot starts with some forward or backward tilt, it will immediately start moving in the direction of the tilt.

A similar example on RobotC is slightly different for a number of reasons. Firstly, the performance of NXT with the firmware of this environment is about 1.4 times higher than that of Robolab, so the scale factor should be increased. Secondly, the RAW values ​​are transmitted in the correct order and you will need to set the reverse of the motors or simply apply a negative control action:

int gray = SensorRaw; int err, errold = 0;

float kp = 25, ki = 350, kd = 0.3; float scale = 14;

float dt = 0.001; float p, i = 0, d, u; while (true)

err = gray-SensorRaw; // Deviation with opposite sign p = kp * err;

i = i + ki * err * dt; d = kd * (err-errold) / dt; errold = err; u = (p + i + d) / scale; motor = u; motor = u; wait1Msec (1);

Elements of the theory of automatic control in school1

An important and interesting methodological task is the "transfer of a bridge" between the areas of knowledge of a specialist and a student, helping school students to see the perspective of their future specialty, i.e. provide career guidance, and students see the practical applicability of their professional knowledge... To achieve a similar effect, methods for calculating regulators were developed using a mathematical apparatus that does not go beyond school curricula in mathematics and physics. In particular, instead of differential equations, difference equations are used that correspond well to the discrete nature of the interaction between the object and the controller under computer control.

Consider, for example, the problem of constructing proportional (P) and proportional-differential (PD) controllers in the problem of controlling the movement of a mobile robot along a wall. Let us denote by x t the distance between the robot and the wall, through θt - the heading angle of the robot, and through u t - the control action at the moment with the ordinal number t, respectively, where t = 0, 1, 2, ... are the numbers of the moments of measurements.

rhenium. It is believed that the polling of the sensors and the change in the magnitude of the control action is performed at regular intervals h. For the tasks of controlling Lego NXT robots, it is natural to assume that the control action is the difference in the angular velocities of the wheels, proportional to the rate of change of the heading angle:

Assuming that the course deviations from the nominal θt = 0 are small, and the average speed of the robot is constant: vt = v, the dynamics of changes in the state variables of the robot in the first approximation can be described linear equations states:

where g = h2vr / b.

Let us set the desired distance to the wall x *> 0 and define the control goal (CC) by the ratio

xt → x * as t → ∞.

Now we naturally introduce at the meaningful level the concept of asymptotic stability as a property of solutions to system (4) that ensures the achievement of control system (5) for any initial conditions that differ little from the target ones. It is easy to see that for u t = 0 the solution to equation (4) is any constant value x t = x *. But since equation (4), corresponding to the model of a double integrator (double adder), does not possess the property of asymptotic stability, control equation (5) for permanent management is not achieved. This is easily demonstrated both analytically - by summing the series

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

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.

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

General theory and specific examples the creation of the program is 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 value of the “ideal point” located on the border of black and white.

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

The ideal calculation option is to measure the value of black and white and take the arithmetic mean.

This can be done manually. But the disadvantages are visible immediately: within even a short time, the illumination may change, and the calculated value will turn out to be incorrect.

This means that you can make the robot do it.

Through our 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.

Ideal point calculation

The next parameter involved in the movement is the steering ratio. The larger it is, the sharper the robot reacts to changes in illumination. But too great importance 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 movement of the robot. An increase in the speed of movement leads to an increase in the response time of the robot to changes in illumination, which can lead to a departure from the trajectory. The value is also selected experimentally.

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

Steering Ratio and Base Power

The logic of movement 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 the form of formulas, 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 as a separate block.

Algorithm implementation

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

A proportional controller is a device that exerts a control effect 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 at a given time;

u (t) = ke (t), where k is a gain factor.
That is, the further the robot deviates from the given course, the more actively the motors should work, aligning it.

Driving along a line with one light sensor using a P-controller

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

This is what will help to build a P-controller.
By defining the state of work as the readings of the light sensor, we will learn to provide a proportional control effect on the motors according to the following law:
e = s1-gray, where s1 is the current sensor reading and gray is the target value.

The coefficient k (which is 2 in this example) must be small enough (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 on the left side of the black line. It is easy to see that the movement along the line on the P-controller is smooth. and in some areas of work moves almost in a straight line or exactly following the bends of the line.

Calibrating the sensor

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

Driving along a line with two light sensors using a P-controller
Correctly driving through an intersection with one light sensor is quite difficult. 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 states of the sensor are possible:

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

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.
An 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 a robot is driving along a black line on a white field, then it must be calibrated on a white one. Those. the position of the robot at the start should be like this:


And one more remark. 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 be different, which will lead to unexpected consequences.