|
4 | 4 | import sys |
5 | 5 | import asyncio |
6 | 6 |
|
7 | | -from science_mode_4 import DeviceP24 |
8 | | -from science_mode_4.low_level.low_level_channel_config import PacketLowLevelChannelConfigAck |
9 | | -from science_mode_4.protocol import ChannelPoint |
10 | | -from science_mode_4.protocol.commands import Commands |
11 | | -from science_mode_4.utils import SerialPortConnection |
12 | | -from science_mode_4.low_level import LayerLowLevel |
13 | | -from science_mode_4.protocol import Connector, Channel |
14 | | -from science_mode_4.low_level import LowLevelHighVoltageSource, LowLevelMode |
| 7 | +from science_mode_4.device_i24 import DeviceI24 |
| 8 | +from science_mode_4.dyscom.dyscom_types import DyscomFilterType, DyscomInitFlag, DyscomInitParams, DyscomPowerModulePowerType,\ |
| 9 | + DyscomPowerModuleType, DyscomSignalType |
| 10 | +from science_mode_4.utils.serial_port_connection import SerialPortConnection |
15 | 11 |
|
16 | 12 |
|
17 | 13 | async def main() -> int: |
18 | 14 | """Main function""" |
19 | 15 |
|
20 | | - # logger().setLevel(logging.DEBUG) |
21 | | - current = 70 |
22 | | - |
23 | | - # # keyboard is our trigger to start specific stimulation |
24 | | - # def input_callback(input_value: str) -> bool: |
25 | | - # """Callback call from keyboard input thread""" |
26 | | - # print(f"Input value {input_value}") |
27 | | - |
28 | | - # nonlocal current |
29 | | - # if input_value == "1": |
30 | | - # send_channel_config(low_level_layer, Connector.GREEN) |
31 | | - # elif input_value == "2": |
32 | | - # send_channel_config(low_level_layer, Connector.YELLOW) |
33 | | - # elif input_value == "+": |
34 | | - # current += 0.5 |
35 | | - # print(f"current: {current}") |
36 | | - # elif input_value == "-": |
37 | | - # current -= 0.5 |
38 | | - # print(f"current: {current}") |
39 | | - # elif input_value == "q": |
40 | | - # # end keyboard input thread |
41 | | - # return True |
42 | | - # else: |
43 | | - # print("Invalid command") |
44 | | - |
45 | | - # return False |
46 | | - |
47 | | - |
48 | | - def send_channel_config(low_level_layer: LayerLowLevel, connector: Connector): |
49 | | - """Sends channel update""" |
50 | | - # device can store up to 10 channel config commands |
51 | | - for channel in Channel: |
52 | | - # send_channel_config does not wait for an acknowledge |
53 | | - low_level_layer.send_channel_config(True, channel, connector, |
54 | | - [ChannelPoint(1000, current), ChannelPoint(4000, 0), |
55 | | - ChannelPoint(1000, -current)]) |
56 | | - |
57 | | - |
58 | | - print("Usage:") |
59 | | - print("Press 1 or 2 to stimulate green or yellow connector") |
60 | | - print("Press + or - to increase or decrease current") |
61 | | - print("Press q to quit") |
62 | | - # create keyboard input thread for non blocking console input |
63 | | - # keyboard_input_thread = KeyboardInputThread(input_callback) |
64 | | - |
65 | | - # get comport from command line argument |
66 | | - # com_port = ExampleUtils.get_comport_from_commandline_argument() |
67 | 16 | # create serial port connection |
68 | 17 | connection = SerialPortConnection(SerialPortConnection.list_science_mode_device_ports()[0].device) |
69 | 18 | # open connection, now we can read and write data |
70 | 19 | connection.open() |
71 | 20 |
|
72 | 21 | # create science mode device |
73 | | - device = DeviceP24(connection) |
| 22 | + device = DeviceI24(connection) |
74 | 23 | # call initialize to get basic information (serial, versions) and stop any active stimulation/measurement |
75 | 24 | # to have a defined state |
76 | 25 | await device.initialize() |
77 | 26 |
|
78 | | - # get low level layer to call low level commands |
79 | | - low_level_layer = device.get_layer_low_level() |
80 | | - |
81 | | - # call init low level |
82 | | - await low_level_layer.init(LowLevelMode.NO_MEASUREMENT, LowLevelHighVoltageSource.STANDARD) |
83 | | - |
84 | | - # now we can start stimulation |
85 | | - # while keyboard_input_thread.is_alive(): |
86 | | - for x in range(500): |
87 | | - if x % 100 == 0: |
88 | | - send_channel_config(low_level_layer, Connector.YELLOW) |
89 | | - # get new packets from connection |
90 | | - ack = low_level_layer.packet_buffer.get_packet_from_buffer() |
91 | | - if ack and ack.command == Commands.LOW_LEVEL_CHANNEL_CONFIG_ACK: |
92 | | - cca: PacketLowLevelChannelConfigAck = ack |
93 | | - # do something with packet ack |
94 | | - # here we print that an acknowledge arrived |
95 | | - print(cca.result.name) |
96 | | - |
97 | | - await asyncio.sleep(0.1) |
98 | | - |
99 | | - # wait until all acknowledges are received |
100 | | - await asyncio.sleep(0.5) |
101 | | - # call stop low level |
102 | | - await low_level_layer.stop() |
103 | | - |
| 27 | + # get dyscom layer to call low level commands |
| 28 | + dyscom = device.get_layer_dyscom() |
| 29 | + |
| 30 | + # call enable measurement power module and memory card for measurement |
| 31 | + await dyscom.power_module(DyscomPowerModuleType.MEASUREMENT, DyscomPowerModulePowerType.SWITCH_ON) |
| 32 | + await dyscom.power_module(DyscomPowerModuleType.MEMORY_CARD, DyscomPowerModulePowerType.SWITCH_ON) |
| 33 | + # call init with lowest sample rate (because of performance issues with plotting values) |
| 34 | + init_params = DyscomInitParams() |
| 35 | + init_params.signal_type = [DyscomSignalType.BI, DyscomSignalType.EMG_1] |
| 36 | + init_params.filter = DyscomFilterType.PREDEFINED_FILTER_1 |
| 37 | + # we want no live data and write all data to memory card |
| 38 | + init_params.flags = [DyscomInitFlag.ENABLE_SD_STORAGE_MODE] |
| 39 | + init_result = await dyscom.init(init_params) |
| 40 | + |
| 41 | + # start dyscom measurement |
| 42 | + await dyscom.start() |
| 43 | + |
| 44 | + # wait 10s to have some measurement data |
| 45 | + await asyncio.sleep(10) |
| 46 | + |
| 47 | + # stop measurement |
| 48 | + await dyscom.stop() |
| 49 | + # turn power module off |
| 50 | + await dyscom.power_module(DyscomPowerModuleType.MEASUREMENT, DyscomPowerModulePowerType.SWITCH_OFF) |
| 51 | + |
| 52 | + # get all meas data |
| 53 | + measurement = await dyscom.get_meas_file_content(init_result.measurement_file_id) |
| 54 | + print(f"Sample rate: {measurement[0].name}") |
| 55 | + for key, value in measurement[1].items(): |
| 56 | + print(f"Signal type: {key.name}, sample count: {len(value)}") |
| 57 | + |
| 58 | + # turn memory card off |
| 59 | + await dyscom.power_module(DyscomPowerModuleType.MEMORY_CARD, DyscomPowerModulePowerType.SWITCH_OFF) |
104 | 60 | # close serial port connection |
105 | 61 | connection.close() |
| 62 | + |
106 | 63 | return 0 |
107 | 64 |
|
108 | 65 |
|
|
0 commit comments