In this first part we stop at an algorithm that is the base for following lines and explains the basic idea for the proportion. A must watch if you want to understand the robot at its behaviour. At the end the program still wont work so check out Part 2 and Part 3 for the final steps.
- 30 Oct 2015
This algorithm for following lines is called Proportional. It proportionally rotates the motors depending on how far from the line the robot is. If the robot is right above the line the proportion between the two motor is equals and they just continue forward. But as the robot looses the line, the algorithm would return it as quickly as possible proportionally to how far away the robot is.
We have marked the video as Advanced because it uses more complex programming abstracts like Variables, Threads and Math Calculations.
In this video we would implement an algorithm for following a line that's called a proportional algorithm. It uses the color sensor and in this video we'll use one of the color sensors. It's a very smooth algorithm for following lines. It works like this.
Finds the line and follows it. Let's see how we can implement this program.
The basic idea of a proportional algorithm is that you move the different motors with different speeds depending on how far away the sensor is from the line. For example, in this program we would use the sensor located on port 1.
If we have our line over here, we know that if our robot is away from the line, like in this situation,
then motor A moves faster than motor D.
If the sensor is near the line then motor A moves slower. It's a different proportion and this algorithm follows the edge between the line and the field. You move the different motors with different speed depending on how near the sensor is to the edge. When the sensor is on the edge you actually move both motors with the same speed. That's the basic idea for proportion. If you are too far away from the line, you need to return very fast and if you are very near to the line you need to just move the robot slightly and then continue forward. The program looks like this. We need to control 2 of the motors and we need to control motor D and we need to move it forward and we need to control motor A. These are the 2 motors that we need to control and we had one sensor, this sensor is located on port 1, it's a color sensor. We want to measure the reflected light, not the color but the reflected light because this is a proportional algorithm. How do we control these motors?
We use the block for detecting the reflected light then we must calculate what should the power of motor A be and what should the power of motor D be.
How do we do this? First we have motor D and in our robot, if you imagine it, in our robot motor D is the left motor. If we have our robot and this here is the front, we have the sensor here and we have one of the motors here and we have the other motor here. As the robot moves forward we have the following numbers. This is the sensor on port 1 and this is motor A and this is motor D and this here is the line, that we are following. We are following the edge of this line, it's all black and we are following the edge of this line. Motor A, motor D. So motor A is our right motor and motor D is our left motor and that's important. How does the proportional algorithm works? We detect with the color sensor and we do this in a loop
and we take the value of the color sensor. This is more advanced program and it will include some variables and some threads but stay with me. Detect the value and then we do a calculation with this value and the calculation is the following.
If we place our robot over the black line so now it's over the black line
and currently this is the value detected by our robot.
We are over black and the sensor detects 16%. If we move the robot over the table which is in our case something like white. We detect a value of 68%. So black is 16 and white is 68.
The value between 68 and 16 is around 40. We can use this as a threshold value. We detect the value of the sensor and we subtract 40, that's the middle. If we have 70 here and 40 here then the result of this operation will be 30 and we also do the same operation but in reversed order. We take the value of the sensor and we subtract this value from 40, so it's 40 minus this value. This is very crucial to understanding this algorithm. When we have the sensor detecting white we have one of the values large and the other value small. If the sensor detects 70 something white we have this block returning 30 and this block returning minus 30. If we are in the middle between the white and the black that means on the edge of the line then both of this blocks will return 0. This is how the proportional works. As we are far away from the line this block will return large number and this block will return a small number.
So we subtract from the detected value 40 and the detected value from 40. Then we do the following thing. We add these values to variables and I'll call this variable motorR (motor Right). That's the power of the right motor. We want to write a numeric value and this numeric value will be the result of this block. We need one more variable that's called motorL (motor Left)
and the result of this block we'll write this result in this variable. Then what you'll do is pass this variables to the motors and you have 2 motors, the first one is motor D and we are starting with on and we set the power of motor D to be equal to the power of the left motor because in our case motor D is the left motor. We read a numeric value and we pass it to the block. We also do the same thing with the other motor. We connect this.
We add the same block for the other motor. We read the value from motorR and we pass this value to the block and it's motor A. Again, we connect this.
Now we have multithreaded program.
It works in the following way. We have 2 separate threads, the first thread is controlling motor D and the second thread is controlling motor A. These 2 threads happen simultaneously and we have a third thread that's actually detecting the value of the sensor. These 2 threads work independently and simultaneously. The first thread calculates the precise value and the second and the third thread set the value to be used by the motors and that's our program. Let's download and run it.
This here is the program that we have just implemented working. The robot finds the black line, moves but does not follow it. That's a problem that we'll solve in the next video.