Drive velocity move

  1'''
  2Drive - Drive_velocity_move.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    except Exception as err:
 77        pywpc.printGenericError(err)
 78    except KeyboardInterrupt:
 79        print("Press keyboard")
 80    finally:
 81        ## Motion stop
 82        err = await dev.Motion_stopProcess_async(port)
 83        print(f"Motion_stopProcess, status: {err}")
 84
 85        ## Motion Servo off
 86        err = await dev.Motion_enableServoOff_async(port)
 87        print(f"Motion_enableServoOff, status: {err}")
 88
 89        ## Motion close
 90        err = await dev.Motion_close_async(port)
 91        print(f"Motion_close, status: {err}")
 92
 93        ## Disconnect device
 94        dev.disconnect()
 95
 96        ## Release device handle
 97        dev.close()
 98
 99
100def main_for_spyder(*args):
101    if asyncio.get_event_loop().is_running():
102        return asyncio.create_task(main(*args)).result()
103    else:
104        return asyncio.run(main(*args))
105
106
107if __name__ == '__main__':
108    asyncio.run(main())  ## Use terminal
109    # await main()  ## Use Jupyter or IPython(>=7.0)
110    # main_for_spyder()  ## Use Spyder