|
1 | 1 | # TBD: https://stackoverflow.com/questions/3898572/what-are-the-most-common-python-docstring-formats |
2 | | -from .sciopy_dataclasses import ScioSpecMeasurementConfig |
| 2 | +from .sciopy_dataclasses import ( |
| 3 | + ScioSpecMeasurementConfig, |
| 4 | + ScioSpecMeasurementSetup, |
| 5 | +) |
3 | 6 | from datetime import datetime |
| 7 | +import numpy as np |
| 8 | +from typing import Union |
| 9 | +import struct |
| 10 | + |
| 11 | + |
| 12 | +def set_measurement_config(serial, ssms: ScioSpecMeasurementSetup) -> None: |
| 13 | + """ |
| 14 | + set_measurement_config sets the ScioSpec device configuration depending on the ssms configuration dataclass. |
| 15 | +
|
| 16 | + Parameters |
| 17 | + ---------- |
| 18 | + serial : _type_ |
| 19 | + serial connection |
| 20 | + ssms : ScioSpecMeasurementSetup |
| 21 | + dataclass with the measurement setup settings. |
| 22 | + """ |
| 23 | + |
| 24 | + def clTbt_sp(val: Union[int, float]) -> list: |
| 25 | + """ |
| 26 | + clTbt_sp converts an integer or float value to a list of single precision bytes. |
| 27 | +
|
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + val : Union[int, float] |
| 31 | + Value that has to be converted |
| 32 | +
|
| 33 | + Returns |
| 34 | + ------- |
| 35 | + list |
| 36 | + list with single precision byte respresentation |
| 37 | + """ |
| 38 | + return [int(bt) for bt in struct.pack(">f", val)] |
| 39 | + |
| 40 | + def clTbt_dp(val: float) -> list: |
| 41 | + """ |
| 42 | + clTbt_dp converts an integer or float value to a list of double precision bytes. |
| 43 | +
|
| 44 | + Parameters |
| 45 | + ---------- |
| 46 | + val : float |
| 47 | + value that has to be converted |
| 48 | +
|
| 49 | + Returns |
| 50 | + ------- |
| 51 | + list |
| 52 | + list with double precision byte respresentation |
| 53 | + """ |
| 54 | + return [int(ele) for ele in struct.pack(">d", val)] |
| 55 | + |
| 56 | + # Set measurement setup: |
| 57 | + serial.write(bytearray([0xB0, 0x01, 0x01, 0xB0])) |
| 58 | + # Set burst count: "B0 03 02 00 03 B0" = 3 |
| 59 | + serial.write(bytearray([0xB0, 0x03, 0x02, 0x00, ssms.burst_count, 0xB0])) |
| 60 | + |
| 61 | + # Excitation amplitude double precision |
| 62 | + # A_min = 100nA |
| 63 | + # A_max = 10mA |
| 64 | + if ssms.amplitude > 0.001: |
| 65 | + print(f"Divide {ssms.amplitude}/1000. Out of available range") |
| 66 | + ssms.amplitude = ssms.amplitude / 1000 |
| 67 | + serial.write( |
| 68 | + bytearray(list(np.concatenate([[176, 9, 5], clTbt_dp(ssms.amplitude), [176]]))) |
| 69 | + ) |
| 70 | + |
| 71 | + # ADC range settings: [+/-1, +/-5, +/-10] |
| 72 | + # ADC range = +/-1 : B0 02 0D 01 B0 |
| 73 | + # ADC range = +/-5 : B0 02 0D 02 B0 |
| 74 | + # ADC range = +/-10 : B0 02 0D 03 B0 |
| 75 | + if ssms.adc_range == 1: |
| 76 | + serial.write(bytearray([0xB0, 0x02, 0x0D, 0x01, 0xB0])) |
| 77 | + elif ssms.adc_range == 5: |
| 78 | + serial.write(bytearray([0xB0, 0x02, 0x0D, 0x02, 0xB0])) |
| 79 | + elif ssms.adc_range == 10: |
| 80 | + serial.write(bytearray([0xB0, 0x02, 0x0D, 0x03, 0xB0])) |
| 81 | + |
| 82 | + # Gain settings: |
| 83 | + # Gain = 1 : B0 03 09 01 00 B0 |
| 84 | + # Gain = 10 : B0 03 09 01 01 B0 |
| 85 | + # Gain = 100 : B0 03 09 01 02 B0 |
| 86 | + # Gain = 1_000 : B0 03 09 01 03 B0 |
| 87 | + if ssms.gain == 1: |
| 88 | + serial.write(bytearray([0xB0, 0x03, 0x09, 0x01, 0x00, 0xB0])) |
| 89 | + elif ssms.gain == 10: |
| 90 | + serial.write(bytearray([0xB0, 0x03, 0x09, 0x01, 0x01, 0xB0])) |
| 91 | + elif ssms.gain == 100: |
| 92 | + serial.write(bytearray([0xB0, 0x03, 0x09, 0x01, 0x02, 0xB0])) |
| 93 | + elif ssms.gain == 1_000: |
| 94 | + serial.write(bytearray([0xB0, 0x03, 0x09, 0x01, 0x03, 0xB0])) |
| 95 | + |
| 96 | + # Single ended mode: |
| 97 | + serial.write(bytearray([0xB0, 0x03, 0x08, 0x01, 0x01, 0xB0])) |
| 98 | + |
| 99 | + # Excitation switch type: |
| 100 | + serial.write(bytearray([0xB0, 0x02, 0x0C, 0x01, 0xB0])) |
| 101 | + |
| 102 | + # Set framerate: |
| 103 | + serial.write( |
| 104 | + bytearray(list(np.concatenate([[176, 5, 3], clTbt_sp(ssms.framerate), [176]]))) |
| 105 | + ) |
| 106 | + |
| 107 | + # Set frequencies: |
| 108 | + # [CT] 0C 04 [fmin] [fmax] [fcount] [ftype] [CT] |
| 109 | + f_min = clTbt_sp(ssms.exc_freq) |
| 110 | + f_max = clTbt_sp(ssms.exc_freq) |
| 111 | + f_count = [0, 1] |
| 112 | + f_type = [0] |
| 113 | + # bytearray |
| 114 | + serial.write( |
| 115 | + bytearray( |
| 116 | + list(np.concatenate([[176, 12, 4], f_min, f_max, f_count, f_type, [176]])) |
| 117 | + ) |
| 118 | + ) |
| 119 | + |
| 120 | + # Set injection config |
| 121 | + |
| 122 | + el_inj = np.arange(1, ssms.n_el + 1) |
| 123 | + el_gnd = np.roll(el_inj, -(ssms.inj_skip + 1)) |
| 124 | + |
| 125 | + for v_el, g_el in zip(el_inj, el_gnd): |
| 126 | + serial.write(bytearray([0xB0, 0x03, 0x06, v_el, g_el, 0xB0])) |
| 127 | + |
| 128 | + # Get measurement setup |
| 129 | + serial.write(bytearray([0xB1, 0x01, 0x03, 0xB1])) |
| 130 | + # Set output configuration |
| 131 | + serial.write(bytearray([0xB2, 0x02, 0x01, 0x01, 0xB2])) |
| 132 | + serial.write(bytearray([0xB2, 0x02, 0x03, 0x01, 0xB2])) |
| 133 | + serial.write(bytearray([0xB2, 0x02, 0x02, 0x01, 0xB2])) |
| 134 | + |
| 135 | + ## start measurement |
| 136 | + # serial.write(bytearray([0xB4, 0x01, 0x01, 0xB4])) |
| 137 | + # stop measurement |
| 138 | + # serial.write(bytearray([0xB4, 0x01, 0x00, 0xB4])) |
4 | 139 |
|
5 | 140 |
|
6 | 141 | def conf_n_el_16_adjacent( |
@@ -86,8 +221,8 @@ def conf_n_el_16_adjacent( |
86 | 221 | serial.write(bytearray([0xB2, 0x02, 0x01, 0x01, 0xB2])) |
87 | 222 | serial.write(bytearray([0xB2, 0x02, 0x03, 0x01, 0xB2])) |
88 | 223 | serial.write(bytearray([0xB2, 0x02, 0x02, 0x01, 0xB2])) |
89 | | - serial.write(bytearray([0xB4, 0x01, 0x01, 0xB4])) |
90 | | - serial.write(bytearray([0xB4, 0x01, 0x00, 0xB4])) |
| 224 | + # serial.write(bytearray([0xB4, 0x01, 0x01, 0xB4])) |
| 225 | + # serial.write(bytearray([0xB4, 0x01, 0x00, 0xB4])) |
91 | 226 | return ScioSpecMeasurementConfig( |
92 | 227 | com_port=serial.name, |
93 | 228 | burst_count=10, |
@@ -188,8 +323,8 @@ def conf_n_el_16_opposite( |
188 | 323 | serial.write(bytearray([0xB2, 0x02, 0x01, 0x01, 0xB2])) |
189 | 324 | serial.write(bytearray([0xB2, 0x02, 0x03, 0x01, 0xB2])) |
190 | 325 | serial.write(bytearray([0xB2, 0x02, 0x02, 0x01, 0xB2])) |
191 | | - serial.write(bytearray([0xB4, 0x01, 0x01, 0xB4])) |
192 | | - serial.write(bytearray([0xB4, 0x01, 0x00, 0xB4])) |
| 326 | + # serial.write(bytearray([0xB4, 0x01, 0x01, 0xB4])) |
| 327 | + # serial.write(bytearray([0xB4, 0x01, 0x00, 0xB4])) |
193 | 328 | return ScioSpecMeasurementConfig( |
194 | 329 | serial.name, |
195 | 330 | burst_count=10, |
@@ -306,8 +441,8 @@ def conf_n_el_32_adjacent( |
306 | 441 | serial.write(bytearray([0xB2, 0x02, 0x01, 0x01, 0xB2])) |
307 | 442 | serial.write(bytearray([0xB2, 0x02, 0x03, 0x01, 0xB2])) |
308 | 443 | serial.write(bytearray([0xB2, 0x02, 0x02, 0x01, 0xB2])) |
309 | | - serial.write(bytearray([0xB4, 0x01, 0x01, 0xB4])) |
310 | | - serial.write(bytearray([0xB4, 0x01, 0x00, 0xB4])) |
| 444 | + # serial.write(bytearray([0xB4, 0x01, 0x01, 0xB4])) |
| 445 | + # serial.write(bytearray([0xB4, 0x01, 0x00, 0xB4])) |
311 | 446 | return ScioSpecMeasurementConfig( |
312 | 447 | serial.name, |
313 | 448 | burst_count=1, |
@@ -424,8 +559,8 @@ def conf_n_el_32_opposite( |
424 | 559 | serial.write(bytearray([0xB2, 0x02, 0x01, 0x01, 0xB2])) |
425 | 560 | serial.write(bytearray([0xB2, 0x02, 0x03, 0x01, 0xB2])) |
426 | 561 | serial.write(bytearray([0xB2, 0x02, 0x02, 0x01, 0xB2])) |
427 | | - serial.write(bytearray([0xB4, 0x01, 0x01, 0xB4])) |
428 | | - serial.write(bytearray([0xB4, 0x01, 0x00, 0xB4])) |
| 562 | + # serial.write(bytearray([0xB4, 0x01, 0x01, 0xB4])) |
| 563 | + # serial.write(bytearray([0xB4, 0x01, 0x00, 0xB4])) |
429 | 564 | return ScioSpecMeasurementConfig( |
430 | 565 | serial.name, |
431 | 566 | burst_count=1, |
|
0 commit comments