mirror of
https://github.com/hubaldv/bioz-host-rs.git
synced 2026-03-10 01:20:31 +00:00
Included bode plot measurements, including different number of points.
This commit is contained in:
106
src/app.rs
106
src/app.rs
@@ -9,14 +9,15 @@ use atomic_float::AtomicF32;
|
||||
use tokio::{sync::mpsc::{Sender}};
|
||||
|
||||
use eframe::egui::{self, Button, CollapsingHeader, Color32, ComboBox, DragValue, Key, Label, Layout, Modifiers};
|
||||
use egui_plot::{Corner, GridInput, GridMark, Legend, Line, Plot, PlotBounds, PlotPoint, PlotPoints, Points};
|
||||
use egui_plot::{Corner, GridInput, GridMark, Legend, Line, Plot, PlotPoint, Points};
|
||||
use egui_dock::{DockArea, DockState, Style};
|
||||
use egui_extras::{TableBuilder, Column};
|
||||
|
||||
use crate::plot::{TimeSeriesPlot, BodePlot};
|
||||
|
||||
use crate::signals::StartStopSignal;
|
||||
|
||||
use crate::icd::{IcdDftNum, NumberOfPoints};
|
||||
use crate::icd::{IcdDftNum, MeasurementPointSet};
|
||||
|
||||
const DFTNUM_VARIANTS: [IcdDftNum; 13] = [
|
||||
IcdDftNum::Num4, IcdDftNum::Num8, IcdDftNum::Num16, IcdDftNum::Num32,
|
||||
@@ -25,6 +26,11 @@ const DFTNUM_VARIANTS: [IcdDftNum; 13] = [
|
||||
IcdDftNum::Num8192, IcdDftNum::Num16384,
|
||||
];
|
||||
|
||||
const MEASUREMENT_POINTS_VARIANTS: [MeasurementPointSet; 2] = [
|
||||
MeasurementPointSet::Eight,
|
||||
MeasurementPointSet::Eighteen,
|
||||
];
|
||||
|
||||
#[derive(Clone, Copy,Debug, PartialEq, Eq)]
|
||||
enum TabActive {
|
||||
Single,
|
||||
@@ -47,7 +53,9 @@ pub struct App {
|
||||
pub data_frequency: Arc<AtomicF32>,
|
||||
pub single_frequency: Arc<Mutex<u32>>,
|
||||
pub dft_num: Arc<Mutex<IcdDftNum>>,
|
||||
pub measurement_points: Arc<Mutex<MeasurementPointSet>>,
|
||||
pub periods_per_dft: Arc<Mutex<Option<f32>>>,
|
||||
pub periods_per_dft_multi: Arc<Mutex<(Vec<f32>, Option<Vec<f32>>)>>,
|
||||
}
|
||||
|
||||
struct TabViewer {
|
||||
@@ -59,7 +67,9 @@ struct TabViewer {
|
||||
on: Arc<Mutex<bool>>,
|
||||
single_frequency: Arc<Mutex<u32>>,
|
||||
dft_num: Arc<Mutex<IcdDftNum>>,
|
||||
measurement_points: Arc<Mutex<MeasurementPointSet>>,
|
||||
periods_per_dft: Arc<Mutex<Option<f32>>>,
|
||||
periods_per_dft_multi: Arc<Mutex<(Vec<f32>, Option<Vec<f32>>)>>,
|
||||
show_settings: bool,
|
||||
show_settings_toggle: Option<bool>,
|
||||
}
|
||||
@@ -171,21 +181,76 @@ impl TabViewer {
|
||||
if let Ok(on) = self.on.lock() {
|
||||
ui.add_enabled_ui(!*on, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("ADC samples per DFT:");
|
||||
let mut dft_num = self.dft_num.lock().unwrap();
|
||||
let mut index = DFTNUM_VARIANTS.iter().position(|&x| x == *dft_num).unwrap_or(0);
|
||||
ComboBox::from_id_salt("Dftnum")
|
||||
ui.label("Measurement Points:");
|
||||
let mut measurement_points = self.measurement_points.lock().unwrap();
|
||||
let mut index = MEASUREMENT_POINTS_VARIANTS.iter().position(|&x| x == *measurement_points).unwrap_or(0);
|
||||
ComboBox::from_id_salt("MeasurementPoints")
|
||||
.width(75.0)
|
||||
.show_index(ui, &mut index, DFTNUM_VARIANTS.len(), |i| {
|
||||
format!("{}", 1 << (2 + i)) // 2^2 = 4, 2^3 = 8, ..., 2^14 = 16384
|
||||
.show_index(ui, &mut index, MEASUREMENT_POINTS_VARIANTS.len(), |i| {
|
||||
format!("{:?}", MEASUREMENT_POINTS_VARIANTS[i].len())
|
||||
});
|
||||
let new_value = DFTNUM_VARIANTS[index];
|
||||
if *dft_num != new_value {
|
||||
*dft_num = new_value;
|
||||
info!("DFTNUM setting changed!");
|
||||
};
|
||||
let new_value = MEASUREMENT_POINTS_VARIANTS[index];
|
||||
if *measurement_points != new_value {
|
||||
*measurement_points = new_value;
|
||||
info!("Measurement Points setting changed!");
|
||||
}
|
||||
});
|
||||
});
|
||||
ui.add_enabled_ui(*on, |ui| {
|
||||
let (freq, periods_per_dft_vec) = self.periods_per_dft_multi.lock().unwrap().clone();
|
||||
|
||||
fn format_frequency(freq: f32) -> String {
|
||||
if freq >= 1_000.0 {
|
||||
// kHz
|
||||
if freq % 1_000.0 == 0.0 {
|
||||
format!("{}k", (freq / 1_000.0) as u64)
|
||||
} else {
|
||||
format!("{:.1}k", freq / 1_000.0)
|
||||
}
|
||||
} else {
|
||||
// Hz
|
||||
if freq % 1.0 == 0.0 {
|
||||
format!("{}", freq as u64)
|
||||
} else {
|
||||
format!("{:.1}", freq)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TableBuilder::new(ui)
|
||||
.column(Column::auto())
|
||||
.columns(Column::remainder().at_most(30.0), freq.len())
|
||||
.header(10.0, |mut header| {
|
||||
header.col(|ui| {
|
||||
ui.label("Frequency [Hz]:");
|
||||
});
|
||||
for freq in &freq {
|
||||
header.col(|ui| {
|
||||
ui.label(format_frequency(*freq));
|
||||
});
|
||||
}
|
||||
})
|
||||
.body(|mut body| {
|
||||
body.row(5.0, |mut row| {
|
||||
row.col(|ui| {
|
||||
ui.label("Periods per DFT:");
|
||||
});
|
||||
if let Some(periods) = periods_per_dft_vec {
|
||||
for period in &periods {
|
||||
row.col(|ui| {
|
||||
ui.label(format!("{:.1}", period));
|
||||
});
|
||||
}
|
||||
} else {
|
||||
for _ in &freq {
|
||||
row.col(|ui| {
|
||||
ui.label("N/A");
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -355,7 +420,9 @@ impl App {
|
||||
let bode_plot = Arc::new(Mutex::new(BodePlot::new()));
|
||||
let single_frequency = Arc::new(Mutex::new(50000));
|
||||
let dft_num = Arc::new(Mutex::new(IcdDftNum::Num2048));
|
||||
let measurement_points = Arc::new(Mutex::new(MeasurementPointSet::Eighteen));
|
||||
let periods_per_dft = Arc::new(Mutex::new(None));
|
||||
let periods_per_dft_multi = Arc::new(Mutex::new((MeasurementPointSet::Eighteen.values().to_vec(), None)));
|
||||
let on = Arc::new(Mutex::new(true));
|
||||
let tab_active = TabActive::Single;
|
||||
|
||||
@@ -368,7 +435,9 @@ impl App {
|
||||
bode_plot: bode_plot.clone(),
|
||||
single_frequency: single_frequency.clone(),
|
||||
dft_num: dft_num.clone(),
|
||||
measurement_points: measurement_points.clone(),
|
||||
periods_per_dft: periods_per_dft.clone(),
|
||||
periods_per_dft_multi: periods_per_dft_multi.clone(),
|
||||
on: on.clone(),
|
||||
show_settings: false,
|
||||
show_settings_toggle: None,
|
||||
@@ -390,13 +459,15 @@ impl App {
|
||||
data_frequency: Arc::new(AtomicF32::new(0.0)),
|
||||
single_frequency,
|
||||
dft_num,
|
||||
measurement_points,
|
||||
periods_per_dft,
|
||||
periods_per_dft_multi,
|
||||
};
|
||||
|
||||
// For testing purposes, populate the Bode plot with a sample low-pass filter response
|
||||
let fc = 1000.0; // cutoff frequency in Hz
|
||||
|
||||
let freqs = NumberOfPoints::TwentyEight.values().to_vec();
|
||||
let freqs = MeasurementPointSet::Eighteen.values().to_vec();
|
||||
let magnitudes = freqs.iter()
|
||||
.map(|&f| {
|
||||
1.0 / (1.0 + (f / fc).powi(2)).sqrt()
|
||||
@@ -408,9 +479,8 @@ impl App {
|
||||
})
|
||||
.collect::<Vec<f32>>();
|
||||
|
||||
app.bode_plot.lock().unwrap().update_magnitudes(magnitudes);
|
||||
app.bode_plot.lock().unwrap().update_phases(phases);
|
||||
|
||||
app.bode_plot.lock().unwrap().update_magnitudes(MeasurementPointSet::Eighteen, magnitudes);
|
||||
app.bode_plot.lock().unwrap().update_phases(MeasurementPointSet::Eighteen, phases);
|
||||
|
||||
app.update_start_stop();
|
||||
app
|
||||
@@ -424,7 +494,7 @@ impl App {
|
||||
}
|
||||
},
|
||||
(TabActive::Multi, true) => {
|
||||
if let Err(e) = self.run_impedancemeter_tx.try_send(StartStopSignal::StartMulti(*self.dft_num.lock().unwrap(), NumberOfPoints::TwentyEight)) {
|
||||
if let Err(e) = self.run_impedancemeter_tx.try_send(StartStopSignal::StartMulti(*self.measurement_points.lock().unwrap())) {
|
||||
eprintln!("Failed to send start command: {:?}", e);
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user