Motion find index

  1'''
  2Motion - Motion_find_index.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        one_pulse_mode = 0
 42        two_pulse_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
 55        ## Find index parameters
 56        dir_reverse = 1
 57        find_index = 1
 58
 59        ## Get firmware model & version
 60        driver_info = await dev.Sys_getDriverInfo_async()
 61        print("Model name: " + driver_info[0])
 62        print("Firmware version: " + driver_info[-1])
 63
 64        ## Motion open
 65        err = await dev.Motion_open_async(port)
 66        print(f"open_async in port {port}, status: {err}")
 67
 68        '''
 69        ## Motion open configuration file
 70        err = await dev.Motion_openCfgFile_async('C:/Users/user/Desktop/3AxisStage_2P.ini')
 71        print(f"openCfgFile_async, status: {err}")
 72
 73        ## Motion load configuration file
 74        err = await dev.Motion_loadCfgFile_async()
 75        print(f"loadCfgFile_async, status: {err}")
 76        '''
 77
 78        ## Motion configure
 79        err = await dev.Motion_cfgAxis_async(port, axis, one_pulse_mode, axis_dir_cw, encoder_dir_cw, active_high)
 80        print(f"cfgAxis_async in axis{axis}, status: {err}")
 81
 82        err = await dev.Motion_cfgLimit_async(port, axis, forward_enable_true, reverse_enable_true, active_high)
 83        print(f"cfgLimit_async in axis{axis}, status: {err}")
 84
 85        err = await dev.Motion_cfgEncoder_async(port, axis, active_high)
 86        print(f"cfgEncoder_async in axis{axis}, status: {err}")
 87
 88        err = await dev.Motion_cfgFindRef_async(port, axis, find_index, dir_reverse, search_velo=10000, search_accle=100000, approach_velo_percent=20, en_reset_posi=0, offset_posi=1500)
 89        print(f"cfgFindRef_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        err = await dev.Motion_rstEncoderPosi_async(port, axis, encoder_posi=0)
 96        print(f"rstEncoderPosi_async in axis{axis}, status: {err}")
 97
 98        ## Motion find reference
 99        err = await dev.Motion_findRef_async(port, axis)
100        print(f"findRef_async in axis{axis}, status: {err}")
101
102        driving_status = 0
103        while driving_status == 0:
104            ## Check driving_status
105            driving_status = await dev.Motion_checkRef_async(port, axis)
106            print(f"driving_status: {driving_status}")
107
108        ## Motion stop
109        err = await dev.Motion_stop_async(port, axis, stop_decel)
110        print(f"stop_async in axis{axis}, status: {err}")
111
112        ## Servo off
113        err = await dev.Motion_enableServoOff_async(port, axis)
114        print(f"enableServoOff_async in axis{axis}, status: {err}")
115
116        ## Motion close
117        err = await dev.Motion_close_async(port)
118        print(f"close_async in port {port}, status: {err}")
119
120    except Exception as err:
121        pywpc.printGenericError(err)
122
123    ## Disconnect device
124    dev.disconnect()
125
126    ## Release device handle
127    dev.close()
128
129    return
130
131def main_for_spyder(*args):
132    if asyncio.get_event_loop().is_running():
133        return asyncio.create_task(main(*args)).result()
134    else:
135        return asyncio.run(main(*args))
136
137if __name__ == '__main__':
138    asyncio.run(main()) ## Use terminal
139    # await main() ## Use Jupyter or IPython(>=7.0)
140    # main_for_spyder() ## Use Spyder