Motion acceleration blending

  1'''
  2Motion - Motion_velocity_blending_acceleration.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        velocity_mode = 2
 45        stop_decel = 0
 46
 47        ## Axis and encoder parameters
 48        axis_dir_cw = 0
 49        encoder_dir_cw = 0
 50
 51        ## Polarity and enable parameters
 52        active_low = 0
 53        active_high = 1
 54        forward_enable_true = 1
 55        reverse_enable_true = 1
 56
 57        ## Get firmware model & version
 58        driver_info = await dev.Sys_getDriverInfo_async()
 59        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 60
 61        ## Motion open
 62        err = await dev.Motion_open_async(port)
 63        print(f"open_async in port {port}, status: {err}")
 64
 65        '''
 66        ## Motion open configuration file
 67        err = await dev.Motion_openCfgFile_async('C:/Users/user/Desktop/3AxisStage_2P.ini')
 68        print(f"openCfgFile_async, status: {err}")
 69
 70        ## Motion load configuration file
 71        err = await dev.Motion_loadCfgFile_async()
 72        print(f"loadCfgFile_async, status: {err}")
 73        '''
 74
 75        ## Motion configure
 76        err = await dev.Motion_cfgAxis_async(port, axis, two_pulse_mode, axis_dir_cw, encoder_dir_cw, active_low)
 77        print(f"cfgAxis_async in axis{axis}, status: {err}")
 78
 79        err = await dev.Motion_cfgAxisMove_async(port, axis, velocity_mode, target_posi=5000, velo=3000, accel=100000, decel=100000)
 80        print(f"cfgAxisMove_async in axis{axis}, status: {err}")
 81
 82        ## Servo on
 83        err = await dev.Motion_enableServoOn_async(port, axis)
 84        print(f"enableServoOn_async in axis{axis}, status: {err}")
 85
 86        err = await dev.Motion_cfgLimit_async(port, axis, forward_enable_true, reverse_enable_true, active_low)
 87        print(f"cfgLimit_async in axis{axis}, status: {err}")
 88
 89        err = await dev.Motion_cfgEncoder_async(port, axis, active_low)
 90        print(f"cfgEncoder_async in axis{axis}, status: {err}")
 91
 92        err = await dev.Motion_rstEncoderPosi_async(port, axis, encoder_posi=0)
 93        print(f"rstEncoderPosi_async in axis{axis}, status: {err}")
 94
 95        ## Motion start
 96        err = await dev.Motion_startSingleAxisMove_async(port, axis)
 97        print(f"startSingleAxisMove_async in axis{axis}, status: {err}")
 98
 99        await asyncio.sleep(5)  ## delay [sec]
100
101        ## Motion override velocity
102        new_velo = -3000
103        new_accel = 100
104        new_decel = 100
105        err = await dev.Motion_overrideAxisVelocity_async(port, axis, new_velo)
106        print(f"overrideAxisVelocity_async in axis{axis}, status: {err}")
107
108        ## Motion override acceleration
109        err = await dev.Motion_overrideAxisAccel_async(port, axis, new_accel, new_decel)
110        print(f"overrideAxisAccel_async in axis{axis}, status: {err}")
111
112        await asyncio.sleep(5)  ## delay [sec]
113
114        new_velo = 6000
115        new_accel = 100000
116        new_decel = 100000
117        ## Motion override velocity
118        err = await dev.Motion_overrideAxisVelocity_async(port, axis, new_velo)
119        print(f"overrideAxisVelocity_async in axis{axis}, status: {err}")
120
121        ## Motion override acceleration
122        err = await dev.Motion_overrideAxisAccel_async(port, axis, new_accel, new_decel)
123        print(f"overrideAxisAccel_async in axis{axis}, status: {err}")
124
125        ## Motion stop
126        err = await dev.Motion_stop_async(port, axis, stop_decel)
127        print(f"stop_async in axis{axis}, status: {err}")
128
129        ## Servo off
130        err = await dev.Motion_enableServoOff_async(port, axis)
131        print(f"enableServoOff_async in axis{axis}, status: {err}")
132
133        ## Motion close
134        err = await dev.Motion_close_async(port)
135        print(f"close_async in port {port}, status: {err}")
136    except Exception as err:
137        pywpc.printGenericError(err)
138
139    finally:
140        ## Disconnect device
141        dev.disconnect()
142
143        ## Release device handle
144        dev.close()
145
146
147def main_for_spyder(*args):
148    if asyncio.get_event_loop().is_running():
149        return asyncio.create_task(main(*args)).result()
150    else:
151        return asyncio.run(main(*args))
152
153
154if __name__ == '__main__':
155    asyncio.run(main())  ## Use terminal
156    # await main()  ## Use Jupyter or IPython(>=7.0)
157    # main_for_spyder()  ## Use Spyder