Now we will learn about how we can control the servo motor using ros2.
And for that we have to include a library which is esp32 servo library, and this will be dealing with
the servo stuff and we will click sketch include library manage libraries.
And from here we can search our library, which is Esp32 and Servo.
This is the library Esp32 server and you can click install and this will be installed.
And now we will go to the file example we will look for that included library which is here Esp32 server
and it has the example code.
We are going to select these three and we will take this code as a reference and we will be editing
and modifying our code and first place we will include this library.
There is an object of desire.
Okay.
Then we have to mention the server pin.
This server pin in our case is.
So is connected to the PIN number 12.
Here you can see.
So we will try to 12.
So in the word setup, we have to mention all these conventions that is provided by the example code.
We will copy this simply and go to the word setup.
And here we will mention this, verify this if everything goes well.
So our library is included successfully and it's also configured.
Now we have to create a subscriber for the server.
Here we will create the object of the server.
And we can now close this in here.
We have to create the subscriber for this.
The message will be again and add because in the server we have to operate the server from angle zero
to angle 45 at the maximum and we can easily handle in in it.
Let's mention a server subscriber here, subscriber and here we will also add this line for a new subscriber.
We are feeding the subscriber to the executor and here we have mentioned the server subscriber server
message and here it is server callback that we have to create right now.
And here we will write the server callback function and and there will be a position of this server.
Let's create a variable that is in server position that will be keeping track of the server position
as we have to limit the server position to 45 degree angle.
So we will be creating a function and limit to maximum value and it takes the input value and the maximum
value.
If the value exceeds the maximum limit, it will then give us the maximum allowed limit.
It means whenever a user inputs 180 degree angle, it will not feed the 180 degree.
Exactly.
Because if it feeds 180 degree angle, the server will be pushing its boundary.
For that reason, we will limit here the server position.
Variable server position is limited to the maximum value and then the angle that is coming from here,
we will give it to the maximum of 14 degree angle just so we save and then write this server position
to the my server.
So this position will be given to the server motor and the server motor will reach that angle.
Let's save this and verify checks.
This is compiled now we have to upload this.
And now this is uploaded.
Now remember that in our circuit, this servo motor will be powered when we turn on the switch.
By using just the power, we cannot operate the servo motor because it draws a lot of current and it
is directly powered by the battery, not from the voltage booster.
So you have to disconnect the wire of the esp32 and then we will turn the switch on.
In order to operate, we will connect via Wi-Fi.
Disconnect from here, turn on the switch.
Now we will be connecting it via micro Ross Agent.
And now we have run the command.
But this is not connected yet.
Now we will do a restart.
And just now this is connected.
There is the topic.
So now you have to publish value over this topic in order to operate this server.
It is not moving.
So we have one thing that we have made a mistake.
We do not allow it to execute two threads.
We have to update this number and change it to two.
Save it.
And now we have to turn off the robot and plug in the esp32.
And now upload it again.
Okay, now disconnect the wire and turn on the button.
Reconnect with the micro station.
This is connected.
And now we will publish.
The published zero.
Yes, this goes to zero.
And if we publish ten.
Justices giving this response, they will give it again.
40.
Yes, this is absolutely working.
That's it for now.