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