Drive find home

  1'''
  2Drive - Drive_find_home.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.EDriveST()
 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        searching_speed = 50000
 41        approaching_speed = 10000
 42        acceleration = 10000
 43        search_direction = 1    ## 1: pointing to forward, -1: pointing to reverse.
 44        approach_direction = 1  ## 1: pointing to forward, -1: pointing to reverse.
 45        offset = 0
 46        reset_position = False
 47        en_forward = 1
 48        en_reverse = 1
 49        active_high = 1
 50
 51        ## Get firmware model & version
 52        driver_info = await dev.Sys_getDriverInfo_async()
 53        print("Model name: " + driver_info[0])
 54        print("Firmware version: " + driver_info[-1])
 55
 56        ## Motion open
 57        err = await dev.Motion_open_async(port)
 58        print(f"Motion_open, status: {err}")
 59
 60        ## Motion config
 61        err = await dev.Motion_cfgLimit_async(port, en_forward, en_reverse, active_high)
 62        print(f"Motion_cfgLimit, status: {err}")
 63
 64        ## Motion reset
 65        err = await dev.Motion_rstEncoderPosi_async(port)
 66        print(f"Motion_resetEncoder, status: {err}")
 67
 68        ## Motion Servo on
 69        err = await dev.Motion_enableServoOn_async(port)
 70        print(f"Motion_enableServoOn, status: {err}")
 71
 72        ## Motion find reference
 73        err = await dev.Motion_startFindHome_async(port, searching_speed, approaching_speed, acceleration, search_direction, approach_direction, offset, reset_position)
 74        print(f"Motion_startFindHome, status: {err}")
 75
 76        status = 1
 77        while status != 0 :
 78            status = await dev.Motion_getProcessState_async(port)
 79            if(status == 0):
 80                print(f"Motion_getProcessState: {status}")
 81
 82        ## Motion get limit status
 83        state_list = await dev.Motion_getLimitStatus_async(port)
 84        print(f"Forward limit status: {state_list[0]}")
 85        print(f"Reverse limit status: {state_list[1]}")
 86        print(f"Home status: {state_list[2]}")
 87
 88    except Exception as err:
 89        pywpc.printGenericError(err)
 90    except KeyboardInterrupt:
 91        print("Press keyboard")
 92    finally:
 93        ## Motion stop
 94        err = await dev.Motion_stopProcess_async(port)
 95        print(f"Motion_stopProcess, status: {err}")
 96
 97        ## Motion Servo off
 98        err = await dev.Motion_enableServoOff_async(port)
 99        print(f"Motion_enableServoOff, status: {err}")
100
101        ## Motion close
102        err = await dev.Motion_close_async(port)
103        print(f"Motion_close, status: {err}")
104
105    ## Disconnect device
106    dev.disconnect()
107
108    ## Release device handle
109    dev.close()
110
111def main_for_spyder(*args):
112    if asyncio.get_event_loop().is_running():
113        return asyncio.create_task(main(*args)).result()
114    else:
115        return asyncio.run(main(*args))
116
117if __name__ == '__main__':
118    asyncio.run(main()) ## Use terminal
119    # await main() ## Use Jupyter or IPython(>=7.0)
120    # main_for_spyder() ## Use Spyder