Motion find limit

  1'''
  2Motion - Motion_find_limit.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.EMotion()
 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        axis = 0
 43        two_pulse_mode = 1
 44        stop_decel = 0
 45
 46        ## Axis and encoder parameters
 47        axis_dir_cw = 0
 48        encoder_dir_cw = 0
 49
 50        ## Polarity and enable parameters
 51        active_low = 0
 52        active_high = 1
 53        forward_enable_true = 1
 54        reverse_enable_true = 1
 55        home_enable_false = 0
 56
 57        ## Find limit parameters
 58        find_limit = 2
 59        dir_reverse = 1
 60
 61        ## Get firmware model & version
 62        driver_info = await dev.Sys_getDriverInfo_async()
 63        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 64
 65        ## Motion open
 66        err = await dev.Motion_open_async(port)
 67        print(f"open_async in port {port}, status: {err}")
 68
 69        '''
 70        ## Motion open configuration file
 71        err = await dev.Motion_openCfgFile_async('C:/Users/user/Desktop/3AxisStage_2P.ini')
 72        print(f"openCfgFile_async, status: {err}")
 73
 74        ## Motion load configuration file
 75        err = await dev.Motion_loadCfgFile_async()
 76        print(f"loadCfgFile_async, status: {err}")
 77        '''
 78
 79        ## Motion configure
 80        err = await dev.Motion_cfgAxis_async(port, axis, two_pulse_mode, axis_dir_cw, encoder_dir_cw, active_low)
 81        print(f"cfgAxis_async in axis{axis}, status: {err}")
 82
 83        err = await dev.Motion_cfgLimit_async(port, axis, forward_enable_true, reverse_enable_true, active_low)
 84        print(f"cfgLimit_async in axis{axis}, status: {err}")
 85
 86        err = await dev.Motion_cfgFindRef_async(port, axis, find_limit, dir_reverse, search_velo=10000, search_accle=100000, approach_velo_percent=20, en_reset_posi=0, offset_posi=1500)
 87        print(f"cfgFindRef_async in axis{axis}, status: {err}")
 88
 89        err = await dev.Motion_cfgHome_async(port, axis, home_enable_false, active_low)
 90        print(f"cfgHome_async in axis{axis}, status: {err}")
 91
 92        ## Servo on
 93        err = await dev.Motion_enableServoOn_async(port, axis)
 94        print(f"enableServoOn_async in axis{axis}, status: {err}")
 95
 96        err = await dev.Motion_rstEncoderPosi_async(port, axis, encoder_posi=0)
 97        print(f"rstEncoderPosi_async in axis{axis}, status: {err}")
 98
 99        ## Motion find reference
100        err = await dev.Motion_findRef_async(port, axis)
101        print(f"findRef_async in axis{axis}, status: {err}")
102
103        driving_status = 0
104        while driving_status == 0:
105            ## Read forward and reverse limit status
106            hit_status = await dev.Motion_getLimitStatus_async(port, axis)
107            forward_hit = hit_status[0]
108            reverse_hit = hit_status[1]
109
110            if forward_hit == 1:
111                print("Forward hit")
112            if reverse_hit == 1:
113                print("Reverse hit")
114
115            ## Read home status
116            home_status = await dev.Motion_getHomeStatus_async(port, axis)
117            if home_status == 1:
118                print("Home hit")
119
120            ## Check finding and found status
121            driving_status = await dev.Motion_checkRef_async(port, axis)
122            print(f"driving_status: {driving_status}")
123
124        ## Motion stop
125        err = await dev.Motion_stop_async(port, axis, stop_decel)
126        print(f"stop_async in axis{axis}, status: {err}")
127
128        ## Servo off
129        err = await dev.Motion_enableServoOff_async(port, axis)
130        print(f"enableServoOff_async in axis{axis}, status: {err}")
131
132        ## Motion close
133        err = await dev.Motion_close_async(port)
134        print(f"close_async in port {port}, status: {err}")
135    except Exception as err:
136        pywpc.printGenericError(err)
137
138    finally:
139        ## Disconnect device
140        dev.disconnect()
141
142        ## Release device handle
143        dev.close()
144
145
146def main_for_spyder(*args):
147    if asyncio.get_event_loop().is_running():
148        return asyncio.create_task(main(*args)).result()
149    else:
150        return asyncio.run(main(*args))
151
152
153if __name__ == '__main__':
154    asyncio.run(main())  ## Use terminal
155    # await main()  ## Use Jupyter or IPython(>=7.0)
156    # main_for_spyder()  ## Use Spyder