Bài 7: Lập trình robot giáo dục KCbot điều khiển động cơ servo kết hợp cảm biến siêu âm

Kidscode STEM
12 người theo dõi
33 Lượt xem video·19 thg 11, 2023
---------- ★ ----------
* Website: http://kidscode.edu.vn/
* Email: [email protected]
* Hotline: 0888 424 222.
---------- ★ ----------
Hello everyone, now I would like to start the lesson for today
We will continue with the next lesson in the theme of STEM robots and autonomous vehicles
Last time we went to lesson 4, and today will be part 3 of lesson 4
We will make a robot that avoids obstacles using a supernova sensor combined with a servo engine
Our new subject today is the servo engine
First of all, we will learn about the servo engine
Then we will make a robot that avoids obstacles using a supernova sensor combined with a servo engine
And finally, I will introduce another application, which is the radar robot
We will use servo and engine to turn it into a radar and place it on the robot
That is the content of today's lesson
First, we will learn about the servo engine
As you can see on the screen, the servo engine is a low-speed engine with high torque
And it has many different engines, unlike the DC engine and the step engine
The servo engine usually does not rotate completely 360 degrees
Instead, it usually has a limit of 180 degrees, 270 degrees, or 90 degrees
Recently, I have seen some that have rotated 360 degrees
For the servo engine, we will use a control signal to locate the origin
First, let's look at the inside of the robot
You will see that the servo engine includes a DC engine, which is a one-way electric motor
A gear, which can be called a gear box
And a variable used to determine the position of the rotor
See where it is
And finally, a control circuit
This is the structure of a servo engine
You will see that the DC engine usually runs when the power is turned on
If you rotate these two poles, it will rotate backwards
And the DC engine is attached to a gear box
This makes the speed of the DC engine decrease
And when the speed decreases, the torque increases
This variable has the task of locating
When this pole rotates, this variable also rotates
This variable rotates, leading to a voltage at the center of the variable
Will be measured
And thanks to this, the DC engine will determine the current position of the pole
This is the basic principle of the servo engine
Next, we will learn about some technical parameters of the servo engine
When we need to use it
First, let's look at the size of the engine
There are many types of servo engines
Nano, sub-micro, small, standard and large
It's quite different
The second parameter we need to pay attention to is the power supply
Most engines range from 4.8V to 6V
I will talk about the servo engines we use to make simple devices
For example, robots or models
There are other large servo engines that I will not mention here
The next technical parameter we need to learn is the material used to make the engine
There are usually two types of materials
The material inside is plastic or metal
For example, as you can see on the screen
The white color is plastic
The other material is metal
With plastic materials, the speed is usually faster
But it is not easy to slip the teeth
When the external force is too strong, the inside of the tooth will slip
On the other hand, a mechanical engine with metal teeth
The speed is usually slower
And it will consume more power
But on the other hand, it is more solid
It is not easy to slip the teeth
The next parameter of the servo engine we need to understand is speed
The speed of the servo engine is calculated as the required time to move the shaft of the servo
1 angle is 60 degrees
Usually we will see that the speed drops to about 2.025 seconds
Divided by 60 degrees
To rotate a 60-degree angle, it takes 2.025 seconds
The next parameter we need to pay attention to is the torque
Torque is a very important parameter
It determines the strength of the engine
Torque determines how much torque the servo can apply to the gear 7
Or how much torque the servo can hold
When the servo is placed near the gear 7, the torque is greater
The further it is, the smaller it is
These are some parameters that you need to pay attention to
In the robot, we usually use an engine called SG90
In addition, there is a black engine
It is bigger and has more power
Here we will learn how to connect the engine
For the engine that has a few types
FUTABA, ZR, HITECH
HITECH SG90
The legs will be a little different
For example, the FUBATA engine is black
The first leg is black, connected to the ground
The second leg is red, connected to the source
Source 4 to 6 volts
The third leg is white, which is the control leg
There are only 3 simple legs
A source leg, a ground leg, and a control leg
The green SG90 engine has yellow, orange, and brown
The control leg is white
When you use the engine, you can check the weight
We usually use 2 engines like this
There are a lot of engine applications
For example, a robot arm
You can see 1 engine, 2 engines, 3 engines
Here is another one, 4
Or a robot
The more engines we have, the more power we consume
So we need a high-performance power supply
We need a good quality battery
When we use devices that have a lot of weight
We should choose engines with metal teeth
The weight of the metal teeth should be light
In our robot, there is a green servo engine
You can see the robot
We will use this servo engine
Combined with a supercharger
To avoid overload
The principle is very simple
When we go straight
If there is no obstacle, we continue to go straight
If there is an obstacle, we stop
Then we turn the servo engine to the left
Measure the distance from the supercharger to the obstacle
Next, we turn the engine to the right
So that the supercharger can measure the distance to the obstacle
Next, we measure the distance from the servo engine to the obstacle
We will compare
If the distance is greater, we will turn to the right
In other words, the principle of the servo engine and the supercharger is the same
We will take 3 values
The first value is the distance
When the servo is at the first angle
We will measure the distance from the servo engine to the obstacle
If the distance is greater than the obstacle
We will measure the distance to the left and right
Compare the left and right values
And decide which direction to turn
The goal of this lesson is to do this
Based on the quality of the supercharger and the continuous rotation of the servo
We will make this rotation not 3 steps
Not left, right and straight
We will make it rotate continuously
Imagine it is a radar
We will determine which direction the object is in
In the distance from the servo engine
Or to fix it
We have a fixed radar
In this lesson, we will implement two applications
First, we will learn more about the servo engine
Now we will move to the simulation software
We still remember the Kiskode framework
First, we need to find out the servo engine
To get the servo engine
Click the P button
Pick
We look for the word motor
There are many types of motors
Today we will learn about the servo
We will find the PWM servo motor
We choose
Here you will see a motor like this
It will have 3 legs
In the simulation, the last 2 legs are the main legs
We don't need to care about which leg is the servo and which leg is the servo
In the simulation
In reality, we need to distinguish which leg is the servo and which leg is the servo
The leg in the middle is the control leg
Now I will simulate
First, you will click here
You will see the parameters to simulate this servo engine
The smallest angle is 90 degrees
The largest angle is 90 degrees
The total is 180 degrees
The speed of rotation is 60
And the spring is 1 millisecond
If we don't want 90 degrees
We can change 0 to 180
Minimum Control Pulse is 1 millisecond
Max is 2 milliseconds
This corresponds to the rotation angle from 0 to 180 degrees
We need to adjust this to fit the actual engine
I set the parameters here
Minimum control pulse is 0.54 milliseconds
Maximum control pulse is 2.4 milliseconds
You will understand why later
Now we will simulate
First, I haven't used any controller
I will simulate this one
Let's see how it runs
In the Proteus software, there are many tools to help you simulate electronic devices
Currently, we have not adjusted anything
We just tested this engine
First, we need to remove this
You just need to double-click on the right
You can remove it
Now we need to get the power
Go to the tool bar
1, 2, 3, 4, 5, 6, 7, 8
Terminal 1
Go to Power
To get the power
We will connect to the power supply above
Ground
We will connect to the leg below
And the controller here
We will connect to a sum
Now I will get a sum
You can get it here
There is a generator mode
You can choose Pulse
You get it here
I got one
We will connect this sum to this servo
Of course, we have to see the parameters of this sum
To know what signal we are giving to this engine
To measure the sum, you will need a device called Oscilloscope
It is in the form like this
If you move to it, you will see that there is no display
It is under the Probe Mode
It will have the name Oscilloscope
Click here
This is a device that is very important for computers
To measure signals that change rapidly
For example, in the form of an oscilloscope
There are 4 channels
Channel A, channel B, channel C and channel D
We can measure 4 signals at the same time
Here we only have 1 signal to measure
We will connect to the first channel, channel A
You can see that channel A corresponds to the waveform
The waveform is drawn in yellow
We will only care about the yellow color
We will connect the channel to the point where we want to measure
Now let's look at the parameters of this generator
The low voltage is 0V
The high voltage is 5V
You can see it here
The power supply is 5V
We will set a sum
This is the magnitude of the sum
As we have just seen
If we set it to 1 ms
How much does this generator rotate per angle?
We will continue to test
You can set it to 1Hz or 50Hz
Normally it will be 50Hz
You have to choose the right function here
Function is sum
You don't have to choose C
There are many other modes
Now let's run it
Press play or press F12
You will see a yellow oscilloscope
You can see that the yellow line has a slight curve
We want to see it clearly
First, we will observe the yellow channels
A, B, C, D
These 4 channels are for 4 different signals
Now we don't care about these 3 channels
We can turn it off
Turn it off, press here
Turn it off
Turn off channel C
There is a yellow channel
These are the adjustment buttons for the frequency of the signal
This is the adjustment button for the time of the signal
If we want to increase the frequency
We have to adjust the gauge here
You can see that the gauge is set to 5V
We will set it to 1V
You can see that it is bigger
If you want to make it easier to observe
We will reduce it
Each channel is 0.5mV
This is the gauge
If we want to increase the frequency
We will increase the gauge
You can see that there is a square
You can see that it is 5mS
You can measure it for me
This image is a bit small
I don't know if you can see it clearly
Ok
Black and white
Ok
Can you see it for me?
This channel has 1, 2 small channels
Each small channel is 5mS
Each channel is 5mS
We will have 10mS
Stop
Let's review the parameters
I set it to 1V
So it's a bit small
Now we set it to 5V
Ok
Play
You will see that the frequency
We will have 1O, 2O, 3O, 4O and 5O
5O corresponds to each channel
We have 5O
And about the frequency or time
We have 5mS
10mS
And the angle of rotation
You can see that it is
90 degrees
For example
I want you to pay attention
To know how many degrees
You look at the number here
It is written as 90
And the point of reference is the blue and yellow poles
Normally this yellow pole
It will rotate this way
The angle is 0 degrees
This angle is 90 degrees
1mS will rotate 90 degrees
Now we will increase this
Increase this
We will set it to
Now it's not 1mS anymore
But we set it to
Let's say we set it to 20mS
Let's see how many degrees it is
It is still 90 degrees
Let's see what's wrong
Nothing wrong
1mS
1mS is 90 degrees
2mS
We get 90 degrees
You will see that the angle of rotation of this
It will be changed by
The frequency of the previous wave
We will try another parameter
For example, we set it to 1.5mS
Let's see how much it is
You can see that it returns to 0 degrees
Plus 24
This is considered to be 0
With each frequency like that
With this thickness
Each magnitude of this
It will determine the angle of rotation of this engine
1.5mS
You will see that it is in the position
The starting position is 0 degrees
And it keeps increasing
Now I will change it very small
For example, 1.5mS
Now we change it to 1.6mS
You can see that it has increased to 18 degrees
This is starting to check
Next we increase it to
1.7mS
You get an angle of 36 degrees
Next we will increase it gradually
Until this angle returns to 180 degrees
Right?
But if we increase it by hand
That is, every time we press like this
Then change
So we are doing it manually
We are trying to understand how this engine works
And what is the conclusion?
With the width of the nozzle
Attached to this leg
It will give us different angles of rotation
And with that conclusion
Now we can use the video controller
This screen
We will change the waveform here
Not by hand
But we will use the program
This will rotate the corresponding angles
Now we will go back to the software
Let's adjust it to make it more automatic
Like this
We turn on the KISCode software
As always, we will choose the block
Start
And now we will choose
There is a block inside
Engine block
For example, we set
For servo
At pin
Here we will use pin A0
This is pin A0
And in the frame
We will mark it as S2
We will connect pin A0
Go straight here
Delete this
Connect pin A0 and go straight here
That is, supply signal from A0
And into the servo engine
Right?
At that time, we have to leave this source
Otherwise it will be stuck together
And this oscillator
We still leave it to fill the signal
Set this to 90 degrees
And now we will try a few times
See the different degrees
First we set
30 degrees
Then we will leave it a little bit
Next we will set
60 degrees
90 degrees
Every time we shoot like this
And the last time we shoot
180 degrees
And we will edit this program
The program has been successfully edited
And we will still
Every time we click on
Select the hack file
And you remember
It is in the story hack
In the kiskode software
It has the kiskode story hack
You search for it
And select
kiskodesket.ino
Open
And F12
Or play
Let's run
We still set it to 90 degrees
So you can't see it
Rotate exactly where you want
We set it to 0
And plus 180
Time as I said before
Set this to 0.54
And this is 2.4
This is what we set
To match the engine
We use
Play
You will see
0, 60, 90, 90
I haven't seen 180
Here is 180
But we are missing a command
Wait
So it will fly back
We can't see it
Let's re-translate
30, 60, 90
180
You see the response
Of the frequency
Of this wave
30, 60, 90
180
You see
And if we want it to run
Continuously
We have to use
A form
Let's use a form
Let's leave it
Re-insert
We go to
Back
We will choose the form
Here
I from 1
I from 1 to
180
Increase 1 degree each time
And we will set the servo
To
I
Perfect
We will put a node here
And choose I
We will let it run continuously
Each time I runs from 1 to 180
Each time it adds a unit
I from 1 to
1 degree
1 second later
Increase I to 1
I from 2 to
1 second later
3 degrees, 4 degrees, 5 degrees, 6 degrees
Up to 180 degrees
Let's run
If you observe closely
It is changing
I leave it for 1 second
It will take a long time
We can increase it
We only need
10ms
You will see it turn
Up to 180 degrees
The number 180 does not see
Because it turns very fast
If we want to reverse
We do this
I from 180
Each time we run
180 to
1
It is still 0
Each time we retreat
You will see it turn
Retreat
You will see it retreat
That's right
That is the principle of operation
This servo
Combine with the principle of the previous supersonic
Today we will combine these two things
Together
To help
Supersonic feel
A wider area
If we run at a very fast speed
We will collect a larger area
The angle of 30 degrees
of the previous supersonic
This part I have
Asked you to prepare a video
Invite you to watch the video here
And now we will go to
Imitate this servo
Instead of the previous servo
We will use 3 servos
In this servo
We only need to use 1 servo
With a servo engine
First we will run
Imitate the servo engine and process it
First you will search
Then you look for the servo engine
Servo
And you will get the first one
For the supersonic
Then you will connect
Legs to 0
You pay attention
We have 3 legs
2 legs on both sides
Is the source
And the leg in the middle is the reference leg
Imitate 2 legs
Is 2 legs on both sides
You pay attention
Legs on both sides
Is 2 legs on both sides
As you can see in the imitation
We have 2 legs here
2 legs on this leg
Now process
You can fix it to 2
First we will set it to 90
Next
You take the time
Or decide to wait a second
Now we will try
Servo engine to open
1 is angle 90
2 is angle 45
Continue
We will wait a second
To be able to observe
Then we will process
After processing
You will run the simulation
You can see here
You can observe
Angle 90
Angle 45
As you know
Servo engine
Now we will process
Today
We will simulate
Servo engine
The principle of operation
When the servo engine receives the simulation
We will stop
Then the servo engine will turn to both sides
Turn to both sides
And check
Which side has a wider distance
We will turn to that side
Now we will process
Next
We will have a condition
If done
First, we will check that the value of both sides
When it is in the middle
Is smaller or larger than the required value
Here you will get more digits
Get more
And here we will get bigger
Next you will continue
Take the command
Set the distance
We will enter the value we want
You will go to the number table
Here
I will set the value to 15
If the distance we receive
Is greater than 15
That is, before the collision
We just have to ask
To move forward
You can move up
And the speed is 200
And if the distance we receive is less than 15
This time
You just have to close the door
If not
We go back to the above
The value we receive is less than 15
We will proceed to process
The first thing we need is to stop the robot
Stop moving
We will stop moving for about
500 milliseconds
After that we will proceed
Return to Percival
To be able to read the value
You will take the command
That is the Percival f1 command
90
Percival f1, Percival f2
You will take f2
And angle
We will take the angle
30
Our eyes will turn to angle 30
After we turn Percival to angle 30
We will wait for 500 milliseconds
Continue 500 milliseconds
To help our eyes stabilize
After that
We will proceed to read the value
You will go to the two sides
First
You will go to the right
You will take the command
That is right
The value of right
Will be read by Percival
We will proceed to wait for 100 milliseconds
To stabilize
After that
We will turn Percival to angle
150 degrees
This angle will be on the left
And similarly
We will wait for 500 milliseconds
To help Percival stabilize
After that
We will read the value of left
On the left
Similarly
The left will be read by Percival
To recognize
After that, we will wait for 100 milliseconds
After 100 milliseconds
We will run Percival
To angle 90 degrees
So
We have read the two values
Right and left
Now we will process
We will need a command
Now we will compare
If the right angle is larger than the left
We will turn to the right
And vice versa
If the right angle is smaller than the left
We will turn to the left
So the command is if
We will get another command
That is if not
We will get a bigger value
First
We will compare the right angle to the left
We will take the command
If the right angle is larger than the left
We will turn to the right
We will take the command
Then
We will turn to the right
With a speed of 1.50
And turn right in 500 milliseconds
Time
500 milliseconds
Similarly, we will turn to the left
Turn left
So we have done
And to help Percival observe
We will add a command
To use the data on the computer
kservo
You will choose the level 96
Here is a command
Transmission
We will call it the value of the circuit
And then you will run
You will run it
If it is successful
You will receive a message
Now we will run the simulation
We will start running
I see here my circuit is 26
26 is larger than 15
So my robot is ahead
I will reduce the distance
Value up to 50
As you can see
Percival has turned right
90
And after reducing the distance
Percival has turned to the right
With a larger distance
To make it easier for you to observe
We will quickly load into the body of the robot
And to make a robot
You will leave the last command
We don't need this value
Now we will load the robot
We will load the robot into the computer
We will connect the robot
We will turn on the robot
We will connect the robot
Press the button
We will choose the light mode
So
We can finish this lesson
In addition
Kservo and Percival
There is another useful application
We can create a radar
Now
Let's do the simulation
Create a radar from the robot Kservo
We will put it on the computer
The first thing we need is the initial command
Here we will
Percival
To get Kservo
We will go to the loop
We will count
I
I will run at the angle of Kservo
But here I only run
At an angle of 170 degrees
Increase one unit at a time
Next
We will set the value
I is the angle of Kservo
You and the engine
Will set Kservo at the leg
As the simulation
We go to the leg A0
We will change
To leg A0
We will not leave it fixed
Here we will put it in
I
We will send the data
To the computer
First
We will send the I to the outside
You will get
That is
Transmission
Note that we will put
At the new line
The first thing we send
That is our I
To know the angle
We will set the angle
Next
You will send a line
You will go to the network
We will send the line
To separate the two data
The data we send
It will be the value of the leg
But we will create a variable
Variable Ks
I agree
This variable we will use the value
The leg we get
Then we send the data
Our Ks
And finally we will continue it
With the line
We send the data
To rotate the angle
Finally we will need
Wait about
10 seconds
And so we got
Angle from
10 to 170
To rotate
According to the leg
We will rotate the leg
From angle 170
To angle 10
To do this
We will increase it by 1
Not 1
And so we are done
Now we will see what we can do with the robot
And so we are done
Now
To get the data for the computer
We will need
A software to support
This
As you can see
The robot is running
As you can see
The robot is running
Now we will set
The robot mode
The robot will receive
The data
And so we have
Adjusted a part of the robot
To become a data
You can use this button
To do many things
So today's lesson is over
