Adventures in Science: How to Use Rotary Encoders

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you needed to know how far you walked how would you do it would you pull out a measuring tape would you buy a speedometer or let's say you knew precisely the length of each step you took you could then count your steps sometimes robots need to know how far they've gone and that's where encoders come in assuming your robot has wheels counting the number of times those wheels turn or better fractions of a turn isn't the easiest thing to do in electronics this is a rotary encoder we can shine a light at a disc with bowls cut into it and we can use a light detector on the other side to count the number of times we see that light and for this example we know that if we see the light five times we've made one revolution there are several ways to measure rotations on a disc that was an example of an optical encoder using visible light we could have used a laser infrared light or even just measured black and white transitions on a disc like this alternatively you could have teeth on a gear that actuate a switch or you can mash some magnets together and count the number of times you see North and South Poles move by a Hall effect sensor encoders have all sorts of uses for example you might find them on user interfaces when you need a continuously rotating knob in fact you probably have an encoder near you right now if you're using a mouse with a scroll wheel that scroll wheel likely uses an encoder to determine how far you've moved the wheel making a user interface is great but how can we use this on robots because who doesn't want to build the next Mars rover well as it turns out measuring how far your robot has moved without the aid of some kind of global positioning system can be quite tricky let's say that you have a ground-based robot with wheels or treads like my little robot friend here I'm going to call him Fred and let's say that we need Fred to move exactly one meter now your first thought might be let's measure how far Fred moves when we turn on the motors for one second and then knowing that we can turn the motors on for a length of time necessary to move that one meter round first DC motors vary their hey depending on the voltage applied to them that means as your battery slowly drain the motors will begin to spin more slowly over time so unless you can use a DP voltage supply or guarantee that your batteries will always be at the same voltage then spinning the motors for one second will result in a different distance every time second most cheap DC motors like these plastic yellow gear motors don't run at the same speeds even with the same voltage that means Fred here will always list lately to one side to fix this we can use encoders this is a magnetic encoder disk with four north poles and four South Poles that alternate and we can either mount it to the output shaft or to the motors shaft note that this gear motor has a forty eight to one gearbox in it which means it takes 48 turns of the motor shaft to turn the output axle one revolution so by adding the disc to the motor shaft we can effectively have more resolution to determine how far our robot has moved I then tuck to the Hall effect sensor under a zip tie to hold it in place and bent it so that the top of the component is as close to the disk as possible as the encoder disc turns the detector a Hall effect sensor in this case outputs a logic high when it sees a North Pole and outputs a logic low when it sees a South Pole the easiest way to measure revolutions is to count one type of edge for example if we see a falling edge that is the detectors output goes from high to low then we know the disk has turned 90 degrees and by that logic if we see four falling edges we know the disk has made one complete revolution if we wanted even better resolution we could count both rising and falling edges then each edge we see we know that the disk has turned 45 degrees and that it takes 8 edges to make up one revolution for this demo I'm not going to power the motor as I just want to show how to read counts from the encoder plug the black wire into ground the red wire into a power pin 3 to 5 volts works and the white wire into an external interrupt pin which is 2 or 3 on the uno or red board I will use pin 2 in a new sketch right in 2 X 10 equals 2 which is the pin our encoder is connected to under that unsigned long ticks equals 0 this is a global variable that stores the number of times we've seen a rising or falling edge on the encoders output in setup right serial begin 9600 and then pin mode and pin comma input underscore pull-up we need to pull up on the Hall effect sensor because it's an open drain output that means the sensor will only pull the line low but it won't pull it high it'll just leave it's floating so we need to pull it up to a voltage then write attach interrupt open parenthesis digital pin to interrupt open parenthesis and pin close parenthesis come up count ticks come a change in all capital letters close parenthesis here we say that the count ticks function should be called every time there is a high to low or low to high transition on our encoder pin in loop write serial dot println ticks to display the current pick counter follow that with a delay 100 then below loop write void count ticks open parentheses close parentheses open curly brace ticks plus + and close it with a curly brace this is the function that gets called each time the external interrupt sees a rising or falling edge on the encoder pin all it does is increment our tick counter upload this to your Arduino as I spin the motor the Hall effect sensor detects the magnetic changes on the encoder disk and toggle the voltage on pin 2 each time the pin changes our counter is incremented by 1 remember every eight counts is equal to one revolution of the motor so how do we calculate how far our robot has moved first we want to find out how many encoder ticks we need to count per revolution of the wheel because of the gearbox one revolution is equal to 48 turns of the DC motor we also know that the encoder toggles the pin eight times per motor revolution so we multiply 48 by 8 to get 384 encoder ticks for each wheel revolution assuming the wheel always stays on the ground and doesn't slip it will move a distance equal to its circumference for each revolution we can calculate the circumference by multiplying PI by the wheels diameter which I measured to be 60 millimeters for Fred this means that Fred's wheels each travel about 188 point five millimeters for each revolution with that we know that if Fred traveled in a straight line then he moves about 180 eight point five millimeters every time we measure 384 ticks divide both sides of the equation by 384 and we see that each tick means that the robot moved about 0.49 millimeters to get Fred to move one meter which is a thousand millimeters we divide a thousand by 0.49 and see that we need to measure 2041 ticks before stopping that solve the distance issue but how do we solve the problem with Fred pulling to one side to do that we'll need two encoders one on each motor in our code we can keep track of the number of times each motor has turned and every few milliseconds we compare those numbers and see if one motor has turned more than the other if it has we decrease power to that motor and increase power to the other we create a feedback loop to ensure that both motors are moving at about the same rate the whole time let's give it a shot Fred should travel in a straight line for one meter which is right about there you'll notice that it might be a few centimeters off and this can be due to potential slipping of the wheels slipping of the encoders missing counsin code or just the sheer momentum of the robot better parts and better code can fix this but for our purposes being a couple of centimeters off is good enough if you're curious the code for making thread drive in a straight line can be found in the comments in fact this is how many of the top competitors at spark funds autonomous vehicle competition make their way around the track the robots move a certain distance turn move another certain distance and so on which is known as dead reckoning one thing we haven't talked about is determining the direction the shaft is spinning if I spin the encoder wheel halfway around and then the other direction halfway around I've effectively made zero revolutions but you counted the same number of light transitions as if I've made one revolution if you were controlling a motor that the encoder is attached to then you should generally know which direction the shaft is spinning but what if you wanted to determine the direction that the shaft is spinning because some external force was moving it like a human to do that we could use a quadrature encoder a quadrature encoder can use the same encoder disk but it has two sensors instead of one the trick here is that the second sensor is placed so that when the first sensor goes from a high to low or low to high transition the second sensor is halfway in one of the zones reading high or low but not a transition so when we spin the wheel detector a follows the same pattern as before but detector B is 90 degrees off in this example whenever we see a falling edge on detector a we can check what state detector B is in if B is high on a s falling edge then we know that the disk is rotating clockwise now rotate the disk counterclockwise and we get a similar pattern but now it appears that B is leading a so if we see a falling edge on a and C that B is low we know that the disk is rotating counterclockwise I've added two light detectors to my demo to turn it into a quadrature encoder when I turn the wheel in one direction the light gets brighter on each tick I can turn it in the other direction to make the light dimmer I've essentially made the world's worst dimmer switch if you want to look at it the code fort can be found in the comments just note that you can make it better by looking for both rising and falling edges instead of just one of those throughout this video we've talked about incremental encoders which work great for measuring distances or counting number of times that an axle has turned there's another type of encoder that we won't cover here and that's the absolute encoder an absolute encoder disk has several different patterns cut or drawn into it if we place three different sensors along a radius inside this disk we get a different binary code for each 45-degree arc we can use this to determine the position of the shaft and not just how much it has moved absolute encoders are useful to know what positions say a robot arm is in or you can attach it to a weather vane to figure out where the wind is coming from well you could spend days researching and studying different types of encoders I hope this has given you a start on building your next in a planetary surface robot or self-driving dune buggy with that good luck fellow explorers making a making a thing well as it turns out using an optical something or other or even the most even the measured of that was an example just spit everywhere and use encoders this nope not that oh there it is with for North Pole's in fourth South fourth fourth South fourth South Pole I can I can I can talk good
Info
Channel: SparkFun Electronics
Views: 171,213
Rating: 4.9483204 out of 5
Keywords:
Id: oLBYHbLO8W0
Channel Id: undefined
Length: 12min 4sec (724 seconds)
Published: Mon Jul 24 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.