1'''
2Motion - Motion_save_configuration_file.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 two_pulse_mode = 1
42 rel_posi_mode = 1
43
44 ## Axis and encoder parameters
45 axis_dir_cw = 0
46 encoder_dir_cw = 0
47
48 ## Polarity and enable parameters
49 active_low = 0
50 active_high = 1
51
52 ## Get firmware model & version
53 driver_info = await dev.Sys_getDriverInfo_async()
54 print("Model name: " + driver_info[0])
55 print("Firmware version: " + driver_info[-1])
56
57 ## Motion open
58 err = await dev.Motion_open_async(port)
59 print(f"open_async in port {port}, status: {err}")
60
61 ## Motion open configuration file
62 err = await dev.Motion_openCfgFile_async('C:/Users/user/Desktop/3AxisStage_2P.ini')
63 print(f"openCfgFile_async, status: {err}")
64
65 ## Motion configure
66 err = await dev.Motion_cfgAxis_async(port, axis, two_pulse_mode, axis_dir_cw, encoder_dir_cw, active_low)
67 print(f"cfgAxis_async in axis{axis}, status: {err}")
68
69 err = await dev.Motion_cfgAxisMove_async(port, axis, rel_posi_mode, target_posi=5000, velo=10000, accel=100000, decel=100000)
70 print(f"cfgAxisMove_async in axis{axis}, status: {err}")
71
72 ## Motion save configuration file
73 err = await dev.Motion_saveCfgFile_async()
74 print(f"saveCfgFile_async, status: {err}")
75
76 ## Motion close
77 err = await dev.Motion_close_async(port)
78 print(f"close_async in port {port}, status: {err}")
79 except Exception as err:
80 pywpc.printGenericError(err)
81
82 ## Disconnect device
83 dev.disconnect()
84
85 ## Release device handle
86 dev.close()
87
88 return
89
90def main_for_spyder(*args):
91 if asyncio.get_event_loop().is_running():
92 return asyncio.create_task(main(*args)).result()
93 else:
94 return asyncio.run(main(*args))
95
96if __name__ == '__main__':
97 asyncio.run(main()) ## Use terminal
98 # await main() ## Use Jupyter or IPython(>=7.0)
99 # main_for_spyder() ## Use Spyder