AO output while AI streaming

  1
  2'''
  3AIO - AO_output_while_AI_streaming.py with asynchronous mode.
  4
  5This example demonstrates the process of AI streaming and AO output from STEM.
  6Not all of sampling rate can alter the output values of AO.
  7Its limitation is that the AI sampling rate and the number of CS must be less than or equal to 3000 Hz.
  8
  9Please invoke the function `Sys_setAIOMode_async` and `AI_enableCS_async`.
 10
 11Example: AI_enableCS_async is {0, 2}
 12Subsequently, the returned value of AI_readOnDemand_async and AI_readStreaming_async will be displayed as follows.
 13data:
 14          CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7, CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7
 15          |                                     |                                      |
 16          |---------------- CS0-----------------|---------------- CS2------------------|
 17[sample0]
 18[sample1]
 19   .
 20   .
 21   .
 22[sampleN]
 23
 24-------------------------------------------------------------------------------------
 25Please change correct serial number or IP and port number BEFORE you run example code.
 26
 27For other examples please check:
 28    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
 29See README.md file to get detailed usage of this example.
 30
 31Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
 32'''
 33
 34## WPC
 35from wpcsys import pywpc
 36
 37## Python
 38import random
 39import asyncio
 40import sys
 41sys.path.insert(0, 'src/')
 42
 43
 44async def main():
 45    ## Get Python driver version
 46    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 47
 48    ## Create device handle
 49    dev = pywpc.STEM()
 50
 51    ## Connect to device
 52    try:
 53        dev.connect("192.168.1.110")  ## Depend on your device
 54    except Exception as err:
 55        pywpc.printGenericError(err)
 56        ## Release device handle
 57        dev.close()
 58        return
 59
 60    try:
 61        ## Parameters setting
 62        slot = 1  ## Connect AIO module to slot
 63        chip_select = [0, 1]
 64        mode = 2  ## 0: On demand, 1: N-samples, 2: Continuous
 65        sampling_rate = 200
 66        read_points = 200
 67        read_delay = 2  ## [sec]
 68
 69        ## Get firmware model & version
 70        driver_info = await dev.Sys_getDriverInfo_async()
 71        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 72
 73        ## Get slot mode
 74        slot_mode = await dev.Sys_getMode_async(slot)
 75        print("Slot mode:", slot_mode)
 76
 77        ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
 78        if slot_mode != "AIO":
 79            err = await dev.Sys_setAIOMode_async(slot)
 80            print(f"Sys_setAIOMode_async in slot {slot}, status: {err}")
 81
 82        ## Get slot mode
 83        slot_mode = await dev.Sys_getMode_async(slot)
 84        print("Slot mode:", slot_mode)
 85
 86        ## Open AO
 87        err = await dev.AO_open_async(slot)
 88        print(f"AO_open_async in slot {slot}, status: {err}")
 89
 90        ## Open AI
 91        err = await dev.AI_open_async(slot)
 92        print(f"AI_open_async in slot {slot}, status: {err}")
 93
 94        ## Set AI acquisition mode to continuous mode (2)
 95        err = await dev.AI_setMode_async(slot, mode)
 96        print(f"AI_setMode_async {mode} in slot {slot}, status: {err}")
 97
 98        ## Set AI sampling rate
 99        err = await dev.AI_setSamplingRate_async(slot, sampling_rate)
100        print(f"AI_setSamplingRate_async {sampling_rate} in slot {slot}, status: {err}")
101
102        ## Enable CS
103        err = await dev.AI_enableCS_async(slot, chip_select)
104        print(f"AI_enableCS_async in slot {slot}, status: {err}")
105
106        ## Open AI streaming
107        err = await dev.AI_openStreaming_async(slot)
108        print(f"AI_openStreaming_async in slot {slot}, status: {err}")
109
110        ## Start AI streaming
111        err = await dev.AI_startStreaming_async(slot)
112        print(f"AI_startStreaming_async in slot {slot}, status: {err}")
113
114        counter = 0
115        data_len = 1
116        ao_list = [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10]
117        while data_len > 0:
118            ## Read data acquisition
119            ai_2Dlist = await dev.AI_readStreaming_async(slot, read_points, read_delay)
120            # print(ai_2Dlist)
121            print(f"Data len = {len(ai_2Dlist)}")
122
123            ## Update data len and counter
124            data_len = len(ai_2Dlist)
125            counter += 1
126
127            if counter % 10 == 0:
128                ## Select AO random value from AO list
129                ao_value = random.choice(ao_list)
130
131                ## Write AO vaule in channel 0
132                err = await dev.AO_writeOneChannel_async(slot, 0, ao_value)
133                print(f"In slot {slot} channel 0, the AO value is {ao_value}, status: {err}")
134
135    except Exception as err:
136        pywpc.printGenericError(err)
137    except KeyboardInterrupt:
138        print("Press keyboard")
139    finally:
140        ## Close AI streaming
141        err = await dev.AI_closeStreaming_async(slot)
142        print(f"AI_closeStreaming_async in slot {slot}, status: {err}")
143
144        ## Close AI
145        err = await dev.AI_close_async(slot)
146        print(f"AI_close_async in slot {slot}, status: {err}")
147
148        ## Close AO
149        err = await dev.AO_close_async(slot)
150        print(f"AO_close_async in slot {slot}, status: {err}")
151
152        ## Disconnect device
153        dev.disconnect()
154
155        ## Release device handle
156        dev.close()
157
158
159def main_for_spyder(*args):
160    if asyncio.get_event_loop().is_running():
161        return asyncio.create_task(main(*args)).result()
162    else:
163        return asyncio.run(main(*args))
164
165
166if __name__ == '__main__':
167    asyncio.run(main())  ## Use terminal
168    # await main()  ## Use Jupyter or IPython(>=7.0)
169    # main_for_spyder()  ## Use Spyder
170