Motion find home

  1'''
  2Motion - Motion_find_home.py with synchronous 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 time
 16
 17## WPC
 18
 19from wpcsys import pywpc
 20
 21def 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        timeout = 3 ## second
 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 home parameters
 58        find_home = 0
 59        dir_reverse = 1
 60
 61        ## Get firmware model & version
 62        driver_info = dev.Sys_getDriverInfo(timeout)
 63        print("Model name: " + driver_info[0])
 64        print("Firmware version: " + driver_info[-1])
 65
 66        ## Motion open
 67        err = dev.Motion_open(port, timeout)
 68        print(f"Motion_open in port {port}, status: {err}")
 69
 70        '''
 71        ## Motion open configuration file
 72        err = dev.Motion_openCfgFile('C:/Users/user/Desktop/3AxisStage_2P.ini')
 73        print(f"openCfgFile, status: {err}")
 74
 75        ## Motion load configuration file
 76        err = dev.Motion_loadCfgFile()
 77        print(f"loadCfgFile, status: {err}")
 78        '''
 79
 80        ## Motion configure
 81        err = dev.Motion_cfgAxis(port, axis, two_pulse_mode, axis_dir_cw, encoder_dir_cw, active_low, timeout)
 82        print(f"Motion_cfgAxis in axis{axis}, status: {err}")
 83
 84        err = dev.Motion_cfgLimit(port, axis, forward_enable_true, reverse_enable_true, active_low, timeout)
 85        print(f"Motion_cfgLimit in axis{axis}, status: {err}")
 86
 87        err = dev.Motion_cfgFindRef(port, axis, find_home, dir_reverse, search_velo=10000, search_accle=100000, approach_velo_percent=20, en_reset_posi=0, offset_posi=1500, timeout=timeout)
 88        print(f"Motion_cfgFindRef in axis{axis}, status: {err}")
 89
 90        err = dev.Motion_cfgHome(port, axis, home_enable_false, active_low, timeout)
 91        print(f"Motion_cfgHome in axis{axis}, status: {err}")
 92
 93        ## Servo on
 94        err = dev.Motion_enableServoOn(port, axis, timeout)
 95        print(f"Motion_enableServoOn in axis{axis}, status: {err}")
 96
 97        err = dev.Motion_rstEncoderPosi(port, axis, encoder_posi=0, timeout=timeout)
 98        print(f"Motion_rstEncoderPosi in axis{axis}, status: {err}")
 99
100        ## Motion find reference
101        err = dev.Motion_findRef(port, axis, timeout)
102        print(f"Motion_findRef in axis{axis}, status: {err}")
103
104        home_status = 0
105        while home_status == 0:
106            ## Read forward and reverse limit status
107            hit_status = dev.Motion_getLimitStatus(port, axis, timeout)
108            forward_hit = hit_status[0]
109            reverse_hit = hit_status[1]
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 = dev.Motion_getHomeStatus(port, axis, timeout)
117            if home_status == 1:
118                print("Home hit")
119
120            ## Check finding and found status
121            driving_status = dev.Motion_checkRef(port, axis, timeout)
122            print(f"driving_status: {driving_status}")
123
124        ## Motion stop
125        err = dev.Motion_stop(port, axis, stop_decel, timeout)
126        print(f"Motion_stop in axis{axis}, status: {err}")
127
128        ## Servo off
129        err = dev.Motion_enableServoOff(port, axis, timeout)
130        print(f"Motion_enableServoOff in axis{axis}, status: {err}")
131
132        ## Motion close
133        err = dev.Motion_close(port, timeout)
134        print(f"Motion_close in port {port}, status: {err}")
135    except Exception as err:
136        pywpc.printGenericError(err)
137
138    ## Disconnect device
139    dev.disconnect()
140
141    ## Release device handle
142    dev.close()
143
144    return
145
146if __name__ == '__main__':
147    main()