|
4 | 4 | import sys |
5 | 5 | import asyncio |
6 | 6 |
|
7 | | -import matplotlib.pyplot as plt |
8 | | -import numpy as np |
| 7 | +from science_mode_4.device_p24 import DeviceP24 |
| 8 | +from science_mode_4.utils.serial_port_connection import SerialPortConnection |
9 | 9 |
|
10 | | -from src.science_mode_4 import LayerDyscom, LayerLowLevel,\ |
11 | | - Commands, Connector, Channel, ChannelPoint,\ |
12 | | - SerialPortConnection,\ |
13 | | - DeviceI24,\ |
14 | | - Ads129xOutputDataRate, Ads129xPowerMode,\ |
15 | | - PacketDyscomGetAckOperationMode, PacketDyscomSendLiveData,\ |
16 | | - DyscomInitParams, DyscomPowerModulePowerType, DyscomPowerModuleType, DyscomSignalType |
17 | 10 |
|
18 | 11 |
|
19 | | - |
20 | | -# print(science_mode_4.__version__) |
21 | | - |
22 | | -def send_channel_config(low_level_layer: LayerLowLevel, connector: Connector): |
23 | | - """Sends channel update""" |
24 | | - # device can store up to 10 channel config commands |
25 | | - for channel in Channel: |
26 | | - # send_channel_config does not wait for an acknowledge |
27 | | - low_level_layer.send_channel_config(True, channel, connector, |
28 | | - [ChannelPoint(4000, 20), ChannelPoint(4000, -20), |
29 | | - ChannelPoint(4000, 0)]) |
30 | | - |
31 | 12 | async def main() -> int: |
32 | 13 | """Main function""" |
33 | 14 |
|
34 | | - connection = SerialPortConnection("COM6") |
| 15 | + devices = SerialPortConnection.list_science_mode_device_ports() |
| 16 | + connection = SerialPortConnection(devices[0].device) |
35 | 17 | # devices = UsbConnection.list_science_mode_devices() |
36 | 18 | # connection = UsbConnection(devices[0]) |
37 | 19 | # connection = NullConnection() |
38 | 20 | connection.open() |
39 | 21 |
|
40 | | - device = DeviceI24(connection) |
| 22 | + device = DeviceP24(connection) |
41 | 23 | await device.initialize() |
42 | | - # general = device.get_layer_general() |
43 | | - # print(f"device id: {general.device_id}") |
44 | | - # print(f"firmware version: {general.firmware_version}") |
45 | | - # print(f"science mode version: {general.science_mode_version}") |
46 | | - |
47 | | - dyscom: LayerDyscom = device.get_layer_dyscom() |
48 | | - # fss: DyscomGetFileSystemStatusResult = await dyscom.get_file_system_status() |
49 | | - # print(f"Ready {fss.file_system_ready}, used size {fss.used_size}, free size {fss.free_size}") |
50 | | - # fbn: DyscomGetFileByNameResult = await dyscom.get_file_by_name() |
51 | | - # print(f"Filename {fbn.filename}, block offset {fbn.block_offset}, filesize {fbn.filesize}, nr of blocks {fbn.number_of_blocks}") |
52 | | - # fv: str = await dyscom.get_firmware_version() |
53 | | - # print(f"Firmware version {fv}") |
54 | | - # nrof = await dyscom.get_list_of_measurement_meta_info() |
55 | | - # print(f"Number of measurement meta info {nrof}") |
56 | | - # did = await dyscom.get_device_id() |
57 | | - # print(f"Device ID {did}") |
58 | | - # fi = await dyscom.get_file_info() |
59 | | - # print(f"File info {fi.filename} {fi.filesize} {fi.checksum}") |
60 | | - # b = await dyscom.get_battery() |
61 | | - # print(f"Battery {b.voltage} {b.current} {b.percentage} {b.temperature} {b.energy_state}") |
62 | | - |
63 | | - # sys_ack: DyscomSysResult = await dyscom.sys(DyscomSysType.DEVICE_STORAGE) |
64 | | - # print(f"Sys {sys_ack.sys_type} {sys_ack.state} {sys_ack.filename}") |
65 | | - |
66 | | - await dyscom.power_module(DyscomPowerModuleType.MEASUREMENT, DyscomPowerModulePowerType.SWITCH_ON) |
67 | | - init_params = DyscomInitParams() |
68 | | - init_params.register_map_ads129x.config_register_1.output_data_rate = Ads129xOutputDataRate.HR_MODE_500_SPS__LP_MODE_250_SPS |
69 | | - init_params.register_map_ads129x.config_register_1.power_mode = Ads129xPowerMode.LOW_POWER |
70 | | - await dyscom.init(init_params) |
71 | | - |
72 | | - fig, ax = plt.subplots() |
73 | | - ax.set(xlabel="Sample Time (µs)", ylabel="Current (mA)", |
74 | | - title="Current measurement") |
75 | | - ax.grid() |
76 | | - plt.ion() |
77 | | - plt.show() |
78 | | - |
79 | | - def update_ylim(data: list[float]): |
80 | | - if len(data) == 0: |
81 | | - return |
82 | | - |
83 | | - new_min = data[0] |
84 | | - new_max = data[0] |
85 | | - for x in data: |
86 | | - new_min = min(new_min, x) |
87 | | - new_max = max(new_max, x) |
88 | | - |
89 | | - offset = (new_max - new_min) * 0.1 |
90 | | - plt.ylim(new_min - offset, new_max + offset) |
91 | | - |
92 | | - plot_buffer: list[float] = [] |
93 | | - plot_data, = ax.plot(np.linspace(0, 100, len(plot_buffer)), plot_buffer) |
94 | | - update_ylim(plot_buffer) |
95 | | - used_signals: set[DyscomSignalType] = set() |
96 | | - await dyscom.start() |
97 | | - |
98 | | - for x in range(1000): |
99 | | - if x % 100 == 0: |
100 | | - dyscom.send_get_operation_mode() |
101 | | - |
102 | | - while True: |
103 | | - ack = dyscom.packet_buffer.get_packet_from_buffer() |
104 | | - if ack: |
105 | | - if ack.command == Commands.DlGetAck: |
106 | | - om_ack: PacketDyscomGetAckOperationMode = ack |
107 | | - print(f"Operation mode {om_ack.operation_mode}") |
108 | | - elif ack.command == Commands.DlSendLiveData: |
109 | | - sld: PacketDyscomSendLiveData = ack |
110 | | - if sld.status_error: |
111 | | - print(f"SendLiveData status error {sld.samples}") |
112 | | - break |
113 | | - if sld.number % 50 == 0: |
114 | | - # print(f"Append {sld.value} {sld.signal_type}") |
115 | | - for s in sld.samples: |
116 | | - used_signals.add(s.signal_type) |
117 | | - if len(plot_buffer) > 250: |
118 | | - plot_buffer.pop(0) |
119 | | - plot_buffer.append(sld.time_offset) # samples[1].value |
120 | | - plot_data.remove() |
121 | | - plot_data, = ax.plot(np.linspace(0, len(plot_buffer), len(plot_buffer)), plot_buffer, color = "b") |
122 | | - # plot_data.set_xdata(np.linspace(0, 100, len(plot_buffer))) |
123 | | - # plot_data.set_ydata(plot_buffer) |
124 | | - # update_ylim(plot_buffer) |
125 | | - fig.canvas.draw() |
126 | | - fig.canvas.flush_events() |
127 | | - |
128 | | - else: |
129 | | - break |
130 | | - |
131 | | - await asyncio.sleep(0.01) |
132 | | - |
133 | | - # wait until all acknowledges are received |
134 | | - await asyncio.sleep(0.5) |
135 | | - |
136 | | - await dyscom.stop() |
137 | | - await dyscom.power_module(DyscomPowerModuleType.MEASUREMENT, DyscomPowerModulePowerType.SWITCH_OFF) |
138 | | - |
139 | | - print(used_signals) |
| 24 | + general = device.get_layer_general() |
| 25 | + print(f"Device id: {general.device_id}") |
| 26 | + print(f"Firmware version: {general.firmware_version}") |
| 27 | + print(f"Science mode version: {general.science_mode_version}") |
140 | 28 |
|
141 | 29 | connection.close() |
142 | 30 |
|
|
0 commit comments