# Gapped & Crossed Line Following. Part 11. The state of "Lost" ProPreview

We discuss the state of "Lost" and the different ways we could escape this state. We also build the next step of our State machine programming pattern where the next state is determined by the previous state.

• #214
• 23 Mar 2016
• 8:38

### State

Moving from one state directly to another without the re-calculation with the sensors is a powerful way to control our state machine. We know that we are lost and know exactly what we should do to escape 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 these 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.

### English

- What I would like to do in this video is to introduce a new state to our robot, and with this state with can solve the problem with...when the robot is lost. Currently, we have our program, and we calculate the states based on the sensors, and then we control the motors based on the state, and at the end, we set the state to minus one. So, we reset the state, and we loop. We loop again, we return to the beginning, we calculate the state again, do something with the motors, we reset the state. In this video, we will introduce a new state that allows us to move from one state of the motor to another state of the motor. And it will be a simple state machine.

The state that we are going to add is actually the "I'm Lost" state, when the robot is lost. And it won't be dependent on the sensors, but it will be dependent on the previous values of the sensors. So when the robot is following [inaudible 00:01:11], and when the robot is lost we will be something like here, so we're controlling the robots...the robot. And while controlling the robot, we detected that we are lost. And from there, we want to create a new state that would return us back to controlling the motors. And this state will be "I'm Lost, " or "Lost State." And for this lost state, we must do something. Search for lines, try to find where we are on the field, and only then return, and again detect something on the sensors, and return to the main loop. So this is the point through...at the new state that will move us from one state of the motors to another state. Let's see how this works in the program. This here here is our program, and in this program we have our past gap block. And what you want to do is to add a new state, and we want to enter this state if we haven't passed the gap, but if we are lost, because the only time we can get lost is while we are passing the gap, and this pass gap must return a result. Have we found the line? Or we haven't found the line. Are we successful in passing the gap, or not so? This pass gap must return a result, and based on this result, we'll set a new state right here in this switch. Looking at the pass gap block, it looks like this, and it does not return any result. We want to change this. And the result will be the following: initially we will set a new variable before the loop that's a logic variable. A logic, and it will be called "lost line." Lost line, okay? And initially, it will be false when we are trying to pass the gap, so we haven't lost the line, we are searching for it. But then, while we are searching, if we reach more than 90 degrees with the motor, motor B, and we haven't found the line, this means that we will write a new logic variable, and this will be "Lost Line," and the value will be true. So we have already lost the line. At the end, I will return this value, lost line, I need two variable blocks, and we here have the write logic for lost line, and here we have the read logic for lost line. And we do something like this.

I would like to write here, just not showing correctly. Write...watch...and we write. Now we extract this whole new block into a new block. Let's select all the blocks except the last one. All the blocks except the last one, and we extract a new block. My block builder, and we call this block "Pass or lost", and "gap." And it will be again with this icon the output will tell us if we have passed, or if we are lost. So it will be true if we have lost the line. Finish, and now this is our block. Just move them closer to each other. Like this. Okay. So now we have a new pass or lost gap block that's returning true if we have lost the line. And I'll take this new block, Ctrl+X, and I'll add it right here, and delete this one. Now the idea is that we preserve the behavior as it is on the robot, but we return a value, and if we have lost the line, we return a value of true. So we can check have we lost the line with the switch. And if we have lost the line, it's a logic switch, then we must set the state to a new state. What will be the new state? Let's see them. One, two, three, the four. And state will be equal to four. So four means that we are lost, and as we are lost, we must do something. Another thing that we should change in our program is if we change the state to four here, and we then change it to minus one here, it won't have any effect, so we must move this change of state in each of the inner blocks. Inner states. One here, then we need one here, and then we need one here. Okay, let's check that we haven't changed the behavior of our program. I'll open the pass gap and in this pass gap, I'll remove this wait block right here for five seconds, and I'll add a new state in our switch for states. This state will be four. State four. And in this state four, we'll wait for, let's say, 10 seconds. And if we are lost, we wait for 10 seconds. That's the behavior that we want on our robot. Let's check if this works on the robot. Position the robot.

No problem, follows the line, goes right, tries to pass the gap, and the robot stops. So we have a forward state, and this state stops the robot currently. Now we must change the block for the first state so that it does not stop, but actually a return, follow, wait for one...or one...left the line, and then continue forward. And this will be the subject of our next video. Check out the links below for the programs, and for the construction of this robot, and we'll finish this course.

## Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

• 12
• 92:11
• 0
• 1
• 0
• 0
• 3d_rotation 0