OK, so I haven't thought about this since January, so forgive me if I repeat myself, or get confused, or cause confusion, or if I explain things too simply.
An Interrupt tells a device to stop what it is doing, run another bit of code, then pass control back to the main program. The 'another bit of code' should be as short as possible.
PLCs can usually be programmed with a range of Interrupts, Interrupts can be called by external signals such as a change of state of an input, or by PLC internal values, a timer reaches a set point, a high speed counter (HSC) reaches a set point, a high speed counter changes direction, a message is available on a communications port, plus others.
I am suggesting that you use two Interrupts, one internal to the PLC, triggered by the HSC reaching a set point, and an external rising edge signal which is the Z output from the encoder.
First just look at the HSC at Set Point Interrupt, it is called when the HSC reaches a Set Point but there is nothing that says the set point has to be the same value for every Interrupt, you can calculate a new set point every machine cycle. I don't know how you set the machine to a start point, but whichever control method you use, you will always need a know start point. The Operator enters a Set Point (OP_SP), and presses a Start button. The PLC reads the current HSC value (HSC_Initial) and adds it to the OP_SP to give the High Speed Counter a first set point. You pass this start value to the HSC Interrupt, then you start the machine moving. At some point there will be an Interrupt raised when the HSC reaches the set point. Within the HSC Interrupt you grab the HSC current value and add the OP_SP to that value to calculate the next Set Point for the HSC. (see note 1). And keep doing that every machine cycle, the HSC counter will keep increasing in value.
On to the encoder Z output PLC rising edge input Interrupt (Z_INT). Encoders can go faulty and stop giving out the right number of pulses per revolution. It is very useful to be able to spot the error and raise a fault on the machine. So with the Interrupt called by Z_INT you read the current HSC value Z_CURRENT and subtract from it the last value (Z_LAST), the calculated value should be the number of pulses per rev of the encoder. If not, raise a fault. The first value will always be wrong, so discard it. On the machines I work with a small error isn't a problem, so I don't raise an alarm unless the error is outside of the 'small'. If you want to be a real smarty pants you can adjust the Operator Target by the error value to try to keep the machine running. But from experience once an encoder starts to go faulty it rapidly fails completely.
Note 1
Depending on how quickly the encoder pulses are received there is a chance that another pulse will have been received by the HSC, in which case you have to compare the Set Point to the HSC current value and adjust the new Set Point by the difference.
I hope I have explained that well enough, if not I don't mind questions
.
B