1'''
2Motion - Motion_1axis_move_with_capture.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 rel_posi_mode = 1
42 stop_decel = 0
43
44 ## Get firmware model & version
45 driver_info = await dev.Sys_getDriverInfo_async()
46 print("Model name: " + driver_info[0])
47 print("Firmware version: " + driver_info[-1])
48
49 ## Capture parameters setting
50 rising_edge = 0
51 capture_logical_position = 0
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_cfgCapture_async(port, axis, rising_edge, capture_logical_position)
67 print(f"cfgCapture_async in axis{axis}, status: {err}")
68
69 err = await dev.Motion_enableCapture_async(port, axis, int(True))
70 print(f"enableCapture_async in axis{axis}, status: {err}")
71
72 err = await dev.Motion_cfgAxisMove_async(port, axis, rel_posi_mode, target_posi=5000, velo=10000, accel=100000, decel=100000)
73 print(f"cfgAxisMove_async in axis{axis}, status: {err}")
74
75 err = await dev.Motion_rstEncoderPosi_async(port, axis, encoder_posi=0)
76 print(f"rstEncoderPosi_async in axis{axis}, status: {err}")
77
78 ## Servo on
79 err = await dev.Motion_enableServoOn_async(port, axis)
80 print(f"enableServoOn_async in axis{axis}, status: {err}")
81
82 ## Motion start
83 err = await dev.Motion_startSingleAxisMove_async(port, axis)
84 print(f"startSingleAxisMove_async in axis{axis}, status: {err}")
85
86 move_status = 0
87 while move_status == 0:
88 move_status = await dev.Motion_getMoveStatus_async(port, axis)
89 print(f"getMoveStatus_async in axis{axis}: {move_status}")
90
91 capture_points = await dev.Motion_readCapturePoint_async(port, axis)
92 print(f"readCapturePoint_async in axis{axis}: {capture_points}")
93
94 ## Motion stop
95 err = await dev.Motion_stop_async(port, axis, stop_decel)
96 print(f"stop_async in axis{axis}, status: {err}")
97
98 ## Servo off
99 err = await dev.Motion_enableServoOff_async(port, axis)
100 print(f"enableServoOff_async in axis{axis}, status: {err}")
101
102 ## Motion close
103 err = await dev.Motion_close_async(port)
104 print(f"close_async in port {port}, status: {err}")
105 except Exception as err:
106 pywpc.printGenericError(err)
107
108 ## Disconnect device
109 dev.disconnect()
110
111 ## Release device handle
112 dev.close()
113
114 return
115
116def main_for_spyder(*args):
117 if asyncio.get_event_loop().is_running():
118 return asyncio.create_task(main(*args)).result()
119 else:
120 return asyncio.run(main(*args))
121
122if __name__ == '__main__':
123 asyncio.run(main()) ## Use terminal
124 # await main() ## Use Jupyter or IPython(>=7.0)
125 # main_for_spyder() ## Use Spyder