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