AI continuous multi slot

  1
  2'''
  3AI - AI_continuous_multi_slot.py with asynchronous mode.
  4
  5This example demonstrates the process of obtaining AI data in continuous mode with multi slot from STEM.
  6
  7To begin with, it demonstrates the steps to open the AI and configure the AI parameters.
  8Next, it outlines the procedure for reading the streaming AI data.
  9Finally, it concludes by explaining how to close the AI.
 10
 11Please invoke the function `Sys_setAIOMode_async`and `AI_enableCS_async`.
 12Example: AI_enableCS_async is {0, 2}
 13Subsequently, the returned value of AI_readOnDemand_async and AI_readStreaming_async will be displayed as follows.
 14data:
 15          CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7, CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7
 16          |                                     |                                      |
 17          |---------------- CS0-----------------|---------------- CS2------------------|
 18[sample0]
 19[sample1]
 20   .
 21   .
 22   .
 23[sampleN]
 24
 25-------------------------------------------------------------------------------------
 26Please change correct serial number or IP and port number BEFORE you run example code.
 27
 28For other examples please check:
 29    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
 30See README.md file to get detailed usage of this example.
 31
 32Copyright (c) 2022-2024 WPC Systems Ltd. All rights reserved.
 33'''
 34
 35## Python
 36import asyncio
 37
 38## WPC
 39
 40from wpcsys import pywpc
 41
 42async def main():
 43    ## Get Python driver version
 44    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 45
 46    ## Create device handle
 47    dev = pywpc.STEM()
 48
 49    ## Connect to device
 50    try:
 51        dev.connect("192.168.1.110") ## Depend on your device
 52    except Exception as err:
 53        pywpc.printGenericError(err)
 54        ## Release device handle
 55        dev.close()
 56        return
 57
 58    try:
 59        ## Parameters setting
 60        slot_list = [1, 2] ## Connect AIO module to slot
 61        mode = 2 ## 0 : On demand, 1 : N-samples, 2 : Continuous.
 62        sampling_rate = 200
 63        read_points = 200
 64        read_delay = 0.2 ## second
 65        chip_select = [0, 1]
 66
 67        ## Get firmware model & version
 68        driver_info = await dev.Sys_getDriverInfo_async()
 69        print("Model name: " + driver_info[0])
 70        print("Firmware version: " + driver_info[-1])
 71
 72        for slot in slot_list:
 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 AI
 87            err = await dev.AI_open_async(slot)
 88            print(f"AI_open_async in slot {slot}, status: {err}")
 89
 90            ## Enable CS
 91            err = await dev.AI_enableCS_async(slot, chip_select)
 92            print(f"AI_enableCS_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            ## Open AI streaming
103            err = await dev.AI_openStreaming_async(slot)
104            print(f"AI_openStreaming_async in slot {slot}, status: {err}")
105
106            ## Start AI streaming
107            err = await dev.AI_startStreaming_async(slot)
108            print(f"AI_startStreaming_async in slot {slot}, status: {err}")
109
110            ## Wait for acquisition
111            await asyncio.sleep(1) ## delay [s]
112
113        data_len = 1
114        while data_len > 0:
115            for slot in slot_list:
116                ## Read data acquisition
117                ai_2Dlist = await dev.AI_readStreaming_async(slot, read_points, read_delay)
118                print(f"Slot{slot}: data len {len(ai_2Dlist)}" )
119
120                ## Update data len and counter
121                data_len = len(ai_2Dlist)
122
123    except Exception as err:
124        pywpc.printGenericError(err)
125    except KeyboardInterrupt:
126        print("Press keyboard")
127    finally:
128        for slot in slot_list:
129            ## Close AI streaming
130            err = await dev.AI_closeStreaming_async(slot)
131            print(f"AI_closeStreaming_async in slot {slot}, status: {err}")
132
133            ## Close AI
134            err = await dev.AI_close_async(slot)
135            print(f"AI_close_async in slot {slot}, status: {err}")
136
137    ## Disconnect device
138    dev.disconnect()
139
140    ## Release device handle
141    dev.close()
142
143    return
144
145def main_for_spyder(*args):
146    if asyncio.get_event_loop().is_running():
147        return asyncio.create_task(main(*args)).result()
148    else:
149        return asyncio.run(main(*args))
150if __name__ == '__main__':
151    asyncio.run(main()) ## Use terminal
152    # await main() ## Use Jupyter or IPython(>=7.0)
153    # main_for_spyder() ## Use Spyder