1'''
2Motion - Motion_2axis_circular_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 axis = 0
41 stop_decel = 0
42 rel_posi_mode = 1
43
44 ## Circular interpolation parameters
45 x_axis = 0
46 y_axis = 1
47 center_point_x = 2000
48 center_point_y = 2000
49 finish_point_x = 0
50 finish_point_y = 0
51 circular_dir_cw = 0
52
53 ## jerk and acceletation mode parameters setting
54 scurve = 1
55 jerk = 1
56
57 ## Get firmware model & version
58 driver_info = await dev.Sys_getDriverInfo_async()
59 print("Model name: " + driver_info[0])
60 print("Firmware version: " + driver_info[-1])
61
62 ## Motion open
63 err = await dev.Motion_open_async(port)
64 print(f"open_async in port {port}, status: {err}")
65
66 ## Motion open configuration file
67 err = await dev.Motion_openCfgFile_async('C:/Users/user/Desktop/3AxisStage_2P.ini')
68 print(f"openCfgFile_async, status: {err}")
69
70 ## Motion load configuration file
71 err = await dev.Motion_loadCfgFile_async()
72 print(f"loadCfgFile_async, status: {err}")
73
74 ## Motion configure
75 err = await dev.Motion_cfgCircularInterpo_async(port, x_axis, y_axis, center_point_x, center_point_y, finish_point_x, finish_point_y, circular_dir_cw, speed=1000, accel=100000, decel=100000)
76 print(f"cfgCircularInterpo_async in port {port}, status: {err}")
77
78 err = await dev.Motion_startCircularInterpo_async(port)
79 print(f"startCircularInterpo_async in axis{axis}, status: {err}")
80
81 err = await dev.Motion_cfgAxisMove_async(port, axis, rel_posi_mode, target_posi=5000, velo=10000, accel=100000, decel=100000)
82 print(f"cfgAxisMove_async in axis{axis}, status: {err}")
83
84 err = await dev.Motion_cfgJerkAndAccelMode_async(port, axis, jerk, scurve)
85 print(f"cfgJerkAndAccelMode_async in axis{axis}, status: {err}")
86
87 err = await dev.Motion_rstEncoderPosi_async(port, axis, encoder_posi=0)
88 print(f"rstEncoderPosi_async in axis{axis}, status: {err}")
89
90 for i in range(4):
91 err = await dev.Motion_enableServoOn_async(port, i)
92 print(f"enableServoOn_async in axis{i}, status: {err}")
93
94 ## Motion start
95 err = await dev.Motion_startSingleAxisMove_async(port, axis)
96 print(f"startSingleAxisMove_async in axis{axis}, status: {err}")
97
98 move_status = 0
99 while move_status == 0:
100 x_axis_move_status = await dev.Motion_getMoveStatus_async(port, x_axis)
101 y_axis_move_status = await dev.Motion_getMoveStatus_async(port, y_axis)
102 move_status = x_axis_move_status & y_axis_move_status
103 if move_status == 0:
104 print("Moving......")
105 else:
106 print("Move completed")
107
108 ## Motion stop
109 err = await dev.Motion_stop_async(port, axis, stop_decel)
110 print(f"stop_async in axis{axis}, status: {err}")
111
112 for i in range(4):
113 err = await dev.Motion_enableServoOff_async(port, i)
114 print(f"enableServoOff_async in axis{i}, status: {err}")
115
116 ## Motion close
117 err = await dev.Motion_close_async(port)
118 print(f"close_async in port {port}, status: {err}")
119 except Exception as err:
120 pywpc.printGenericError(err)
121
122 ## Disconnect device
123 dev.disconnect()
124
125 ## Release device handle
126 dev.close()
127
128 return
129
130def main_for_spyder(*args):
131 if asyncio.get_event_loop().is_running():
132 return asyncio.create_task(main(*args)).result()
133 else:
134 return asyncio.run(main(*args))
135
136if __name__ == '__main__':
137 asyncio.run(main()) ## Use terminal
138 # await main() ## Use Jupyter or IPython(>=7.0)
139 # main_for_spyder() ## Use Spyder