Hello, guys, and welcome back and this lecture, we're going to demonstrate how we can control a servo
motor using Adreno.
So let us get started by first getting our Adreno, as usual with Bridport.
And let's play it now, we need some survival mode.
Let's go to components and hit on all and then let's write Servoz.
And let's take this micro server right here and place it.
Here.
Now we have three terminals, the power genndy and signal, as usual, power will be connected to the
positive, Genndy will be connected to the negative and signal will be connected to any B.W. And then
we have talked about GWM and we know that it sends pulses and it fluctuates between on and off in order
to create an illusion of analog signal.
So how do we know which PIN supports BW?
M. Now not all Bence do support W.M. because again the AFP considers intervals.
So there was a timer inside the Adreno that's going to give you the required signal with the right modulation,
meaning with the right cycle percentage.
So if you take a look at the numbers between zero and 13, you'll see that some of the numbers have
this girlie line next to it.
You have at the three, five, six, nine, 10 and 11.
So all of these are B.W. And as you can see here, you'll see that digital or if it has this girlie
line.
So any of this would work to generate a signal.
So let us connected to these three.
And, well, let us start writing some code.
Let's go to code blocks, choose text, continue and let us start writing.
No.
There is a library that helps us.
To control the serval, so it has everything we need in order to control it.
We just give it the right angle and the motor is going to rotate accordingly.
So if you go here to libraries and click on it, you'll see multiple libraries that can be used, external
libraries that are not included in the standard Audrina package.
So let's say that we want this serval.
Click on include and you're going to see this line added here.
So now we have a new library with the newer functions that we can use.
Next, I want to define some things as a global.
Now, a global definition is when you define variables that can be seen from any scale.
It could be seen from set up and it could be seen from a loop.
Now, when we go and talk about functions, you're going to understand the scope a little bit more.
But for now, let us define some valuables here at the top.
So I'm going to define Servoz and it's going to be called control server.
So I am defining something called an object, which is called Servoz, and I'm creating an instance
of it, which is called controlled servo.
Now, I don't want you to go hard on yourself and say, OK, I don't know what is an object.
Well, these are all C++ terms, but it's very easy to use them here in Adreno.
OK, so you create an object, which means it's like a package that has everything in order to control
a servo.
And you call this package control servo.
So this name cannot be changed.
It is coming from inside this library.
So this library has has multiple classes and this class is called Servoz.
So you're creating just an instant of this servo and it's called controlled servo.
OK, so this name cannot be changed, but this name can be anything you want.
So it's like defining a survival device and you're calling it controlled servo.
OK, next, I want to define another variable, which is the position.
So I'm going to call here integer position, and it's going to equal to zero, so this is the position
of my motor.
You know, this motor, the servo motor rotates between zero and 180 degrees.
So it's not a continuous rotation at 360 degrees.
No servo is used in order to control angles.
You just want to control something at a certain angle.
You go for servo motor.
Now, a servo motor, as I said, can rotate, only have rotation between zero and 180.
So this position is going to determine what angle are we going to rotate next.
I need to define which bend do I need to use in order to control my servo.
OK, so remember I created an object called Controlled Servo, so I'm going to say control servo.
Not attach and the name of the pin we are using, which is PIN number three.
So what is this again?
Imagine that this is your servo device and it's called controlled servo.
Now, this control circle has multiple options inside it.
Some of them are used to drive the circle.
Some of them are used to configure the servo.
So I am configuring this controlled servo and I'm saying attach pin number three.
Now, all of these key words are coming from inside this library, so they cannot be changed.
The only thing that can be changed is the name of this device anyways.
So we are saying that I have installed my control servo at PIN number three.
This is what this is how we configure our servo motor.
Next, I want to rotate this motor so I could simply say control servo dart.
Which function do I want to use, let's say?
Right.
It means that I'm going to control the angle.
Let's say I say right 90 or let's say I'm going to write 50.
What this will do is it's going to rotate my motor to an angle of 15 if I click on simulation.
My motor will rotate at 50 degrees.
That's a great.
Now, let's say I want to rotate it at 50 and let's say I want to wait for two seconds using my delay
function, so I delay, let's say two thousand milliseconds, meaning two seconds, and then I want
to rotate it, let's say, to 180 degrees.
And then I want to wait another two seconds.
By that, I want rotated, let's say, back to 10 degrees.
So what is this this library here has the capability of controlling the server without actually working
with DPW and directly, so I, I would just call the method here of the object and it's called.
Right.
So I'm going to say my controls control what device?
I'm going to write its angle to it to 50.
Then I'm going to wait for two seconds by using the delay function.
Two seconds, I mean 2000 milliseconds.
So I write two thousand then I want to change the angle to 180, wait two seconds and then change it
to eight to ten and then this will repeat then going back to one hundred eighty ten and so on and so
forth.
Let us click on Simulation.
And here we go, two seconds.
Two seconds.
Now, why here we are not waiting between the 10 and the 50 because there is no delay after that then
and this is repeating this is why it seems like only to actions.
So I'm going to add one more delay here.
So let me start simulation first angle.
Second angle.
First angle, second angle, third angle, this 180, this is 10, this is 50, this is on the data
and it's repeating for, you know, how about we utilize for loops in order to create a continuous rotation
between zero and 80 and then back from other data to zero.
In order to do that, we just going to say for.
Position equals zero position is defined here, or actually we can just remove it from here and just
define it inside the for loop like the entire we have learned about so integral position semicolon.
I want to go till position is equal or less to 180.
What's the what I'm going to do is I'm going to say position.
Increment.
So I'm going to increment between zero and one hundred eighty one by one.
Now control Servoz.
And I'm going to say, right, let's say the position and then I'm going to delay for around 50 milliseconds,
OK?
So at every iteration I am increasing the position by one degree and then waiting for 50 milliseconds.
So a full rotation should last 180 multiplied by 50 is around 10 seconds.
Now, we forgot one thing that we need the curly.
Bracket right here and this full rotation will last 50 times under 80, which is around nine seconds
left of this.
So a full rotation between zero 180 is lasting exactly like this, and as you can see, the position
is resetting to zero every time.
So this is going back to zero now, how about we forgot to add this curly bracket, what's going to
happen?
Well, what's going to happen is the follow up is all will be connected to this line and this delay
will be totally meaningless.
So we will see it in a fraction of a second, rotating between zero and hundred eighty.
Take a look here.
And as you can see, it is having some jitter right here because it's going back to zero one hundred
eighty really quickly and it's barely enough for the motor to rotate.
So it's like you're telling it, go to zero, one, two, three, up 280 really quickly and then you're
going to count one 012, up 280 over and over and over again really fast so that the motor cannot really
keep up with this counting.
So this is why we need those curly brackets here right now.
How about we look the rotation go back from 180 to zero also within nine seconds.
So if I copy this right now, it's returning really fast to a degree.
So in order to do that or just copy this pasted here.
No, I'm going to count backwards from hundred eighty back to zero, so the condition here is going
to be I want to keep counting down until it is larger or equal to zero.
OK, and by that I want a decremental 180.
Remember, when this loop ends, it's going to end at 280.
When this little begins, it starts at 180 and it decreases back.
OK, so this is smooth and continuous.
It should take around 18 seconds to go 280 and go back to zero.
That a simulation.
And here we go, we are going back really smoothly.
That's amazing.
So the motor is really doing well, so by that we are done controlling this motor.