Encoder Revolution Counter

What do you mean by 12-bit counter (0-4095)? I thought your counter was rolling over at 32k?

Anyway, if you have a power-of-2 counter, say 1024 (= 210), then you don't have to fiddle about with the rollover offset. Because a bit-wise AND with 1023 (=2n-1, where n=10) of the counter will give you the absolute rotation state, and you can multiply by (360.0/1024.0) to get degrees. When the counter rolls over at some larger power-of-2, the counter.ACC AND 1023 expression result will rollover from 1023 to 0 (or rollunder from 0 to 1023), which will not change the offset.

You will still need to home the system by holding the footplates wherever 0° is and resetting the counter to 0, but after that it is trivial to get the current angle:
Code:
ActualPosition := (counter.ACC AND 1023) * 360.0 / 1024.0;

Modulo 1024 would be an alternative equivalent to AND 1023, and might be easier to understand.
 
Shift a 12 bit counter left 4 places and treat is like a 32 bit value/counter. Then the lower 16 bits is fraction of a turn and upper 16 bits is the number of turns. The lower 16 bits can be multiplied by 360 to get the degrees in the upper 16 bits and the fraction of degree in the lower 16 bits.
Divides are still relatively expensive compared to shifts, adds and multiplies.
 
What do you mean by 12-bit counter (0-4095)? I thought your counter was rolling over at 32k?
**16- bit, unfortunate typo.

Could you explain the counter.ACC AND 1023 expression and the relation of the elapsed time to the 1023 or Mod 1024? I want to better understand what the expression is doing.

After looking into power of 2 counters again and seeing cost I remember why I went with incremental over absolute ones.
 
Last edited:
After looking into power of 2 counters again and seeing cost I remember why I went with this incremental encoder over those... the cheapest I have seen is automation direct, but its 3 times what the 360 ppr is since those are considered absolute encoders. I'd like to get what I have to work, and if I exhaust that option jump back to a 128 or 256 ppr option.

I will try out the delta approach as many of you have explained. I think my confusion is regarding knowing when a scan occurs. Is there a scan bit that I can use as my pulse to trigger calculations each scan cycle?
 
for the delta approach it matter neither when a scan cycle occurs nor when a counter scan* occurs, as long at any increment of the counter accumulator is << the capacity of the counter.

* A "counter scan" moves the value of the hardware counter accumulator to an image of the counter accumulator in memory; the PLC program only looks at the latter.
 
If you do not understand the binary representation of integers in a computer, refer to here first.

If you do not understand boolean math, refer to here first.

If you do not understand bitwise operations on integers, refer to here first.

Could you explain the counter.ACC AND 1023 expression and the relation of the elapsed time to the 1023 or Mod 1024? I want to better understand what the expression is doing.

After looking into power of 2 counters again and seeing cost I remember why I went with incremental over absolute ones.
Consider a 3-bit signed counter*, triggered by a shaft encoder that generates 4 pulses per revolution = 4 pulses per 360° = 1 pulse per 90°

* 3-bit counter will increment, on each encoder pulse, from 0, +1, +2, _3, (=>rollover=>) -4, -3, -2, -1, and back to 0
=> e.g. like your 16-bit counter 0, +1, +2, ..., +32766, +32767, (=>rollover=>) -32768, -32767, ... -2, -1, and back to 0

The mask to use would be 3decimal = 0b011 (binary), one less than the power of two number of encoder pulses per revolution

Say the current counter accumulator value is -2decimal = 0b110, so the shaft has rotated six pulses, or 540°, from position 0:
  • 0 to 3, 3 pulses;
  • 3 to -4 (rollover), 1 pulse;
  • -4 to -2, 2 pulses
So the shaft is a angle 180° (= 540° MODULO 360°).

The expression counter.ACC AND mask would be evaluated bit-wise i.e. would be evaluated bit-by-bit:

Code:
    110   counter.ACC, value = -2decimal
AND 011   mask, value = 3decimal
    ===
    010   counter.ACC AND mask
    |||
    ||+-- 0 AND 1=> 0
    ||
    |+-- 1 AND 1 => 1
    |
    +-- 1 AND 0 => 0

And result of the bit-wise is 2decimal, meaning the encoder shaft is 2 pulses past its "zero," and 2pulses x 90°/pulse = 180°.

Here are all of the possible counter accumulator values
  • pulse in decimal (first column),
  • pulse in binary (second column),
  • pulse AND 0b011: the result after the bit-wise AND with 3=0b011 in binary (third column),
  • pulse AND 3: the result after the bit-wise AND with 3 in decimal (fourth column),
  • (pulse AND 3) x 90° the result as an angle reduced modulo 360

Code:
ACCUM    Bits    Mask w/3   Mask w/3
Value    210     (w/0b011)  (w/0b011)
Decimal  Binary  Binary     Decimal

  0      0b000   0b000      0   Shaft at position 0 = 0°
 +1      0b001   0b001      1   Shaft at position 1 = 90°
 +2      0b010   0b010      2   Shaft at position 2 = 180°
 +3      0b011   0b011      3   Shaft at position 3 = 270°
 -4      0b100   0b000      0   Shaft at position 0 = 360° ≡ 0°
 -3      0b101   0b001      1   Shaft at position 1 = 450° ≡ 00°
 -2      0b110   0b010      2   Shaft at position 2 = 540° ≡ 180°
 -1      0b111   0b011      0   Shaft at position 3 = 630° ≡ 270°
 
Full revolution time = 2 s
pulses per revolution =1440

Hopefully the attached curves will lead the OP to some questions...
 

Attachments

  • speed_pos.png
    speed_pos.png
    11 KB · Views: 4
  • pulses.png
    pulses.png
    5.2 KB · Views: 5

Similar Topics

Sorry in advance for the long post, but this requires a little back story. I work in a facility with a couple hundred VFDs Most are in a web...
Replies
14
Views
217
I have an application using an incremental encoder and then I convert it to degree (0-360) using calculation program. For a while, the calculation...
Replies
8
Views
277
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
1,013
Dears, i am trying to change the series of encoder from A to B, but the program do not has this option (Rslogix5000, 20.06 the old encoder was...
Replies
2
Views
209
Hi all, I am implementing an incremental encoder sensor (ABZ) to replace the existing "manual" encoder wheel I have in my device. This is a 360...
Replies
0
Views
166
Back
Top Bottom