1'''
2Motion - Motion_3axis_synchronous_move.py with asynchronous mode.
3
4-------------------------------------------------------------------------------------
5Please change correct serial number or IP and port number BEFORE you run example code.
6
7For other examples please check:
8 https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
9See README.md file to get detailed usage of this example.
10
11Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
12'''
13
14## WPC
15from wpcsys import pywpc
16
17## Python
18import threading
19import time
20import asyncio
21
22
23async def getAxisStatus(handle, port, axis, delay=0.005):
24 move_status = await handle.Motion_getMoveStatus_async(port, axis)
25 if move_status != 0:
26 print(f"Move completed axis {axis}...")
27
28 ## Wait for seconds
29 await asyncio.sleep(delay) ## delay [sec]
30 return move_status
31
32
33def Axis1_thread(handle, port, axis, delay=0.005):
34 move_status = 0
35 while move_status == 0:
36 move_status = asyncio.run(getAxisStatus(handle, port, axis, delay))
37
38 ## Wait for seconds
39 time.sleep(delay) ## delay [sec]
40
41
42def Axis2_thread(handle, port, axis, delay=0.005):
43 move_status = 0
44 while move_status == 0:
45 move_status = asyncio.run(getAxisStatus(handle, port, axis, delay))
46
47 ## Wait for seconds
48 time.sleep(delay) ## delay [sec]
49
50
51def Axis3_thread(handle, port, axis, delay=0.005):
52 move_status = 0
53 while move_status == 0:
54 move_status = asyncio.run(getAxisStatus(handle, port, axis, delay))
55
56 ## Wait for seconds
57 time.sleep(delay) ## delay [sec]
58
59
60async def main():
61 ## Get Python driver version
62 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
63
64 ## Create device handle
65 dev = pywpc.EMotion()
66
67 ## Connect to device
68 try:
69 dev.connect("192.168.1.110") ## Depend on your device
70 except Exception as err:
71 pywpc.printGenericError(err)
72 ## Release device handle
73 dev.close()
74 return
75
76 try:
77 ## Parameters setting
78 port = 0 ## Depend on your device
79 axis1 = 0
80 axis2 = 1
81 axis3 = 2
82 two_pulse_mode = 1
83 rel_posi_mode = 1
84 stop_decel = 0
85
86 ## Axis and encoder parameters
87 axis_dir_cw = 0
88 encoder_dir_cw = 0
89
90 ## Polarity and enable parameters
91 active_low = 0
92 active_high = 1
93 forward_enable_false = 0
94 reverse_enable_false = 0
95
96 ## Get firmware model & version
97 driver_info = await dev.Sys_getDriverInfo_async()
98 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
99
100 ## Define Axis1 ~ Axis3 thread
101 thread_1 = threading.Thread(target=Axis1_thread, args=[dev, port, axis1, 0.005])
102 thread_2 = threading.Thread(target=Axis2_thread, args=[dev, port, axis2, 0.005])
103 thread_3 = threading.Thread(target=Axis3_thread, args=[dev, port, axis3, 0.005])
104
105 ## Thread start
106 thread_1.start()
107 thread_2.start()
108 thread_3.start()
109
110 ## Motion open
111 err = await dev.Motion_open_async(port)
112 print(f"open_async in port {port}, status: {err}")
113
114 '''
115 ## Motion open configuration file
116 err = await dev.Motion_openCfgFile_async('C:/Users/user/Desktop/3AxisStage_2P.ini')
117 print(f"openCfgFile_async, status: {err}")
118
119 ## Motion load configuration file
120 err = await dev.Motion_loadCfgFile_async()
121 print(f"loadCfgFile_async, status: {err}")
122 '''
123
124 ## Motion configure for axis1
125 err = await dev.Motion_cfgAxis_async(port, axis1, two_pulse_mode, axis_dir_cw, encoder_dir_cw, active_low)
126 print(f"cfgAxis_async in axis{axis1}, status: {err}")
127
128 err = await dev.Motion_cfgLimit_async(port, axis1, forward_enable_false, reverse_enable_false, active_low)
129 print(f"cfgLimit_async in axis{axis1}, status: {err}")
130
131 err = await dev.Motion_rstEncoderPosi_async(port, axis1, encoder_posi=0)
132 print(f"rstEncoderPosi_async in axis{axis1}, status: {err}")
133
134 err = await dev.Motion_cfgAxisMove_async(port, axis1, rel_posi_mode, target_posi=1000, velo=10000, accel=100000, decel=100000)
135 print(f"cfgAxisMove_async in axis{axis1}, status: {err}")
136
137 ## Servo on
138 err = await dev.Motion_enableServoOn_async(port, axis1)
139 print(f"ServoOn in axis{axis1}, status: {err}")
140
141 ## Motion configure for axis2
142 err = await dev.Motion_cfgAxis_async(port, axis2, two_pulse_mode, axis_dir_cw, encoder_dir_cw, active_low)
143 print(f"cfgAxis_async in axis{axis2}, status: {err}")
144
145 err = await dev.Motion_cfgLimit_async(port, axis2, forward_enable_false, reverse_enable_false, active_low)
146 print(f"cfgLimit_async in axis{axis2}, status: {err}")
147
148 err = await dev.Motion_rstEncoderPosi_async(port, axis2, encoder_posi=0)
149 print(f"rstEncoderPosi_async in axis{axis2}, status: {err}")
150
151 err = await dev.Motion_cfgAxisMove_async(port, axis2, rel_posi_mode, target_posi=1000, velo=10000, accel=100000, decel=100000)
152 print(f"cfgAxisMove_async in axis{axis2}, status: {err}")
153
154 ## Servo on
155 err = await dev.Motion_enableServoOn_async(port, axis2)
156 print(f"ServoOn in axis{axis2}, status: {err}")
157
158 ## Motion configure for axis3
159 err = await dev.Motion_cfgAxis_async(port, axis3, two_pulse_mode, axis_dir_cw, encoder_dir_cw, active_low)
160 print(f"cfgAxis_async in axis{axis3}, status: {err}")
161
162 err = await dev.Motion_cfgLimit_async(port, axis3, forward_enable_false, reverse_enable_false, active_low)
163 print(f"cfgLimit_async in axis{axis3}, status: {err}")
164
165 err = await dev.Motion_rstEncoderPosi_async(port, axis3, encoder_posi=0)
166 print(f"rstEncoderPosi_async in axis{axis3}, status: {err}")
167
168 err = await dev.Motion_cfgAxisMove_async(port, axis3, rel_posi_mode, target_posi=-5000, velo=10000, accel=100000, decel=100000)
169 print(f"cfgAxisMove_async in axis{axis3}, status: {err}")
170
171 ## Servo on
172 err = await dev.Motion_enableServoOn_async(port, axis3)
173 print(f"ServoOn in axis{axis3}, status: {err}")
174
175 ## Motion start
176 err = await dev.Motion_startMultiAxisMove_async(port, [axis1, axis2, axis3])
177 print(f"startMultiAxisMove_async in port {port}, status: {err}")
178
179 ## Wait for thread completion
180 thread_1.join()
181 print("Axis1_Thread returned.")
182
183 thread_2.join()
184 print("Axis2_Thread returned.")
185
186 thread_3.join()
187 print("Axis3_Thread returned.")
188
189 ## Servo off
190 for i in [axis1, axis2, axis3]:
191 err = await dev.Motion_enableServoOff_async(port, i)
192 print(f"ServoOff in axis{i}, status: {err}")
193
194 ## Motion stop
195 for i in [axis1, axis2, axis3]:
196 err = await dev.Motion_stop_async(port, i, stop_decel)
197 print(f"stop_async in axis{i}, status: {err}")
198
199 ## Motion close
200 err = await dev.Motion_close_async(port)
201 print(f"close_async in port {port}, status: {err}")
202 except Exception as err:
203 pywpc.printGenericError(err)
204
205 finally:
206 ## Disconnect device
207 dev.disconnect()
208
209 ## Release device handle
210 dev.close()
211
212
213def main_for_spyder(*args):
214 if asyncio.get_event_loop().is_running():
215 return asyncio.create_task(main(*args)).result()
216 else:
217 return asyncio.run(main(*args))
218
219
220if __name__ == '__main__':
221 asyncio.run(main()) ## Use terminal
222 # await main() ## Use Jupyter or IPython(>=7.0)
223 # main_for_spyder() ## Use Spyder