Today we are going to become familiar with a
pid-controller node while working in the XOD platform. A proportional–integral–derivative controller or PID-controller is a control loop feedback mechanism used in automatic control systems to maintain the value of the measured parameter. We won’t go into the details of how this mechanism works or analyze its math functions. Instead, we will try to understand the usage of a PID-controller in the XOD visual programming environment.
You can always learn more about a PID-controller device from other sources, such as Wikipedia.
PID-controllers are mostly implemented in a variety of applications such as electric heating and cooling systems. They are famous for their accuracy and efficiency so you might even find some of them at home. Do not rush taking apart your refrigerator because we have an mBot platform by Makeblock to program a PID-controller as an example.
There is an mBot in the picture above. It has an mCore board installed with the “Me ultrasonic sensor” connected to the second RJ25 port and a couple of motors connected to M1 and M2 ports. The mCore board is specially designed for the mBot. It is based on Arduino Uno and so is XOD compatible.
Let’s use XOD to program a simple follower bot which will keep a specified distance to an object in front of it using the ultrasonic sensor. XOD`s
pid-controller core node will help us to make this recognition clear.
First of all, mBot is a moving bot, so we need to place an
mbot-motor node on the patch.
This node is used to control motors and can be found within the
gabbapeople/mbot-lib library, which was created to make mBot programming even simpler. Follow the path
File > Add library > gabbapeople/mbot-lib in the menu to add it to your project
mbot-motor consists of two input pins
M2 according to relevant motors connected to M1 and M2 ports on the mCore board. Zero value on a pin means that the motor is not moving. A positive value makes the wheel move backward while negative makes it move forward. So if you want mBot to turn around you can simply set
M2 values with opposite signs.
In the next step, we place the
mbot-ultrasonic-sensor node. This node is used for Makeblock’s ultrasonic sensor. It calculates the distance to an obstacle and outputs the result through the
DIST pin in meters.
PORT input pin value equals the RJ25 number of the port to which the sensor is connected.
“Me ultrasonic sensor” has a yellow label, so we connect it to the yellow labeled port 2 on the controller board and place value 2 into the
Let’s link the
mbot-motors nodes through the
pid-controller node will track changes in distance between the defined value and a stream of fuzzy values received from the sensor. After calculations node will output motor speed according to our parameters.
The Patch is almost ready, but we decided to expand it with a small motor check function.
mbot-motors node takes motor speed values in the range of 0 to 1. If the speed isn’t high enough motors start buzzing. To avoid this sound, we added a small check. When the speed value is greater than -0.2 and less than 0.2, it resets to 0.
Have a look at the finished patch for our hand follower bot.
Now it’s time to figure out what parameters the
pid-controller node has and how we should use them.
A PID controller calculates an “error” value as the difference between a measured input value and the desired setpoint. The controller attempts to minimize the error by adjusting the output. To control the output, a PID-controller uses coefficients Kp, Ki, and Kd which are based on error changes.
In our example the error concept is a difference between the current distance from the sensor and the distance we want mBot to hold.
IN pin — is a pin for a measured value. In our case it receives the distance value which can change from 0 to 4 m.
TARG pin — is a pin for a desired setpoint. In
TARG we put the distance which we want our mBot to hold. In this case we we want the mbot to keep 15cm distance to any object in front of it, so we put 0.15 value in the field.
Kp pin — proportional factor. Coefficient Kp is proportional to the current value of error.
Ki pin — integral factor. This coefficient is used to neutralize the accumulating part of the error. It can eliminate system errors accumulate after a certain amount of algorithm loops.
Kd pin — derivative factor. It estimates the trend of future errors using Kd rate of change. The more rapidly the rate of error changes, the higher the controlling or dampening effect of PID-controller will be.
There are no specific values of Kp, Ki, Kd coefficients. Each system with a PID-controller has individually tuned optimal ratios. We are going to choose parameters manually.
Kp is the first factor for us to practice with. Kp coefficient sets some kind of bounds for the output value. It describes what the output value can be.
For example, if the error of the input value is large and the Kp coefficient value is high, the PID-controller output will be proportionately large. While the error value is high and the Kp coefficient value is low, the output changes insignificantly.
Kp coefficient can also be negative. You should use it when you need PID-controller to output negative value while error change is positive.
Let’s place value 5 to the
Kp field and leave
Kd coefficients equal zero. Now let’s take a look at the mBot behavior after deploying the patch to the mCore board.
We set a relatively small value of the Kp coefficient. When the distance changes, the motors speed changes slowly. What will be if the Kp value will equal 90?
Now, when the distance changes the motors speed increases too fast and sharply. Furthermore, when mBot starts moving, it jumps up like a race car. Seems that we need to experimentally find the average value for Kp. Let’s set up Kp to 40.
Now let’s practice with the Kd value. Kd describes how quick PID-controller response to the error value changes.
The higher is the Kd value the faster PID-controller changes the output. By this coefficient, we can control the sensitivity of mBot.
For example, now we change the distance value in
TARG to 40 cm (0.40) and leave the Kd value equals zero. After deploying the patch, we create a continuous obstacle change in front of the mBot.
The controller with a zero Kd value, does not react fast enough to such obstacle change. As a result, the mbot hits the cardboard box. To make our system more sensitive we change the Kd value to 10.
Much better now. Specifically for your device, the Kd value change make the
pid-controller node reacts faster or slower.
Ki coefficient is designed to eliminate the error that constantly occurs.
Imagine that we are going to move the obstacle away from the robot endlessly. At first, the robot will not lag behind us as the integral error will be quite small. However, after a considerable period of time, the robot will fall behind on a very large and visible distance. In this case, the variations of the Ki coefficient will solve the problem.
For the hand follower changing of the Ki, coefficient do not give any visible changes.
If the distance to the obstacle is higher than the specified error has a positive value. And if the distance becomes less than the specified error becomes negative. Alternating positive and negative error values neutralize each other and do not allow the constant part of the error to accumulate.
Although in your projects with a
pid-controller node, this value can be very beneficial! For example, if you are working with inaccuracy of temperature or pressure sensors, so feel free to experiment.
We have become experienced with a PID-controller node and realized that working in XOD with it is quite simple.
In the next article, we will describe more complicated project example of a
pid-controller node and program a line follower robot using the mBot platform.