1'''
2Drive - Drive_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.EDriveST()
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 position = -80000
43 position1 = 80000
44 position2 = -80000
45 speed = 50000
46 acceleration = 10000
47 deceleration = 10000
48 mode = 1 ## 0: absolute, 1: relative.
49 active_high = 1
50 en_forward = 1
51 en_reverse = 1
52
53 ## Get firmware model & version
54 driver_info = await dev.Sys_getDriverInfo_async()
55 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
56
57 ## Motion open
58 err = await dev.Motion_open_async(port)
59 print(f"Motion_open, status: {err}")
60
61 ## Motion configure
62 err = await dev.Motion_cfgLimit_async(port, en_forward, en_reverse, active_high)
63 print(f"Motion_cfgLimit, status: {err}")
64
65 ## Motion reset
66 err = await dev.Motion_rstEncoderPosi_async(port)
67 print(f"Motion_resetEncoder, status: {err}")
68
69 ## Motion Servo on
70 err = await dev.Motion_enableServoOn_async(port)
71 print(f"Motion_enableServoOn, status: {err}")
72
73 ## Motion start
74 err = await dev.Motion_startPositionMove_async(port, position, speed, acceleration, deceleration, mode)
75 print(f"Motion_startPositionMove, status: {err}")
76
77 status = 1
78 while status != 0:
79 status = await dev.Motion_getProcessState_async(port)
80 if status == 0:
81 print(f"Motion_getProcessState: {status}")
82
83 ## Motion start
84 err = await dev.Motion_startPositionMove_async(port, position1, speed, acceleration, deceleration, mode)
85 print(f"Motion_startPositionMove, status: {err}")
86
87 status = 1
88 while status != 0:
89 status = await dev.Motion_getProcessState_async(port)
90 if status == 0:
91 print(f"Motion_getProcessState: {status}")
92
93 ## Motion start
94 err = await dev.Motion_startPositionMove_async(port, position2, speed, acceleration, deceleration, mode)
95 print(f"Motion_startPositionMove, status: {err}")
96
97 status = 1
98 while status != 0:
99 status = await dev.Motion_getProcessState_async(port)
100 if status == 0:
101 print(f"Motion_getProcessState: {status}")
102
103 except Exception as err:
104 pywpc.printGenericError(err)
105 except KeyboardInterrupt:
106 print("Press keyboard")
107 finally:
108 ## Motion stop
109 err = await dev.Motion_stopProcess_async(port)
110 print(f"Motion_stopProcess, status: {err}")
111
112 ## Motion Servo off
113 err = await dev.Motion_enableServoOff_async(port)
114 print(f"Motion_enableServoOff, status: {err}")
115
116 ## Motion close
117 err = await dev.Motion_close_async(port)
118 print(f"Motion_close, status: {err}")
119
120 ## Disconnect device
121 dev.disconnect()
122
123 ## Release device handle
124 dev.close()
125
126
127def main_for_spyder(*args):
128 if asyncio.get_event_loop().is_running():
129 return asyncio.create_task(main(*args)).result()
130 else:
131 return asyncio.run(main(*args))
132
133
134if __name__ == '__main__':
135 asyncio.run(main()) ## Use terminal
136 # await main() ## Use Jupyter or IPython(>=7.0)
137 # main_for_spyder() ## Use Spyder