1'''
2Motion - Motion_find_limit.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 stop_decel = 0
43
44 ## Axis and encoder parameters
45 axis_dir_cw = 0
46 encoder_dir_cw = 0
47
48 ## Polarity and enable parameters
49 active_low = 0
50 active_high = 1
51 forward_enable_true = 1
52 reverse_enable_true = 1
53 home_enable_false = 0
54
55 ## Find limit parameters
56 find_limit = 2
57 dir_reverse = 1
58
59 ## Get firmware model & version
60 driver_info = await dev.Sys_getDriverInfo_async()
61 print("Model name: " + driver_info[0])
62 print("Firmware version: " + driver_info[-1])
63
64 ## Motion open
65 err = await dev.Motion_open_async(port)
66 print(f"open_async in port {port}, status: {err}")
67
68 '''
69 ## Motion open configuration file
70 err = await dev.Motion_openCfgFile_async('C:/Users/user/Desktop/3AxisStage_2P.ini')
71 print(f"openCfgFile_async, status: {err}")
72
73 ## Motion load configuration file
74 err = await dev.Motion_loadCfgFile_async()
75 print(f"loadCfgFile_async, status: {err}")
76 '''
77
78 ## Motion configure
79 err = await dev.Motion_cfgAxis_async(port, axis, two_pulse_mode, axis_dir_cw, encoder_dir_cw, active_low)
80 print(f"cfgAxis_async in axis{axis}, status: {err}")
81
82 err = await dev.Motion_cfgLimit_async(port, axis, forward_enable_true, reverse_enable_true, active_low)
83 print(f"cfgLimit_async in axis{axis}, status: {err}")
84
85 err = await dev.Motion_cfgFindRef_async(port, axis, find_limit, dir_reverse, search_velo=10000, search_accle=100000, approach_velo_percent=20, en_reset_posi=0, offset_posi=1500)
86 print(f"cfgFindRef_async in axis{axis}, status: {err}")
87
88 err = await dev.Motion_cfgHome_async(port, axis, home_enable_false, active_low)
89 print(f"cfgHome_async in axis{axis}, status: {err}")
90
91 ## Servo on
92 err = await dev.Motion_enableServoOn_async(port, axis)
93 print(f"enableServoOn_async in axis{axis}, status: {err}")
94
95 err = await dev.Motion_rstEncoderPosi_async(port, axis, encoder_posi=0)
96 print(f"rstEncoderPosi_async in axis{axis}, status: {err}")
97
98 ## Motion find reference
99 err = await dev.Motion_findRef_async(port, axis)
100 print(f"findRef_async in axis{axis}, status: {err}")
101
102 driving_status = 0
103 while driving_status == 0:
104 ## Read forward and reverse limit status
105 hit_status = await dev.Motion_getLimitStatus_async(port, axis)
106 forward_hit = hit_status[0]
107 reverse_hit = hit_status[1]
108
109 if forward_hit == 1:
110 print("Forward hit")
111 if reverse_hit == 1:
112 print("Reverse hit")
113
114 ## Read 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