We follow a line. We start from the Smooth Proportional Line Following program and modify it a little for this program. We follow the line with the middle sensor attached on port 2.
- 03 Feb 2016
Before continuing with this course, we recommend you take a look at the Proportional Line Following with LEGO Mindstorms EV3. This will give you the base for working with smooth line following programs and would make this course much more clear.
If Proportional Line Followig seems to difficult at first then look at the EV3 basics course. Color Sensor
The line is black, the field is white and we follow the black line. We create a "follow" state and a "follow" behaviour in our state machine.
Building instructions for the robot:
Building instructions with the three sensors at the front:
We start from what we have and this is the proportional line following algorithm. It behaves like this. The robot follows the line, because there is a gap it lost the line and the robot tries to find the line again, but it can't. What we should build is a program that works in the following way.
Robot follows the line, moves, detects the gap, aligns, moves again finds the next line. This is the way we should build our program and now we'll go into details about the programming.
This is how the default program for proportional line following downloaded from the site looks like and we must change it first, we must change the port of the sensor, that detects the line, because this is port 2 and our left motor is motor C and our right motor is motor B. This whole program is a proportional line following, but that's not the only program that we should execute in our line following challenge. We must group all the logic. The first thing I would do is move the 2 motors together.
Now we have one of the threads calculating the values for motor left and motor right and the other thread controlling the motors. I would do even more grouping here and I would just move the whole blocks for controlling the motors after the calculations.
Now we have our program for proportional line following, it will behave in the same way and it calculates the values for left and motor right and then it sets the values for motor left and motor right. I will extract this whole functionality in a new block and this new block will be called proportional line following. Before extracting this new block I would like to take the value from sensor 2 and write this value in a new variable. And I call this new variable Sensor2 (S2). This will give me the chance to just measure the sensor only once and then use this value in the rest of the program. I'll write this S2 and then I'll read this from S2
This without any change of the behavior changes the structure of our program. What we can do is to extract this whole collection of blocks into a new My block. How do we do this, we select all the blocks. Click on tools, my block builder and this is our proportional line following.
Let's use a sensor for this block. The input is actually the value of the sensor.
An icon for our block, I'll choose just a number.
Now what we have is a much more clear program, where we have only 2 blocks. The first block is detecting the value of the sensor and the second is following a line. Let see if this works.
As you can see without modifying the behavior of the program We've just modified it's structure, So we have refactored our program, but the robot is still following the line. Our next step is to program the different states of our robot. The first state that we're sure of is if the sensor detects a line, we're following the line, but if the sensor detects the table or something white, we are either following the line or we have just lost the line. So we've been here and while we are following we're on black and then white and then we start searching for the line and if we search for a line, for a number of seconds, let's say 1 sec and we can't find a line for 1 second, we can assume and this will be a correct assumption that we've lost the line. So there must be a gap here. If with the proportional line following we've lost the line and we haven't found the line for 1 or 2 seconds, depends on the robot.
If we haven't found a line, this means that we've lost the line and we have the second state, which is state of a gap and we must react to this state of a gap. Let's detect where we exactly know that we've lost the line. For this we'll use a timer, when we are following the line, each time we detect white, we detect the table, we start the timer and if this timer reaches 1 second and we haven't reset the timer, this means that we've lost our line. So each time we detect black we reset the timer, each time we detect white, we increment the timer and if we don't detect black for a number of seconds this means that we've lost the line. I think that when you get to the program it will become more clear. Let's try the program. While our program is running we detect the value of sensor 2, we write this value in a new variable, that's called S2 and then we pass this value to the proportional line following block. This allows us to read from the sensor, only once and then use the different variables with the different values. Now we must follow the line, if we are in the state of following line. How do we know if we are in that state, we must have a variable, We'll call it State.
If our state initially, let's set it initially before the loop with a value of -1 and now we must define what are our states. For the states we need different values. The first state is following a line. If we are over a black line with the second sensor, this means that we are detecting a line, so we must follow this line. For this we'll use a switch.
The switch is the following. If we have a numeric value for S2, the value detected by sensor 2, if we are over a black line which means that S2 must be less then 40, because 40 is the treshold in our case. We take S2 and we compare it with our treshold value for black and it is 40. This means that this is the value between 10 which is black and 70 which is white.
The value inbetween is 40, this means that this is the treshold value. Now if we are less than or equal to the treshold value, this means that we over a black line. And if we are over a black line then our state should be state - 0.
This is the way we calculate our state. Again we detect with the sensor and if we are over a black line, the state is 0. This is something that we now defined. State 0 will be follow a line.
Then we must follow the line only if we are in state 0. So we'll take another switch and check are we in state 0. How do we do this. We take a Switch, numeric,
and we compare the state, we read from the state variable
and if we are in state 0, only then we follow the line proportionally.
For state -1, this is the default state, where we haven't calculated the state.
At the end of our switch we would like to reset the state back to -1.
The logic is the following. We would use different states that will tell us wether we should turn left, right, continue forward and then depending on the state we'll do different things with the motors. And at the end of the program, we reset the state back to -1, which is unknown state and then we continue the loop again. We calculate what is our state based on the sensors, then we execute something and then we reset and this continues forever until we reach the end of the line. Currently we have only one state that is state 0 and it means that we are following the line, we are over the black line. Let's check if the program works.
As you can see the robot is now following the line. So we've made a change in our program and it's not working correctly. When we are over black we are trying to reach the white, but when we are over white, we just continue forward. This is the problem for the next video. We'll continue in the next video, because this is getting quite long.