Motion 3axis sync move

  1'''
  2Motion - Motion_3axis_synchronous_move.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 threading
 16import time
 17import asyncio
 18## WPC
 19
 20from wpcsys import pywpc
 21
 22
 23async def getAxisStatus(handle, port, axis, delay=0.005):
 24    move_status = await handle.Motion_getMoveStatus_async(port, axis)
 25    if move_status != 0:
 26        print(f"Move completed axis {axis}...")
 27
 28    ## Wait for seconds
 29    await asyncio.sleep(delay)  ## delay [s]
 30    return move_status
 31
 32def Axis1_thread(handle, port, axis, delay=0.005):
 33    move_status = 0
 34    while move_status == 0:
 35        move_status = asyncio.run(getAxisStatus(handle, port, axis, delay))
 36
 37        ## Wait for seconds
 38        time.sleep(delay) ## delay [s]
 39
 40def Axis2_thread(handle, port, axis, delay=0.005):
 41    move_status = 0
 42    while move_status == 0:
 43        move_status = asyncio.run(getAxisStatus(handle, port, axis, delay))
 44
 45        ## Wait for seconds
 46        time.sleep(delay) ## delay [s]
 47
 48def Axis3_thread(handle, port, axis, delay=0.005):
 49    move_status = 0
 50    while move_status == 0:
 51        move_status = asyncio.run(getAxisStatus(handle, port, axis, delay))
 52
 53        ## Wait for seconds
 54        time.sleep(delay) ## delay [s]
 55
 56async def main():
 57    ## Get Python driver version
 58    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 59
 60    ## Create device handle
 61    dev = pywpc.EMotion()
 62
 63    ## Connect to device
 64    try:
 65        dev.connect("192.168.1.110") ## Depend on your device
 66    except Exception as err:
 67        pywpc.printGenericError(err)
 68        ## Release device handle
 69        dev.close()
 70        return
 71
 72    try:
 73        ## Parameters setting
 74        port = 0 ## Depend on your device
 75        axis1 = 0
 76        axis2 = 1
 77        axis3 = 2
 78        two_pulse_mode = 1
 79        rel_posi_mode = 1
 80        stop_decel = 0
 81
 82        ## Axis and encoder parameters
 83        axis_dir_cw = 0
 84        encoder_dir_cw = 0
 85
 86        ## Polarity and enable parameters
 87        active_low = 0
 88        active_high = 1
 89        forward_enable_false = 0
 90        reverse_enable_false = 0
 91
 92        ## Get firmware model & version
 93        driver_info = await dev.Sys_getDriverInfo_async()
 94        print("Model name: " + driver_info[0])
 95        print("Firmware version: " + driver_info[-1])
 96
 97        ## Define Axis1 ~ Axis3 thread
 98        thread_1 = threading.Thread(target = Axis1_thread, args = [dev, port, axis1, 0.005])
 99        thread_2 = threading.Thread(target = Axis2_thread, args = [dev, port, axis2, 0.005])
100        thread_3 = threading.Thread(target = Axis3_thread, args = [dev, port, axis3, 0.005])
101
102        ## Thread start
103        thread_1.start()
104        thread_2.start()
105        thread_3.start()
106
107        ## Motion open
108        err = await dev.Motion_open_async(port)
109        print(f"open_async in port {port}, status: {err}")
110
111        '''
112        ## Motion open configuration file
113        err = await dev.Motion_openCfgFile_async('C:/Users/user/Desktop/3AxisStage_2P.ini')
114        print(f"openCfgFile_async, status: {err}")
115
116        ## Motion load configuration file
117        err = await dev.Motion_loadCfgFile_async()
118        print(f"loadCfgFile_async, status: {err}")
119        '''
120
121        ## Motion configure for axis1
122        err = await dev.Motion_cfgAxis_async(port, axis1, two_pulse_mode, axis_dir_cw, encoder_dir_cw, active_low)
123        print(f"cfgAxis_async in axis{axis1}, status: {err}")
124
125        err = await dev.Motion_cfgLimit_async(port, axis1, forward_enable_false, reverse_enable_false, active_low)
126        print(f"cfgLimit_async in axis{axis1}, status: {err}")
127
128        err = await dev.Motion_rstEncoderPosi_async(port, axis1, encoder_posi=0)
129        print(f"rstEncoderPosi_async in axis{axis1}, status: {err}")
130
131        err = await dev.Motion_cfgAxisMove_async(port, axis1, rel_posi_mode, target_posi=1000, velo=10000, accel=100000, decel=100000)
132        print(f"cfgAxisMove_async in axis{axis1}, status: {err}")
133
134        ## Servo on
135        err = await dev.Motion_enableServoOn_async(port, axis1)
136        print(f"ServoOn in axis{axis1}, status: {err}")
137
138        ## Motion configure for axis2
139        err = await dev.Motion_cfgAxis_async(port, axis2, two_pulse_mode, axis_dir_cw, encoder_dir_cw, active_low)
140        print(f"cfgAxis_async in axis{axis2}, status: {err}")
141
142        err = await dev.Motion_cfgLimit_async(port, axis2, forward_enable_false, reverse_enable_false, active_low)
143        print(f"cfgLimit_async in axis{axis2}, status: {err}")
144
145        err = await dev.Motion_rstEncoderPosi_async(port, axis2, encoder_posi=0)
146        print(f"rstEncoderPosi_async in axis{axis2}, status: {err}")
147
148        err = await dev.Motion_cfgAxisMove_async(port, axis2, rel_posi_mode, target_posi=1000, velo=10000, accel=100000, decel=100000)
149        print(f"cfgAxisMove_async in axis{axis2}, status: {err}")
150
151        ## Servo on
152        err = await dev.Motion_enableServoOn_async(port, axis2)
153        print(f"ServoOn in axis{axis2}, status: {err}")
154
155        ## Motion configure for axis3
156        err = await dev.Motion_cfgAxis_async(port, axis3, two_pulse_mode, axis_dir_cw, encoder_dir_cw, active_low)
157        print(f"cfgAxis_async in axis{axis3}, status: {err}")
158
159        err = await dev.Motion_cfgLimit_async(port, axis3, forward_enable_false, reverse_enable_false, active_low)
160        print(f"cfgLimit_async in axis{axis3}, status: {err}")
161
162        err = await dev.Motion_rstEncoderPosi_async(port, axis3, encoder_posi=0)
163        print(f"rstEncoderPosi_async in axis{axis3}, status: {err}")
164
165        err = await dev.Motion_cfgAxisMove_async(port, axis3, rel_posi_mode, target_posi=-5000, velo=10000, accel=100000, decel=100000)
166        print(f"cfgAxisMove_async in axis{axis3}, status: {err}")
167
168        ## Servo on
169        err = await dev.Motion_enableServoOn_async(port, axis3)
170        print(f"ServoOn in axis{axis3}, status: {err}")
171
172        ## Motion start
173        err = await dev.Motion_startMultiAxisMove_async(port, [axis1, axis2, axis3])
174        print(f"startMultiAxisMove_async in port {port}, status: {err}")
175
176        ## Wait for thread completion
177        thread_1.join()
178        print("Axis1_Thread returned.")
179
180        thread_2.join()
181        print("Axis2_Thread returned.")
182
183        thread_3.join()
184        print("Axis3_Thread returned.")
185
186        ## Servo off
187        for i in [axis1, axis2, axis3]:
188            err = await dev.Motion_enableServoOff_async(port, i)
189            print(f"ServoOff in axis{i}, status: {err}")
190
191        ## Motion stop
192        for i in [axis1, axis2, axis3]:
193            err = await dev.Motion_stop_async(port, i, stop_decel)
194            print(f"stop_async in axis{i}, status: {err}")
195
196        ## Motion close
197        err = await dev.Motion_close_async(port)
198        print(f"close_async in port {port}, status: {err}")
199    except Exception as err:
200        pywpc.printGenericError(err)
201
202    ## Disconnect device
203    dev.disconnect()
204
205    ## Release device handle
206    dev.close()
207
208    return
209
210def main_for_spyder(*args):
211    if asyncio.get_event_loop().is_running():
212        return asyncio.create_task(main(*args)).result()
213    else:
214        return asyncio.run(main(*args))
215
216if __name__ == '__main__':
217    asyncio.run(main()) ## Use terminal
218    # await main() ## Use Jupyter or IPython(>=7.0)
219    # main_for_spyder() ## Use Spyder