examples/a121/algo/vibration/example_app.py

examples/a121/algo/vibration/example_app.py#

  1# Copyright (c) Acconeer AB, 2024
  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 APPROX_BASE_STEP_LENGTH_M
 16from acconeer.exptool.a121.algo.vibration import (
 17    ExampleApp,
 18    ExampleAppConfig,
 19    ExampleAppResult,
 20    get_high_frequency_config,
 21)
 22
 23
 24SENSOR_ID = 1
 25
 26
 27def main():
 28    args = a121.ExampleArgumentParser().parse_args()
 29    et.utils.config_logging(args)
 30
 31    example_app_config = get_high_frequency_config()
 32
 33    client = a121.Client.open(**a121.get_client_args(args))
 34
 35    example_app = ExampleApp(
 36        client=client,
 37        sensor_id=SENSOR_ID,
 38        example_app_config=example_app_config,
 39    )
 40
 41    pg_updater = PGUpdater(example_app_config)
 42    pg_process = et.PGProcess(pg_updater)
 43
 44    example_app.start()
 45    pg_process.start()
 46
 47    interrupt_handler = et.utils.ExampleInterruptHandler()
 48    print("Press Ctrl-C to end session")
 49
 50    while not interrupt_handler.got_signal:
 51        example_app_result = example_app.get_next()
 52
 53        try:
 54            pg_process.put_data(example_app_result)
 55        except et.PGProccessDiedException:
 56            break
 57
 58    print("Disconnecting...")
 59    pg_process.close()
 60    example_app.stop()
 61
 62
 63class PGUpdater:
 64    def __init__(self, example_app_config: ExampleAppConfig):
 65        self.sensor_config = ExampleApp._get_sensor_config(example_app_config)
 66        self.meas_dist_m = example_app_config.measured_point * APPROX_BASE_STEP_LENGTH_M
 67
 68    def setup(self, win):
 69        pen_blue = et.utils.pg_pen_cycler(0)
 70        pen_orange = et.utils.pg_pen_cycler(1)
 71        brush = et.utils.pg_brush_cycler(0)
 72        brush_dot = et.utils.pg_brush_cycler(1)
 73        symbol_kw = dict(symbol="o", symbolSize=1, symbolBrush=brush, symbolPen="k")
 74        feat_kw_blue = dict(pen=pen_blue, **symbol_kw)
 75        feat_kw_orange = dict(pen=pen_orange)
 76        symbol_dot_kw = dict(symbol="o", symbolSize=10, symbolBrush=brush_dot, symbolPen="k")
 77
 78        # presence plot
 79        self.object_detection_plot = pg.PlotItem()
 80        self.object_detection_plot.setMenuEnabled(False)
 81        self.object_detection_plot.showGrid(x=False, y=True)
 82        self.object_detection_plot.setLabel("left", "Max amplitude")
 83        self.object_detection_plot.setLabel("bottom", "Distance (m)")
 84        self.object_detection_plot.setXRange(self.meas_dist_m - 0.001, self.meas_dist_m + 0.001)
 85        self.presence_curve = self.object_detection_plot.plot(
 86            **dict(pen=pen_blue, **symbol_dot_kw)
 87        )
 88
 89        self.presence_threshold = pg.InfiniteLine(pen=pen_blue, angle=0)
 90        self.object_detection_plot.addItem(self.presence_threshold)
 91        self.presence_threshold.show()
 92
 93        self.smooth_max_presence = et.utils.SmoothMax(tau_decay=10.0)
 94
 95        # sweep and threshold plot
 96        self.time_series_plot = pg.PlotItem()
 97        self.time_series_plot.setMenuEnabled(False)
 98        self.time_series_plot.showGrid(x=True, y=True)
 99        self.time_series_plot.setLabel("left", "Displacement (<font>&mu;</font>m)")
100        self.time_series_plot.setLabel("bottom", "History")
101        self.time_series_curve = self.time_series_plot.plot(**feat_kw_blue)
102
103        self.time_series_plot.setYRange(-1000, 1000)
104        self.time_series_plot.setXRange(0, 1024)
105
106        self.text_item_time_series = pg.TextItem(
107            fill=pg.mkColor(0xFF, 0x7F, 0x0E, 200),
108            anchor=(0.5, 0),
109        )
110        self.text_item_time_series.hide()
111        self.time_series_plot.addItem(self.text_item_time_series)
112
113        sublayout = win.addLayout(row=0, col=0)
114        sublayout.layout.setColumnStretchFactor(1, 5)
115        sublayout.addItem(self.object_detection_plot, row=0, col=0)
116        sublayout.addItem(self.time_series_plot, row=0, col=1)
117
118        self.smooth_lim_time_series = et.utils.SmoothLimits(tau_decay=0.5, tau_grow=0.1)
119
120        self.fft_plot = win.addPlot(col=0, row=1)
121        self.fft_plot.setMenuEnabled(False)
122        self.fft_plot.showGrid(x=True, y=True)
123        self.fft_plot.setLabel("left", "Displacement (<font>&mu;</font>m)")
124        self.fft_plot.setLabel("bottom", "Frequency (Hz)")
125        self.fft_plot.setLogMode(False, True)
126        self.fft_plot.addItem(pg.PlotDataItem())
127        self.fft_curve = [
128            self.fft_plot.plot(**feat_kw_blue),
129            self.fft_plot.plot(**feat_kw_orange),
130            self.fft_plot.plot(**dict(pen=pen_blue, **symbol_dot_kw)),
131        ]
132
133        self.text_item_fft = pg.TextItem(
134            fill=pg.mkColor(0xFF, 0x7F, 0x0E, 200),
135            anchor=(0.5, 0),
136        )
137        self.text_item_fft.hide()
138        self.fft_plot.addItem(self.text_item_fft)
139
140        self.smooth_max_fft = et.utils.SmoothMax()
141
142    def update(self, example_app_result: ExampleAppResult) -> None:
143        # Extra result
144        time_series = example_app_result.processor_extra_result.zm_time_series
145        lp_displacements_threshold = (
146            example_app_result.processor_extra_result.lp_displacements_threshold
147        )
148        amplitude_threshold = example_app_result.processor_extra_result.amplitude_threshold
149
150        # Processor result
151        lp_displacements = example_app_result.lp_displacements
152        lp_displacements_freqs = example_app_result.lp_displacements_freqs
153        max_amplitude = example_app_result.max_sweep_amplitude
154        max_displacement = example_app_result.max_displacement
155        max_displacement_freq = example_app_result.max_displacement_freq
156        time_series_rms = example_app_result.time_series_std
157
158        # Plot object presence metric
159        self.presence_curve.setData([self.meas_dist_m], [max_amplitude])
160        self.presence_threshold.setValue(amplitude_threshold)
161        lim = self.smooth_max_presence.update(max_amplitude)
162        self.object_detection_plot.setYRange(0, max(1000.0, lim))
163
164        # Plot time series
165        if time_series is not None and amplitude_threshold < max_amplitude:
166            assert time_series_rms is not None
167            lim = self.smooth_lim_time_series.update(time_series)
168            self.time_series_plot.setYRange(lim[0], lim[1])
169            self.time_series_plot.setXRange(0, time_series.shape[0])
170
171            self.text_item_time_series.setPos(time_series.size / 2, lim[1] * 0.95)
172            html_format = (
173                '<div style="text-align: center">'
174                '<span style="color: #FFFFFF;font-size:15pt;">'
175                "{}</span></div>".format("RMS : " + str(int(time_series_rms)))
176            )
177            self.text_item_time_series.setHtml(html_format)
178            self.text_item_time_series.show()
179            self.time_series_curve.setData(time_series)
180
181        # Plot spectrum
182        if lp_displacements is not None:
183            assert time_series is not None
184            assert lp_displacements is not None
185
186            self.fft_curve[0].setData(lp_displacements_freqs, lp_displacements)
187            self.fft_curve[1].setData(lp_displacements_freqs, lp_displacements_threshold)
188            lim = self.smooth_max_fft.update(np.max(lp_displacements))
189            self.fft_plot.setYRange(-1, np.log10(lim))
190
191            if max_displacement_freq is not None and max_displacement is not None:
192                self.fft_curve[2].setData([max_displacement_freq], [max_displacement])
193
194                # Place text box centered at the top of the plotting window
195                self.text_item_fft.setPos(max(lp_displacements_freqs) / 2, np.log10(lim) * 0.95)
196                html_format = (
197                    '<div style="text-align: center">'
198                    '<span style="color: #FFFFFF;font-size:15pt;">'
199                    "{}</span></div>".format(
200                        "Frequency: "
201                        + str(int(max_displacement_freq))
202                        + "Hz Displacement: "
203                        + str(int(max_displacement))
204                        + "<font>&mu;</font>m"
205                    )
206                )
207                self.text_item_fft.setHtml(html_format)
208                self.text_item_fft.show()
209            else:
210                self.fft_curve[2].setData([], [])
211                self.text_item_fft.hide()
212
213
214if __name__ == "__main__":
215    main()