Rotary Quadrature Decoder - Page 1
Forum section: electronics / projects

author: OM222O
author_rank: Frequent Contributor
timestamp: August 24, 2020, 09:51:24 am
content: Hi, I need to build a quadrature decoder for a motor controller project as this would reduce the MCU overhead considerably as it doesn't need to process all of the interrupts!I searched a lot and this TI document seemed to be the most robust option with ability to have a high bit counter (the app note only uses 8 ) but I'm not sure how to implement this as the IO expander chip seems to output 2 bytes, one for direction and one for count. I really need 16 bits, otherwise the communication overhead defeats the purpose. a bit of help in this regard would be great.Does anyone have experience with designing similar circuits? T.I didn't mention things like maximum pulse rate or timing diagrams, just a few scope captures of it in action.My encoders are optical so there is no need for denouncing and signals are not noisy. Are there improvements that can be made to the suggested circuit that can be made? for example newer, better parts? or should I just follow the suggested circuit exactly as it is?

author: mark03
author_rank: Frequent Contributor
timestamp: August 24, 2020, 03:50:23 pm
content: Which MCU are you using? Many MCUs have dedicated hardware for this nowadays, no interrupts required. Most of the STM32 series, for example, feature a built-in quadrature decoder as part of the timer peripheral.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: August 24, 2020, 04:17:31 pm
content: Same here.One of the reasons I got interested in STM32 is because they do the quadrature decoding and counting in hardware. Then I bumped in the EUR3 Blue Pills on Ali and I never looked further t other microcontrollers.But I do sort of assume there are others which can do the same.Very unfortunately, the good deal on Blue Pills is past tence. There are at least 8 different clones of the STM32F103, and all the Ali vendors seem to use the "cheapest of this week", then rubb off the text and print STM32 on it. With the STM32F4xx this seems less of a problem, or buy your chips from a more reliable source.

author: LaserSteve
author_rank: Super Contributor
timestamp: August 24, 2020, 06:32:58 pm
content: I've had good luck with this family of parts:https://www.usdigital.com/products/accessories/interfacesSteve

author: Benta
author_rank: Super Contributor
timestamp: August 24, 2020, 06:42:56 pm
content: Which TI document?

author: OM222O
author_rank: Frequent Contributor
timestamp: August 24, 2020, 08:56:02 pm
content: my bad, I forgot to link the actual documenthttps://www.ti.com/lit/ug/tiduaa8/tiduaa8.pdf?ts=1598274515607&ref_url=https%253A%252F%252Fwww.ti.com%252Ftool%252FTIDA-00580%253FkeyMatch%253DQUADRATURE%2BDECODER%2526tisearch%253DSearch-EN-everythingI'm using arduinos so unfortunately no built in decoder. Might upgrade to something later on, but if I can get this to work now it should be fine.I think I just need to add more counters and use a shift register to load the data serially rather than using an x bit IO expander. I can latch the output and reset the count at the same time, then shift it out from the latch. but that makes me want to invest in a small, cheap FPGA

author: mjkuwp
author_rank: Unknown
timestamp: August 24, 2020, 10:03:22 pm
content: more details would help.what kind of motor?what type of control?minimum rpm, maximum rpm that you need to handleresolution of the encoder.you might be trying to find a solution for a problem that you do not have.

author: langwadt
author_rank: Super Contributor
timestamp: August 24, 2020, 10:20:26 pm
content: Quote from: OM222O on August 24, 2020, 08:56:02 pmif you really really insist on using an AVR, just use two. anyway, interfacing an extra avr or some external hairball of logicis going to take more effort and cost more than just using a mcu that can do it all

author: Benta
author_rank: Super Contributor
timestamp: August 24, 2020, 11:00:28 pm
content: It's completely unclear to me how many pulses per revolution you have and what the rotational speed is. Without that information, we'll have to search for our crystal ball...

author: OM222O
author_rank: Frequent Contributor
timestamp: August 25, 2020, 07:08:40 am
content: It's "too much" the MCU has to handle a lot more than just an encoder. Please stay focused on the main discussion topic rather than suggesting alternatives. If upgrade the MCU down the line, then I might consider using an STM like I mentioned before.

author: capt bullshot
author_rank: Super Contributor
timestamp: August 25, 2020, 07:20:28 am
content: One way to implement if your MCU doesn't have the quadrature decoder / counter unit:Use some simple external logic to decode the quadrature signal into counting up and counting down pulses. Some FFs and gates or something like an ancient GAL should do that job. From your MCU peripherals, use two simple counters, feed them with the up resp. down counting pulses and subtract the counter registers in your software. This approach did work for me using an ancient 8031 MCU back in the 90's of last century.

author: mino-fm
author_rank: Regular Contributor
timestamp: August 25, 2020, 08:50:06 am
content: I've got two examples for quadrature decoder to IIC-interface.One using a ATtiny25 (only .hex-file:http://www.mino-elektronik.de/mt12_iic/mt12_iic.htm#qcnt_tiny25) and one a newer ATtiny202, 4xx, 8xx:http://www.mino-elektronik.de/mt12_iic/mt12_iic.htm#qcnt_tiny202.Quadratur_cnt.zip contains a complete project for IAR_EW for AVR. Using demo version will be sufficient.Counter length is 32 bit and maximum clock rate is up to 350 kHz.

author: Kjelt
author_rank: Super Contributor
timestamp: August 25, 2020, 09:24:30 am
content: It all depends on the speed of your motor application, the resulted frequency of the quad encoder you get, the speed you need to stop the motor in time and the delay you get from using protocols to read the current "position".For most high speed applications where accuracy is needed on position, speed and acceleration, the common WoW was to integrate the decoder and microcontroller.Even in the 90s with the slow 8 bit 68xx and 8051 based uC they used dedicated ic's like the LM628 to accomplish that (check out the datasheet of the lm628 to understand what I am trying to communicate here).These ic's have been outdated/replaced by the arrival of the 32bits uCs with integrated quad decoders at a pricepoint of $2.So if you don't need the speed go for an external i2c or spi based quad decoder but realize that during motion you always get data from the past and you have adjust your PID control loops to facilitate that. So that means esrly deacceleration and slower movement to the end position than with a dedicated uC. So check your requirements and see if that still fits.

author: OM222O
author_rank: Frequent Contributor
timestamp: August 25, 2020, 12:06:07 pm
content: The LM628 seems like a very interesting part that takes care of all the headache (generating trapezoidal velocity profiles, decoder, PID loop, etc).I have searched around but there doesn't seem to be any MCU project or product that's that feature rich and easy to implement. I honestly don't want to start learning a whole new workflow method and invest a ton of time and money into getting the programmers and the software etc working.Are there newer motion controller chips that are similar to the LM628? or do you think rolling my own using newer MCUs is the way to go?

author: H.O
author_rank: Frequent Contributor
timestamp: August 25, 2020, 03:02:57 pm
content: If you want an IC (really, it's a PIC18F2431 with firmware in it) to build your own servo drive around you might take a look at the PIC-Servo SC from JRKerr. If you want DIY motion control via G-code then look at any of the 3D printer firmware for your Arduino and connect the step/dir output to an off-the-shelf servo drive that handles the closed loop. Or you might want to look at a full blown motion controller from companies such as Galil, MESA, Dynomotion etc, etc.If you DO want to roll your own and can't (or don't want to) use an uC with a dedicated quardature counter peripheral I can recommend the path outlined earlier - decode the quadrature signals into discrete up/down counts and feed those into two separate counter peripherals on your uC. That way there's basically no software overhead for counting (as opposed to using interrupts) and no delays/latency etc as with using a separate counter chip.For off the shelf IC's (also mentioned before) US Digital has a couple of different ones. The LS7166 24bit quarature counter being a bit of a "classic". I don't know if US Digital has bought the rights to produce these IC's or perhaps even bought the company the originally designed themAs far as the TI circuit goes it looks to me as if it does NOT do x4 decoding and is therefor more suitable for "encoder knobs" - ie the ones with one full quadrature cycle per detent - but I might be wrong.

author: capt bullshot
author_rank: Super Contributor
timestamp: August 25, 2020, 04:18:52 pm
content: The "usual suspects" (like TI, STM, Renesas, maybe Infineon) offer "FOC motor control" demo boards and / or "ready to use" FOC motor control libraries for their microcontrollers. Arduino / Atmel and Microchip isn't in that league, so you should consider a switch to one of the MCU manufactureres I've mentioned.E.g. STM has a motor control addon board for their Nucleo series eval boards, I'd expect them to be the cheapest available.

author: Kjelt
author_rank: Super Contributor
timestamp: August 25, 2020, 05:39:20 pm
content: Quote from: OM222O on August 25, 2020, 12:06:07 pmI haven 't found any.What I noticed is that almost all commercial servo/closedloop-stepper drivers use a single TI microcontroller to do everything. These drivers have (opto isolated) logical levels with signals p: Step/Pulse , Dir and Enable. These cost around $40 to $80 and contain everything except the powersupply.Or you could go that way as Cpt Bullshot above advised, where an STM or TI motorcontroller with example code would take a week of effort to have something running.But again it depends on your speed requirements. If you have to do pid on velocity and position yourself it could take some effort to build your own but in the end way cheaper than dedicated chips or drivers. If you only need max a hundred ptoducts, I would go for a big chinese company such as Leadshine and ask about the cost for 100 ready made drivers.

author: Benta
author_rank: Super Contributor
timestamp: August 25, 2020, 08:10:37 pm
content: Quote from: Benta on August 24, 2020, 11:00:28 pmYou still haven't answered this. Just to remind you.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: August 25, 2020, 09:02:36 pm
content: I've had an ISR on an AVR update a Quadrature counter in around 50 CPU cycles, and up 100.000 ISR/s is possible, It can even be done reliably, but an absolute must is then of course that it has to be the only ISR in the system.Finnicking with bits on this level was fun 20 years ago, and is still a good exercise for students, but the availability of EUR2 uC's with built in hardware for the counting wipes away the competition.So just do your homework and select a uC with this capability.Your mention of "I really need 16 bits, otherwise the communication overhead defeats the purpose. " is a strong indication you're struggling to get this to work on on some 8-bitter.I'm not saying this can not be done. I've been there and done it myself. But it's really a stubborn remnant from 20 year old practices." I honestly don't want to start learning a whole new workflow method and invest a ton of time and money into getting the programmers and the software etc working."This is also recognizable. I've been both afraid and curious of the 32-bit uC world, and I've taken the "difficult route", and regret it. Very recently though I stumbled into the book:Beginning STM32 Developing with FreeRTOS, libopencm3 and GCC.ISBN-13 (pbk): 978-1-4842-3623-9ISBN-13 (electronic): 978-1-4842-3624-6https://doi.org/10.1007/978-1-4842-3624-6It's written by Warren Gay, call sign VE3WWG.It's very well written, has all the info I scrambled together in the few years I tinkered with the "Blue Pill" board after tempted by their low price.(Warning: The days of the "Blue Pills" are over. The ceap boards from Ali / Ebay can have any of some 8 different clones, with various incompatibilities. Don't buy them. Maybe the stm32f4xx series is a better starting point, or the STM Discovery boards)You can buy STM32 "Discovery boars" for EUR20 or so, and they have a built in programmer. Hobbyists can also put one of the available open source projects in an STM32 and build a decent programmer that way. I don't know what the official tools cost, but very likely below EUR 100 these days. Compilers and IDE's are also free. GCC rules almost everywhere, though some vendors try to hide their toolchain is built around it.Ah yes, your first projects for a 32bit ARM uC will probably be a bit of a struggle, but after that it's not much different from working with the old 8-bitters. You just need a template that works for you to set up the main clock, clock domains for I/O, configure multiple ISR levels and some other things.Digikey still sells the LM628. It only costs USD58.LM628 is also designed for controlling Brushed DC motors (single PWM output?)It may be a compromise for your application to make it a 2 processor job.Putting some PID loop into a 32 bit uC capable of hardware quadrature counting is a relatively small job. With a little adapter PCB you can even make something that's pin compatible with the LM628, although almost all 32 bit uC's are 3V3 only.

author: Kjelt
author_rank: Super Contributor
timestamp: October 12, 2020, 09:28:10 am
content: Quote from: Doctorandus_P on August 25, 2020, 09:02:36 pmThanks, a nice book I now use to get back with my own STM32 development after having worked in the industry with extremely large 3rd party maintained projects where I only had to change already existing c files.The only comment I have about this book is that the author used the so called "blue pill" boards for al his examples and forgets to mention in the beginning of the book what extra hardware you need to play along.I would really have told him to also use the ST standard and well available cheap nucleo boards for this book, even contact ST for their cooperation so more boards are available.Anyway I use the nucleo board with his examples, it is just a PITA to change all the GIO ports to the nucleo boardOh well......