Motion Control PSO Position Synchronous Output (1) Hardware Control & PSO Commands
Tags: programming, tutorial
Published: 2023-07-28 08:02:27

Here, PSO function of motion controller will be introduced. PSO is not "particle swarm optimization" explained by Wikipedia. PSO means Position Synchronous Output function  in motion control, or hardware comparison output. Through this function, it can output signals synchronously at high-speed. Then in real applications, high-speed and high-precision requirements in motion control or automation solutions can be met, which improves the efficiency and quality, and reduces the labor cost, then brings a wonderful development.

In Zmotion technical support, there are three parts information to be shown in total.

That is:

[Motion Control Position Synchronous Output (1) | Hardware Platform & PSO Instructions](http://www.zmotionglobal.com/support_info_104.html)

Motion Control Position Synchronous Output (2) | PSO Modes

Motion Control Position Synchronous Output (3) | High-precision 2D/3D PSO

Now, let's enter our first "motion controller position synchronization output"learning journey.


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

[hardware platform environment building](https://youtu.be/S0gUAx2minY)


## 01 Hardware Platform
Position synchronous output PSO function is required, which means hardware should support PSO function. That is, select the motion controller that is with PSO function and with required axes for actual applications.

In Zmotion, there are several series of motion controllers. Of course, some support position synchronized output function, for example, ZMC406 bus EtherCAT motion controller, ZMC460N dual-bus (EtherCAT & RTEX) bus motion controller, etc., and here, take these two motion controllers as examples to describe the hardware comparison output function.

PSO hardware comparison output function uses high-speed IO, the respond frequency is 1MHz, and the respond speed can be up to us level.

More controller specification parameters can be checked through "?*max" in ZDevelop.

"?*set" can view instruction parameter values.
"?*port" can view communication channels.


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

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


## 02 Position Synchronization Output PSO Function
Position synchronized output function is used to compare real-time captured encoder measurement position (pulse position can be used when there is no encoder) and position set by comparison mode, then control OP to output signals synchronously at high-speed.

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

Generally, PSO function outputs signals synchronously with the laser (or dispensing spraying valve devices) to synchronize phases. In motion trajectory all stages (acceleration, deceleration and constant speed) , trigger output switch in constant space or constant time to achieve that pulse energy are distributed in processed object.

"Position synchronization output" function ensures high-speed and stable output signals, in whole motion trajectory , trigger output signal with fixed distance, no need to consider the whole speed due to high enough output precision.

Specifically, in linear part, it is with rapid speed. In fillet deceleration part, it ensures constant space output. Usually, fillet processing part occupies a small part in whole processing process, which means processing effect is ensured and production can be promoted at the same time.

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



## 03 Position Synchronization Output Commands
PSO hardware comparison output function is mainly achieved by "HW_PSWITCH2", "MOVE_HWPSWITCH2" and "HW_TIMER". Former two commands are used to set the distance of triggering comparison output . The last one is used for hardware timer , which can be matched with former to control pulse width precisely.

#### (1) HW_PSWITCH2 -- Hardware Position Comparison Output

Instruction Description:

Through setting comparison conditions, control OP port to high-speed output signals continuously, and please note controller must be with output port that supports hardware comparison output. For example, ZMC406 can use OUT0/1/2/3, ZMC460N can use OUT0-OUT11.

It can compare pulse axis position, encoder measurement position and bus axis position. And the comparison position is set by ATYPE. When it compares main axis that is with encoder input, automatically using encoder position to trigger, when no encoder, comparing pulse output.

If needs to adjust output precise time, "MOVEOP_DELAY" can be used.

Notes:

-- for general controllers, each system cycle can only compare once, which can be checked through "SERVO_PERIOD". But when system period is too large, and comparison output pulse width is less than system cycle, it will cause abnormal output. For some controllers, ZMC460N, ZMC504SCAN, multiple comparison can be achieved by one system cycle.

--"HW_PSWITCH 2" command and "MOVE_OP" precision command use same hardware resources, it is not recommended to use them at the same one channel, but they can be used in different channels synchronously.

--While calling TABLE position data, don't modify TABLE before completing all comparison points.

--While using pulse motors, measurement position (MPOS) is compared only when ATYPE is 4. If ATYPE is 1/7 (default), command position (DPOS) is compared.

Instruction Grammar:

HW_PSWITCH2(mode, [...])

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

Different modes need to fill in different parameters, below shows 2 simple examples, for more, please refer to ZBasic manual.

A. Mode = 1: single-axis comparison

Mode=1: single axis comparison
HW_PSWITCH2(1,opnum,opstate,tablestart,tableend[,Direction])
mode: 1-open comparer
opnum: relevant outputs
opstate: output status of the first comparison position
tablestart: TABLE No. that saves the first comparison point's absolute coordinate
tablesend: TABLE No. that saves the last comparison point's absolute coordinate
direction: the first coordinate to judge direction, 0-negative, 1-positive, -1-no direction used.

This mode is very easy. It needs TABLE register to save position coordinates that requires comparison output, then under PSO control, when reaching one comparison point position, it will inverse it, until all coordinates completed. Please refer to below image, it shows 6 comparison points' OP output, P means comparison point.

For example: mode=1, single-axis mode, compare positions in TABLE

```
BASE(0)
ATYPE=0
UNITS=10000/10
SPEED=100
ACCEL=1000
DECEL=1000
SRAMP=100
DPOS=0
MPOS=0
OP(0,OFF)
TABLE(0,50,100,150,200)   'comparison position coordinates configuration
HW_PSWITCH2(2)            'stop and delete comparison that not finished
HW_PSWITCH2(1, 0, 1, 0, 3,1)   'compare 4 positions, operate OUT0
TRIGGER                          'trigger the oscilloscope
MOVE(300)
END
```

captured oscilloscope waveform: when reached TABLE four coordinates 50, 100, 150, 200, OP reverses.

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

B. Mode=6: vector comparison method, cycle mode, used together with HW_TIMER
HW_PSWITCH2 (mode,opnum,opstate,vectstart,repes,cycledis)
mode: 6-open comparer
opnum: relevant outputs
opstate: output status of the first comparison position
vectstart: comparison point VECTOR_MOVED current motion distance
repes: repeat period, compare once in one cycle
cycledis: period distance, output opstate every time after cycledis

This mode doesn't need TABLE, it only needs to specify the "VECTOR_MOVED" of the first trigger position, including the times of comparison period, the distance of each output. Then, use "HW_TIMER" to control the pulse width and times of position output when achieves one period each time.

Below shows parameters configuration, red one is HW_PSWITCH2 instruction, orange one is HW_TIMER instruction.

And all distance involved in this mode are vector coordinates, which are used for comparison output under single-axis motion or interpolation motion mode.

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

For example: mode=6, period mode used together with HW_TIMER

```
RAPIDSTOP(2)
WAIT IDLE(0)
BASE(0)
ATYPE=1
UNITS=10000/10
SPEED=100
ACCEL=1000
DECEL=1000
SRAMP=100
DPOS=0
MPOS=0
OP(0,OFF)
TRIGGER
VECTOR_MOVED=0   'set vector initial position as 0 for viewing
HW_PSWITCH2(2)      'stop and delete comparison that not finished.
HW_PSWITCH2(6,0,1,100,4,50)  'start to compare from position 100, compare 4 times, period distance is 50, output valid time is determined by HW_TIMER
HW_TIMER(2,100000,50000,2,off,0)  'after 50ms, OUT becomes off from on
MOVE(400)
END
```

captured waveform:

Output port is OP(0), the first comparison output status is ON , and the vector coordinate of the first OUT is 100, compare 4 times, and trigger once when spans 50.

When each time triggered, the output time of OP is set by HW_TIMER. Here, each pulse output cycle is 100ms, valid width is 50ms, and trigger twice continuously for each comparison position.

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

under YT mode:

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

Change the single axis motion as MOVE(200, 300) two-axis interpolation, others are the same, compare combined vector position of two axes "VECTOR_MOVED".

```
RAPIDSTOP(2)
WAIT IDLE(0)
BASE(0)
ATYPE=1
UNITS=10000/10
SPEED=100
ACCEL=1000
DECEL=1000
SRAMP=100
DPOS=0
MPOS=0
OP(0,OFF)
TRIGGER
VECTOR_MOVED=0   'set vector initial position as 0 for viewing
HW_PSWITCH2(2)      'stop and delete comparison that not finished.
HW_PSWITCH2(6,0,1,100,4,50)  'start to compare from position 100, compare 4 times, period distance is 50, output valid time is determined by HW_TIMER
HW_TIMER(2,100000,50000,2,off,0)  'after 50ms, OUT becomes off from on
MOVE(200,300)  'two-axis linear interpolation
END
```


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

under XYZ mode:

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



#### (2) HW_TIMER -- Hardware Timing

Instruction Description:

Hardware timer is used to restore electric level after hardware comparison output for a certain time. There is only one "HW_TIMER", and when HW_TIMER is called each time, the former calling will be stopped compulsively.

And OP and MOVE_OP can close HW_TIMER pulse that is operating, which means HW_TIMER can be used as PWM. OP outputs, pulse output will be on, then next OP outputs, pulse output will be closed. When MOVE_OP precision output is used, infinite pulse function of precision PWM output can be realized.

Use ?*HW_TIMER to see the number of remaining pulses.

Notes:

Each period outputs signal once, and the period time must be larger than system period, otherwise, output will be abnormal. Also, used output ports must support position synchronized output function.

Instruction Grammar:

HW_TIMER(mode, cyclonetime, optime, reptimes, opstate, opnum )
mode: 0-stop, 1-run
cyclonetime: cycle time, us is the unit
optime: valid time, us is the unit
reptimes: repeat times, start the mode, when reptimes=0, HW_TIMER will be softly closed, and uncompleted pulses will keep outputting.
opstate: output default status, start to do timing when output port is not current state
opnum: output NO., the port must support hardware comparison output.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i06p29ninu6vcg36gj6k.png)

For example:

```
RAPIDSTOP(2)
WAIT IDLE(0)
BASE(0)
ATYPE=1
UNITS=100
SPEED=100
ACCEL=100
DECEL=100
DPOS=0
TRIGGER
DELAY(100)
OP(0, OFF)
HW_TIMER(2, 10000, 5000, 30, OFF, 0)  'after OUT0 is changed to ON, hardware timer is triggered to do timing, the cycle is 10ms, after 5ms, it becomes OFF, until 30 times.
OP(0, ON)         'trigger timing
END
```

Please refer to below captured OP(0), the period is 1ms, which means the time unit is 1ms. After delay 100ms, it starts to trigger OP high-speed output, the total time is "10000us*30". The third parameter is used to adjust output pulse's width.

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



#### (3) MOVE_HWPSWITCH2 -- Hardware Comparison Output in Buffer

This command's function and usgae are same as "HW_PSWITCH2", the difference is that this command enters motion buffer, that is, execute comparison in buffer.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8bri77g9gi4fs9iyz3bc.jpg)

[Command Video Description, Welcome!](https://youtu.be/sfyQM15E8tw)


## 4. Example: Output Pulses with Equal Space
"HW_PSWITCH2" mode 6 is used, and use with "HW_TIMER" together to control single-axis high-speed equal space output.

```
RAPIDSTOP(3)
WAIT  IDLE(0)
GLOBAL CONST Axis_X = 0       'define physical axis No.
GLOBAL CONST Out_Pso0 = 0  'PSO hardware output No.

BASE(Axis_X)        'initialize axis parameters
UNITS=1000        'pulse amount, the number of pulses corresponding to 1MM
ATYPE=4             'axis type, pulse output + encoder input axis
DPOS=0
MPOS=0
SPEED=100
ACCEL=2000
DECEL=0
MERGE=1
OP(Out_Pso0,OFF)

GLOBAL g_cmd
g_cmd = 0
WHILE 1
   IF g_cmd =  1 THEN
      g_cmd = 0
      TRIGGER
      Function_Test1()  
   ENDIF
WEND
'********************************************************************************************
'generate one PWM pulse under equal-space mode, pulse output time can be adjusted.
'lv_WidthTime  pulse width time us
'lv_Interval       2 pulse spans mm
'lv_StrartPos    trigger initial position mm
'lv_EndPos       end position
'********************************************************************************************
GLOBAL SUB Function_Test1()   'in 20-120, output one continuous 2ms pulses with a spacing of 1ms each time
   LOCAL lv_WidthTime,lv_Interval,lv_StrartPos,lv_EndPos  'define local variables
   lv_WidthTime = 2000    'pulse width 2000us  
   lv_Interval = 1               'pulse space 1mm
   lv_StrartPos = 20           'trigger initial position 20mm
   lv_EndPos = 120            'end position 120mm
   
   OP(Out_Pso0,OFF)  'close the output port
   BASE(Axis_X)           'select axis X
   ATYPE = 4              'axis type, when the axis is with encoder axis, encoder position is compared by default.
   SPEED = 100          'speed is 100mm/s
   FORCE_SPEED=60  'SP speed is 60mm/s
   MOVEABS(0)          'move to 0
   WAIT IDLE
   VECTOR_MOVED = 0 'clear interpolation vector distance to 0
   
   LOCAL iTime
   iTime =ABS(lv_EndPos - lv_StrartPos) \ lv_Interval  'calculate comparison times
   TRACE lv_StrartPos,iTime,lv_Interval,lv_WidthTime
   
   HW_PSWITCH2(2)  'clear HW comparison buffer area
   HW_PSWITCH2(6,Out_Pso0,ON,lv_StrartPos,iTime,lv_Interval)  'start to trigger from lv_StrartPos, the span is lv_Interval, compare iTime times.
   HW_TIMER(2,lv_WidthTime+100,lv_WidthTime,1,OFF,Out_Pso0)  'after Out_Pso is triggered to be ON, and after open lv_Width us, close output
   
   MOVEABS(50)        'single-axis motion, speed is 100
   MOVEABSSP(100)  'SP single-axis motion, speed is 60
   MOVEABS(150)      'single-axis motion, speed is 100
   WAIT IDLE             'wait for motion to stop
   HW_PSWITCH2(2)  'clear HW comparison buffer area
END SUB
```


## 5. PSO Curve in ZDevelop
3 single-axis linear motion, the comparison range is position 20-120, compare once when spans one unit, the total is 100 time. 
The middle segment is equal-space comparison, the motion speed is small, so OP reverse speed becomes slower.
OP holding high level time is 2ms.


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

Under XY mode, OP changes with motion distance. 

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

It can be seen comparison output is not effected by speed under this equal-space mode, the output is distributed all the time.