Hey, now we're going to add the last topic in the code and our report will be ready.
And this topic is on which we will be publishing velocity and report will be we are going to utilize
this code and make some changes in our code.
It is important to twist message.
So we will be copying this and paste it here.
This is the first step and then then it is adding the objects of subscriber and the messages.
And in here there is a callback function that it is calling and here it is, the Micro-roasters subscriber.
Here are the objects of the subscriber and the message.
And now we are going to add the subscriber.
Here it is, the subscriber with the topic.
And under here we are going to add a new thread and here we will just write it for because this is a
new thread and here it is, the callback function that we are going to define up here.
And this is the callback function in which we are just getting the value of the linear x and, and the
we are getting the value of angular C and we are assigning it to the angular and then we are calculating
the motor speed and the motor speed is the linear minus angular over two multiply by 2000.
This is not the actual formula to calculate the speed of the motor, but for the sake of simplicity,
we are using this formula so that we can move our robot at the basic level.
Now we have to move our fields as well.
We have connected our fields on PIN 25, 26 and 27 and 14, and here are the pins.
If we give some value to the PIN 14, then the left motor will be moving forward.
And if we give some value to the 27 pin, it will be moving backward, the left motor and for the right
motor these two pins are choosing.
Now we have to set the mode of these pins to the output mode and the void setup.
Now we will again go to the callback function and we also have to initialize these motor speed variables
and we will be initializing it here and come back to the callback function callback.
And in here we are going to use some flex and heresy logic for motor speed.
Left is greater than zero.
Then the left motor is going to move and here we are playing with the number 4010.
What does it do?
The value on which the words is from 0 to 255.
But if we give a value of ten, the motor does not move because the motor has the internal resistance.
There is also some weight of the robot.
So this is why the motor does not move.
So if we give 40 value to the pen, then it starts to move very slowly.
Then we can increase and the speed will be gradually increased with this, with the higher value of
petroleum.
Now we have to assign these speed value to the particular fields of the robot.
For that, we are going to create a function.
And in here, here it is the function set motor and it takes the left speed and speed, right.
If the speed left is greater than zero, then it is going to set the end to pin to low.
Because if the this pin is high, then the left motor will be moving backward.
So if this is low, then it's fine.
It is not going to move anywhere and then it assigns the value to the end pin.
Fun in pin one will be driving left wheel.
There is also a limit to the pin it the maximum it can go as to 50 because if we give the higher speed
of the value to the motor driver, the motor cross a lot of current and in some cases it it turns off
the esp32.
So this is like a precaution.
You can play with your robot and you can also find out what value suits your robot.
If the left speed is less than zero, then it is setting the in one pin low and then giving the value
to the into.
The same is the case with the speed.
Right.
Okay.
And this condition checks.
If the boats speeds are zero, then it's going to set all the pins to low.
Okay.
So this is the function that we are going to call in here.
And we will also have to define the prototype.
So these are the function prototypes.
And now we are going to upload the code.
So now plug the esp32 in upload so the upload is successful.
Now plug out the esp32 and turn on the robot connected via microprocessor.
So go to your robot workspace and solve it first.
And there you are connecting.
So we will give it a restart.
This is connected now.
So there we go.
There is the topic.
Now we are going to publish our.
And now press I and support is moving.
The value is a little bit higher, so I will decrease it via pressing the Z.
There you go.
It is working now.