How to calculate absolute encoder position?

g.mccormick

Lifetime Supporting Member
Join Date
Jul 2012
Location
IN
Posts
960
I've never really thought about these before, but a potential use in knowing the angle of a table has me looking at them. I don't really konow anything about gray code, but seemingly you have gray code from the encoder. You convert the gray code to binary, then the binary to decimal.
So if an encoder was 1024 ppr, and the decimal value of the current position is 512, you would know the encoder is at 180 degrees correct?
Looking at automation direct, the 1024 ppr encoder is a 10bit gray code.
How would I convert the gray code into binary?
 
Fun fact; Gray code should be written with a capital G, after Dr. Frank Gray of Bell Labs who invented the reflected binary code in 1947. He wrote in his patent application that his method "at present had no recognized name".

Most PLC vendors have an example of how to decode Gray code inputs into a decimal value. You're correct about the basic idea that a 1024 count (= 10 bit) absolute encoder would decode to a value of 512 at the middle position.

One important thing about doing Gray code inputs is that you have to sample them all at once; with any networked I/O device you need to take care to be sure that you don't get any bytes out of sync.
 
Last edited:
I found a link and I think I understood it enough. I attempted some code in productivity suite. My PDF print is too large to attach.
 
Its just exclusive or. MSB Gray is MSB binary.

Here is a sample program. Amazing stuff. An optical beam of light shines through a rotating disk with a unique angular pattern. Its done in Gray Code because only one bit changes at a time as opposed to binary such as 0111 (7 in decimal) to 1000 (8 in decimal)
 
Gray code was developed so that hardware can detect an error, as only 1 bit changes from one count to the next.

In most situations using Gray encoders, you would use a specialist input module that is designed to decode the n-bit Gray inputs into an absolute decimal number, and at the same time would notify you if the Gray transitions (i.e. only 1 bit can change) were violated in any way.

Most PLCs cannot detect that error condition - because the input data is not being presented to the PLC continuously, but it is either "scanned" or "consumed" at a (sometimes variable) rate. In that scenario it is highly probable that the encoder has turned enough that the input data will violate the Gray rule.

In a nutshell, if you connected a Gray encoder to a standard PLC input module, you can do no better than to just convert Gray to Decimal, and accept the fact that the "feature" of Gray encoding is unusable, and a standard Absolute (binary) encoder would be just as good.
 
Gray code was developed so that hardware can detect an error, as only 1 bit changes from one count to the next.

In most situations using Gray encoders, you would use a specialist input module that is designed to decode the n-bit Gray inputs into an absolute decimal number, and at the same time would notify you if the Gray transitions (i.e. only 1 bit can change) were violated in any way.

Most PLCs cannot detect that error condition - because the input data is not being presented to the PLC continuously, but it is either "scanned" or "consumed" at a (sometimes variable) rate. In that scenario it is highly probable that the encoder has turned enough that the input data will violate the Gray rule.

In a nutshell, if you connected a Gray encoder to a standard PLC input module, you can do no better than to just convert Gray to Decimal, and accept the fact that the "feature" of Gray encoding is unusable, and a standard Absolute (binary) encoder would be just as good.

Are you saying that the plc would not be able to detect I'd the encoder moved out of position and then back within the same plc scan cycle? No I guess it wouldn't as the decimal would be the same.
 
I think addressing the possibility of a misread in a parallel transmission system because of skew is minimized by the Gray Code. Of course if the parallel transitions are occurring faster than the response time of the input device then the benefit is mostly lost.

As a side note, Gray Code encoders are available for numbers which aren't powers of 2.
 
I agree with Bernie on this. The inability of the plc to read each position transition (it may be able to; the OP never said anything about speed) doesn't change the fact that Gray code significantly reduces the chances of slew related misreads. That fact alone is enough to move one to use Gray code as opposed to natural binary.

Keith
 
I agree with Bernie on this. The inability of the plc to read each position transition (it may be able to; .....

You agreed with Bernie, who I believe agreed with me...

bernie_carlton said:
Of course if the parallel transitions are occurring faster than the response time of the input device then the benefit is mostly lost.

then you concluded with...

.... That fact alone is enough to move one to use Gray code as opposed to natural binary.

Gray code encoders are only an advantage if the input circuitry is able to detect any false transitions as the inputs move from one state to the next - they don't give you any benefits in any situation where the "input" data is either "scanned" or "consumed" at a rate that could be slower than the encoder's data transitions.

I doubt there are many PLCs that could reliably detect Gray coded data violations, because the input data is "sampled" in some way.
 

Similar Topics

Hi. I've got an absolute grey code encoder (10 bit) on a bi-directional mobile shuttle. I've converted the grey code to a 0-1023 integer value...
Replies
6
Views
5,008
Hi everyone, This is my first time posting, so please forgive any omissions or mistakes. I am attempting to control the velocity of a stepper...
Replies
18
Views
744
Good morning, I have a question. I don't know much about ST yet I would like to calculate the average number of products per hour. how do I do...
Replies
22
Views
2,871
Can someone help me with this? I'm no good at SCL - virtually everything I've done so far has been ladder logic. The return value from the...
Replies
13
Views
1,020
I want to calculate the energy consumed from instantaneous power. Can this be done using the TOT block with timebase set to Hour?
Replies
2
Views
655
Back
Top Bottom