Tracking Raspberry Pi Robot’s Distance with Encoders | Twitter Controlled Robot Ep 4
Articles,  Blog

Tracking Raspberry Pi Robot’s Distance with Encoders | Twitter Controlled Robot Ep 4


Hello and welcome back to a robotic
series. In our last video, we figured out how fast the motor is spinning and what
direction it was moving. But we don’t know how far it’s traveling. To do that
you might think that we could set a throttle value and just give it a
certain duration, track how far it’s gone, and use that as a calibration for the
distance traveled. But the problem is that the battery voltage is going to
change. If you remember from our last video, that’s what dictates how fast our
motor is moving. So as our robot is driving around, it’s draining the battery
and the voltage is decreasing. And if we look at these PWM graphs on the
oscilloscope, we can see that a fresh pack of batteries gives us about 6 volts
for our peak voltage. And a drained battery is going to drop down to about 5 volts.
So we can’t use that to measure the distance. But we can use an encoder. And
that’s going to track the number of rotations that a wheel makes. And these
encoder disks go on the outside of the motor. And as the motor rotates, so does
the encoder disk. And I’m gonna call them encoder disks, because I don’t want to
get them confused with wheels. And as we’re tracking these with sensors, we’ll
read high and low values. And these state changes will tell us how far we’ve
rotated. And the wheel’s rotations times its circumference will give us the
distance that it’s traveled. These encoder disks have two states that are
going to cause a sensor to go between a high and a low. And if we had a phrase of
the video it would be “state changes.” We need to really track the number of
states that we’re going through in order to know how far our wheel has rotated.
For this encoder disk, there are 12 state changes for every one rotation. And if
we’re tracking it and we see 15 state changes, we know that we’ve moved one and
a quarter turns. Let’s look at different types of encoder discs and how to use
them to track state changes. We have an optical encoder and a magnetic encoder.
For the optical encoder, there’s a plastic disk with twenty slits and twenty
blockages. The magnetic disk has four North and four South magnets around the
outside. The magnet types alternate, which is why the permanent magnet jumps as
we’re moving it around. Each encoder has a sensor that reads these state changes.
The optical encoder uses an IR light sensor. The slots in the disk allow the
light to travel from the transmitting diode to the receiver. And this is just
like the break beam sensors that we used in our giant ramp.
The metal ball was able to trip the sensors as it rolled down. The receiving
half of the break beam sensor had three wires a power, a ground, and a signal The
signal wire went to the digital input of the Arduino. And depending on whether the
light was received or blocked, we either got a high or a low. The optical encoder
has a five volt power pin, ground pin, and signal pin. When wired to the Raspberry
Pi, a red LED turns on. This lets us know that the board is powered and that the IR
light is being received. And if we block this light, the LED turns off and the
signal on the oscilloscope jumps from zero volts up to five volts. So we’ve
gone from a low to a high. For the magnetic encoder disk, a Hall effect sensor is
used. It also has a five volt power wire, a ground wire, and a signal wire. The
oscilloscope jumps from a low to a high as the Hall effect sensor moves across
the magnetic disk. Attaching the optical encoder disk the motor, we can see that
there are a series of state changes. This disk is attached to the gearbox that’s
opposite the wheel. There is a one to one relationship between the rotation of the
encoder disk and the wheel. When we look at the magnetic disk, we see there are
many more state changes. This disk is attached to the top part of the motor
and is not on the geared down shaft. The gearing means that the motor turns 48
times for every one time that the wheel turns. This provides a higher torque
value to the wheel. This means we have to multiply the eight state changes that
the motor seized by 48. So we know how many use state changes that the wheel
sees. And when we do this we get 384 state changes for every one rotation of
the wheel. The optical encoder has a resolution of 40 and the magnetic
encoder has a resolution of 384. And we wanted to be able to control these
encoders. So we added some lines of code to our existing motor program. This is
not at all what would use to control the robot. It’s just for playing around with
the encoders and getting a feel for how they work. So at the top we defined our GPIO
pins and set is a read value. So we want to know if it’s a high or low from pin
17. then we need to track some things, the last state, we saw the current state, the
number of rotations, the number of steps. And we want to know the distance
traveled per step (state change). So we took the circumference of the
wheel and divided it by the number of steps per rotation. And then I set up a
try and accept part, because when I didn’t have this and I exited a program
the motor just kept running. So when we hit control-c now it’s gonna set the
throttle value to zero for the motor, and it’s gonna stop. Then we have a while
loop. We’re asking a couple of questions. First we want to know what is our value
that we see from pin 17. And if this value is different from the last value,
that we read then we need to increment the number of state changes. And if the
number of state changes is equal to the number of states per rotation, then we
need to increment the number of rotations we’ve seen so far and set our
tracker back to zero. To figure out the distance we’ve traveled and not use
whole rotations, we can look at the total number of state changes we’ve seen and
multiply it by the distance we have for every state change. And this is the basic
program we have for our encoders. If you want to learn more about encoders, we
have a few more videos where we talk about quadrature encoders and that will
tell you what direction you’re rotating in. And absolute encoders that tell you a
unique position that you’re at with your motors rotation. If you’ve any questions
or comments you can leave them down below. In our next video we’re going to
actually put the motors and two encoders onto this 3d printed chassis and start
wiring everything up. So thank you guys so much for learning with me today, and
I’ll see you next time.

4 Comments

  • Laurence Obi

    Been following your work. Must say I'm impressed. Yours is the first video I found on here that actually attaches the encoders to running motors. Was beginning to think it was a taboo to show the work in real time scenarios. Kudos.

  • Kingston Eldridge

    You never did the next video. How do you keep track of the encoder pulses in a program that also is controlling and tracking the rest of the robot functions? As your code is processing other things, it will miss state changes.

Leave a Reply

Your email address will not be published. Required fields are marked *