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