Motion 3axis sync move

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