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