Skip to content

Commit

Permalink
Add archive window
Browse files Browse the repository at this point in the history
  • Loading branch information
IhsenBouallegue committed Nov 17, 2023
1 parent 1d0a34f commit 155a804
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 86 deletions.
76 changes: 6 additions & 70 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use web_time::Instant;

use eframe::egui;
use egui::FontFamily::Proportional;
use egui::{Align, Button, CollapsingHeader, Color32, FontFamily, FontId, Key, Layout, Modifiers, Vec2};
use egui::{Align2, ProgressBar, TextStyle::*};
use egui::TextStyle::*;
use egui::{Align, CollapsingHeader, Color32, FontFamily, FontId, Key, Layout, Modifiers, Vec2};

use futures::StreamExt;
use log::*;
Expand All @@ -29,41 +29,20 @@ mod simulation_settings;
mod tabs;
mod theme;
mod top_bar;
pub mod windows; // TODO: make this private (it is public because it has ARCHIVE)

use crate::data_source::*;
use crate::settings::AppSettings;
use crate::simulation::*;
use crate::state::*;

use crate::gui::components::top_menu_bar::*;
use crate::gui::components::top_menu_bar::create_top_menu_bar;
use crate::gui::fc_settings::*;
use crate::gui::simulation_settings::*;
use crate::gui::tabs::*;
use crate::gui::theme::*;
use crate::gui::top_bar::*;

// Log files included with the application. These should probably be fetched
// if necessary to reduce application size.
// TODO: migrate old launches
pub const ARCHIVE: [(&str, Option<&'static str>, Option<&'static str>); 5] = [
("Zülpich #1", None, None),
("Zülpich #2", None, None),
(
"DARE (FC A)",
Some("https://raw.githubusercontent.com/tudsat-rocket/sam/main/archive/dare_launch_a_telem_filtered.json"),
Some("https://raw.githubusercontent.com/tudsat-rocket/sam/main/archive/dare_launch_a_flash_filtered.json"),
),
(
"DARE (FC B)",
Some("https://raw.githubusercontent.com/tudsat-rocket/sam/main/archive/dare_launch_b_telem_filtered.json"),
Some("https://raw.githubusercontent.com/tudsat-rocket/sam/main/archive/dare_launch_b_flash_filtered.json"),
),
(
"EuRoC 2023 (ÆSIR Signý)",
Some("https://raw.githubusercontent.com/tudsat-rocket/sam/main/archive/euroc_2023_telem_filtered.json"),
Some("https://raw.githubusercontent.com/tudsat-rocket/sam/main/archive/euroc_2023_flash_filtered.json"),
),
];
use crate::gui::windows::archive::open_archive_window;

#[derive(Debug)]
enum ArchiveLoadProgress {
Expand Down Expand Up @@ -399,51 +378,8 @@ impl Sam {

// A window to open archived logs directly in the application
let mut archive_open = self.archive_window_open; // necessary to avoid mutably borrowing self
egui::Window::new("Flight Archive")
.open(&mut archive_open)
.min_width(300.0)
.anchor(Align2::CENTER_CENTER, [0.0, 0.0])
.resizable(false)
.collapsible(false)
.show(ctx, |ui| {
ui.add_space(10.0);

for (i, (title, telem, flash)) in ARCHIVE.iter().enumerate() {
if i != 0 {
ui.separator();
}

ui.horizontal(|ui| {
ui.label(*title);
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
if ui.add_enabled(flash.is_some(), Button::new("🖴 Flash")).clicked() {
self.open_archive_log(flash.unwrap());
}

if ui.add_enabled(telem.is_some(), Button::new("📡 Telemetry")).clicked() {
self.open_archive_log(telem.unwrap());
}
});
});
}
open_archive_window(ctx, &mut archive_open, self);

ui.add_space(10.0);
ui.horizontal(|ui| {
ui.add_visible_ui(self.archive_progress.is_some(), |ui| {
let (done, total) = self.archive_progress.unwrap_or((0, 0));
let f = (total > 0).then(|| done as f32 / total as f32).unwrap_or(0.0);
let text = format!(
"{:.2}MiB / {:.2}MiB",
done as f32 / (1024.0 * 1024.0),
total as f32 / (1024.0 * 1024.0)
);
ui.add_sized([ui.available_width(), 20.0], ProgressBar::new(f).text(text));
});
});
ui.add_space(10.0);

ui.checkbox(&mut self.replay_logs, "Replay logs");
});
self.archive_window_open = archive_open;

if self.data_source.simulation_settings().is_some() {
Expand Down
4 changes: 1 addition & 3 deletions src/gui/simulation_settings.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use egui::{DragValue, InnerResponse, Ui};

use crate::gui::windows::archive::ARCHIVE;
use crate::simulation::SimulationSettings;

use super::ARCHIVE;

pub trait SimulationSettingsUiExt {
fn ui(&mut self, ui: &mut Ui) -> InnerResponse<()>;
}
Expand Down
74 changes: 74 additions & 0 deletions src/gui/windows/archive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use crate::gui::Sam;
use eframe::egui;
use egui::{Align, Align2, Button, Layout, ProgressBar};

// Log files included with the application. These should probably be fetched
// if necessary to reduce application size.
// TODO: migrate old launches
pub const ARCHIVE: [(&str, Option<&'static str>, Option<&'static str>); 5] = [
("Zülpich #1", None, None),
("Zülpich #2", None, None),
(
"DARE (FC A)",
Some("https://raw.githubusercontent.com/tudsat-rocket/sam/main/archive/dare_launch_a_telem_filtered.json"),
Some("https://raw.githubusercontent.com/tudsat-rocket/sam/main/archive/dare_launch_a_flash_filtered.json"),
),
(
"DARE (FC B)",
Some("https://raw.githubusercontent.com/tudsat-rocket/sam/main/archive/dare_launch_b_telem_filtered.json"),
Some("https://raw.githubusercontent.com/tudsat-rocket/sam/main/archive/dare_launch_b_flash_filtered.json"),
),
(
"EuRoC 2023 (ÆSIR Signý)",
Some("https://raw.githubusercontent.com/tudsat-rocket/sam/main/archive/euroc_2023_telem_filtered.json"),
Some("https://raw.githubusercontent.com/tudsat-rocket/sam/main/archive/euroc_2023_flash_filtered.json"),
),
];

pub fn open_archive_window(ctx: &egui::Context, archive_open: &mut bool, sam: &mut Sam) {
egui::Window::new("Flight Archive")
.open(archive_open)
.min_width(300.0)
.anchor(Align2::CENTER_CENTER, [0.0, 0.0])
.resizable(false)
.collapsible(false)
.show(ctx, |ui| {
ui.add_space(10.0);

for (i, (title, telem, flash)) in ARCHIVE.iter().enumerate() {
if i != 0 {
ui.separator();
}

ui.horizontal(|ui| {
ui.label(*title);
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
if ui.add_enabled(flash.is_some(), Button::new("🖴 Flash")).clicked() {
sam.open_archive_log(flash.unwrap());
}

if ui.add_enabled(telem.is_some(), Button::new("📡 Telemetry")).clicked() {
sam.open_archive_log(telem.unwrap());
}
});
});
}

ui.add_space(10.0);
ui.horizontal(|ui| {
ui.add_visible_ui(sam.archive_progress.is_some(), |ui| {
let (done, total) = sam.archive_progress.unwrap_or((0, 0));
let f = (total > 0).then(|| done as f32 / total as f32).unwrap_or(0.0);
let text = format!(
"{:.2}MiB / {:.2}MiB",
done as f32 / (1024.0 * 1024.0),
total as f32 / (1024.0 * 1024.0)
);
ui.add_sized([ui.available_width(), 20.0], ProgressBar::new(f).text(text));
});
});
ui.add_space(10.0);

ui.checkbox(&mut sam.replay_logs, "Replay logs");
});
}
1 change: 1 addition & 0 deletions src/gui/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod archive;
42 changes: 29 additions & 13 deletions src/simulation.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use std::collections::VecDeque;

use rand::SeedableRng;
use nalgebra::{UnitQuaternion, Vector3};
use rand::distributions::Distribution;
use rand::RngCore;
use rand::SeedableRng;

use mithril::settings::*;
use mithril::state_estimation::*;
use mithril::telemetry::*;

use crate::state::VehicleState;

use crate::gui::ARCHIVE;
use crate::gui::windows::archive::ARCHIVE;

#[cfg(any(target_os="windows", target_os="android"))]
#[cfg(any(target_os = "windows", target_os = "android"))]
type Rng = rand::rngs::StdRng;
#[cfg(not(any(target_os="windows", target_os="android")))]
#[cfg(not(any(target_os = "windows", target_os = "android")))]
type Rng = rand::rngs::SmallRng;

const GRAVITY: f32 = 9.80665;
Expand Down Expand Up @@ -146,7 +146,8 @@ impl SimulationState {

// TODO: do this nicer (separate thread/task, progress, caching, support WASM)
#[cfg(not(target_arch = "wasm32"))]
let mut remaining_replication_states = settings.replication_log_index
let mut remaining_replication_states = settings
.replication_log_index
.map(|i| ARCHIVE.get(i))
.flatten()
.map(|(_, _, flash)| *flash)
Expand All @@ -162,7 +163,8 @@ impl SimulationState {
remaining_replication_states.pop_front();
}

let (altitude, latitude, longitude) = remaining_replication_states.front()
let (altitude, latitude, longitude) = remaining_replication_states
.front()
.map(|vs| (vs.altitude_gps.unwrap(), vs.latitude.unwrap() as f64, vs.longitude.unwrap() as f64))
.unwrap_or((settings.altitude_ground, settings.launch_latitude, settings.launch_longitude));

Expand All @@ -171,7 +173,8 @@ impl SimulationState {
remaining_replication_states.pop_front();
}

let mut state_estimator = StateEstimator::new(1000.0 / (SIMULATION_TICK_MS as f32), settings.fc_settings.clone());
let mut state_estimator =
StateEstimator::new(1000.0 / (SIMULATION_TICK_MS as f32), settings.fc_settings.clone());

let mut time = 0;
let mut mode = FlightMode::Idle;
Expand Down Expand Up @@ -217,7 +220,7 @@ impl SimulationState {
magnetometer,
altitude_baro,

remaining_replication_states
remaining_replication_states,
}
}

Expand Down Expand Up @@ -318,12 +321,25 @@ impl SimulationState {
// TODO: linear interpolation is probably not the best choice here.
// On the other hand, simplying applying more noise here isn't great either
let delta_time = next.time - self.time;
self.gyroscope = Some(self.gyroscope.unwrap() + (next.gyroscope.unwrap() - self.gyroscope.unwrap()) / delta_time as f32);
self.accelerometer1 = Some(self.accelerometer1.unwrap() + (next.accelerometer1.unwrap() - self.accelerometer1.unwrap()) / delta_time as f32);
self.accelerometer2 = Some(self.accelerometer2.unwrap() + (next.accelerometer2.unwrap() - self.accelerometer2.unwrap()) / delta_time as f32);
self.magnetometer = Some(self.magnetometer.unwrap() + (next.magnetometer.unwrap() - self.magnetometer.unwrap()) / delta_time as f32);
self.gyroscope =
Some(self.gyroscope.unwrap() + (next.gyroscope.unwrap() - self.gyroscope.unwrap()) / delta_time as f32);
self.accelerometer1 = Some(
self.accelerometer1.unwrap()
+ (next.accelerometer1.unwrap() - self.accelerometer1.unwrap()) / delta_time as f32,
);
self.accelerometer2 = Some(
self.accelerometer2.unwrap()
+ (next.accelerometer2.unwrap() - self.accelerometer2.unwrap()) / delta_time as f32,
);
self.magnetometer = Some(
self.magnetometer.unwrap()
+ (next.magnetometer.unwrap() - self.magnetometer.unwrap()) / delta_time as f32,
);
let altitude_baro = next.pressure_baro.map(|p| 44307.694 * (1.0 - (p / 1013.25).powf(0.190284)));
self.altitude_baro = Some(self.altitude_baro.unwrap() + (altitude_baro.unwrap() - self.altitude_baro.unwrap()) / delta_time as f32);
self.altitude_baro = Some(
self.altitude_baro.unwrap()
+ (altitude_baro.unwrap() - self.altitude_baro.unwrap()) / delta_time as f32,
);
}

false
Expand Down

0 comments on commit 155a804

Please sign in to comment.