Skip to content

ADC

ADC

ADC(path: Path, logpath: str | None = None, gain_config: int | None = None)

Initialize ADC sensor.

Parameters:

Name Type Description Default
path Path

Path to ADC data file directory.

required
logpath Optional[str]

Path to log file (optional).

None
gain_config Optional[int]

ADC gain configuration (1, 2, 4, 8, or 16). If None, attempts to read from config.yml in the same folder. Defaults to 16 if not found.

None
Source code in pils/sensors/adc.py
def __init__(
    self, path: Path, logpath: str | None = None, gain_config: int | None = None
) -> None:
    """
    Initialize ADC sensor.

    Parameters
    ----------
    path : Path
        Path to ADC data file directory.
    logpath : Optional[str], optional
        Path to log file (optional).
    gain_config : Optional[int], optional
        ADC gain configuration (1, 2, 4, 8, or 16).
        If None, attempts to read from config.yml in the same folder.
        Defaults to 16 if not found.
    """
    files = list(path.glob("*"))

    for f in files:
        if f.name.lower().endswith("adc.bin"):
            self.data_path = f

    self.data = None
    self.tstart = None

    # Handle logpath
    if logpath is not None:
        self.logpath = logpath
    else:
        try:
            self.logpath = get_logpath_from_datapath(self.data_path)
        except FileNotFoundError:
            self.logpath = None

    with open(self.data_path, "rb") as f:
        data = f.read()
        self.is_ascii = is_ascii_file(data)
        f.close()

    # Auto-detect gain from config file if not provided
    if gain_config is None:
        gain_config = self._read_gain_from_config()

    self.gain_config = gain_config
    self.gain = ADS1015_VALUE_GAIN[gain_config]

load_data

load_data() -> None

Load ADC data from file (auto-detects ASCII or binary format).

Source code in pils/sensors/adc.py
def load_data(self) -> None:
    """Load ADC data from file (auto-detects ASCII or binary format)."""
    if self.is_ascii:
        self.data = decode_adc_file_ascii(self.data_path, self.gain_config)
    else:
        self.data = decode_adc_file_struct(self.data_path)

plot

plot() -> None

Plot ADC amplitude vs time.

Raises:

Type Description
ValueError

If no data loaded.

Source code in pils/sensors/adc.py
def plot(self) -> None:
    """Plot ADC amplitude vs time.

    Raises
    ------
    ValueError
        If no data loaded.
    """
    if self.data is None:
        raise ValueError("No data loaded. Call load_data() first.")
    plt.figure(figsize=(10, 5))
    plt.plot(self.data["timestamp"], self.data["amplitude"], color="crimson")
    plt.ylabel("ADC amplitude [mV]")
    plt.xlabel("Time [s]")
    plt.show()