LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to handle quadrature encoder pulses when the rotation direction changes

I'm collecting data from a quadrature encoder (channels A, B, M/marker/index) with a NI 9411 with LV FPGA where I stream changes (positive going or negative going transitions along with logic state of the other 2 channels) on A, B, and M to LV RT (Linux-based cRIO-9030)  along with a U64 FPGA timestamp (25 nsec resolution; sample period is 500 nsec, limited to 9411 speed) to LV RT which stores the data to a USB hard drive.  I collect about a hundred gigabytes/day which I post-process in LV-Windows, looking for the needle in a haystack.

 

The reason I am doing this is because the drive the encoder is connected to sees an incorrect number of counts on channel A or B or both when the index/marker pulse is seen, which happens when the drive is commanded to move in a direction (doesn't always happen).  I see the same problem in the data I collected...no problems once the dyno is up and running in one direction.

 

Today I discovered precisely what is going on and seems to be a problem with how a quadrature encoder works.  If the encoder is rotated ever so slightly back and forth such that only one channel (A or B) changes state, the counted pulses on that channel will always increment or decrement (depending on the non-changing state of the other channel).  Attached pictures show a) LV-Windows code to process the LV-RT data (count pulses), and 2) screenshot of an Excel file showing an extra pulse on channel B while channel A doesn't change.

 

I very well could be wrong with the conclusion what I am seeing is a fundamental flaw with a quadrature encoder...maybe the LV code to count pulses could be improved, but the logic I used matches several other examples of code I have seen on the internet.

 

Comments?

 

Thanks,

 

Todd

Download All
0 Kudos
Message 1 of 16
(5,635 Views)

From what I read I have to ask...

 

Have you tried to replace the encoder?

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 2 of 16
(5,619 Views)

Yes, 2x.  Also happens on multiple drives.  The solution was to mask the fault on the drives, but I'd like to have a definitive answer why this is happening.

Message 3 of 16
(5,615 Views)

The problem is not the encoder, it's the code.   You need a better internet.

 

I don't know what the CH A/B "PGT" columns are supposed to mean, I'd have guessed from the code that they *should* mean something about whether the channel had a state transition, but the spreadsheet data isn't consistent with that interpretation.

 

Further, it isn't clear why there would be separate columns for counts on channels A & B.  That's not how quadrature decoding should be done.

 

Generally the code looks sorta approximately reasonable, but the overall form seems wrong.   My understanding is that the correct way to handle quadrature decode includes recognizing valid vs. invalid transitions in combined A,B state, *NOT* trying to first interpret each channel individually and then combining the result.

 

There are 4 quadrature states - {(0,0), (0,1), (1,1), (1,0)}.  Direction of motion is determined by whether your state transition moved you by one element from left to right in that list or right to left.   You can't move by two elements at a time -- these are illegal transitions for quadrature decoding, suggesting that both A & B changed simultaneously.   The quadrature count either increments or decrements by 1 each time a valid quadrature state transition happens.  Invalid state transitions should cause a fault.

 

...(time passes).  Does "PGT" stand for "positive going transition"?   If so, there's definitely a problem with the code.  Quadrature decode needs to care about both rising *AND* falling edges on both A and B.   Any algorithm that ignores falling edges cannot do a proper quadrature decode.

 

EDIT: please note that in the discussion below, I'm talking from the point of view of standard x4 quadrature decoding.  It appears you're looking to do x1 decoding.  x4 decoding is actually more straightforward b/c there's no special rules.  x1 decoding kinda requires you to designate some specific pair of states as the "master transition" pair -- your count only changes when transitioning between that specific pair of states in one direction or the other.

 

You can unit test your algorithm with some test scenarios to prove it out.

1.  a stream of (A,B) states that represent continuous positive rotation.  Each state transition should cause a +1 increment in quadrature count.

2.  a stream of (A,B) states that represent continuous negative rotation.  Each state transition should cause a -1 decrement in quadrature count.

3.  a set of 4 streams of (A,B) states.  Each starts at a different one of the 4 possible quadrature states.  The rest of the stream follows by first toggling CH A back and forth several times, ending on the original quad state. You should always end back at your original count (probably 0).

4. same as before but toggle CH B several times.  You should again end at 0.

 

It appears to me that your code would fail steps 3 & 4.  I didn't work through steps 1 & 2 carefully enough to comment either way.

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
Message 4 of 16
(5,605 Views)

P.S.   The *good* news is you don't need to scan through 100's of GB per day looking for a needle in the haystack.  I just handed you the needle.  You've just gotta sharpen it.

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
Message 5 of 16
(5,596 Views)

Hi Kevin,

 

Thanks for the lengthy response!

 

I'm processing channel A and channel B to ensure the # of counts received on both match the encoder PPI...trying to mimic what the drive is doing which is to diagnose the encoder (while rotating).  So far the drive manufacturer is silent on how they handle encoder pulses...maybe that's why their answer is to mask the fault.  I suspect their logic is similar to mine since the 'faults' I find happen exactly at the same point as theirs happen.

 

Yes, PGT is "positive going transition".

 

When I get back to this I'll deep-dive on your answer and make some code modifications...I have been moved onto another challenge.

 

Thanks,

 

Todd

0 Kudos
Message 6 of 16
(5,583 Views)

I agree with Kevin: That is not the correct code to get a position from an quadrature encoder. Google will find examples very easily, here's one:

http://www.ni.com/example/26580/en/

 

Perhaps it is an good idea to combine this with your code where you try to count the number of pulses on each channel seperately.

 

Regards, Jens

Kudos are welcome...
0 Kudos
Message 7 of 16
(5,572 Views)

FYI, unit test descriptions #3 & 4 I wrote about would mimic a real-world situation where motion comes to rest *extremely* close to a transition position for one of the encoder channels, A or B.  And then you add just a *little* bit of vibration (either motion settling response or externally induced).  That'll make just one channel toggle rapidly back and forth between high and low states.

 

One of the key advantages of (correct) quadrature decode is to be insensitive to such issues.  Instead of accumulating counts the way you would with just channel A, you instead toggle the count value back and forth with no cumulative error.  The code you posted that looks at both A & B but only reacts to rising edge transitions would *also* erroneously accumulate counts.

 

Your real-life motion & environment probably doesn't stumble into this specific situation very often, hence you only get a 1 count discrepancy, and it only happens rarely.

 

Dunno about the drive mfgr. and the "faults" you reference.   I'd generally be surprised if a commercial drive handled quadrature wrong, it's a standard solved problem that's implemented in commodity chips.  It's unfortunate that your prior internet checks steered you wrong on your algorithm.  It won't be hard to get it right, now that you know what to be careful of, why it's important, and how to test it.

 

 

-Kevin P

 

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 8 of 16
(5,567 Views)

For X4, you need to change the count value on any change of state on A or B.

So simply (A XOR A') OR (B XOR B')

 

Whether you increment or decrement depends on whether A = B'

 

If A = B' you increment

IF A != B' decrement.

 

Best regards,

 

0xDEAD

 

 

0 Kudos
Message 9 of 16
(5,558 Views)

Understood.  I was mainly trying to mimic what the drive does (counts pulses on A and B)...I find 'faults' just as the drive does.  Now I understand better how a quadrature encoder works...I re-wrote the code to follow allowed states, which works far better, but, I still have occasional incorrect counts that I may still investigate (I have spent far too long on this...the conclusion is correct to ignore the faults on the drives).

0 Kudos
Message 10 of 16
(5,529 Views)