Motion 1axis move

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