1'''
2Motion - Motion_find_home.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 rel_posi_mode = 1
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 home_enable_false = 0
55
56 ## Find home parameters
57 find_home = 0
58 dir_reverse = 1
59
60 ## Get firmware model & version
61 driver_info = await dev.Sys_getDriverInfo_async()
62 print("Model name: " + driver_info[0])
63 print("Firmware version: " + driver_info[-1])
64
65 ## Motion open
66 err = await dev.Motion_open_async(port)
67 print(f"open_async in port {port}, status: {err}")
68
69 '''
70 ## Motion open configuration file
71 err = await dev.Motion_openCfgFile_async('C:/Users/user/Desktop/3AxisStage_2P.ini')
72 print(f"openCfgFile_async, status: {err}")
73
74 ## Motion load configuration file
75 err = await dev.Motion_loadCfgFile_async()
76 print(f"loadCfgFile_async, status: {err}")
77 '''
78
79 ## Motion configure
80 err = await dev.Motion_cfgAxis_async(port, axis, two_pulse_mode, axis_dir_cw, encoder_dir_cw, active_low)
81 print(f"cfgAxis_async in axis{axis}, status: {err}")
82
83 err = await dev.Motion_cfgLimit_async(port, axis, forward_enable_true, reverse_enable_true, active_low)
84 print(f"cfgLimit_async in axis{axis}, status: {err}")
85
86 err = await dev.Motion_cfgFindRef_async(port, axis, find_home, dir_reverse, search_velo=10000, search_accle=100000, approach_velo_percent=20, en_reset_posi=0, offset_posi=1500)
87 print(f"cfgFindRef_async in axis{axis}, status: {err}")
88
89 err = await dev.Motion_cfgHome_async(port, axis, home_enable_false, active_low)
90 print(f"cfgHome_async in axis{axis}, status: {err}")
91
92 ## Servo on
93 err = await dev.Motion_enableServoOn_async(port, axis)
94 print(f"enableServoOn_async in axis{axis}, status: {err}")
95
96 err = await dev.Motion_rstEncoderPosi_async(port, axis, encoder_posi=0)
97 print(f"rstEncoderPosi_async in axis{axis}, status: {err}")
98
99 ## Motion find reference
100 err = await dev.Motion_findRef_async(port, axis)
101 print(f"findRef_async in axis{axis}, status: {err}")
102
103 home_status = 0
104 while home_status == 0:
105 ## Get forward and reverse limit status
106 hit_status = await dev.Motion_getLimitStatus_async(port, axis)
107 forward_hit = hit_status[0]
108 reverse_hit = hit_status[1]
109 if forward_hit == 1:
110 print("Forward hit")
111 if reverse_hit == 1:
112 print("Reverse hit")
113
114 ## Get home status
115 home_status = await dev.Motion_getHomeStatus_async(port, axis)
116 if home_status == 1:
117 print("Home hit")
118
119 ## Check finding and found status
120 driving_status = await dev.Motion_checkRef_async(port, axis)
121 print(f"driving_status: {driving_status}")
122
123 ## Motion stop
124 err = await dev.Motion_stop_async(port, axis, stop_decel)
125 print(f"stop_async in axis{axis}, status: {err}")
126
127 ## Servo off
128 err = await dev.Motion_enableServoOff_async(port, axis)
129 print(f"enableServoOff_async in axis{axis}, status: {err}")
130
131 ## Motion close
132 err = await dev.Motion_close_async(port)
133 print(f"close_async in port {port}, status: {err}")
134 except Exception as err:
135 pywpc.printGenericError(err)
136
137 ## Disconnect device
138 dev.disconnect()
139
140 ## Release device handle
141 dev.close()
142
143 return
144
145def main_for_spyder(*args):
146 if asyncio.get_event_loop().is_running():
147 return asyncio.create_task(main(*args)).result()
148 else:
149 return asyncio.run(main(*args))
150
151if __name__ == '__main__':
152 asyncio.run(main()) ## Use terminal
153 # await main() ## Use Jupyter or IPython(>=7.0)
154 # main_for_spyder() ## Use Spyder