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-2024 WPC Systems Ltd. All rights reserved.
 32'''
 33
 34## Python
 35import time
 36import random
 37
 38## WPC
 39
 40from wpcsys import pywpc
 41
 42def 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        chip_select = [0, 1]
 62        mode = 2 ## 0 : On demand, 1 : N-samples, 2 : Continuous.
 63        sampling_rate = 200
 64        read_points = 200
 65        read_delay = 2 ## second
 66        timeout = 3 ## second
 67
 68        ## Get firmware model & version
 69        driver_info = dev.Sys_getDriverInfo(timeout)
 70        print("Model name: " + driver_info[0])
 71        print("Firmware version: " + driver_info[-1])
 72
 73        ## Get slot mode
 74        slot_mode = dev.Sys_getMode(slot, timeout)
 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 = dev.Sys_setAIOMode(slot, timeout)
 80            print(f"Sys_setAIOMode in slot {slot}, status: {err}")
 81
 82        ## Get slot mode
 83        slot_mode = dev.Sys_getMode(slot, timeout)
 84        print("Slot mode:", slot_mode)
 85
 86        ## Open AO
 87        err = dev.AO_open(slot, timeout)
 88        print(f"AO_open in slot {slot}, status: {err}")
 89
 90        ## Open AI
 91        err = dev.AI_open(slot, timeout)
 92        print(f"AI_open in slot {slot}, status: {err}")
 93
 94        ## Set AI acquisition mode to continuous mode (2)
 95        err = dev.AI_setMode(slot, mode, timeout)
 96        print(f"AI_setMode {mode} in slot {slot}, status: {err}")
 97
 98        ## Set AI sampling rate
 99        err = dev.AI_setSamplingRate(slot, sampling_rate, timeout)
100        print(f"AI_setSamplingRate {sampling_rate} in slot {slot}, status: {err}")
101
102        ## Enable CS
103        err = dev.AI_enableCS(slot, chip_select, timeout)
104        print(f"AI_enableCS in slot {slot}, status: {err}")
105
106        ## Open AI streaming
107        err = dev.AI_openStreaming(slot, timeout)
108        print(f"AI_openStreaming in slot {slot}, status: {err}")
109
110        ## Start AI streaming
111        err = dev.AI_startStreaming(slot, timeout)
112        print(f"AI_startStreaming 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 = dev.AI_readStreaming(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 = dev.AO_writeOneChannel(slot, 0, ao_value, timeout)
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 = dev.AI_closeStreaming(slot, timeout)
142        print(f"AI_closeStreaming in slot {slot}, status: {err}")
143
144        ## Close AI
145        err = dev.AI_close(slot, timeout)
146        print(f"AI_close in slot {slot}, status: {err}")
147
148        ## Close AO
149        err = dev.AO_close(slot, timeout)
150        print(f"AO_close in slot {slot}, status: {err}")
151
152        ## Disconnect device
153        dev.disconnect()
154
155        ## Release device handle
156        dev.close()
157
158        return
159
160if __name__ == '__main__':
161    main()