Bài 8: Lập trình robot giáo dục KC-Bot dò theo vạch sử dụng 4 mắt hồng ngoại

Kidscode STEM
12 người theo dõi
1 Lượt xem video·19 thg 11, 2023
---------- ★ ----------
* Website: http://kidscode.edu.vn/
* Email: [email protected]
* Hotline: 0888 424 222.
---------- ★ ----------
Let's continue with the STEM science of robots and autonomous vehicles.
In the last lesson, we learned about the first topic, which is the wind tunnel,
and we used simple two-eyes.
In today's lesson, we will continue the topic of the wind tunnel robot using external sensors,
but we will use more external sensors.
First of all, let's watch a video about the wind tunnel robot using external sensors.
I guess not many people have seen this before.
This is a robot of many world-class robots.
The wind tunnel robot has started.
The wind is cool.
It's cool.
16.589 seconds.
It's very stable.
The robot has come out.
Wow!
The robot is still moving.
It's so cool.
The third time.
This is the second official run.
To make a robot run that fast,
we need to upgrade some tools to make it work.
In today's lesson, we will go through a little bit.
Maybe we haven't reached the speed of the robot yet,
but we can increase the speed of the current KC-4.
As you know, the previous day,
normally, we only need two sensors to be able to use the wind tunnel robot.
The left and right eyes like this,
and a black bar in the middle.
That's the general principle.
You know that the wind tunnel robot is very popular today,
especially in the manufacturing industry.
Currently, there are quite a few Vietnamese startups
that are making wind tunnel robots based on the wind tunnel robot principle.
However, with two eyes like this,
the first is that its oscillation is high,
and the speed is certainly not high enough
for the robot to be able to go outside.
The solution is to increase the number of these eyes.
So what is the purpose of increasing the number of these eyes?
We will see in the next part.
As you all know,
we will use a pair of infrared light sensors.
The infrared light sensor is a LED that emits infrared light,
which means we can't see the eyes.
We can see this infrared light by using the phone
and turning on the movie mode.
We look into the eyes of this LED,
and we can see the infrared light.
And a photodiode like this.
To make it simple, we can use multiple single modules.
Each module will have an infrared light and a photodiode like this.
On the module, there will be a variable
to adjust the brightness or brightness of this module.
When we put this module in the black band,
it must reach level 1 or level 0,
depending on the power supply.
If the black band is not at that level,
we will adjust this variable
to adjust the color of the black band and the white band.
In addition, there are many other modules
that are designed to be organized like this.
Because if we use modules like this,
there will be a lot of wires,
and it looks a bit mysterious.
And it can also be confusing
when we are programming or playing.
So it is more convenient to use modules like this.
Basically, there are only two variables
to determine the position of the robot compared to the black band.
However, the robot is often due to inertia,
so it is too far from the required level.
For example, if we turn left or right,
when we increase the speed,
the robot will often feel like a drunk person
shaking on the black band.
So to simplify,
we will use more variables
to increase the accuracy and convenience of the vehicle.
For example, I will take 5 variables like this,
in which the black band is the one that the robot needs to follow.
Depending on how we arrange the variables,
this is a way that people usually arrange
with the number of variables is 0.
Normally, the center is in the middle of the black line,
and the eyes on both sides are far from the black line.
With a lot of variables,
we can distinguish the different degrees of inertia.
For example, if the eyes are in the middle of the black line,
we will see that our robot is in the middle of the black line,
and we just have to let it go straight.
When the robot turns right or left,
it means that these variables will have a signal,
because then it will go into the black band.
Depending on the degree of inertia,
we will observe which variables have a signal.
With 5 variables like this,
if we combine them together,
we will have quite a lot of cases
to distinguish the position of the robot.
This is a picture that you can use as a module like this.
This is what I set up to see the position of the sensor.
With 5 sensors,
the number 1 shows when the sensor is in the black band.
In the first case, we will focus on the middle case.
This is the middle case.
We will have a sensor in the middle with a value of 1.
This sensor has a value of 1.
And both left and right sensors have a value of 0.
At that time, our robot is in the middle of the black band.
This is the first case.
From this, we can determine the value of the error.
We can set it to a value of 0.
With each case,
we will produce a value, a variable,
to distinguish the degree of inertia of the robot.
It's very easy to understand.
For example, in this case,
we have 2 sensors.
We can say it's the right-hand side.
A middle sensor and a right-hand sensor.
In the first case,
we have 2 sensors,
for example,
in the black band.
This means that our robot is turning to the left.
Both sensors,
a middle sensor and a right-hand sensor,
are in the black band.
Our robot is turning to the left.
Similarly,
if we push it higher,
we can see that the right-hand sensor is in the black band.
This means that our robot is turning to the left.
Conversely,
the left-hand sensor is in the black band.
Our robot is turning to the right.
With the combination of these 5 sensors,
we can distinguish 9 robot states.
The first state is that the robot is straight.
The second state is that the robot is turning to the left.
The robot is turning to the right,
turning to the left high and turning to the left very high.
For example, we can distinguish the states like that.
The right-hand sensor is straight,
the right-hand sensor is high,
and the right-hand sensor is very high.
According to each state,
we will create a loop.
During the process, we will set it into a variable.
Depending on the value of this variable,
we will set the engine speed.
For example,
when the robot is turning to the left,
the right-hand sensor is in the black band.
The more the sensor is in the loop,
the higher the speed of the left engine.
The more it turns,
the higher the speed of the engine,
so that it returns to the loop quickly.
This is a picture that you can imagine more clearly.
When the sensor on one side touches the black band,
plus the sensor in the middle,
the opposite wheel has to turn harder
to push the car back to its original position.
Looking at this picture, we can analyze it like this.
For example, this is when the car is going straight.
You can see that the sensor in the middle is straight,
and the two sensors on both sides are in the white band.
Then the speed of the left and right engines
will be equal.
This is the variable in Vietnamese,
and this is the variable in English.
These two variables are equal.
At that time, we will set the value to 0.
If the value is 0,
we will continue to let the car go straight.
If the value is 0,
we will continue to let the car go straight.
This means that the speed of these two wheels will be equal.
In case number 2,
you can see that the sensor in the middle is still in the white band,
and the sensor on the left hand
is also in the white band.
This means that our robot is turning right.
The sensor on the left is in the white band,
which means that the robot is turning right.
When the robot is turning right like this,
it is clear that for the robot to return to its original position,
the engine on the right hand has to increase the speed,
and the engine on the left hand has to decrease the speed.
It will quickly bring our robot back to its original position.
We draw a very simple rule.
If the robot is turning right,
the speed of the right wheel will increase,
which means that the speed of the left wheel will decrease.
To make it easier to remember,
we call it turning right,
increasing right,
turning right, increasing speed right,
turning left, increasing speed left.
We can use a short sentence like that to make it easier to remember.
Let's continue to look at the picture like this.
After increasing the speed of the right wheel,
the robot will gradually return to its original position.
At this time, the state is 1.
When the robot reaches a balance, it will return to 0.
In this case, the robot will be turned to the left.
At that time, we will increase the speed of the left wheel.
As I just said, turn left, increase left.
That's the rule.
Turn right, increase right, turn left, increase left.
That's the rule to help the robot return to its original position.
So the calculation here is
if the speed we set is appropriate,
you can imagine it like this.
If we turn left like this,
if we increase the speed of the right wheel very high,
the robot will immediately turn to the left.
If the speed is too high, the robot will turn very strongly.
Turn left, turn right, turn left, turn right.
At first, I called it going like a drunkard.
So how appropriate is the speed we set?
We need to adjust it in practice.
There are many ways to do this.
The easiest way is to increase the speed slowly.
For example, we set the speed to turn right, increase right.
Let's increase it a little bit.
If the robot still feels that it is not fast enough,
we will continue to increase it.
Continuously, we will create a set of values
so that the robot can return to its original position as quickly as possible.
However, in practice, people use a calculation technique called PID
to help the robot adjust the speed quickly
but not too fast, not like a drunkard.
That is, if you use PID,
the robot will run fast and smoothly on the track.
Today, I don't have enough time to talk about PID.
This is something you should watch to adjust your robot in the game.
The difference between teams' robots will be quite different here.
Therefore, our organization has given you a choice
to create this track module yourself
so that you can apply the PID algorithm here
to help our robot run the fastest.
As you can see, the robot runs very fast.
Teams will distinguish each other by the time the robot runs
and the speed of the track.
As I said, this is a KC-Bot robot that you can receive.
You can use PID to make the robot run at a very high speed.
We will see later.
This is the video we made with 6 eyes of KC-Bot.
As I said at the beginning of the video,
the robot moves out of the track.
In practice, this is called a loop.
The robot moves too fast to the desired speed.
The speed is not good enough.
If you use the PID algorithm,
the robot will not run as fast as this.
I will write the PID algorithm in another article.
I don't have time to talk about it today.
That's what you need to learn in the WinFast M1 competition.
That's the first part.
We learned about the self-made modules and the PID algorithm.
Next, we will go to the execution of today's lesson.
We will set up the KC-Bot module.
It is a 4-eyed module.
On KC-Bot, you have a module.
Here we have 4 eyes.
You can use these 4 eyes.
You can set up the KC-Bot module.
If you want to make it yourself,
you can use 5 eyes, 7 eyes, 6 eyes.
It's up to you.
In the next video, we will see how to set up the KC-Bot module.
Now, let's go to today's lesson.
In today's lesson, we will set up the 4-eyed KC-Bot module.
You can see here.
This is the 4-eyed KC-Bot module.
On this module, there are 4 eyes with pink eyes.
You can see here, there are 4 eyes.
These 4 eyes are used with A4, A6, A7 and A5 legs.
Now, let's analyze what can happen
when we use the 4-eyed KC-Bot module.
As you can see here,
these 4 eyes correspond to A4, the first eye on the left hand side.
A6 is the next eye on the left side.
A7 is the first eye on the right hand side.
And A5 is the last eye on the right hand side.
We have 4 eyes outside.
Now, you will see 4 cases.
The first case can happen.
This is the most expected case.
It is the case where the robot is in the middle of our screen.
At that time, both A6 and A7 eyes will receive signals.
In the previous lesson, we have analyzed and measured.
When our eyes are on the black screen,
their signals will return to the average value.
In this case, the A6 and A7 eyes will receive a signal that is greater than its average value.
This is our most perfect case.
The simple thing is to move the robot forward.
The second case is when our robot is on the right side.
Here, it is only a little bit to the right.
At that time, both A7 and A5 eyes will receive signals on the black screen.
At that time, we will turn the robot to the right a little bit at low speed.
So that we can reach the perfect position.
That is, both A6 and A7 eyes are on the black screen.
Like the first case.
The next case is when our robot has turned too much to the right.
At that time, only A5 eyes receive signals or are on the black screen.
At that time, we will turn the robot to the right at a higher speed.
So that we can move the robot to the middle position.
Like the first case.
The next case is when our robot has turned to the left.
This is the second case.
Now, only A4 and A6 eyes receive signals or are on the black screen.
We will turn the robot to the left at a low speed.
So that we can reach the perfect position.
That is, both A6 and A7 eyes are on the black screen.
The next case is like the third case.
Our robot has turned too much to the left.
At that time, only A4 eyes receive signals or are on the black screen.
Like the first case.
We will turn the robot to the left at a higher speed.
So that we can reach the perfect position.
The last case is when our robot has turned too much to the right.
We will turn the robot to the left at a low speed.
Because when we move the robot, the robot will move forward.
When we move the robot, the robot will move forward.
We have just seen what can happen when we use 4 pink eyes to track.
Now, we will do the adjustment.
The first thing we need to do is to check the parameters of the pink eyes.
Now, we will do the adjustment to get the parameters.
First, you will go to the media tab.
In the previous case, I just showed you how to get the parameters.
This time, I will do it quickly.
First, you will get the speed of light.
You will set it to 9x6.
Next, you will get the starting power.
Then, you will get the zero-in command.
First, you will send it to the legs.
We will check A4 first.
Continue.
We will send the value to A4.
Similarly, we will do the same with A6.
We will read the values in sequence according to the position of the legs.
Continue.
A7.
And finally, A5.
When we send the value to the legs, we will need a little delay.
So we can get the data separately.
We will observe.
We will send the data one by one.
After sending, we will wait for 1 second.
This is the program.
Now, we will install the program in the robot.
After that, we will go to the tools.
Change the tool.
We call it COM5.
We choose 9x6.
Connect.
Here it is.
This is our robot.
Now, we will put our eyes in the black box.
We put it on the outside to measure the value.
And get the average value.
Now, we will read the eyes to get the value.
In the black box.
And we will get the average value.
As you can see, I have taken the values inside.
Outside.
And the average value of each eye.
And here I have listed 6 cases that we have just talked about.
Now, we will adjust according to these 6 cases.
Now, I will delete this.
I will hide the source code so that it will be easier to observe.
First, we will go to the program.
We will attach our eyes.
First, we will attach the eyes A4.
Eyes A4.
By the same value that we get at leg A4.
Do the same.
We will read leg A5.
We will read the value of the average to A5.
Similarly, we will read the value of leg A6.
Similarly, at leg A6.
And finally, at leg A7.
We will get the value at leg A7.
Now, we have the parameters at each leg.
From A4 to A7.
Now, we will add our cases.
Case 1.
That is, only 2 eyes A6 and A7 are on the black box.
The black box has a value greater than its average value.
Here, we will add 4 cases.
The first case is the average value of A4.
And we will attach it to the value we just calculated.
That is 806.
Similarly, the average value of A5.
We will get the next one.
This is the average value of A6.
Finally, this is the average value of A7.
And we will attach it to the values we just calculated.
A6 is 803.
A7 is 786.
And finally, A5 is 800.
And now, we will add to the cases.
Now, we will add to the cases we just put out.
Case 1.
That is, only 2 eyes A6 and A7 are on the black box.
The black box has a value greater than its average value.
Here, we will add 4 cases.
You will get a command.
Execute.
And continue the logic.
You will get the value of VAR.
Here, we will simultaneously set the possible cases.
We will need 4 VAR equations.
Assign.
First, we will check the eyes A4.
10 equations.
Equals.
You will assign.
We will assign.
Add.
And you choose A4.
We will set if A4 has a value greater than the average value of A4.
Continue to assign.
Average value of A4.
A4 is less than the average value of A4.
That is, A4 is on the black box.
Similarly.
Close this link quickly.
We will compare A6.
Case 1.
A6 is greater than its average value.
You will assign.
And here is the average value of A6.
Similarly.
A7 is greater than the average value of A7.
And finally, it is the eye A5.
Here, we try an equation.
You leave it.
It is the eye A5.
You will compare it with the average value of the eye A5.
Here, it will be smaller.
This is case 1.
Case 1.
It has 2 eyes, A6 and A7, on the black box.
You will continue.
This is case 2.
Here, we will compare it with the case.
Case 2.
We will compare it with the cases we have calculated.
This case is case 1.
Continue.
To be quick.
You can guess.
Right click on the arrow.
Create a star.
We will have a block.
In case 2.
That is.
2 eyes, A6 and A4 are on the black box.
And A7 and A5 are on the black box.
You will gradually change the value.
A6 will be smaller.
And A5 will be larger.
According to the case we set.
And this case is case 2.
Continue.
You double our equation.
In case 3.
That is.
Each eye, A5 is on the black box.
And A7 is outside the black box.
You will calculate A7.
A7 will be smaller than its value.
And our value, A5, is larger than its value.
This is case 3.
Continue.
In case 4.
That is.
2 eyes, A6 and A4 are on the black box.
You will change the value.
At A4 and A6.
A6 will be larger.
And this case is case 4.
Continue.
In case 5.
Only eyes, A4, are on the black box.
And the remaining eyes are outside the black box.
This is case 5.
And the last case.
All eyes are outside the black box.
This is case 6.
So we have created a table.
To separate cases.
Depending on our eye type.
Now.
We will check this case variable.
To give the direction of processing.
Again.
We need one more question.
If we do.
Now.
Instead of checking the eyes.
We will check directly.
Our case variable.
You will go to the logic.
Check.
By.
Variable.
Add.
Here we will take.
It is the TH variable.
This is the first case.
If.
Our case variable.
Is equal to 1.
If it is equal to 1.
We will move forward.
And here.
We use.
Instead of using the rule.
We use the rule.
