1'''
2Motion - Motion_position_blending.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 two_pulse_mode = 1
44 rel_posi_mode = 1
45 stop_decel = 0
46 new_position = 0
47
48 ## Axis and encoder parameters
49 axis_dir_cw = 0
50 encoder_dir_cw = 0
51
52 ## Polarity and enable parameters
53 active_low = 0
54 active_high = 1
55 forward_enable_true = 1
56 reverse_enable_true = 1
57 home_enable_false = 0
58
59 ## Get firmware model & version
60 driver_info = await dev.Sys_getDriverInfo_async()
61 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
62
63 ## Motion open
64 err = await dev.Motion_open_async(port)
65 print(f"open_async in port {port}, status: {err}")
66
67 '''
68 ## Motion open configuration file
69 err = await dev.Motion_openCfgFile_async('C:/Users/user/Desktop/3AxisStage_2P.ini')
70 print(f"openCfgFile_async, status: {err}")
71
72 ## Motion load configuration file
73 err = await dev.Motion_loadCfgFile_async()
74 print(f"loadCfgFile_async, status: {err}")
75 '''
76
77 ## Motion configure
78 err = await dev.Motion_cfgAxis_async(port, axis, two_pulse_mode, axis_dir_cw, encoder_dir_cw, active_low)
79 print(f"cfgAxis_async in axis{axis}, status: {err}")
80
81 err = await dev.Motion_cfgLimit_async(port, axis, forward_enable_true, reverse_enable_true, active_low)
82 print(f"cfgLimit_async in axis{axis}, status: {err}")
83
84 err = await dev.Motion_cfgHome_async(port, axis, home_enable_false, active_low)
85 print(f"cfgHome_async in axis{axis}, status: {err}")
86
87 err = await dev.Motion_cfgAxisMove_async(port, axis, rel_posi_mode, target_posi=-5000, velo=10000, accel=100000, decel=100000)
88 print(f"cfgAxisMove_async in axis{axis}, status: {err}")
89
90 err = await dev.Motion_rstEncoderPosi_async(port, axis, encoder_posi=0)
91 print(f"rstEncoderPosi_async in axis{axis}, status: {err}")
92
93 ## Servo on
94 err = await dev.Motion_enableServoOn_async(port, axis)
95 print(f"enableServoOn_async in axis{axis}, status: {err}")
96
97 ## Motion start
98 err = await dev.Motion_startSingleAxisMove_async(port, axis)
99 print(f"startSingleAxisMove_async in axis{axis}, status: {err}")
100
101 move_status = 0
102 while move_status == 0:
103 err = await dev.Motion_overrideAxisPosi_async(port, axis, new_position)
104 # print(f"overrideAxisPosi_async in axis{axis}, status: {err}")
105 move_status = await dev.Motion_getMoveStatus_async(port, axis)
106 logical_posi = await dev.Motion_getLogicalPosi_async(port, axis)
107 encoder_posi = await dev.Motion_getEncoderPosi_async(port, axis)
108 print(f"logical_posi: {logical_posi}, encoder_posi: {encoder_posi}")
109
110 ## Motion stop
111 err = await dev.Motion_stop_async(port, axis, stop_decel)
112 print(f"stop_async in axis{axis}, status: {err}")
113
114 ## Servo off
115 err = await dev.Motion_enableServoOff_async(port, axis)
116 print(f"enableServoOff_async in axis{axis}, status: {err}")
117
118 ## Motion close
119 err = await dev.Motion_close_async(port)
120 print(f"close_async in port {port}, status: {err}")
121 except Exception as err:
122 pywpc.printGenericError(err)
123
124 finally:
125 ## Disconnect device
126 dev.disconnect()
127
128 ## Release device handle
129 dev.close()
130
131
132def main_for_spyder(*args):
133 if asyncio.get_event_loop().is_running():
134 return asyncio.create_task(main(*args)).result()
135 else:
136 return asyncio.run(main(*args))
137
138
139if __name__ == '__main__':
140 asyncio.run(main()) ## Use terminal
141 # await main() ## Use Jupyter or IPython(>=7.0)
142 # main_for_spyder() ## Use Spyder