Bài 5: Lập trình Robot giáo dục KCbot và cảm biến siêu âm

Kidscode STEM
12 người theo dõi
193 Lượt xem video·21 thg 11, 2023
---------- ★ ----------
* Website: http://kidscode.edu.vn/
* Email: [email protected]
* Hotline: 0888 424 222.
---------- ★ ----------
Hello teachers and students
Welcome back to STEM Science Robots and Autonomous Vehicles
I'm Hoang Van Dong, founder of Kikko STEM
Continuing from the previous lesson, today we will move on to the 4th lesson
with the theme of Robots Avoiding Obstacles Using Supersonic Sensors
In this 4th lesson, we will have 3 sessions
In each session, we will focus on one aspect of the theme of Robots Avoiding Obstacles Using Supersonic Sensors
In the first session, we will focus on learning about supersonic sensors, their principles and supersonic sensor modules
and use them to make a simple robot avoiding obstacles
In the 2nd session, we will use a lot of supersonic sensors at once to make the robot avoid obstacles more flexibly
In the 3rd session, we will learn about Robots Avoiding Obstacles Using Supersonic Sensors Combined with Supersonic Motors
That is, we only use one supersonic sensor combined with a supersonic motor to rotate corners to find the way
That is the content of the 3 sessions in the 4th lesson
In this session, we will focus on the first topic, which is Supersonic Sensors and Robots Avoiding Obstacles
The content of today's lesson has 3 parts
The first part is to learn about the concept of supersonic sensors
Next, we will learn about the operating principles of supersonic sensors
and then we will proceed with the process to make the robot avoid obstacles using supersonic sensors
The first part is about supersonic sensors
As everyone knows, animals use supersonic sensors
In the daytime, we can see with our eyes, but at night, we will use our nose or mouth to detect insects
In the seabed, there is not much light, so we have to use a supersonic signal to detect small fish
We humans also know how to use supersonic sensors to locate submarines
These are some typical applications of supersonic sensors
This application is called echolocation
In English, echolocation means echo
In Vietnamese, we call it echo
Echolocation is the use of sound waves and echo to locate objects in space
For example, if you want to locate an insect, it will emit a supersonic wave
When interacting with an insect, this supersonic wave will send a feedback wave
The drone will be able to calculate the distance from itself to the insect
Therefore, it applies the principle of positioning by sound
The drone uses this ability to locate and locate food in the dark
To have sound, the drone will send a sound wave from the mouth or nose
The frequency can be different
Normally, to create a sound wave, the drone emits a sound with a proper frequency
When concentrating on the insect, the drone emits a supersonic wave with a higher frequency to be able to detect the insect
When the supersonic wave touches an object, it will reflect back and create a sound
This is the principle of echolocation
If you pay attention, you will find that the blind often have a better perception than the average person
Because the blind also uses the principle of echolocation to locate objects in space
In fact, because we are used to the sound, we do not pay attention
If we turn off all the sound in this space, we will not be able to locate our space
There is a video that you can watch on YouTube
Without sound, it is very difficult to locate space
We also know that the supersonic wave has a higher frequency than our sound
Our sound is the green line here
It ranges from 20Hz to 20kHz
Includes humans and pets in the house
Between 20Hz and 20kHz, we call it a sound
Electron is an elephant, it can emit and hear the sound
And larger than 20kHz, we also have a yellow line, which is the supersonic wave
Electron is an elephant, it can emit and hear the supersonic wave
A very important example of supersonic wave is in medicine
We often use supersonic waves to check our health
That is the general principle of supersonic waves
Next, I would like to introduce the supersonic sensor
On the screen, you can see the image of a very common supersonic module
Named SC-SR04
In addition to this SC-SR04 module, there are other models on the market
Such as SC-SR05, SC-US05, etc.
However, all of these sensors are different in accuracy and range
However, we will use SC-SR04
SC-SR04 sensor module uses supersonic waves
To determine the distance without having to touch an object
Like the way Mr. Roi and Ms. Ao did above
This module is not affected by sunlight
Or affected by a black material
Compared to other modules that we often use as external sensors
It is affected by sunlight
This supersonic module is not affected by sunlight
Usually, a supersonic module will have a pair of tools and supersonic waves
As you can see on the screen
The supersonic wave set includes
It is marked next to the letter T
The principle of the word is TRANSMIT
And the supersonic wave receiver is marked with the letter R
Which is RECEIVE
Each module usually has 4 pins
The first pin is VCC
It is the power supply for the module
The power supply here is VCC, so we will supply 5V
The next pin is TRIG
It is the IN pin, which is the input pin of this module
When this pin receives a square wave
It will activate the T pin here to generate a supersonic wave
The next pin is ECHO
It is the OUT pin of this module
This pin will tell us that there is a supersonic wave
When this pin emits and hits a white object
It will reflect back
When this receiver receives that reflection signal
This ECHO pin will tell us
And the last pin is GND
It is the ground pin in the power supply
That is the structure of the supersonic wave module
Some notes and some parameters of the supersonic wave module
We need to pay attention to
First, the physical size of the supersonic wave module
15mm, 43mm and 20mm
In length, width and height
However, there is a parameter that we need to pay attention to
That is the operating angle
Or the operating market of the supersonic wave module
That market is at 30 degrees
The best in 30 degrees
It means that in the objects at 30 degrees
This supersonic wave module is the best
And all the objects outside
Our supersonic wave module will not be able to detect
This leads to the third article
We will attach a servo motor to rotate this angle
Rotate the supersonic wave module
It may expand the supersonic observation market
We will discuss in the third article
There are two points
When we supply power, we remember to supply the GND pin first
When supplying the VCC pin
And the surface of the object that we need to measure the distance
Should have an area of at least 0.5 square meters
To work best
However, in practice, I find it smaller
It can still be relatively stable
But this is an advice from the manufacturer
Some other technical parameters of the supersonic wave module
That we need to pay attention to
The power supply is 5V and above
The current when not in operation will be less than 2mA
The current when the supersonic wave module is in operation is 15mA
This parameter is only needed when we design the circuit
When we design the circuit, we need to pay attention to these parameters
15mA
The operating angle is 15 degrees
The measurement distance is from 2cm to 400cm
Which is about 4m
The width is 0.3cm
We can measure distances of 2cm, 2.3cm, 2.6cm, 2.9cm, etc.
Up to 400cm
That is the width
The measurement angle is 30 degrees
As I analyzed above
The thickness of the spring that enters the trigger leg
Is at least 10μs
That is, we need to put a spring of about 10μs into this leg
Then the supersonic wave is emitted at this face
If it is smaller, the supersonic wave will not be emitted at this face
That is, it has not been activated yet
The size is 45mm x 20mm x 15mm
These are the technical parameters of this supersonic wave module
Now let's move on to how to make the supersonic wave module
Can measure the distance from it to the object without contact
This is the formula that helps us calculate
As you know, the supersonic wave module has two faces
One emitting and one receiving
The principle is very simple
When the emitting face emits a supersonic signal
This supersonic signal will hit the object
And there will be a feedback wave
That is, an echo
When the feedback wave returns, the receiving face will receive
This signal is based on that feature that people calculate as follows
Suppose d is the distance that we need to find
From the supersonic wave module
Or our robot
To the object
And t is the time that the supersonic wave transmits from the emitting face
In the emitting face until it receives the receiving face
These are the two parameters that we have named here
So we will have
S is the path of the supersonic wave
As we have just analyzed above
The supersonic wave will go from the emitting face to the object
Then return
So S will be equal to 2 times d
One turn to and one turn back
On the other hand, we also have
S by V times t
According to the formula, the path is the speed factor times time
In which V is the speed of the supersonic wave
The speed of the supersonic wave is also the speed of sound
Equal to 344 meters per second
And 0.0344 cm per second
By combining these two formulas
We will have 2 times d by S
And V times t
Substitute above
From there we will get d
It will be equal to 0.0344 times t
Divide by 2
And it is equal to t
Divide by 58
Where d is calculated in centimeters
And t is measured in microseconds
So to find d
We just have to find t
That is, the time that the supersonic wave
From the emission to the return
Knowing t, divide by 58
We will get the distance
Distance from the supersonic module to the object
That is the operating principle of the supersonic sensor module
In the control process
We have to do it step by step
However, with the linear program
It is very simple
We just need to draw a line to measure this distance
All the image formulas
Have been hidden
In the Hiscope software
And in the program code
If you want to find out
You can open the C program
Some notes
When we use the supersonic sensor
Especially when we arrange it
Sometimes this feeling
It is not accurate
Because of the following reasons
For example, the first case
The object is too far
Compared to this module
Here you see
It is larger than 3.3 meters
It has reduced the accuracy
According to the parameters of the manufacturer
It is 4 meters
But in fact, when measuring
It is about 4.5 meters
It can still be measured
The best accuracy is within 3.3 meters
The second case
The surface of the object
It is not perpendicular
With our supersonic module
Like this
Then we can immediately understand
If the theta angle is less than 45 degrees
Then the wave
It will not be reflected back
And it will dissipate like this
Leading to no feedback
This is the second case
We can't measure the distance
The third case
The object in front of the eye
It is too small
This is easy to understand
If we send it out
There is still a reflection
But its frequency is small
It will lead to no feedback
The fourth case
The object is too soft
Or hollow
Like this
It can also reduce the accuracy
The result is
When we measure
We need to be careful
Next day
We will have a competition
Using this supersonic module
To go over a box
Or a maze
The maze itself
Will have to avoid materials
Too thin or too thick
Like this
When you measure
You have to make this eye
Try to be square
So that the object
Or at least check a moderate degree
Then our module can recognize
The reflection back
I want you to pay attention to this point
Because the sensor
Measure the initial value
It determines the entire process
The third part
We will move to
Process for
Robot to avoid materials
Using supersonic sensors
This is our box
Our robot
On the box
You still have 2 modules
M1 and M2
Our supersonic sensor module
Will plug into the plug
We also see there are 4 wires
Because it is standard
We will not be afraid of each other
We also have VCC
Trigger
Trig
Echo
GND
This is the design
On this leg
Trig leg
We are already connected to leg 2
Echo leg
We are already connected to leg 12
In the process
We have to choose the correct leg 2 and leg 12
If we don't want to use these 2 legs
We have to plug this supersonic sensor
Out of the other legs
And not plug here
In practice, we just need to take this wire
Hold it straight here
We can run
In this case
We will use a simple command
Read distance
This command will return the distance
From the robot
To the robot's eye object
Length from
2 cm to
400 cm
Here you can see
Number 2 and number 12
Number 2 is the trigger
Trig
Echo
GND
We are already connected to leg 12
So we only need to plug in 2 numbers
All of these commands
Will return the value
Distance from the robot
To its eye object
In the measurement of the supersonic sensor
Our task
In this lesson
There will be 3 tasks
The first task is to read the distance from the supersonic sensor
And send the read value
To the computer screen
To read the distance from the supersonic sensor
We just need to pull this command
And to send it to the computer screen
Why do we have to send it to the computer?
Because we want to observe the data
But we need a screen to see
The most convenient is the computer screen
To send data
To the computer screen
We will use 2 commands
2 blue commands
The first command is
Set the mode for communication
Between robot and computer
Set the serial speed
9600 bits per second
This is the first command
The second command is
Serial in
In our communication
If there is any data
We will print it on the screen
What is our data?
It is the distance
The distance from the robot to the sensor
We will send the distance
To the computer screen
By this command
You just need to pull a program like this
And you put your hand
On the supersonic module
The speed will change
That is the first task
The second task is
We need to schedule for the robot to work
If there is no sensor
We will move forward
If there is a light, we will stop
If there is a sensor
We will stop
That is the program
I will analyze the program first
And we will do it later
If the distance
We can read the distance
If the distance from the robot to the sensor
Is greater than 2.5 cm
That is the distance
We will continue to move forward
If the distance
From the robot to the sensor
Is less than 2.5 cm
We will stop
That is the next example
The third task
We will increase a little
The second task
If we don't stop
We will continue to move forward
And move
It is similar to the above command
But now we will add
Another command
If the distance
Is greater than 2.5 cm
We will continue to move forward
If the distance is less than 2.5 cm
We will stop
Then we will move the robot
To the sensor
These are simple examples
To make a robot
To the sensor
In the second and third tasks
We will go deeper
In the sensor
Now we will simulate
The robot
We invite you to watch
The video we have prepared
Today we will go to the simulation task
Simulate the robot to the sensor
First we will simulate
The robot to the sensor
Open the simulation file
We have done in the power tab
This is the engine simulation file
We will continue to use
In this simulation file, we have the engine simulation
And the sensor
We need another sensor
We will find
V and find Ultra
We will go to the Steam Education Box
We will get the UltraSolid
This is our sensor
We put it out
Here
In the power tab
The sensor is connected to the number 2 and the number 12
The particle is equal to the number 2
And the electron is equal to the number 12
We will connect
Electron is equal to the number 12
And the particle is equal to the number 2
To simulate, we do not need to connect
5V and GND
To simulate the action of the sensor
We need a resistor
We will find
V
We call it VHT
We will use
VHT
We will take it out
This is our resistor
We use 1K
You will connect the resistor
to the SIM pin
We will connect the power
to the ground of the resistor
The connection is complete
The connection is still hard
To run the simulation
of our sensor
To run the simulation of our sensor
You will need to run the hack
for this sensor
You click on the sensor image
In the UltraSolid.hack
Click and choose
In our library
You type in
kitcode.edu.vn
You open it
You will see a file called UltraSolidSensor.hack
You click on it
And open
That's it
We have finished
Connecting and running the hack for the sensor
Now we run the simulation
with the K4 machine
Next, you will open the simulation file
and type the code
Now we run the simulation
In the first step
We will send the data
of the sensor to the
transmission port of the robot
We will display the
distance on the computer screen
You will see here
We will have a transmission port
You will get the password
The speed of the load is 300
The speed of the load
We can use different speeds
But the usual speed is 9600
You will choose 9600
Next
You will get the starting block
In the KC4 port
You go back to the transmission port
Get the zero-in command
Click on the start button
And what to print
We will print the value of the beam
The value of the beam
We will read in the KC4 port
You find the command
The distance between the feet is 2
And the number of feet is 12
From the feet
We will print the value of the beam
You will combine the command
and the zero-in command
When you put the command close to the beam
You will see the light
and the music
The two commands will be combined
as you see on the screen
Next, we will add a command
We will wait for 1 second
We will display once
Wait for 1 second
OK, we have finished
the data transfer
of the beam to the computer
Now we will do the translation
You wait for the translation
of our code
So it should be successful
Now we will go back
to the simulation
You go to the stem education board
Then in the program file
You choose
the simulation
OK
And
In order to receive the data
that Microsoft sends
We need a terminal
In the simulation
We will zoom in
in this part
like a pin
Choose the visual terminal
and choose the visual terminal
Then you will go here
Our x-axis and x-axis
x-axis we go to
x-axis of the terminal
and x-axis we go to
x-axis of the terminal
Tx is for transfer
and Rx is for receive
Transfer we go to receive
and receive we go to transfer
Then we are done
Now we will go to the room
You see here
Now I will change the way
You see here
We will receive the value
of the beam when this percentage
is less than 40 years
Gradually
You will notice that the value of this side
will gradually decrease
The value of this side is 1 cm
I will reduce it
to 10%
As you can see here
when I reduce it to 10%
The value we receive is only
111 cm
We reduce it to 5
We only have 55 cm
So
Our simulation program has been successful
Now we will load the program into the robot
to see how it works
Now we will connect the robot to
our computer
Connect
Connection
And you will load the program
As you can see here
We have finished loading the program
Now to check
You will go to the tool
of the tool software
This is the part
to receive the data from the robot
The load speed here
As you can see in the software
The speed is 9.6
You choose 9.6
Plus
You choose the robot combo
Connection
As you can see here
The value we receive is
the value of this side
Now we will change the value
I will reduce it
to 8
As you can see here
The value we receive is only 4 cm
This distance is the distance from the
edge of our whiteboard
So we can use
this distance to check
if the robot is touching the whiteboard
Check if the robot is touching the whiteboard
So we have just
completed the simulation
and simulation for the robot
to send data from the whiteboard
to the computer
Now we will perform
the second task
which is to simulate for the robot
When the robot touches the whiteboard
The robot will stop and turn to the left
The left side is our M1
Now our simulation
will not change anything
Because we already have the engine
Camtasia and PowerMax
Now we will
do the simulation
Here we already know
that the distance is the distance
from the edge of the whiteboard
to the computer
Now we will
change the value
I will reduce it
to 8
As you can see here
The distance is the distance
from the edge of the whiteboard
to the computer
Now we will
change the value
I will reduce it
to 8
If the value
we get is less than 30
That is, there is a edge in front of it
then it will stop
Now we continue
to the logic command
We will take an arrow
You will paste it in the command
If it is done, you move it closer and release it
The arrow here we will take
is a bigger arrow
You click on the arrow
We will take a bigger arrow
This is the value of the arrow we received
It is the command to read the distance
And the rest we will take
in the math part
We will take the number 0
and change it to number 30
That is, if our distance
is greater than 30
Then we will just go
We will just move the arrow forward
and paste the number 4
Take the command to move the arrow
And we have measured
200
You just know how to create
Here
If the distance is
less than 30
Then we will stop the robot
Here we will take the command
Stop moving
At the bottom, stop moving
You paste it here
We have finished the robot
If there is no obstacle, then continue
If there is an obstacle within 30
Then it will stop
Now we will start
Now we will go back to the room
You see here
Now I will continue to run the room
Now we don't need this anymore
We can delete it
As you can see here, as I said earlier
5% is equal to 50
Now I will lower the center
2%
As you can see here
2% is equal to 20 cm
Then our robot will stop
If our robot is
smaller than 30
Then it will stop
Now I will increase the distance
30
Then the robot will start moving
As you can see
The robot has started moving
And if there is an obstacle
For 20 cm
Then it will stop
But our request is to fix the robot
When there is an obstacle
Stop and turn it to the left
Here
We will take one more command
We will let the robot stop in the room
200 ms
Then let the robot turn left
300 ms
I will continue to cut the code
Here you can see
Turn left
We can increase the speed to 100
Continue to turn left
200 ms
I will stop
And then we will run the room
As you can see here
You can see that the robot has just stopped
We will let the robot turn left
Because we just want to stop
The distance here is only 20 cm
I will increase the distance
As you can see, the robot has turned forward
And when stopping
And when the distance is smaller than 20 cm
And when the distance is smaller than 30 cm
The robot will immediately stop
And the robot will turn left
So we have successfully finished the room
Now we will run on the real robot
We will run this program on the robot
5 robots
Including the robot
And the program
As you can see
The program is successful
Now we will run
Hello everyone
Good night
See you again in the next part
