Pid controller ev3 movement along the line. International robot competitions - Rules - Examples of robots - Trajectory robot based on LEGO EV3. Robot control with four light sensors

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.

Proportional controller

Description

In 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 set value x0(t):

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

This is the Polzunov-Watt principle of regulation by deviation, or the feedback principle. The mathematical expression of the functional dependence of the desired control action u0(t) on the quantities measured by the controller is called the law or control algorithm discussed 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 controller gain.

The given state x0 is usually called the setpoint, and the deviation from it e 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 like a robot on a relay controller does. We need to come up with an algorithm that will hold the motor holding the sword in a strictly fixed position (Fig. 7.1). The P-regulator will help with this.

Let e ​​1 - the readings of the speed sensor 1 on motor A - be an adjustable variable. The setting x0 = 45, and the residual 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 factor, for example 5, which will enhance the motor 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 encoder), a predefined Robolab environment variable.

If there is a deviation in positive side a negative control input will be applied to the motor, and vice versa. This control can be applied to the motor in a cycle with a short delay of 1-10 ms to relieve the controller (Fig. 7.8).

Rice. 7.8. Motor control algorithm using a proportional controller.

If the gain factor is increased from 5 to 100, our proportional controller will begin to act like a relay, causing strong fluctuations due to the overshoot effect.

The RobotC language does not have such a convenient notation for 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 deliver a “strike with a sword”, it is enough to have a variable instead of the number 45 and change its value from the outside, for example, from a parallel task. This is covered in the section on robot 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 a constant and has not changed, should begin to move towards an increase or decrease. Obeying the regulator, the motor will inevitably follow it. The simplest tool for continuously incrementing the setpoint value is a timer.

The NXT controller has four built-in timers, each of which can measure time in tenths, hundredths and thousandths of a second. Let’s master the first timer, which performs 10 “tips” per second.

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

Let us make the motor deflection angle alpha, specified in the previous example at 45, dependent on the readings of the timer with the accelerating coefficient k 2:

alpha = k2 ∙ T1.

Control action will remain the same with amplification factor k 1:

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

Briefly, in the program in the Robolab language, we will apply the control action directly 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 alpha value increases 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, as adopted in C (and Robolab) for variables of integer type, it is possible to achieve discrete angle changes, i.e. increments it once per second:

alpha = T 1 / 10 ∙ k 2.

With a coefficient k 2 = 60, the movement of the beam will correspond to the movement of the second hand on the watch dial. But that's not enough

noticeable. 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 integer division and multiplication operations; changing their order or “reducing” them will certainly change the result (Fig. 7.10).

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

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

alpha = T 1% 2 ∙ k 2.

Increasing the deviation by k 2 = 15 times, we get an oscillating setpoint alpha, which will force the controller to move the motor 5 times per second, either 0º or 15 degrees. Changes in the program are minor. Let's look at an example in 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. Movement along the line on a proportional controller with a floating coefficient.

The resulting gain control law can be applied not only to the proportional component, but also to any other component, 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 a huge number of devices of the most different types, which require fast response and positioning accuracy of the system. 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 supplied 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 may cause overshoot with subsequent self-oscillations. That is, the P-regulator may “overdo it” and the robot will start to drift from side to side.

The integral component accumulates negative experience (sums up errors) and produces a compensating effect. With minimal deviations, the proportional component “weakens” and the integral component, due to its rapid increase by summation, helps to “reach” the controlled value to the set point.

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 in sign to the proportional one, and in others it is the same.

We are already familiar with the proportional component, the differential one is described in the previous chapter 6. Let's take on the integral one. This component is determined dynamically, summed 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 duration of the system being in an error state. Since the coefficient k i is taken out of brackets, we can talk about the value i as the sum of error durations. Thus, we find the integral by summation.

Let's consider the use of a PID controller using the example of a robot balancing on two wheels. This classic problem can be solved using different sensors in a variety of ways. In the proposed example, a light sensor is used and simplest form PID controller. However, achieving robot stabilization will require using more accurate sensor readings.

RAW format

Sensor data enters the NXT controller in unprocessed, raw form. All sensors transmit operating system a digital value from 0 to 1023, which is then processed by the corresponding driver and reduced to a more understandable form (distance 0...255, illumination 0...100, touch 0 or 1, etc.). But data can be received directly, bypassing the driver. This raw format is usually called RAW (from English “raw”). In some cases, it can be used to achieve greater accuracy. For example, the range of light sensor values ​​can increase by approximately 10 times. It is this opportunity that is used further.

You can receive data in RAW format in both Robolab and RobotC. To do this, the sensor is initialized accordingly, and data is read from it using a special predefined variable.

Balancing robot

The design of the Segway robot is shown in Fig. 7.38: vertically positioned controller, closely placed wheels and downward-facing light sensor. The algorithm will be somewhat more complicated.

The principle of stabilizing a Segway in a balanced position is as follows. If the robot leans forward, the reading on the light sensor increases due to the reflected light. In response to this, a control action is generated, forcing the robot to move forward and thereby again assume a vertical position.

When moving backwards, the sensor readings decrease and the robot begins to move backwards. The proportional component is responsible for all this. The role of the integral and differential components is played by insurance against overshoot.

Rice. 7.38. Balancing Segway robot.

In Fig. 7.39 shows the algorithm in Robolab. Most of it is occupied by the initialization of variables. To improve accuracy, not only is the sensor data read in RAW format, but most 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 gray variable as a setpoint - the average readings of the light sensor in the equilibrium position. The new scale parameter specifies the scaling of the control action. This is essentially an attenuation factor because 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, but the coefficients will be the same.

With the given coefficients, the robot stabilizes well on a plain, light-colored linoleum or desk. That is, he does not need white surfaces. To launch, you need to accurately set the Segway to the equilibrium position. If the robot starts with some tilt forward or backward, it will immediately begin moving in the direction of the tilt.

A similar example in RobotC is slightly different for a number of reasons. Firstly, the performance of NXT with the firmware of this environment is approximately 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 motors to reverse 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= grey-SensorRaw; //Deviation with the opposite sign p=kp*err;

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

Elements of the theory automatic control at school1

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

Let us consider, for example, the problem of constructing proportional (P) and proportional-derivative (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, by θt - the heading angle of the robot, and by u t - the control action at the moment with serial number t, respectively, where t = 0, 1, 2, ... - numbers of moments of change

rhenium. It is believed that the polling of sensors and changes in the magnitude of the control action are carried out at equal intervals of time h. For control tasks of Lego NXT robots, it is natural to assume that the control action is the difference angular velocities wheel rotation, proportional to the rate of change of heading angle:

Assuming that 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 can be described in a first approximation by linear equations of state:

where g = h2vr / b.

Let's set the desired distance to the wall x*> 0 and determine the control goal (CT) by the relation

xt → x* as t→∞.

Now naturally Let us introduce at the substantive level the concept of asymptotic stability as a property of solutions to system (4), ensuring the achievement of the target value (5) under any initial conditions that differ quite 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 double integrator (double adder) model, does not have the property of asymptotic stability, control center (5) is not achieved with constant control. This is easily demonstrated analytically by summing up a number of

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. Increasing the speed of movement 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 middle category 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 input is sent to the 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!