Drive velocity blending acceleration

  1'''
  2Drive - Drive_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.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        speed = 50000
 43        dir = 1
 44        acceleration = 10000
 45        deceleration = 10000
 46        active_high = 1
 47        en_forward = 1
 48        en_reverse = 1
 49
 50        ## Get firmware model & version
 51        driver_info = await dev.Sys_getDriverInfo_async()
 52        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 53
 54        ## Motion open
 55        err = await dev.Motion_open_async(port)
 56        print(f"Motion_open, status: {err}")
 57
 58        ## Motion configure
 59        err = await dev.Motion_cfgLimit_async(port, en_forward, en_reverse, active_high)
 60        print(f"Motion_cfgLimit, status: {err}")
 61
 62        ## Motion reset
 63        err = await dev.Motion_rstEncoderPosi_async(port)
 64        print(f"Motion_resetEncoderPosi, status: {err}")
 65
 66        ## Motion Servo on
 67        err = await dev.Motion_enableServoOn_async(port)
 68        print(f"Motion_enableServoOn, status: {err}")
 69
 70        ## Motion start
 71        err = await dev.Motion_startVelocticyMove_async(port, speed, dir, acceleration, deceleration)
 72        print(f"Motion_startVelocticyMove, status: {err}")
 73
 74        ## Wait for seconds for moving
 75        await asyncio.sleep(3)  ## delay [sec]
 76
 77        ## Motion start
 78        new_speed = 10000
 79        new_dir = -1
 80        new_acceleration = 20000
 81        new_deceleration = 20000
 82        err = await dev.Motion_startVelocticyMove_async(port, new_speed, new_dir, new_acceleration, new_deceleration)
 83        print(f"Motion_startVelocticyMove, status: {err}")
 84
 85        ## Wait for seconds for moving
 86        await asyncio.sleep(3)  ## delay [sec]
 87    except Exception as err:
 88        pywpc.printGenericError(err)
 89    except KeyboardInterrupt:
 90        print("Press keyboard")
 91    finally:
 92        ## Motion stop
 93        err = await dev.Motion_stopProcess_async(port)
 94        print(f"Motion_stopProcess, status: {err}")
 95
 96        ## Motion Servo off
 97        err = await dev.Motion_enableServoOff_async(port)
 98        print(f"Motion_enableServoOff, status: {err}")
 99
100        ## Motion close
101        err = await dev.Motion_close_async(port)
102        print(f"Motion_close, status: {err}")
103
104        ## Disconnect device
105        dev.disconnect()
106
107        ## Release device handle
108        dev.close()
109
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
117
118if __name__ == '__main__':
119    asyncio.run(main())  ## Use terminal
120    # await main()  ## Use Jupyter or IPython(>=7.0)
121    # main_for_spyder()  ## Use Spyder