1'''
2Motion - Motion_2axis_linear_interpolation.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 asyncio
16
17## WPC
18
19from wpcsys import pywpc
20
21async def main():
22 ## Get Python driver version
23 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
24
25 ## Create device handle
26 dev = pywpc.EMotion()
27
28 ## Connect to device
29 try:
30 dev.connect("192.168.1.110") ## Depend on your device
31 except Exception as err:
32 pywpc.printGenericError(err)
33 ## Release device handle
34 dev.close()
35 return
36
37 try:
38 ## Parameters setting
39 port = 0 ## Depend on your device
40 stop_decel = 0
41
42 ## Linear interpolation parameters
43 axis1 = 0
44 dest_posi1 = 2000
45 axis2 = 1
46 dest_posi2 = 2000
47
48 ## Get firmware model & version
49 driver_info = await dev.Sys_getDriverInfo_async()
50 print("Model name: " + driver_info[0])
51 print("Firmware version: " + driver_info[-1])
52
53 ## Motion open
54 err = await dev.Motion_open_async(port)
55 print(f"open_async in port {port}, status: {err}")
56
57 ## Motion open configuration file
58 err = await dev.Motion_openCfgFile_async('C:/Users/user/Desktop/3AxisStage_2P.ini')
59 print(f"openCfgFile_async, status: {err}")
60
61 ## Motion load configuration file
62 err = await dev.Motion_loadCfgFile_async()
63 print(f"loadCfgFile_async, status: {err}")
64
65 ## Motion configure
66 err = await dev.Motion_cfg2AxisLinearInterpo_async(port, axis1, dest_posi1, axis2, dest_posi2, speed=2000, accel=100000, decel=100000)
67 print(f"cfg2AxisLinearInterpo_async in axis{axis1} and {axis2}, status: {err}")
68
69 ## Motion start
70 err = await dev.Motion_startLinearInterpo_async(port)
71 print(f"startLinearInterpo_async in port {port}, status: {err}")
72
73 move_status = 0
74 while move_status == 0:
75 axis1_move_status = await dev.Motion_getMoveStatus_async(port, axis1)
76 axis2_move_status = await dev.Motion_getMoveStatus_async(port, axis2)
77 move_status = axis1_move_status & axis2_move_status
78 if move_status == 0:
79 print("Moving......")
80 else:
81 print("Move completed")
82
83 ## Motion stop
84 for i in [axis1, axis2]:
85 err = await dev.Motion_stop_async(port, i, stop_decel)
86 print(f"stop_async axis{i}, status: {err}")
87
88 ## Motion close
89 err = await dev.Motion_close_async(port)
90 print(f"close_async in port {port}, status: {err}")
91 except Exception as err:
92 pywpc.printGenericError(err)
93
94 ## Disconnect device
95 dev.disconnect()
96
97 ## Release device handle
98 dev.close()
99
100 return
101
102def main_for_spyder(*args):
103 if asyncio.get_event_loop().is_running():
104 return asyncio.create_task(main(*args)).result()
105 else:
106 return asyncio.run(main(*args))
107
108if __name__ == '__main__':
109 asyncio.run(main()) ## Use terminal
110 # await main() ## Use Jupyter or IPython(>=7.0)
111 # main_for_spyder() ## Use Spyder