1'''
2Drive - Drive_velocity_blending_acceleration.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 speed = 50000
41 dir = 1
42 acceleration = 10000
43 deceleration = 10000
44 active_high = 1
45 en_forward = 1
46 en_reverse = 1
47
48 ## Get firmware model & version
49 driver_info = await dev.Sys_getDriverInfo_async()
50 print("Model name: " + driver_info[0])
51 print("Firmware version: " + driver_info[-1])
52
53 ## Motion open
54 err = await dev.Motion_open_async(port)
55 print(f"Motion_open, status: {err}")
56
57 ## Motion configure
58 err = await dev.Motion_cfgLimit_async(port, en_forward, en_reverse, active_high)
59 print(f"Motion_cfgLimit, status: {err}")
60
61 ## Motion reset
62 err = await dev.Motion_rstEncoderPosi_async(port)
63 print(f"Motion_resetEncoderPosi, status: {err}")
64
65 ## Motion Servo on
66 err = await dev.Motion_enableServoOn_async(port)
67 print(f"Motion_enableServoOn, status: {err}")
68
69 ## Motion start
70 err = await dev.Motion_startVelocticyMove_async(port, speed, dir, acceleration, deceleration)
71 print(f"Motion_startVelocticyMove, status: {err}")
72
73 ## Wait for seconds for moving
74 await asyncio.sleep(3) ## delay [s]
75
76 ## Motion start
77 new_speed = 10000
78 new_dir = -1
79 new_acceleration = 20000
80 new_deceleration = 20000
81 err = await dev.Motion_startVelocticyMove_async(port, new_speed, new_dir, new_acceleration, new_deceleration)
82 print(f"Motion_startVelocticyMove, status: {err}")
83
84 ## Wait for seconds for moving
85 await asyncio.sleep(3) ## delay [s]
86 except Exception as err:
87 pywpc.printGenericError(err)
88 except KeyboardInterrupt:
89 print("Press keyboard")
90 finally:
91 ## Motion stop
92 err = await dev.Motion_stopProcess_async(port)
93 print(f"Motion_stopProcess, status: {err}")
94
95 ## Motion Servo off
96 err = await dev.Motion_enableServoOff_async(port)
97 print(f"Motion_enableServoOff, status: {err}")
98
99 ## Motion close
100 err = await dev.Motion_close_async(port)
101 print(f"Motion_close, status: {err}")
102
103 ## Disconnect device
104 dev.disconnect()
105
106 ## Release device handle
107 dev.close()
108
109 return
110
111def main_for_spyder(*args):
112 if asyncio.get_event_loop().is_running():
113 return asyncio.create_task(main(*args)).result()
114 else:
115 return asyncio.run(main(*args))
116
117if __name__ == '__main__':
118 asyncio.run(main()) ## Use terminal
119 # await main() ## Use Jupyter or IPython(>=7.0)
120 # main_for_spyder() ## Use Spyder