How to Use a Quadrature Encoder _ Let's Make Robots!

Embed Size (px)

DESCRIPTION

encoder datasheet

Citation preview

  • How to use a quadrature encoderLogin or register to post comments by OddBot Collected by 49 userssensor, rover, rotation, Encoder, quadrature, measureTip/walkthrough

    By OddBot@ January 3, 2011measures speed / distance

    A quadrature encoder, also known as an incremental rotary encoder measures the speed and direction of a rotating shaft. Quadratureencoders can use different types of sensors, optical and hall effect are both commonly used. The photo shows inside of a Rover 5gearbox. There are two IR sensors on the PCB that look at the black and white pattern on one of the gears. No matter what type ofsensors are used the output is typically two square waveforms 90 out of phase as shown below.

    Start HereRulesTopTipsRobotsSomething elseBlogsReviews3D print clubChallengesForumsRecentAboutMy AccountMy stuff

    Home

    Search

    User loginUsername: *

    Password: *

    Create new accountRequest new password

    Log in using OpenIDRecent blog posts

    super fast robotic arm!Using the Arduino PIDLibrary for preciseposition control of X andY axis on RepScrapprinterA simple HTTPcommunication protocolbetween wifi and ArduinoFurther Progress onframing my RepScrap3D printer.Arduissimo: MultiCoreArduino indiegogoprojectYahmez Goodies!Robots Ready toRumble!Xinjiang, robots andbombs!My Repscrap: DCmotors and rotaryencoders for Z-Axis too?Why I Respect my RobotElders

    more

    Recently submittedlinks:

    RC Tank ChassisCrawler IntelligentBarrowload TractorObstacle CaterpillarWall-e InfraredUltrasonic Patrol2D Ruben tubeControl Your Robot WithNode.js - Raspberry PI

    How to use a quadrature encoder | Let's Make Robots! http://letsmakerobots.com/node/24031

    1 of 6 5/15/2014 2:24 AM

  • If you only wish to monitor the speed of rotation then you can use either output and simply measure the frequency. The reason forhaving two outputs is that you can also determine the direction of shaft rotation by looking at the pattern of binary numbers generatedby the two outputs.

    Depending on the direction of rotation you will get either:

    00 = 0 01 = 1 11 = 3 10 = 2

    or

    00 = 0 10 = 2 11 = 3 01 = 1

    By feeding both outputs into an XOR gate (exclusive OR) you will get a square wave with twice the frequency regardless of direction.This can be useful as it allows one interrupt pin to monitor both encoder inputs.

    and ArduinoPoppy Project - OpenSource Humanoid robotRoboRium - The RobotEmporiumHackaday in ShanghaiKickstarter: MicroView:Chip-sized Arduino withbuilt-in OLED Display!A comprehensivepath-finding library. (linksto python and javascriptsource)Conductive Ink ... is yourBath Sponge SafeFoxyTronics

    Who's onlineThere are currently 27 usersand 21 guests online.Online users

    Polar VortexmintveltDan MDuane Degnlamorak24unix_gurulazdHackgarhoff70gamborgkariloyHaZZaRoxanna77fritsllukeyesjscottbLadvienGromerbirdmunPhotolongTinHeadbstaghoff70ossipeeLadvienmogulwebmaster

    Quick linksLMR on Google+LMR on FacebookLMR on FlickerLMR on TwitterLMR ScrapbookUser listUnread postsRSS feedsSpam Control

    How to use a quadrature encoder | Let's Make Robots! http://letsmakerobots.com/node/24031

    2 of 6 5/15/2014 2:24 AM

  • I was looking at how to write efficient code to convert these binary inputs into a simple "forward or backward" output. I ended up witha 2 dimensional array (matrix) that made the code quick and easy.The binary values above convert to 0,1,3,2 or 0,2,3,1 depending on the direction. This pattern repeats continuously. By using thecurrent value from the encoder to index one dimension of the array and the previous value to index the other dimension you canquickly get a -1, 0, or +1 output. My array looks like this.

    As you can see, if the value has not changed then the output is 0.The sequence of 0, 1, 3, 2 gives an output of -1.The sequence of 0, 2, 3, 1 gives an output of +1.

    X represents a disallowed state and would most likely occur if the encoder outputs are changing too quickly for your code to keep up.Normally this should not happen. In my code I put a 2 here. When I get an output of 2 I know that I got an error, perhaps due toelectrical noise or my code being too slow. If you replace X with 0 then the disallowed state will be ignored.

    In my Arduino code I make this a 1 dimensional array. that looks like this:

    How to use a quadrature encoder | Let's Make Robots! http://letsmakerobots.com/node/24031

    3 of 6 5/15/2014 2:24 AM

  • int QEM [16] = {0,-1,1,2,1,0,2,-1,-1,2,0,1,2,1,-1,0}; // Quadrature Encoder Matrix

    To read the array my index is: Old * 4 + NewSo my code reads like this:

    Old = New;New = digitalRead (inputA) * 2 + digitalRead (inputB); // Convert binary input to decimal valueOut = QEM [Old * 4 + New];Good luck and enjoy.

    Comment viewing options

    Select your preferred way to display the comments and click "Save settings" to activate your changes.By Hyvok@ Tue, 2012-07-10 15:03

    Tool for creating optical encoder discsLogin or register to post commentsThanks for the tutorial! If someone wants a simple way to make encoder discs check out my post about an extension that I wrote forInkscape: http://www.dgkelectronics.com/inkscape-extension-for-creating-optical-rotary-encoder-discs/

    You can make single- and dual-track (quadrature) encoders with it, in any size/number of segments.By chrisc@ Sun, 2012-05-20 12:51

    thx for the help!Login or register to post commentsI've been building my own quadrature encoder and this was awesome! Thanks OddBot!

    Info is here if anyone wants a look - it covers building the encoder and then uses the work OddBot did above to read it:

    http://robotblogging.blogspot.co.uk/2012/05/quadrature-encoder.html

    By afeibig@ Mon, 2012-02-20 03:59

    Im I allowed to use the article elsewhere?Login or register to post commentsHi, I'm new in the site, and i like to use some parts of this article to write another one, about my own encoders for a personalprojecto, obviously respecting you as the owner.

    Thanks in advice.

    By OddBot@ Mon, 2012-02-20 12:55

    No problem. The informationLogin or register to post commentsNo problem. The information is posted here for everyone to learn from.

    By Anas@ Sun, 2012-01-08 16:56

    How to use a quadrature encoder | Let's Make Robots! http://letsmakerobots.com/node/24031

    4 of 6 5/15/2014 2:24 AM

  • the XOR will give 1/2 theLogin or register to post commentsthe XOR will give 1/2 the encoder rate(2X). To get the full rate(4X) you should use transitions on both A and B. the LS7183/LS7184 are CMOS QUADRATURE CLOCK CONVERTER that may help in this.(http://www.lsicsi.com/pdfs/Data_Sheets/LS7183_LS7184.pdf)For Ardunio users, A high performance Encoder library is now available, with 4X counting and very efficient interrupt routines. Usesthe External Interrupt pins of the MCU.(download it here http://www.pjrc.com/teensy/td_libs_Encoder.html)

    By Superus@ Wed, 2012-01-11 19:30

    It worked!Login or register to post commentsThank you very much! You saved my day=)With this library I'm able to get correct results from my encoder and it works beautifully. Thank you=)

    By OddBot@ Fri, 2011-12-30 13:26

    I have had that problem whenLogin or register to post commentsI have had that problem when I have had more than 1 interrupt enabled at once. It seems as though the processor is having troublekeeping up with multiple interrupts. I found that changing my interrupt mode from "Changing" to "Rising" helped as it halved the numberof interrupt requests.

    By Superus@ Fri, 2011-12-30 19:38

    It's only one interruptLogin or register to post commentsThank you for that quick answer yet again, but..(here it comes)... it is only one interrupt in use and it is already in rising mode. Ihave cut the sketch down into only the necessary code and tried with an RoMeo board too, without luck. I have probed theoutputs of the encoders with an oscilloscope and changed the XOR chip, but I found no irregularities. I have tested the "samplingrate" of the interrupt and found out that it could handle much higher speeds.

    Too be honest, I have no clue of why this is happening. I appreciate any kind of help, and thank you very much if you help me=)By OddBot@ Sun, 2012-01-01 07:25

    I cannot say.The encoderLogin or register to post commentsI cannot say.

    The encoder boards are factory tested on an oscilloscope to calibrate the sensor. They are adjusted to give two 50%dutycycle square waves 90 degrees out of phase.

    I would rule out electrical noise from the motor as the test with the oscilloscope is done with the motor running and there is nonoise on the display.

    When I get back to the office I can do a test.

    How to use a quadrature encoder | Let's Make Robots! http://letsmakerobots.com/node/24031

    5 of 6 5/15/2014 2:24 AM

  • By OddBot@ Fri, 2011-12-23 16:39

    Check your data typesLogin or register to post commentsI've had weird results before due to not using the correct variable types. In one case where I used an integer instead of a byte I hadmy code work perfectly when I first tested it but later give me weird results.

    1 2 3 next last ALL LMR ARE BELONG TO US!Let's make robots!

    How to use a quadrature encoder | Let's Make Robots! http://letsmakerobots.com/node/24031

    6 of 6 5/15/2014 2:24 AM