Motion 1axis move with breakpoint

  1'''
  2Motion - Motion_1axis_move_with_breakpoint.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-2025 WPC Systems Ltd. All rights reserved.
 12'''
 13
 14## WPC
 15from wpcsys import pywpc
 16
 17## Python
 18import asyncio
 19import sys
 20sys.path.insert(0, 'src/')
 21
 22
 23async def main():
 24    ## Get Python driver version
 25    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 26
 27    ## Create device handle
 28    dev = pywpc.EMotion()
 29
 30    ## Connect to device
 31    try:
 32        dev.connect("192.168.1.110")  ## Depend on your device
 33    except Exception as err:
 34        pywpc.printGenericError(err)
 35        ## Release device handle
 36        dev.close()
 37        return
 38
 39    try:
 40        ## Parameters setting
 41        port = 0  ## Depend on your device
 42        axis = 0
 43        rel_posi_mode = 1
 44        stop_decel = 0
 45
 46        ## Polarity and enable parameters
 47        active_low = 0
 48        active_high = 1
 49
 50        ## Breakpoint parameters
 51        start_position = 100
 52        pulse_width = 100
 53        pulse_period = 100
 54        pulse_number = 100
 55
 56        ## Get firmware model & version
 57        driver_info = await dev.Sys_getDriverInfo_async()
 58        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 59
 60        ## Motion open
 61        err = await dev.Motion_open_async(port)
 62        print(f"open_async in port {port}, status: {err}")
 63
 64        ## Motion open configuration file
 65        err = await dev.Motion_openCfgFile_async('C:/Users/user/Desktop/3AxisStage_2P.ini')
 66        print(f"openCfgFile_async, status: {err}")
 67
 68        ## Motion load configuration file
 69        err = await dev.Motion_loadCfgFile_async()
 70        print(f"loadCfgFile_async, status: {err}")
 71
 72        ## Motion configure
 73        err = await dev.Motion_cfgBreakPoint_async(port, axis, rel_posi_mode, active_high, start_position, pulse_width, pulse_period, pulse_number)
 74        print(f"cfgBreakPoint_async in axis{axis}, status: {err}")
 75
 76        err = await dev.Motion_enableBreakPoint_async(port, axis, int(True))
 77        print(f"enableBreakPoint_async in axis{axis}, status: {err}")
 78
 79        err = await dev.Motion_cfgAxisMove_async(port, axis, rel_posi_mode, target_posi=5000, velo=10000, accel=100000, decel=100000)
 80        print(f"cfgAxisMove_async in axis{axis}, status: {err}")
 81
 82        err = await dev.Motion_rstEncoderPosi_async(port, axis, encoder_posi=0)
 83        print(f"rstEncoderPosi_async in axis{axis}, status: {err}")
 84
 85        ## Servo on
 86        err = await dev.Motion_enableServoOn_async(port, axis)
 87        print(f"enableServoOn_async in axis{axis}, status: {err}")
 88
 89        ## Motion start
 90        err = await dev.Motion_startSingleAxisMove_async(port, axis)
 91        print(f"startSingleAxisMove_async in axis{axis}, status: {err}")
 92
 93        move_status = 0
 94        while move_status == 0:
 95            move_status = await dev.Motion_getMoveStatus_async(port, axis)
 96            print(f"getMoveStatus in axis{axis}: {move_status}")
 97
 98        ## Motion stop
 99        err = await dev.Motion_stop_async(port, axis, stop_decel)
100        print(f"stop_async in axis{axis}, status: {err}")
101
102        ## Servo off
103        err = await dev.Motion_enableServoOff_async(port, axis)
104        print(f"enableServoOff_async in axis{axis}, status: {err}")
105
106        ## Motion close
107        err = await dev.Motion_close_async(port)
108        print(f"close_async in port {port}, status: {err}")
109    except Exception as err:
110        pywpc.printGenericError(err)
111
112    finally:
113        ## Disconnect device
114        dev.disconnect()
115
116        ## Release device handle
117        dev.close()
118
119
120def main_for_spyder(*args):
121    if asyncio.get_event_loop().is_running():
122        return asyncio.create_task(main(*args)).result()
123    else:
124        return asyncio.run(main(*args))
125
126
127if __name__ == '__main__':
128    asyncio.run(main())  ## Use terminal
129    # await main()  ## Use Jupyter or IPython(>=7.0)
130    # main_for_spyder()  ## Use Spyder