Motion Controller Position Latch Function
Published: 2023-06-20 08:04:48

In motion control solutions, latch function is usually required.

Today, Zmotion brings [“Motion Controller Position Latch”](http://www.zmotionglobal.com/support_info_100.html) function for you, there are several latch modes, you can select flexibly according to your specific requirements. In this article, take Zmotion motion controller ZMC408CE ([a video description](https://youtu.be/d8BzwwviSNo)) as an example to share latch related knowledge with you.


## 01 What is Latch Function?
The function of the [latch](https://en.wikipedia.org/wiki/Latch) is to respond immediately when the external IO signal is triggered, then lock the current position of the motor/encoder. It is usually used to latch the position of the product when the optical fiber sensor is encountered on the assembly line, and lock the position of the color mark on the packaging material.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7c3hlkz7oepyko4wfv2m.png)



#### (1) Characteristics of Latch

-- it supports latching encoder axis, bus axis, pulse axis and virtual axis (different types of controllers support different types of axes for latching).

--there are single latch and high-speed continuous latch modes.

-- it supports 4-channel simultaneous latching , which are R0, R1, R2, and R3 four latching channels respectively, and up to 8 latch ports can be latched at the same time, latch respond speed is fast.

-- the value of MPOS is latched when there is encoder feedback, and the value of DPOS is latched when there is no encoder feedback.

Please note different models controllers support different latching channel numbers, for specific  information, refer to the corresponding hardware manual.

Here, take motion controller ZMC408CE as the example.

ZMC408CE motion controller supports 4 latching channels, relevant interfaces are IN0-IN3.

For communication interfaces, it includes RS232, RS485, EtherNET, CAN bus, EtherCAT bus and U disk interface, and there are 8 differential pulse output interfaces on board (including encoder input), 1 dedicated handwheel interface and AD/DA analog interface.  


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hr8kjahlovlam4rjygek.png)



#### (2) Steps to Latch

--Usage of Latch--

a. to determine whether the current hardware conditions meet the latching requirements , it is necessary to determine the axis of the latching position, and the IO signal should be connected to the input port IN that supports latching.

b. set the latch input mapping port REG_INPUT , the function is to map the latched channel R0/R1/R2/R3 to the physical input port IN, and the input port needs to support the latch function.

c. set the latch mode through REGIST , which needs to be selected according to the type of the latched axis.

d. wait for the latch to trigger MARK / MARKB / MARKC / MARKD, when the latch is triggered, it becomes true.

e. after the latch is completed, print the latch position information REG_POS / REG_POSB / REG_POSC / REG_POSD.

f. the starting and ending coordinates of the latched position can be read , and the latched position can be called by other commands.

--Latch Related Commands--

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/un71np34rmzp9y4cggxp.png)

REG_INPUTS -- Map latch input

REGIST -- Set latch mode

MARK/MARKB/MARKC/MARKD -- Judge whether latch is triggered

REG_POS/REG_POSB/REG_POSC/REG_POSD -- store position after latched

When the latch occurs, the MARK / MARKB / MARKC / MARKD corresponding to the latch channel will be set to ON, and the latched position will be stored in the parameter REG_POS / REG_POSB / REG_POSC / REG_POSD.


#### (3) Map Latch Input (REG_INPUTS) Description

REG_INPUTS mapping rules are as follows, the setting of REGIST latch mode needs to be set in conjunction with REG_INPUTS.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4xgbayr4dhmrroftdi2.png)

For example:

REG_INPUTS = $3210, which means R3, R2, R1 and R0 correspond to IN3, IN2, IN1 and IN 0 respectively.

REG_INPUTS = $1023, which means R3, R2, R1 and R0 correspond to IN1, IN0, IN2 and IN 3 respectively.

REG_INPUTS = $1000, which means R3, R2, R1 and R0 correspond to IN1, IN0, IN0 and IN 0 respectively.

In this way, R0, R1, R2 and R3 signals matched by REGIST mode are not physical IO channel, but it can brings flexibility.

Actually, the output signal R0 can be relative to any one of IN(0)...IN(7) on the device (the optional input channel must be the latch channel specified in the hardware manual), or both R0 and R3 correspond to the same input port.


#### (4) REG_POS Latch Position Description

The local IO used can latch the mapping of the channel through REG_INPUTS. Please note the data storage locations of different latch signal channels are different, as shown in the table below. For details, refer to the description of the REGIST instruction.  


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vsta4hr8xld51lrkmuzv.png)




## 02 Latch Mode -- REGIST
As mentioned above, latch mode is set through REGIST, that is, select suitable latch mode according to axis type that is to be latched. There are single-time latch and continuous latch.

Therefore, different latch methods are with different trigger marks for latch signals, and latched position data are stored into different positions.

Latch channel supported by different axis types:

--encoder axis and pulse axis with feedback are latched by R0, R1, and Z pulse.

--the pulse axis and virtual axis without feedback are latched by R0 and R1.

--EtherCAT or RTEX bus axis types are latched by R2 and R3.

In addition, the EtherCAT bus can use the drive's own latch mode. For details, refer to the drive manual.


#### (1) Single-time Latch

REGIST(mode)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndv85bfj6lj95k8l9fja.png)

Note: rising edge and falling edge correspond to hardware state of controller inside. For ZMC series motion controller, it is valid when in OFF state, so falling edge is from without signal to with signal. For ECI series motion controller, it is valid when in ON state, rising edge is from without signal to with signal.

It is recommended to test the regist edge through below simple routine, then apply into project.


#### (2) Continuous Latch

Continuous latching is supported by adding 100 to the mode, and the latching result is stored in TABLE.

REGIST(100+mode, tableindex, numes)

mode: latch mode.

tableindex: the table position where the content of the continuous latch is stored. The first table element stores the number of latches, and the coordinates of the latches are stored later. The number of max stored = numes -1, when it exceeds, write in cycle.

numes: the number of tables occupied.

The continuous latch mode performs continuous latching on the two channels respectively, which can realize continuous latching of the upper and lower edges.

(ECI20150829 and above firmware support, 4 series controller 20170523 and above firmware support)

100+mode: only a single channel mode can be used, adding 100 means using continuous latch.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0unu9ndcmcgq5c9y6s1d.png)




## 03 Latch Routines

#### (1) Latch for Pulse-axis (without feedback) / Virtual-axis

It can use R0 or R1 channel, ATYPE=1/7 is set as pulse-axis, ATYPE=0 means virtual axis, then MPOS value is latched (no measurement, MPOS is false, then copy DPOS), when it is with feedback, real MPOS value measured by encoder is latched. If supports Z signal, Z signal mode also can be used.

Reference configuration:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r97pl7dny7uirkk87sgi.png)

Routine:

```
BASE(0)
ATYPE=1         'pulse axis
UNITS=100
DPOS=0
SPEED=10
ACCEL=100
DECEL=100
REG_INPUTS=0   'R0-R3 all correspond to IN0, that is, signals connect to IN (0)
REGIST(4)        'select R0 latch mode
TRIGGER         'trigger the oscilloscope
VMOVE(1)        'axis motion
WAIT UNTIL MARK 'wait for latch to be triggered
PRINT REG_POS   'print latch position
END
```

It can be seen from the waveform, IN(0) has signal to trigger latch, and REGIST(4) takes effect to latch current DPOS position then store it into REG_POS.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gc6btwuiee47foa0sxmm.png)

Modify the mode as REGIST(3), others are the same.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hd9t5b46choizsxr7fcr.png)


#### (2) Latch for Pulse-axis (with feedback) / Encoder-axis

It can use R0, R1 or Z channels (it is only valid in equipment that is with Z signal), when ATYPE is set to 4 or 5, which means pulse-axis, when it is 3 or 6, which means MPOS value is latched.

Routine:

```
BASE(0) 
ATYPE=4          'pulse axis with encoder feedback
UNITS=100
SPEED=10
ACCEL=100
DECEL=100
DPOS=0
MPOS=0
REG_INPUTS=$0   ' R0-R3 all correspond to IN0, that is, signals connect to IN (0)
REGIST(15)        'select R1 latch mode
TRIGGER
VMOVE(1)         'axis motion
WAIT UNTIL MARKB 'wait for latch to be triggered
PRINT REG_POSB  'print latch position
END
```

It can be seen from the waveform, IN(0) has signal to trigger latch, at this time, latch current DPOS position then store it into REG_POS.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f7dcyk3wbn4e5xm89upc.png)


#### (3) Latch Multi-axis Position

When multi-axis position is latched, it needs to set latch for each axis separately. In below interpolation, there are 2 axes' positions are latched.

Routine:

```
BASE(0,1)
ATYPE=1,1             'pulse-axis
UNITS=100,100
DPOS=0,0
SPEED=10,10
ACCEL=100,100
DECEL=100,100
REG_INPUTS=$0       'R0-R3 all correspond to IN0, that is, signals connect to IN (0)
REGIST(4) AXIS(0)      'select R0 latch mode for axis 0
REGIST(4) AXIS(1)      'select R0 latch mode for axis 1
TRIGGER              'trigger the oscilloscope
MOVE(1000,800)       'axis motion
WAIT UNTIL MARK(0) AND MARK(1)    'wait for latch to be triggered
PRINT REG_POS(0), REG_POS(1)       'print axis 0 and axis 1 latch position
END
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/khyqr1qilu5pl1bvrdb5.png)

Note: when multi-axis use same one latch hardware input port, same latch R channel needs to be used (for example, mode 3 / mode 4). when use different R-channel, it needs to map into different hardware inputs.


#### (4) Continuous Latch Mode

The position after the continuous latch signal is triggered. The above axis types all support the continuous latch mode. It is recommended to open a separate task to execute the continuous latch program without interfering with the operation of other programs. And the latching times and the position data can be read at any time through the TABLE register.

Routine:
```
BASE(0)
ATYPE=1             'pulse-axis
UNITS=100
DPOS=0
SPEED=10
ACCEL=100
DECEL=100
REG_INPUTS=$0      'R0-R3 all correspond to IN0, that is, signals connect to IN (0)
TRIGGER             'trigger the oscilloscope
VMOVE(1)            'axis motion
REGIST(100+4,0,100)  'continuous latch, R0 channel, table(0) saves the latching times, table (1-100) save the data that is latched each time, when it latches over 99 times, table(0) will be cleared to 0, record the data starting from table (1) again.
WAIT UNTIL MARK
```

The position data of continuous latch captured by oscilloscope: no need WHILE loop, continuous latch can be achieved.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31cyf94otxq7zlbx4e24.png)

Read latching times and position data through register:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1lwc0gr5r1394emsck6k.png)


#### (5) Latch Bus Driver

R2 and R3 channels can be used. It supports pulse-axis by setting ATYPE as 4/5, and it supports both EtherCAT and RTEX bus by setting ATYPE as 65/50, then latch MPOS value.

Using the EtherCAT bus driver, latch mode provided by the controller can be used , and the configuration method is similar to the above. Also, the latch mode that comes with the EtherCAT bus driver can be used (refer to the driver manual to complete the configuration).

When using the latch mode that comes with the EtherCAT bus driver, select the probe that the driver supports latch, and then access the latch signal.

Note: the drive PDO needs to contain the 60b8h latched data dictionary, and DRIVE_PROFILE directly selects the mode test with latch.

The latch mode adopts the mode provided by REGIST (test which modes support is required). After triggering the driver to latch, the driver will automatically transfer the latch position to the corresponding REG_POS / REG_POSB / REG_POSC / REG_POSD, and then the corresponding MARK becomes true, the user does not need to get the information from the driver data dictionary.

Routine:

```
'********************************************************************************************
'bus initialization enable program, when initialization completed, it can run below latch program.
'initialize to configure the data dictionary that needs to be included by driver PDO, DRIVE_PROFILE selects the mode that is with latch to test.
'********************************************************************************************
RAPIDSTOP
WAITIDLE
DIM num,AXIS_Max,TEMP
FOR num=0 TO 7 STEP 1
   BASE(num)
   ATYPE(num)=0
   AXIS_ADDRESS(num)=(-1<<16)+num
   ATYPE(num)=0
NEXT
num=0
SLOT_SCAN(0)
IF RETURN THEN
   ?"bus scanned","the number of connected devices："NODE_COUNT(0)
   'i is the slot No., bit axes
   FOR i=0 to NODE_COUNT(0)-1
      AXIS_Max=NODE_AXIS_COUNT(0,i) 'totals connected by single-device
      ?"AXIS_Max="AXIS_Max
      IF AXIS_Max<>0 THEN
         FOR j=0 TO AXIS_Max-1
            AXIS_ADDRESS(num)=(i<<16)+num+1
            ATYPE(num)=65             'the last step of axis mapping
            'units(num)=2^23/360        'set single-axis pulse amount
            DRIVE_PROFILE(num)=11     'set PDO function
            disable_group(num)          'make a group for each axis independently
            num=num+1                'current device total axes
         NEXT
      ELSE
         ?"no axes for current device"
         END
      ENDIF
   NEXT
   ?"axis mapped! Total axes: "num  
ELSE
   ?"bus scan failed"
   END
ENDIF
DELAY(100)
SLOT_START(0)
IF RETURN THEN
   ?"bus opened"
   DELAY(100)
   DATUM(0)                'clear all axes error states
   DELAY(100)
   ?"start to do axis enable"
   FOR i=0 to num-1
      base(i)
      AXIS_ENABLE=1       'single-axis enable
   NEXT
   WDOG=1                 'axis enable master switch is ON
   ?"axis enabled"
ELSE
   ?"bus open failed"
ENDIF
?"configuration completed"
adasda()                    'call latch function
END
'********************************************************************************************
'Latch function
'select the probe whose driver supports the latch, then connect to latch signal.
Latch mode uses the mode provided by REGIST, after latch is triggered, driver will pass latch position to REG_POS. 
'********************************************************************************************
WHILE 1
   IF OP(0) = ON THEN
      OP(0, OFF)
      temp=-1
   ENDIF
      temp=0
WEND
GLOBAL sub adasda()
   dim num, temp
   num=1
   temp=0
   BASE(0)
   REGIST(100+3,0,100)AXIS(0)    'automatically cycle, no need to write into while loop, table(0) saves the latching times, table (1-100) save the data that is latched each time, when it latches over 99 times, table(0) will be cleared to 0, record the data starting from table (1) again.
   'REGIST(3)
   WHILE 1
      ?"*********************************************************"         
      WA 10
      ?"reg_pos="REG_POS," latch value TABLE="TABLE(num)," occupy TABLE="TABLE(0)       'print
      ?"driver probe mode="NODE_PDOBUFF(0,0,$60B8,0,6)
      ?"driver probe state ="NODE_PDOBUFF(0,0,$60B9,0,6)
      ?"driver latch value ="NODE_PDOBUFF(0,0,$60BA,0,7)
      IF num=100 THEN
         num=1
      ELSE
         num=num+1
      ENDIF
      WA 100     'delay 1ms, anti-shaking
   wend
ENDSUB
```

Use the continuous latch mode REGIST(100+3,0,100), and use 100 spaces starting from TABLE(0) to save the latch data, where TABLE(0) saves the number of consecutive latches, TABLE(1)- TABLE(99) save the position of each latch.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gwdq3wro8udi8xj6rqz5.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oiyot7nw7kjxg6gm0iee.png)




ABOUT ZMOTION

That's all, thank you for your reading -- [Zmotion Motion Controller Position Latch Function](http://www.zmotionglobal.com/support_info_100.html)

Hope to meet you, talk with you and be friends with you.  Welcome!

This article is edited by ZMOTION, here, share with you, let's learn together.

Note: Copyright belongs to Zmotion Technology, if there is reproduction, please indicate article source. Thank you.

Have a good day, best wishes, see you next time.