mirror of
https://github.com/hubaldv/bioz-host-rs.git
synced 2025-12-06 05:11:17 +00:00
Added bode plots to GUI.
This commit is contained in:
@@ -11,17 +11,18 @@ use std::sync::{Arc, Mutex};
|
||||
use crate::icd;
|
||||
use crate::client::WorkbookClient;
|
||||
|
||||
use crate::plot::TimeSeriesPlot;
|
||||
use crate::plot::{TimeSeriesPlot, BodePlot};
|
||||
|
||||
use crate::signals::SingleFrequencySignal;
|
||||
use crate::signals::StartStopSignal;
|
||||
|
||||
pub async fn communicate_with_hardware(
|
||||
mut run_impedancemeter_rx: Receiver<SingleFrequencySignal>,
|
||||
run_impedancemeter_tx: Sender<SingleFrequencySignal>,
|
||||
mut run_impedancemeter_rx: Receiver<StartStopSignal>,
|
||||
run_impedancemeter_tx: Sender<StartStopSignal>,
|
||||
magnitude: Arc<Mutex<f32>>,
|
||||
phase: Arc<Mutex<f32>>,
|
||||
magnitude_series: Arc<Mutex<TimeSeriesPlot>>,
|
||||
phase_series: Arc<Mutex<TimeSeriesPlot>>,
|
||||
bode_series: Arc<Mutex<BodePlot>>,
|
||||
connected: Arc<AtomicBool>,
|
||||
data_frequency: Arc<AtomicF32>,
|
||||
) {
|
||||
@@ -37,7 +38,7 @@ pub async fn communicate_with_hardware(
|
||||
|
||||
#[derive(Default)]
|
||||
struct Settings {
|
||||
frequency: Option<SingleFrequencySignal>,
|
||||
frequency: Option<StartStopSignal>,
|
||||
}
|
||||
let mut settings = Settings::default();
|
||||
|
||||
@@ -58,19 +59,20 @@ pub async fn communicate_with_hardware(
|
||||
}
|
||||
};
|
||||
|
||||
let mut sub = workbook_client
|
||||
// Subscribe to SingleImpedanceOutputTopic
|
||||
let mut single_impedance_sub = workbook_client
|
||||
.client
|
||||
.subscribe_multi::<icd::ImpedanceOutputTopic>(8)
|
||||
.subscribe_multi::<icd::SingleImpedanceOutputTopic>(8)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let data = (magnitude_series.clone(), phase_series.clone(), magnitude.clone(), phase.clone());
|
||||
let data_counter_clone = data_counter_clone.clone();
|
||||
let data_counter_clone_single = data_counter_clone.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
while let Ok(val) = sub.recv().await {
|
||||
while let Ok(val) = single_impedance_sub.recv().await {
|
||||
let mut mag_plot = data.0.lock().unwrap();
|
||||
let mut phase_plot = data.1.lock().unwrap();
|
||||
let mut phase_plot = data.1.lock().unwrap();
|
||||
let mut mag_val = data.2.lock().unwrap();
|
||||
let mut phase_val = data.3.lock().unwrap();
|
||||
|
||||
@@ -80,29 +82,59 @@ pub async fn communicate_with_hardware(
|
||||
mag_plot.add(val.magnitude as f64);
|
||||
phase_plot.add(val.phase as f64);
|
||||
|
||||
data_counter_clone.fetch_add(1, Ordering::Relaxed);
|
||||
data_counter_clone_single.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
info!("ImpedanceTopic subscription ended.");
|
||||
info!("SingleImpedanceOutputTopic subscription ended.");
|
||||
});
|
||||
|
||||
// Subscribe to MultiImpedanceOutputTopic28
|
||||
let mut multi_impedance_28_sub = workbook_client
|
||||
.client
|
||||
.subscribe_multi::<icd::MultiImpedanceOutputTopic28>(8)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let data = bode_series.clone();
|
||||
let data_counter_clone_multi = data_counter_clone.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
while let Ok(val) = multi_impedance_28_sub.recv().await {
|
||||
let mut bode_plot = data.lock().unwrap();
|
||||
|
||||
let magnitudes = val.magnitudes.into_iter().collect::<Vec<f32>>();
|
||||
bode_plot.update_magnitudes(magnitudes);
|
||||
let phases = val.phases.into_iter().collect::<Vec<f32>>();
|
||||
bode_plot.update_phases(phases);
|
||||
|
||||
data_counter_clone_multi.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
});
|
||||
|
||||
loop {
|
||||
select! {
|
||||
Some(frequency) = run_impedancemeter_rx.recv() => {
|
||||
match frequency {
|
||||
SingleFrequencySignal::Start(freq, dft_num) => {
|
||||
if let Err(e) = workbook_client.start_impedancemeter(freq, dft_num).await {
|
||||
StartStopSignal::StartSingle(freq, dft_num) => {
|
||||
if let Err(e) = workbook_client.start_impedancemeter_single(freq, dft_num).await {
|
||||
error!("Failed to start impedancemeter: {:?}", e);
|
||||
} else {
|
||||
settings.frequency = Some(SingleFrequencySignal::Start(freq, dft_num));
|
||||
settings.frequency = Some(StartStopSignal::StartSingle(freq, dft_num));
|
||||
info!("Impedancemeter started at frequency: {}", freq);
|
||||
}
|
||||
},
|
||||
SingleFrequencySignal::Stop => {
|
||||
StartStopSignal::StartMulti(dft_num, num_points) => {
|
||||
if let Err(e) = workbook_client.start_impedancemeter_multi(dft_num, num_points).await {
|
||||
error!("Failed to start multi-point impedancemeter: {:?}", e);
|
||||
} else {
|
||||
settings.frequency = Some(StartStopSignal::StartMulti(dft_num, num_points));
|
||||
info!("Multi-point Impedancemeter started.");
|
||||
}
|
||||
},
|
||||
StartStopSignal::Stop => {
|
||||
if let Err(e) = workbook_client.stop_impedancemeter().await {
|
||||
error!("Failed to stop impedancemeter: {:?}", e);
|
||||
} else {
|
||||
settings.frequency = Some(SingleFrequencySignal::Stop);
|
||||
settings.frequency = Some(StartStopSignal::Stop);
|
||||
info!("Impedancemeter stopped.");
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user