AO waveform generation

  1
  2'''
  3AO - AO_waveform_gen.py with asynchronous mode.
  4
  5This example demonstrates the process of writing AO signal of EthanO.
  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-2024 WPC Systems Ltd. All rights reserved.
 18'''
 19
 20## Python
 21import asyncio
 22
 23## WPC
 24
 25from wpcsys import pywpc
 26
 27async def main():
 28    ## Get Python driver version
 29    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 30
 31    ## Create device handle
 32    dev = pywpc.EthanO()
 33
 34    ## Connect to device
 35    try:
 36        dev.connect("192.168.1.110") ## Depend on your device
 37    except Exception as err:
 38        pywpc.printGenericError(err)
 39        ## Release device handle
 40        dev.close()
 41        return
 42
 43    try:
 44        ## Parameters setting
 45        port = 0 ## Depend on your device
 46        mode = 2 ## 0: on demand, 1: N-samples, 2: Continuous
 47        sampling_rate = 10000
 48        number_of_sample = 10000
 49        form_mode = 3 ##  0:DC voltage, 1: retangular, 2: triangular, 3: sine.
 50        amplitude = 1
 51        offset = 0.1
 52        freq_0 = 10
 53        freq_1 = 20
 54
 55        ## Get firmware model & version
 56        driver_info = await dev.Sys_getDriverInfo_async()
 57        print("Model name: " + driver_info[0])
 58        print("Firmware version: " + driver_info[-1])
 59
 60        ## Open AO
 61        err = await dev.AO_open_async(port)
 62        print(f"AO_open_async in port {port}, status: {err}")
 63
 64        ## Set AO generation mode
 65        err = await dev.AO_setMode_async(port, mode)
 66        print(f"AO_setMode_async in port {port}, status: {err}")
 67
 68        ## Set AO sampling rate to 10k (Hz)
 69        err = await dev.AO_setSamplingRate_async(port, sampling_rate)
 70        print(f"AO_setSamplingRate_async in port {port}, status: {err}")
 71
 72        ## Set AO NumSamples to 10000
 73        err = await dev.AO_setNumSamples_async(port, number_of_sample)
 74        print(f"AO_setNumSamples in port {port}, status: {err}")
 75
 76        ## Set AO enabled channels
 77        err = await dev.AO_setEnableChannels_async(port, [0, 1])
 78        print(f"AO_setEnableChannels_async in port {port}, status: {err}")
 79
 80        ## Set AO form in channel 0
 81        err = await dev.AO_setForm_async(port, 0, form_mode)
 82        print(f"AO_setForm_async in channel 0 in port {port}, status: {err}")
 83
 84        ## Set AO form in channel 1
 85        err = await dev.AO_setForm_async(port, 1, form_mode)
 86        print(f"AO_setForm_async in channel 1 in port {port}, status: {err}")
 87
 88        ## Set Channel 0 form parameters
 89        err = await dev.AO_setFormParam_async(port, 0, amplitude, offset, freq_0)
 90        print(f"AO_setFormParam_async in channel 0 in port {port}, status: {err}")
 91
 92        ## Set Channel 1 form parameters
 93        err = await dev.AO_setFormParam_async(port, 1, amplitude, offset, freq_1)
 94        print(f"AO_setFormParam_async in channel 1 in port {port}, status: {err}")
 95
 96        ## Open AO streaming
 97        info = await dev.AO_openStreaming_async(port)
 98        print(f"mode {info[0]}, sampling rate {info[1]}")
 99
100        ## Start AO streaming
101        err = await dev.AO_startStreaming_async(port)
102        print(f"AO_startStreaming_async in port {port}, status: {err}")
103
104        ## Wait for generating form
105        await asyncio.sleep(10)  ## delay [s]
106
107        ## Close AO streaming
108        err = await dev.AO_closeStreaming_async(port)
109        print(f"AO_closeStreaming_async in port {port}, status: {err}")
110
111        ## Close AO
112        err = await dev.AO_close_async(port)
113        print(f"AO_close_async in port {port}, status: {err}")
114    except Exception as err:
115        pywpc.printGenericError(err)
116
117    ## Disconnect device
118    dev.disconnect()
119
120    ## Release device handle
121    dev.close()
122
123    return
124def main_for_spyder(*args):
125    if asyncio.get_event_loop().is_running():
126        return asyncio.create_task(main(*args)).result()
127    else:
128        return asyncio.run(main(*args))
129if __name__ == '__main__':
130    asyncio.run(main()) ## Use terminal
131    # await main() ## Use Jupyter or IPython(>=7.0)
132    # main_for_spyder() ## Use Spyder