Hey, now we are going to read the values of the encoders.
Each motor have three magnetic encoders and we will get three value encoder spin are connected to 2322
for the left motor, 19 and 18 for the right motor.
We are going to digitally these pins and convert it to the encoder texts.
We use the encoders for the odometry calculation that how much the robot has traveled after implementing
some mathematical equation.
So let's modify our micro Ros code.
Now we are going to make two Publisher one will be publishing the left encoder ticks, the other one
will be publishing the right encoder ticks.
Now we will import Int32 because this is going to handle a bigger number this time zero and 32.
As we have created the publisher for the Battery, we are going to create two publishers more.
And now we have to define the pins for the encoders of the motor.
The C1 of the left motor is connected at 23 and C2 for the left motor is connected at 22.
And in the case of the right motor, the C1 is connected at 19 and C2 is connected at 18 right it here
encoder spin.
And now we will be creating the variables that will be storing value of encoder from the left and right
motors.
Also their particular prototype function that will be calculating the number of ticks.
Now we are going to add two publisher in the world setup as we have added some previous publishers.
And here it is the battery publisher.
Under this, we will be adding our two new publishers.
These two are the topic of publisher, the left motor tics and the right motor ticks.
And down here we will not need to change anything because these additional publisher will be publishing
the value inside the timer callback function.
So will we will not change any parameter here because we have already set up the timer and it sets will
be three again and above here.
We also have to set up the pins and we will set up two input mode.
And we have also used the interrupt convention when the Esp32 pin will be detecting a change in the
pin, it will go to the callback function and we are going to define these two callback function in
the end like this.
So these two are the callback functions for the left encoder and the right encoder we have to set same
as previous.
Then it will be adding the number by one.
And if state is not the same, it will be decreasing the number by one.
So now we are going to publish this number on to the topics and the timer callback.
So here we will first set the the message of the left motor encoder and the right motor encoder because
these two motors are placed opposite to each other.
So the right water will be working fine.
But in the case of left motor, when the wheel goes forward, it will be giving us the value in the
minus.
But if it goes backward, it will be giving us the positive.
So this is why we will put an extra negative sign in in front of the left encoder count.
Now, we are publishing these messages in here.
Now we are going to connect the robot esp32 with the PC.
Upload the sketch.
This sketch is uploaded successfully.
Now we will plug out this connected for a micro Ros agent.
And turn on the button.
It will be connecting.
This is connected.
Ross Topic list.
There we go.
Left motor takes and the right motor takes.
And we will be echoing both of them.
Left motor dick and it is saying that it is zero.
And we are also going to echo, right?
Motor encoder.
And this is also zero.
Now, first, let's let's change the left motor encoder.
And we are going to rotate the wheel in the forward direction.
There is changing the value.
And if we see the right encoder, we are moving it forward.
And this is adding.
And if we move it backward, this is going to subtract.
Alright, this is working.
That's good.
Now we are going to see the RCT.
She has the lion turtle.
Esp32 is publishing the left and right motor ticks.