AO waveform generation

  1
  2'''
  3AO - AO_waveform_gen.py with asynchronous mode.
  4
  5This example demonstrates the process of writing AO signal of USBDAQF1AOD.
  6To begin with, it demonstrates the steps to open AO and configure the AO parameters.
  7Next, it outlines the procedure for AO streaming.
  8Finally, it concludes by explaining how to close AO.
  9
 10-------------------------------------------------------------------------------------
 11Please change correct serial number or IP and port number BEFORE you run example code.
 12
 13For other examples please check:
 14    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
 15See README.md file to get detailed usage of this example.
 16
 17Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
 18'''
 19
 20## WPC
 21from wpcsys import pywpc
 22
 23## Python
 24import asyncio
 25import sys
 26sys.path.insert(0, 'src/')
 27
 28
 29async def main():
 30    ## Get Python driver version
 31    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 32
 33    ## Create device handle
 34    dev = pywpc.USBDAQF1AOD()
 35
 36    ## Connect to device
 37    try:
 38        dev.connect("default")  ## Depend on your device
 39    except Exception as err:
 40        pywpc.printGenericError(err)
 41        ## Release device handle
 42        dev.close()
 43        return
 44
 45    try:
 46        ## Parameters setting
 47        port = 0  ## Depend on your device
 48        mode = 2  ## 0: on demand, 1: N-samples, 2: Continuous
 49        sampling_rate = 10000
 50        number_of_sample = 10000
 51        form_mode = 3  ## 0: DC voltage, 1: retangular, 2: triangular, 3: sine
 52        amplitude = 1
 53        offset = 0.1
 54        freq_0 = 10
 55        freq_1 = 20
 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        ## Open AO
 62        err = await dev.AO_open_async(port)
 63        print(f"AO_open_async in port {port}, status: {err}")
 64
 65        ## Set AO generation mode
 66        err = await dev.AO_setMode_async(port, mode)
 67        print(f"AO_setMode_async in port {port}, status: {err}")
 68
 69        ## Set AO sampling rate to 10k (Hz)
 70        err = await dev.AO_setSamplingRate_async(port, sampling_rate)
 71        print(f"AO_setSamplingRate_async in port {port}, status: {err}")
 72
 73        ## Set AO NumSamples to 10000
 74        err = await dev.AO_setNumSamples_async(port, number_of_sample)
 75        print(f"AO_setNumSamples in port {port}, status: {err}")
 76
 77        ## Set AO enabled channels
 78        err = await dev.AO_setEnableChannels_async(port, [0, 1])
 79        print(f"AO_setEnableChannels_async in port {port}, status: {err}")
 80
 81        ## Set AO form in channel 0
 82        err = await dev.AO_setForm_async(port, 0, form_mode)
 83        print(f"AO_setForm_async in channel 0 in port {port}, status: {err}")
 84
 85        ## Set AO form in channel 1
 86        err = await dev.AO_setForm_async(port, 1, form_mode)
 87        print(f"AO_setForm_async in channel 1 in port {port}, status: {err}")
 88
 89        ## Set Channel 0 form parameters
 90        err = await dev.AO_setFormParam_async(port, 0, amplitude, offset, freq_0)
 91        print(f"AO_setFormParam_async in channel 0 in port {port}, status: {err}")
 92
 93        ## Set Channel 1 form parameters
 94        err = await dev.AO_setFormParam_async(port, 1, amplitude, offset, freq_1)
 95        print(f"AO_setFormParam_async in channel 1 in port {port}, status: {err}")
 96
 97        ## Open AO streaming
 98        info = await dev.AO_openStreaming_async(port)
 99        print(f"mode {info[0]}, sampling rate {info[1]}")
100
101        ## Start AO streaming
102        err = await dev.AO_startStreaming_async(port)
103        print(f"AO_startStreaming_async in port {port}, status: {err}")
104
105        ## Wait for generating form
106        await asyncio.sleep(10)   ## delay [sec]
107
108        ## Close AO streaming
109        err = await dev.AO_closeStreaming_async(port)
110        print(f"AO_closeStreaming_async in port {port}, status: {err}")
111
112        ## Close AO
113        err = await dev.AO_close_async(port)
114        print(f"AO_close_async in port {port}, status: {err}")
115    except Exception as err:
116        pywpc.printGenericError(err)
117
118    finally:
119        ## Disconnect device
120        dev.disconnect()
121
122        ## Release device handle
123        dev.close()
124
125
126def main_for_spyder(*args):
127    if asyncio.get_event_loop().is_running():
128        return asyncio.create_task(main(*args)).result()
129    else:
130        return asyncio.run(main(*args))
131
132
133if __name__ == '__main__':
134    asyncio.run(main())  ## Use terminal
135    # await main()  ## Use Jupyter or IPython(>=7.0)
136    # main_for_spyder()  ## Use Spyder