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-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        rel_posi_mode = 1
 42        stop_decel = 0
 43
 44        ## Polarity and enable parameters
 45        active_low = 0
 46        active_high = 1
 47
 48        ## Breakpoint parameters
 49        start_position = 100
 50        pulse_width = 100
 51        pulse_period = 100
 52        pulse_number = 100
 53
 54        ## Get firmware model & version
 55        driver_info = await dev.Sys_getDriverInfo_async()
 56        print("Model name: " + driver_info[0])
 57        print("Firmware version: " + driver_info[-1])
 58
 59        ## Motion open
 60        err = await dev.Motion_open_async(port)
 61        print(f"open_async in port {port}, status: {err}")
 62
 63        ## Motion open configuration file
 64        err = await dev.Motion_openCfgFile_async('C:/Users/user/Desktop/3AxisStage_2P.ini')
 65        print(f"openCfgFile_async, status: {err}")
 66
 67        ## Motion load configuration file
 68        err = await dev.Motion_loadCfgFile_async()
 69        print(f"loadCfgFile_async, status: {err}")
 70
 71        ## Motion configure
 72        err = await dev.Motion_cfgBreakPoint_async(port, axis, rel_posi_mode, active_high, start_position, pulse_width, pulse_period, pulse_number)
 73        print(f"cfgBreakPoint_async in axis{axis}, status: {err}")
 74
 75        err = await dev.Motion_enableBreakPoint_async(port, axis, int(True))
 76        print(f"enableBreakPoint_async in axis{axis}, status: {err}")
 77
 78        err = await dev.Motion_cfgAxisMove_async(port, axis, rel_posi_mode, target_posi=5000, velo=10000, accel=100000, decel=100000)
 79        print(f"cfgAxisMove_async in axis{axis}, status: {err}")
 80
 81        err = await dev.Motion_rstEncoderPosi_async(port, axis, encoder_posi=0)
 82        print(f"rstEncoderPosi_async in axis{axis}, status: {err}")
 83
 84        ## Servo on
 85        err = await dev.Motion_enableServoOn_async(port, axis)
 86        print(f"enableServoOn_async in axis{axis}, status: {err}")
 87
 88        ## Motion start
 89        err = await dev.Motion_startSingleAxisMove_async(port, axis)
 90        print(f"startSingleAxisMove_async in axis{axis}, status: {err}")
 91
 92        move_status = 0
 93        while move_status == 0:
 94            move_status = await dev.Motion_getMoveStatus_async(port, axis)
 95            print(f"getMoveStatus in axis{axis}: {move_status}")
 96
 97        ## Motion stop
 98        err = await dev.Motion_stop_async(port, axis, stop_decel)
 99        print(f"stop_async in axis{axis}, status: {err}")
100
101        ## Servo off
102        err = await dev.Motion_enableServoOff_async(port, axis)
103        print(f"enableServoOff_async in axis{axis}, status: {err}")
104
105        ## Motion close
106        err = await dev.Motion_close_async(port)
107        print(f"close_async in port {port}, status: {err}")
108    except Exception as err:
109        pywpc.printGenericError(err)
110
111    ## Disconnect device
112    dev.disconnect()
113
114    ## Release device handle
115    dev.close()
116
117    return
118
119def main_for_spyder(*args):
120    if asyncio.get_event_loop().is_running():
121        return asyncio.create_task(main(*args)).result()
122    else:
123        return asyncio.run(main(*args))
124
125if __name__ == '__main__':
126    asyncio.run(main()) ## Use terminal
127    # await main() ## Use Jupyter or IPython(>=7.0)
128    # main_for_spyder() ## Use Spyder