XOD powered line follower mBot
In the previous article, we learned how to use a PID-controller in the XOD visual programming environment. Also, we turned the mBot platform by Makeblock into a simple hand follower robot. If you want to recall the topic or you know nothing about what is a PID-controller, I advise you to read the first article in the series.
In this article, we continue the PID-controller experiment. We’re going to use the mBot platform from Makeblock as we did before. The next more challenging task for us is to create a line follower robot. To make this possible, we should extend the mBot capabilities. We add a “Me Line Follower’ sensor. Since this sensor is made by the same manufacturer, it perfectly fits our robot and is ideal for our task.
There is a XOD library to repeat described examples. Check out the overview at the XOD website and add it to your XOD project by hitting
File → Add Library in the menu and typing
gabbapeople/mbot-lib. The library has improved since the last release, so if you have it already make sure to add it again to get the latest version.
Take a look at our improved mBot.
Let’s begin robot programming by reading incoming values from the line sensor. The first node to place into the patch is
This node communicates with the sensor through the
S2 input pins. It has two output pins
R according to the left and right infrared sensor on the board. True value on an output pin means that there is a white or bright color in front of the infrared sensor. In turn, false value means that the color is black or dark.
“Me Line Follower” has a blue label. Physically we connect it to a suitable blue labeled “port 1” on the controller board. In the program we put a corresponding node
rj25-port1-blue into the patch and create necessary links.
What are the possible states of the line sensor? Look at the sketch.
- Situation 1. Both IR LEDs are on the line.
Rpins. mBot should move forward.
- Situation 2. The left IR LED is on the line while the right deviates to the right.
R. mBot should turn to the left.
- Situation 3. The right IR LED is on the line while the left deviates to the left.
R. mBot should turn to the right.
- Situation 4. Both IR LEDs are out of the line.
mbot-line-sensornode outputs two
truevalues. mBot should turn around to find the missing line.
However, it is better to use analog sensors instead of digital. At the same time a line follower sensor array is better than a couple of IR LEDs but it is ok to use this sensor for our example.
For a PID-controller an input should be a single variable while the
mbot-line-sensor outputs a couple. To solve this problem, we add the
subtract node. This node makes a subtraction of two boolean variables and produces a number.
In this way, the
subtract node can output the values:
- 0 for the first situation;
- -1 for the second situation;
- 1 for the third situation;
- 0 for the fourth situation.
The input is ready and it’s time to link it with the
We use those four situations to form the “error” concept for the PID-controller. The situation when the incoming value is 0 and both IR LEDs are in line is ideal for us. That’s why we should set up the the
TARG pin to 0.
But this patch in its form can work correctly only with the second and third situations. When mBot completely loses the line the incoming value for the
pid-controller node will be 0 too. For this case we have to introduce a special condition.
We place three additional nodes
multiply to the patch and make links as shown on the picture below. Take a look.
and node makes a condition for the situation 4. The
if-else node is used to define an amplification factor coefficient. Assign the
T pin to a value of 25 and
F pin to a value of 1. Finally, the
multiply node multiplies the output of the
pid-controller by the coefficient and passes it further.
If it is not the situation 4, then the coefficient is 1, and the output of the
pid-controller remains unchanged. If it is then the coefficient is 25, and the output increases significantly.
The value at the
F pin shows how rapid mBot turns around looking for the line. We choose the 25 value empirically. Adjust it in your project to find what works best for you.
Now the second, third, and forth situations are described, and only the first is left untouched. To fix this, we need to place the only one node
nor and link it as shown in the picture below.
In the first situation, both
R pin values are false, and the mBot moves forward. There is no error. We link the
nor node with the
RST pin of the
pid-controller node to wait for a new incoming deviation.
The last step of making the patch is to pass the multiplied value to the motors. We put three nodes
add to the patch. Also, we set up a constant to store the base speed value of the mBot and make links. The finished patch is shown below.
mbot-motors node controls the engines. A positive value makes the wheel move backward while negative makes it move forward. The
SPD constant node stores the basic mBot speed. It ranges from -1 to 0 and we set it up to -0.8. The
add nodes calculate the motor speed by the multiplied
pid-controller output and the set up constant. The patch is ready for tests. Now it is time to tune the PID-controller.
As you remember, a PID-controller adjusts the output operating three factors. They are the proportional, integral, and derivative. Leaving all the coefficients equal to zero makes the mbot just wander around without any behavior at all. As we did in the previous examples, we need to choose the coefficient values experimentally.
Let’s put the value 3 into the
Kp pin, leave other coefficients equal 0, and upload the patch. To test the behavior of the robot we make a small track part on the floor.
As you see, with this coefficient oscillation is too high. mBot is trying to follow the line, but it twitches and finally leaves the track. Let’s set the 0,1
Now, when the robot loses the line it returns back too long. That happens because the error has too little effect on the motor speed change. By trial and error we select a proper value 0,7.
Take a look at the graphical explanation of the mBot behavior. It shows how the
Kp value changes the robot movement.
Kp coefficient, we make the movement smoother. However, still, the robot can’t finish the track. Apparently, the second turn on his path is too steep, and the
pid-controller node reacts to slow. To solve this problem, we should set a non-zero
The first time we put the 3 value of the
Kd. It turned out that the 3 value is far away from the correct. We make lots of tests to find the proper value. We choose the 1 value as the best one.
The mBot finishes the track. Take a look at the sketch below. Increasing the
Kd value, we increase the time period for the robot to be on line and reduce the time period to return to the line in case of loss.
Kd coefficients are quite suitable for our robot. But what about the
In fact, we do not need to set it up. For a line follower robot, the
Ki can be useful in very particular cases.
For example. The track on the floor is endlessly straight. The robot moves strictly along the line, but there is tiny manufacturing disagreement of the motors. After a considerable period, this small issue will turn into an uncontrolled twitch. The
Ki factor eliminates such problems.
We will try to use
Ki in a very unusual way.
When the robot is out of the line, the input for the
pid-controller is 0. If the the
Ki is 0 the multiplied value to the motors is 0 too (25 * 0 = 0).
The thing is that the robot does not move in ideal conditions. The IR sensors always touch bad pieces of the track line or interfere with dark spots on the floor. So while the robot is off the line the input of the
pid-controller isn’t constantly 0. By adding the
Ki, we get rid of the zero multiplied value. The 25 coefficient at the
T pin of the
if-else node makes the robot rotate very fast if it loses the line. For the test, we put the 2
We made a cool line follower robot with a simple XOD program. Thanks to the PID controller it was so easy.
Be sure to follow new articles cause we will do new instructive projects!