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