1'''
2Motion - Motion_servo_on.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
42 ## Get firmware model & version
43 driver_info = await dev.Sys_getDriverInfo_async()
44 print("Model name: " + driver_info[0])
45 print("Firmware version: " + driver_info[-1])
46
47 ## Motion open
48 err = await dev.Motion_open_async(port)
49 print(f"open_async in port {port}, status: {err}")
50
51 ## Servo on
52 err = await dev.Motion_enableServoOn_async(port, axis)
53 print(f"enableServoOn_async in axis{axis}, status: {err}")
54
55 ## Wait
56 await asyncio.sleep(5) ## delay [s]
57
58 ## Servo off
59 err = await dev.Motion_enableServoOff_async(port, axis)
60 print(f"enableServoOff_async in axis{axis}, status: {err}")
61
62 ## Motion close
63 err = await dev.Motion_close_async(port)
64 print(f"close_async in port {port}, status: {err}")
65 except Exception as err:
66 pywpc.printGenericError(err)
67
68 ## Disconnect device
69 dev.disconnect()
70
71 ## Release device handle
72 dev.close()
73
74 return
75
76def main_for_spyder(*args):
77 if asyncio.get_event_loop().is_running():
78 return asyncio.create_task(main(*args)).result()
79 else:
80 return asyncio.run(main(*args))
81
82if __name__ == '__main__':
83 asyncio.run(main()) ## Use terminal
84 # await main() ## Use Jupyter or IPython(>=7.0)
85 # main_for_spyder() ## Use Spyder