Motion 3axis async move

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