0:20
everyone welcome to another video of
0:25
this video will cover two things
0:28
first we will see how to use the
0:32
and in the second half we will use this
0:35
encoder to control the positioning of
0:39
by controlling the position i mean the
0:41
angle by which the servo rotates
0:44
so let's start with the encoder first
0:48
i have this encoder here and it's rotary
0:51
as you can rotate the shaft
0:56
the shaft is free to rotate in either
0:59
as there is no limit on the rotation
1:04
it have five pins but we are interested
1:07
in the bottom two pins
1:09
the pin names do not justify their
1:12
so we will call the clock bin as pin a
1:16
pin as pin b the middle one is the
1:20
and it represents the push button on the
1:23
that's it about the encoder now let's
1:32
i got this gif from the wikipedia and it
1:34
shows exactly what happens
1:37
think of the black region as ground and
1:42
so it start with both the pins in
1:44
contact with the white region
1:46
so both the pins are high now let's
1:49
say we move the shaft clockwise
1:52
so the outer pin goes low while the
1:55
inner one is still high
1:57
now the inner pin goes low and both the
2:01
and now they are going back to high
2:10
if we move it counterclockwise the inner
2:14
then the outer one and then they both
2:19
this is the entire working basically
2:22
we just have to check which pin goes low
2:24
first and based on that we can figure
2:27
whether the shaft moved clockwise or
2:31
let's see one more time with the logic
2:36
here i have connected the channels to
3:04
pay attention i am rotating the shaft
3:09
here we got the signal on both pins
3:16
let's zoom in we have some unwanted
3:20
but we will take care of them in the
3:24
let's focus on the main part as you can
3:28
the first pin goes low and after some
3:32
the second one goes low
3:39
now i am rotating it in the other
3:46
this time the second one goes to low
3:48
first and after some time
3:50
the first one goes low
3:55
this is exactly what's shown in this
4:02
this is the entire working of this
4:03
encoder and we will use these pins to
4:06
identify the direction of rotation
4:12
let's create a project in cube id
4:19
give some name to the project and click
4:26
i am selecting the external crystal for
4:30
select serial wire debug
4:33
i have eight megahertz crystal and i
4:36
want the system to run at maximum 72
4:43
let's select two input pins which will
4:46
be connected to the encoder
4:52
click save to generate the project
5:05
here first we will read the pin a
5:12
if this pin a is low then we will check
5:24
actually i am considering this situation
5:27
when the pinner goes low at the moment
5:30
pinby was already low
5:35
and if this happens we will increment
5:44
now before incrementing this counter we
5:47
will wait for the pin b to go back to
5:50
this is necessary to avoid those
5:52
unwanted fluctuations
5:54
also we need to do the same for pin a as
5:57
there are fluctuations in pin a also
6:00
so we will wait for the pin a to go high
6:07
as you can see the pin goes low again
6:10
and it will change the counter value
6:16
so to avoid this let's wait for some
6:18
time before reading the pins again
6:24
similarly when the shaft is rotated in
6:28
we will use the same code with minor
6:37
this time we will check if the pin b is
6:40
this is the part where pinner goes low
6:43
and at this particular time
6:44
if the pin b is high that means the
6:46
movement was different
6:56
now we will wait for the pin b to go low
7:05
this is to avoid these fluctuations
7:19
now when the pin b is low we will
7:32
then we will wait for the pin a to go
7:34
high and then for the pin b
7:55
let's say i want to restrict this
7:57
counter value between
8:06
this should be integer let's debug it
8:32
note the counter value in the live
8:36
i am rotating in counter-clockwise
8:39
and you can see the value is increasing
8:55
now if i rotate in clockwise direction
8:58
the value starts decreasing
9:00
the value is limited between 0 to 20.
9:12
so the encoder is working well but i
9:14
would like the value to increase in the
9:18
so i am changing the counter operation
9:26
this is it about the encoder part now we
9:28
will use this encoder to control the
9:30
movement of servo motor
9:33
i have already covered the servo motor a
9:36
and this is the tutorial for that
9:40
i am taking this as the reference for
9:46
servo motor works with the pulse width
9:49
and like it's mentioned here that a
9:51
pulse of one millisecond will move the
9:54
angle then 1.5 millisecond pulse will
9:58
move it to 90 degrees
10:00
and 2 milliseconds pulse will move it to
10:05
the pulse width needs to be 20
10:08
translates to 50 hertz frequency
10:16
i have also mentioned that some motor
10:18
works between 0.5 millisecond to 2.5
10:23
and i have that kind
10:43
so first of all we will enable the pwm
10:47
i am using timer 1 channel 1 for the pwm
11:03
you can see bot the timer clocks are at
11:08
but if you want to know where the timer
11:10
is connected you can check the
11:11
controller data sheet
11:27
here you can see the timer 1 is
11:29
connected to the ap b2 clock
11:38
and if you see the clock setup the ap
11:40
apb2 timer clock is running at 72
11:46
now we need to bring this clock down to
11:50
if we divide 72 megahertz by 50 hertz
11:54
we get four hundred and forty thousand
11:57
we need to split this value between
12:00
and the auto reload register
12:14
i am using the prescaler of 144
12:18
and auto reload of 10 000 keep the auto
12:23
as this is where the percentage for the
12:25
duty cycle will be calculated
12:27
so if we keep it high the percentage
12:31
and hence more accurate
12:34
let's save this to generate the project
12:38
now like i said the motor needs the
12:40
pulse width between 0.5 milliseconds to
12:50
this means that the motor covers the
12:52
angle of 180 degrees in the difference
13:01
this would translate to 11.11
13:03
microseconds per degree rotation
13:11
let's start the timer in pwm mode
13:17
we will limit the counter between 0 to
13:21
as per the servo rotation now we have
13:25
11.11 microseconds for 1 degree rotation
13:28
and the pulse width is 20 milliseconds
13:33
this is the duty cycle for 1 degree
13:38
let me explain this properly
13:42
in between 0.5 millisecond to 2.5
13:47
the motor covers 180 degrees
13:51
this means the motor will take 11.11
13:54
microseconds for 1 degree rotation
14:05
again we have 20 milliseconds pulse
14:15
so the percentage duty cycle for 1
14:17
degree rotation is 0.000
14:30
this is the percentage from 100
14:37
but we have the auto reload of 10 000
14:40
and hence we will calculate the
14:43
zero 0.000 five five percent of ten
14:51
this will come around five point five
14:55
this is the value which corresponds to
15:09
let's define a variable to store the pwm
15:12
value now we will multiply this value to
15:18
this will give us the duty cycle
15:20
corresponding to the counter value
15:23
and now we will feed this value to the
15:26
captcha compare register
15:28
i am using channel 1 and that's why ccr1
15:33
if you are using any other channel use
15:36
the respective capture compare register
15:42
there is one last thing the motor timing
15:45
starts from the 0.5 milliseconds
15:49
when the pulse width is 0.5 the motor
15:52
will be at 0 degrees
15:55
so we need to also take this into
16:01
the duty percentage for 0.5 is 0.025
16:10
which in our case will become 250
16:23
so we will add this 250 to each of our
16:26
values this will make sure that when the
16:31
the motor must be supplied with 0.5
16:35
and with the increment in the angle the
16:38
time will be added to this 0.5
16:44
this is the servo i am using it's the
16:53
it have three pins the red and brown are
16:58
and the yellow is for the signal
17:07
let's debug it now check the counter
17:11
as it is the angle by which the servo is
17:20
clockwise rotation is increasing the
17:33
it have reached 90 and the servo has
17:35
also rotated by the same amount
17:44
now we are at the maximum 180 degrees
17:49
if we reset the controller here the
17:51
counter will go back to zero
17:53
and the servo will go back to its
17:57
clockwise rotation is increasing the
18:00
counterclockwise rotation is decreasing
18:04
and the servo is also rotating in the
18:11
if you want to debug the registers you
18:19
here check the value of the ccr1
18:34
it's not changing in the real time but
18:38
you can see the value
18:44
this is it for this tutorial i hope
18:49
before using the code check the server
18:53
as it might not work within the 0.5 to
18:57
range most of the servo motors work
19:00
between 1 milliseconds to 2 milliseconds
19:03
so make sure to check this part that's
19:08
you can download the code from the link
19:12
leave comments in case of any doubt keep
19:16
and have a nice day ahead