Gapped & Crossed Line Following. Part 10. Robot is lost Pro Preview

All worked as expected, up until know because the robot got lost. This happens when we turn right and the line does not continue to the right. Now the robot must somehow understand that it is "lost" and escape.

  • #213
  • 16 Mar 2016
  • 9:20


We need a state of "Lost" and behaviour for escaping this state.

Courses with this episode

The episode is used in the following courses where you can find additional tasks for it to use in class

The power of State Machines. Gapped and Crossed Line Following with LEGO Mindstorms EV3

Use this tasks in you STEM classes:

Tasks description, submission and evaluation are available to subscribed users.

Subscribe now to access the full capacity and get feedback.


The last problem in our line following program is how to solve this situation. And on the field that's shown below in the pictures, you can have the following situations. You can move in this section through here and then you must turn left. That's a valid case. There is also the situation that you must come from here and you must also turn left. But our program says that each time we see something on the right, we turn right. But this here is the end. So we reach the end and we detect a gap. How to move right and detect that we have finished and there are no more line here and we must actually return back and follow left. This is the program for this video.

First let's look at the program and if we start from here. I start the program, then I detect something on the left and we move correctly. But now if we come from here.

We detect something on the right, we detect the gap and now we start searching for a line, but there is no line because this is the end.

What we should do is we should find a way to detect there are no more lines and that we are turning forever and then just return and continue following the line again. So this is the problem that we are currently solving. In the program it works like this. This here is the state in which we detect that we have a gap. This is the state 1 and in this state we have a block that's called pass gap. And we must change it so that it's more intelligent. In this PassGap we are stopping the motors,

after that we align to the line, then we just turn left, move forward and detect the next line. But what happens if there is no next line. What happens here if we can't detect the new line. What will happen is that we'll turn forever. We've lost the line and we must change this logic here. We must add some timer, some rotation counter or something that would tell us that it's turning for 1 or 2 rotations and there is no black line, so probably this is the end of the line, so we must turn back to initial position and move left, not right. How do we do this? First, we take a motor rotation, the moment we start searching for the line with motor B, we reset the rotations for motor B, then we start with motor B and we constantly check if the rotations we've done with motor B are greater than or equal to 90 degrees.

We'll do this in a loop.

We are constantly checking if the motor has rotated more than 90 degrees and if the motor has rotated more than 90 degrees and it hasn't detected the line, then we must do something. the other thing that we should check is if we are detecting a line, so we take color sensor 2 and we compare if we see black. And if we see we have detected a line. So these are the 2 conditions that we must meet. First we can arrive at the black line or if we haven't then we must wait for 90 degrees and decide that we've lost the line.

This here is a Loop forever. And in it we are checking if we've detected a black line.

If we did, then we must break this loop. How do we break a loop? We have a loop interrupt block. If we detect a black line we break the loop for, let's call it, GapWaiting. We break the gap waiting loop. And this means that we've detected the line and now we can follow it.

Next if we haven't detected a line, we are waiting for the line. But we're also trying to see if we have rotated motor B more than 90 degrees. Than this means that we've lost the line. And if we've lost the line I'll just stop the motors.

Stop motor B, because we are turning with it. And wait for let's say 5 seconds. Just so that you can see it on the camera.

Now this is more complex behavior of our PassGap loop. Again, each time we detect a gap, we start searching for a new line, we reset the rotation sensor, then we start rotating with motor B and we are constantly checking if we detect a line with color 2, we break this loop,

but if we don't detect, we constantly check with the rotation sensor if we have rotated for more than 90 degrees with this motor. Let's see how this program works. Placing the robot, start the program, we detected a gap, now we search for the line, but because there's no line, where we can continue the robot stops for about 5 seconds. And it waits there. Now what we should do from here is to turn the robot

and after we turn the robot, we can start following this black line, but as we're following we'll detect here the line on the left and we turn left, but we don't want to detect the line on the left. We must somehow tell the robot that it shouldn't take into account the next line it detects. That's 1 solution, the other one is to just turn, then move forward, detect a line, then move over this line here and then continue.

These are the 2 solutions that we have and we'll explore one of them in the next video.

Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

Image for The power of State Machines. Gapped and Crossed Line Following with LEGO Mindstorms EV3
  • 12
  • 92:11
  • 0
Image for Robot is lost
  • 1
  • 0
  • 0
  • 3d_rotation 0