CNC Services Northwest

Centroid CNC control sales, service, training and support


MPU11 PLC Programming: Integer Divide and Modulo

The MPU11 PLC language includes integer (word) variables, and includes division and modulus (remainder) operators to use on them.

Modulus is often useful when looking at rotational counters that need to wrap around, such as a tool turret or a spindle rotation encoder.

For example, if a machine has a 1024-line (4096-count) spindle encoder, and the PLC program needs to determine spindle orientation, then an encoder position of 200, or 4296, or 8392, or -3896 all indicate the same position of the spindle (just differing numbers of whole turns).

To get the PLC code for this correct, we need to know how the language treats negative numbers. Some experimentation shows:
ABA / BA % B
++++
-+--
+--+
--+-

Sample Code

  ; Read spindle encoder value from axis #6 encoder input,
  ; then use modulo to determine counts away from index pulse
  ; (assuming that the spindle encoder value was zeroed at the index)
  IF True THEN SpinEncoder_W = SV_MPU11_ABS_POS_5 % 4096

  ; If the spindle position was negative, then the result of modulo
  ; will be negative.  Fix that so we can consistently compare it to
  ; a target position, regardless of which direction the spindle moved.
  IF SpinEncoder_W < 0 THEN SpinEncoder_W = SpinEncoder_W + 4096
  
  ; Now compare to a target position stored in a parameter, allowing
  ; for a few counts tolerance (in-position window +/- 20 counts)
  IF ABS(SpinEncoder_W - SV_MACHINE_PARAMETER_954) < 20 THEN JMP NextStage


CNC Services Northwest Home

Copyright © 2017 Marc Leonard
Last updated 14-Aug-2017 MBL