AI continuous

  1'''
  2AI - AI_continuous.py with asynchronous mode.
  3
  4This example demonstrates the process of obtaining AI data in continuous mode with 8 channels from STEM.
  5
  6To begin with, it demonstrates the steps to open the AI and configure the AI parameters.
  7Next, it outlines the procedure for reading the streaming AI data.
  8Finally, it concludes by explaining how to close the AI.
  9
 10If your product is "STEM", please invoke the function `Sys_setAIOMode_async`and `AI_enableCS_async`.
 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-2024 WPC Systems Ltd. All rights reserved.
 32'''
 33
 34## Python
 35import asyncio
 36
 37## WPC
 38
 39from wpcsys import pywpc
 40
 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 = 1 ## 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        ## Get slot mode
 73        slot_mode = await dev.Sys_getMode_async(slot)
 74        print("Slot mode:", slot_mode)
 75
 76        ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
 77        if slot_mode != "AIO":
 78            err = await dev.Sys_setAIOMode_async(slot)
 79            print(f"Sys_setAIOMode_async in slot {slot}, status: {err}")
 80
 81        ## Get slot mode
 82        slot_mode = await dev.Sys_getMode_async(slot)
 83        print("Slot mode:", slot_mode)
 84
 85        ## Open AI
 86        err = await dev.AI_open_async(slot)
 87        print(f"AI_open_async in slot {slot}, status: {err}")
 88
 89        ## Enable CS
 90        err = await dev.AI_enableCS_async(slot, chip_select)
 91        print(f"AI_enableCS_async in slot {slot}, status: {err}")
 92
 93        ## Set AI acquisition mode to continuous mode (2)
 94        err = await dev.AI_setMode_async(slot, mode)
 95        print(f"AI_setMode_async {mode} in slot {slot}, status: {err}")
 96
 97        ## Set AI sampling rate
 98        err = await dev.AI_setSamplingRate_async(slot, sampling_rate)
 99        print(f"AI_setSamplingRate_async {sampling_rate} in slot {slot}, status: {err}")
100
101        ## Open AI streaming
102        err = await dev.AI_openStreaming_async(slot)
103        print(f"AI_openStreaming_async in slot {slot}, status: {err}")
104
105        ## Start AI streaming
106        err = await dev.AI_startStreaming_async(slot)
107        print(f"AI_startStreaming_async in slot {slot}, status: {err}")
108
109        ## Wait for acquisition
110        await asyncio.sleep(1) ## delay [s]
111
112        ## Close AI streaming
113        err = await dev.AI_closeStreaming_async(slot)
114        print(f"AI_closeStreaming_async in slot {slot}, status: {err}")
115
116        data_len = 1
117        while data_len > 0:
118            ## Read data acquisition
119            ai_2Dlist = await dev.AI_readStreaming_async(slot, read_points, read_delay)
120            print(f"number of samples = {len(ai_2Dlist)}" )
121
122            ## Update data len
123            data_len = len(ai_2Dlist)
124
125        ## Close AI
126        err = await dev.AI_close_async(slot)
127        print(f"AI_close_async in slot {slot}, status: {err}")
128    except Exception as err:
129        pywpc.printGenericError(err)
130
131    ## Disconnect device
132    dev.disconnect()
133
134    ## Release device handle
135    dev.close()
136
137    return
138
139def main_for_spyder(*args):
140    if asyncio.get_event_loop().is_running():
141        return asyncio.create_task(main(*args)).result()
142    else:
143        return asyncio.run(main(*args))
144if __name__ == '__main__':
145    asyncio.run(main()) ## Use terminal
146    # await main() ## Use Jupyter or IPython(>=7.0)
147    # main_for_spyder() ## Use Spyder