examples/a121/algo/phase_tracking/phase_tracking.py

examples/a121/algo/phase_tracking/phase_tracking.py#

  1# Copyright (c) Acconeer AB, 2022-2026
  2# All rights reserved
  3
  4from __future__ import annotations
  5
  6import numpy as np
  7
  8# Added here to force pyqtgraph to choose PySide
  9import PySide6  # noqa: F401
 10
 11import pyqtgraph as pg
 12
 13import acconeer.exptool as et
 14from acconeer.exptool import a121
 15from acconeer.exptool.a121.algo._utils import get_distances_m
 16from acconeer.exptool.a121.algo.phase_tracking import (
 17    Processor,
 18    ProcessorConfig,
 19    ProcessorContext,
 20    get_sensor_config,
 21)
 22
 23
 24def main():
 25    args = a121.ExampleArgumentParser().parse_args()
 26    et.utils.config_logging(args)
 27
 28    sensor_config = get_sensor_config()
 29
 30    client = a121.Client.open(**a121.get_client_args(args))
 31    metadata = client.setup_session(sensor_config)
 32
 33    processor = Processor(
 34        sensor_config=sensor_config,
 35        metadata=metadata,
 36        processor_config=ProcessorConfig(),
 37        context=ProcessorContext(),
 38    )
 39    pg_updater = PGUpdater(sensor_config, metadata)
 40    pg_process = et.PGProcess(pg_updater)
 41    pg_process.start()
 42
 43    client.start_session()
 44    interrupt_handler = et.utils.ExampleInterruptHandler()
 45
 46    while not interrupt_handler.got_signal:
 47        result = client.get_next()
 48        plot_data = processor.process(result)
 49        try:
 50            pg_process.put_data(plot_data)
 51        except et.PGProccessDiedException:
 52            break
 53
 54    print("Disconnecting...")
 55    client.close()
 56
 57
 58class PGUpdater:
 59    def __init__(self, sensor_config: a121.SensorConfig, metadata: a121.Metadata):
 60        self.distances_m = get_distances_m(sensor_config, metadata)
 61
 62    def setup(self, win):
 63        pens = [et.utils.pg_pen_cycler(i) for i in range(3)]
 64        brush = et.utils.pg_brush_cycler(0)
 65        symbol_kw = {"symbol": "o", "symbolSize": 1, "symbolBrush": brush, "symbolPen": "k"}
 66        feat_kws = [{"pen": pen, **symbol_kw} for pen in pens]
 67
 68        # sweep and threshold plot
 69        self.sweep_plot = win.addPlot(row=0, col=0)
 70        self.sweep_plot.setMenuEnabled(False)
 71        self.sweep_plot.showGrid(x=True, y=True)
 72        self.sweep_plot.addLegend()
 73        self.sweep_plot.setLabel("left", "Amplitude")
 74        self.sweep_plot.setLabel("bottom", "Distance (m)")
 75        self.sweep_plot.addItem(pg.PlotDataItem())
 76        self.sweeps_curve = [self.sweep_plot.plot(**feat_kw) for feat_kw in feat_kws]
 77        self.sweep_vertical_line = pg.InfiniteLine(pen=pens[2])
 78        self.sweep_plot.addItem(self.sweep_vertical_line)
 79        self.sweep_smooth_max = et.utils.SmoothMax()
 80        sweep_plot_legend = pg.LegendItem(offset=(0.0, 0.5))
 81        sweep_plot_legend.setParentItem(self.sweep_plot)
 82        sweep_plot_legend.addItem(self.sweeps_curve[0], "Sweep")
 83        sweep_plot_legend.addItem(self.sweeps_curve[1], "Threshold")
 84
 85        # argument plot
 86        argument_plot = win.addPlot(row=1, col=0)
 87        argument_plot.setMenuEnabled(False)
 88        argument_plot.showGrid(x=True, y=True)
 89        argument_plot.addLegend()
 90        argument_plot.setLabel("bottom", "Distance (m)")
 91        argument_plot.setLabel("left", "Phase")
 92        argument_plot.setYRange(-np.pi, np.pi)
 93        argument_plot.getAxis("left").setTicks(et.utils.pg_phase_ticks)
 94        argument_plot.setYRange(-np.pi, np.pi)
 95        argument_plot.addItem(pg.ScatterPlotItem())
 96        self.argument_curve = argument_plot.plot(pen=None, symbol="o", symbolSize=5, symbolPen="k")
 97        self.argument_vertical_line = pg.InfiniteLine(pen=pens[2])
 98        argument_plot.addItem(self.argument_vertical_line)
 99
100        # history plot
101        self.history_plot = win.addPlot(row=2, col=0)
102        self.history_plot.setMenuEnabled(False)
103        self.history_plot.showGrid(x=True, y=True)
104        self.history_plot.addLegend()
105        self.history_plot.setLabel("left", "Distance (mm)")
106        self.history_plot.setLabel("bottom", "Time (s)")
107        self.history_plot.addItem(pg.PlotDataItem())
108        self.history_curve = self.history_plot.plot(**feat_kws[0])
109
110        self.sweep_smooth_max = et.utils.SmoothMax()
111        self.distance_hist_smooth_lim = et.utils.SmoothLimits(tau_decay=0.5, tau_grow=0.1)
112
113    def update(self, processor_result):
114        assert processor_result is not None
115        assert processor_result.threshold is not None
116
117        sweep = processor_result.lp_abs_sweep
118        threshold = processor_result.threshold * np.ones(sweep.size)
119        angle_sweep = processor_result.angle_sweep
120        peak_loc = processor_result.peak_loc_m
121        history = processor_result.distance_history
122        rel_time_stamps = processor_result.rel_time_stamps
123
124        # update sweep plot
125        self.sweeps_curve[0].setData(self.distances_m, sweep)
126        self.sweeps_curve[1].setData(self.distances_m, threshold)
127        max_val_in_sweep_plot = max(np.max(sweep), np.max(threshold))
128        self.sweep_plot.setYRange(0, self.sweep_smooth_max.update(max_val_in_sweep_plot))
129
130        # update argument plot
131        self.argument_curve.setData(self.distances_m, angle_sweep)
132
133        if peak_loc is not None:
134            # update vertical lines
135            self.sweep_vertical_line.setValue(peak_loc)
136            self.argument_vertical_line.setValue(peak_loc)
137            self.sweep_vertical_line.show()
138            self.argument_vertical_line.show()
139        else:
140            self.sweep_vertical_line.hide()
141            self.argument_vertical_line.hide()
142
143        if history.shape[0] != 0:
144            # update history plot
145            self.history_curve.setData(rel_time_stamps, history)
146            lims = self.distance_hist_smooth_lim.update(history)
147            self.history_plot.setYRange(lims[0], lims[1])
148        self.history_plot.setXRange(-Processor.TIME_HORIZON_S, 0)
149
150
151if __name__ == "__main__":
152    main()

View this example on GitHub: acconeer/acconeer-python-exploration