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-2025 WPC Systems Ltd. All rights reserved.
12'''
13
14## WPC
15from wpcsys import pywpc
16
17## Python
18import asyncio
19import sys
20sys.path.insert(0, 'src/')
21
22
23async def main():
24 ## Get Python driver version
25 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
26
27 ## Create device handle
28 dev = pywpc.EMotion()
29
30 ## Connect to device
31 try:
32 dev.connect("192.168.1.110") ## Depend on your device
33 except Exception as err:
34 pywpc.printGenericError(err)
35 ## Release device handle
36 dev.close()
37 return
38
39 try:
40 ## Parameters setting
41 port = 0 ## Depend on your device
42 axis = 0
43 rel_posi_mode = 1
44 stop_decel = 0
45
46 ## Get firmware model & version
47 driver_info = await dev.Sys_getDriverInfo_async()
48 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
49
50 ## Capture parameters setting
51 rising_edge = 0
52 capture_logical_position = 0
53
54 ## Motion open
55 err = await dev.Motion_open_async(port)
56 print(f"open_async in port {port}, status: {err}")
57
58 ## Motion open configuration file
59 err = await dev.Motion_openCfgFile_async('C:/Users/user/Desktop/3AxisStage_2P.ini')
60 print(f"openCfgFile_async, status: {err}")
61
62 ## Motion load configuration file
63 err = await dev.Motion_loadCfgFile_async()
64 print(f"loadCfgFile_async, status: {err}")
65
66 ## Motion configure
67 err = await dev.Motion_cfgCapture_async(port, axis, rising_edge, capture_logical_position)
68 print(f"cfgCapture_async in axis{axis}, status: {err}")
69
70 err = await dev.Motion_enableCapture_async(port, axis, int(True))
71 print(f"enableCapture_async in axis{axis}, status: {err}")
72
73 err = await dev.Motion_cfgAxisMove_async(port, axis, rel_posi_mode, target_posi=5000, velo=10000, accel=100000, decel=100000)
74 print(f"cfgAxisMove_async in axis{axis}, status: {err}")
75
76 err = await dev.Motion_rstEncoderPosi_async(port, axis, encoder_posi=0)
77 print(f"rstEncoderPosi_async in axis{axis}, status: {err}")
78
79 ## Servo on
80 err = await dev.Motion_enableServoOn_async(port, axis)
81 print(f"enableServoOn_async in axis{axis}, status: {err}")
82
83 ## Motion start
84 err = await dev.Motion_startSingleAxisMove_async(port, axis)
85 print(f"startSingleAxisMove_async in axis{axis}, status: {err}")
86
87 move_status = 0
88 while move_status == 0:
89 move_status = await dev.Motion_getMoveStatus_async(port, axis)
90 print(f"getMoveStatus_async in axis{axis}: {move_status}")
91
92 capture_points = await dev.Motion_readCapturePoint_async(port, axis)
93 print(f"readCapturePoint_async in axis{axis}: {capture_points}")
94
95 ## Motion stop
96 err = await dev.Motion_stop_async(port, axis, stop_decel)
97 print(f"stop_async in axis{axis}, status: {err}")
98
99 ## Servo off
100 err = await dev.Motion_enableServoOff_async(port, axis)
101 print(f"enableServoOff_async in axis{axis}, status: {err}")
102
103 ## Motion close
104 err = await dev.Motion_close_async(port)
105 print(f"close_async in port {port}, status: {err}")
106 except Exception as err:
107 pywpc.printGenericError(err)
108
109 finally:
110 ## Disconnect device
111 dev.disconnect()
112
113 ## Release device handle
114 dev.close()
115
116
117def main_for_spyder(*args):
118 if asyncio.get_event_loop().is_running():
119 return asyncio.create_task(main(*args)).result()
120 else:
121 return asyncio.run(main(*args))
122
123
124if __name__ == '__main__':
125 asyncio.run(main()) ## Use terminal
126 # await main() ## Use Jupyter or IPython(>=7.0)
127 # main_for_spyder() ## Use Spyder