Skip to content

Commit

Permalink
Reuse VehicleState struct from firmware after Embassy rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
KoffeinFlummi committed Nov 24, 2023
1 parent d7ce654 commit a324d19
Show file tree
Hide file tree
Showing 13 changed files with 462 additions and 281 deletions.
467 changes: 424 additions & 43 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use mithril::settings::*;
use mithril::telemetry::*;

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

pub mod log_file;
pub mod serial;
Expand Down
1 change: 0 additions & 1 deletion src/data_source/log_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use mithril::settings::*;
use mithril::telemetry::*;

use crate::data_source::DataSource;
use crate::state::*;

pub struct LogFileDataSource {
path: Option<PathBuf>,
Expand Down
3 changes: 1 addition & 2 deletions src/data_source/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use mithril::telemetry::*;

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

pub const BAUD_RATE: u32 = 115_200;
pub const MESSAGE_TIMEOUT: Duration = Duration::from_millis(500);
Expand Down Expand Up @@ -388,7 +387,7 @@ impl DataSource for SerialDataSource {
.vehicle_states
.iter()
.rev()
.find_map(|(_t, msg)| msg.telemetry_data_rate)
.find_map(|(_t, msg)| msg.data_rate)
.unwrap_or(TelemetryDataRate::Low);
let expected = match telemetry_data_rate {
// TODO: don't hardcode these?
Expand Down
1 change: 0 additions & 1 deletion src/data_source/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use mithril::telemetry::*;

use crate::data_source::DataSource;
use crate::simulation::*;
use crate::state::VehicleState;

#[derive(Default)]
pub struct SimulationDataSource {
Expand Down
2 changes: 1 addition & 1 deletion src/gui/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl MapCache {
let new_data = data_source.vehicle_states()
.skip(keep_first)
.filter(|(_t, vs)| vs.latitude.is_some() && vs.longitude.is_some())
.map(|(_t, vs)| (vs.latitude.unwrap(), vs.longitude.unwrap(), vs.altitude_gps.unwrap_or(0.0)))
.map(|(_t, vs)| (vs.latitude.unwrap(), vs.longitude.unwrap(), vs.altitude_gps_asl.unwrap_or(0.0)))
.map(|(lat, lng, alt)| (lat as f64, lng as f64, alt as f64));

if keep_first > 0 {
Expand Down
17 changes: 8 additions & 9 deletions src/gui/panels/header.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use egui::{CollapsingHeader, Vec2, Layout, Align};
use mithril::telemetry::Command;
use mithril::telemetry::*;

use crate::gui::top_bar::*;
use crate::data_source::DataSource;
use crate::state::VehicleState;

pub struct HeaderPanel {}

Expand All @@ -25,9 +24,9 @@ impl HeaderPanel {
let rssi = Self::current(data_source, |vs| vs.gcs_lora_rssi.map(|x| x as f32 / -2.0));
let mode = Self::current(data_source, |vs| vs.mode).map(|s| format!("{:?}", s));

let alt_ground = Self::current(data_source, |vs| vs.altitude_ground).unwrap_or(0.0);
let alt_agl = Self::current(data_source, |vs| vs.altitude.map(|a| a - alt_ground));
let alt_max = Self::current(data_source, |vs| vs.altitude_max.map(|a| a - alt_ground));
let alt_ground = Self::current(data_source, |vs| vs.altitude_ground_asl).unwrap_or(0.0);
let alt_agl = Self::current(data_source, |vs| vs.altitude_asl.map(|a| a - alt_ground));
let apogee_agl = Self::current(data_source, |vs| vs.apogee_asl.map(|a| a - alt_ground));
let vertical_accel = Self::current(data_source, |vs| vs.vertical_accel_filtered);

let last_gps = data_source
Expand Down Expand Up @@ -56,7 +55,7 @@ impl HeaderPanel {
ui.set_width(ui.available_width() / 2.0);
ui.add_space(spacing);
ui.nominal_value("📈", "Altitude (AGL) [m]", alt_agl, 1, -1.0, 10000.0);
ui.nominal_value("📈", "Max Alt. (AGL) [m]", alt_max, 1, -1.0, 10000.0);
ui.nominal_value("📈", "Apogee (AGL) [m]", apogee_agl, 1, -1.0, 10000.0);
ui.nominal_value("☁", "Baro. Alt. (ASL) [m]", Self::current(data_source, |vs| vs.altitude_baro), 1, -100.0, 10000.0);
ui.nominal_value("⏱", "Vertical Speed [m/s]", Self::current(data_source, |vs| vs.vertical_speed), 2, -1.0, 1.0);
ui.nominal_value("⬆", "Vertical Accel. [m/s²]", vertical_accel, 1, -1.0, 1.0);
Expand All @@ -68,7 +67,7 @@ impl HeaderPanel {
ui.telemetry_value("🌍", "GPS Status", gps_status);
ui.nominal_value("📶", "# Sats", Self::current(data_source, |vs| vs.num_satellites.map(|n| n as f32)), 0, 5.0, 99.0);
ui.nominal_value("🎯", "HDOP", hdop, 2, 0.0, 5.0);
ui.nominal_value("📡", "GPS Alt. (ASL) [m]", Self::current(data_source, |vs| vs.altitude_gps), 1, -100.0, 10000.0);
ui.nominal_value("📡", "GPS Alt. (ASL) [m]", Self::current(data_source, |vs| vs.altitude_gps_asl), 1, -100.0, 10000.0);
ui.telemetry_value("🌐", "Coords", coords);
});
}
Expand All @@ -87,7 +86,7 @@ impl HeaderPanel {

ui.separator();

let current_data_rate = Self::current(data_source, |vs| vs.telemetry_data_rate).unwrap_or_default();
let current_data_rate = Self::current(data_source, |vs| vs.data_rate).unwrap_or_default();
let current_transmit_power = Self::current(data_source, |vs| vs.transmit_power).unwrap_or_default();

if vertical {
Expand Down Expand Up @@ -118,7 +117,7 @@ impl HeaderPanel {
ui.command_button("⟲ Reboot", Command::Reboot, data_source);
ui.command_button("🗑 Erase Flash", Command::EraseFlash, data_source);
ui.flash_bar(ui.available_width() * 0.6, Self::current(data_source, |vs| vs.flash_pointer));
ui.battery_bar(ui.available_width(), Self::current(data_source, |vs| vs.battery_voltage));
ui.battery_bar(ui.available_width(), Self::current(data_source, |vs| vs.battery_voltage.map(|v| v as f32 / 1000.0)));
});

ui.separator();
Expand Down
1 change: 0 additions & 1 deletion src/gui/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use eframe::egui::PointerButton;
use egui_plot::{AxisBools, Corner, Legend, Line, LineStyle, PlotBounds, VLine};

use crate::gui::*;
use crate::state::*;
use crate::telemetry_ext::*;

fn plot_time(x: &Instant, data_source: &dyn DataSource) -> f64 {
Expand Down
29 changes: 11 additions & 18 deletions src/gui/tabs/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ impl PlotTab {
.line("True Vario [m/s]", B1, |vs| vs.true_vertical_speed);

let altitude_plot = PlotState::new("Altitude (ASL)", (None, None), shared_plot.clone())
.line("Altitude (Ground) [m]", BR, |vs| vs.altitude_ground)
.line("Altitude (Ground) [m]", BR, |vs| vs.altitude_ground_asl)
.line("Altitude (Baro) [m]", B1, |vs| vs.altitude_baro)
.line("Altitude [m]", B, |vs| vs.altitude)
.line("Altitude (GPS) [m]", G, |vs| vs.altitude_gps);
.line("Altitude [m]", B, |vs| vs.altitude_asl)
.line("Altitude (GPS) [m]", G, |vs| vs.altitude_gps_asl);

let gyroscope_plot = PlotState::new("Gyroscope", (Some(-10.0), Some(10.0)), shared_plot.clone())
.line("Gyro (X) [°/s]", R, |vs| vs.gyroscope.map(|a| a.x))
Expand All @@ -129,32 +129,25 @@ impl PlotTab {
.line("Mag (Z) [µT]", B, |vs| vs.magnetometer.map(|a| a.z));

let barometer_plot = PlotState::new("Pressures", (None, None), shared_plot.clone())
.line("Barometer [bar]", C, |vs| vs.pressure_baro.map(|p| p / 1000.0))
.line("Drogue Cartridge [bar]", R1, |vs| vs.drogue_cartridge_pressure)
.line("Drogue Chamber [bar]", G1, |vs| vs.drogue_chamber_pressure)
.line("Main Cartridge [bar]", R, |vs| vs.main_cartridge_pressure)
.line("Main Chamber [bar]", G, |vs| vs.main_chamber_pressure);
.line("Barometer [bar]", C, |vs| vs.pressure_baro.map(|p| p / 1000.0));

let temperature_plot = PlotState::new("Temperatures", (Some(25.0), Some(35.0)), shared_plot.clone())
.line("Baro. Temp. [°C]", C, |vs| vs.temperature_baro)
.line("Core Temp. [°C]", B, |vs| vs.temperature_core);
.line("Baro. Temp. [°C]", C, |vs| vs.temperature_baro);

let power_plot = PlotState::new("Power", (Some(0.0), Some(9.0)), shared_plot.clone())
.line("Arm Voltage [V]", O, |vs| vs.arm_voltage)
.line("Battery Voltage [V]", G, |vs| vs.battery_voltage)
.line("Current [A]", O1, |vs| vs.current)
.line("Charge Voltage [V]", B, |vs| vs.charge_voltage)
.line("Breakwire Open?", R, |vs| vs.breakwire_open.map(|bw| bw.then(|| 1.0).unwrap_or(0.0)));
.line("Arm Voltage [V]", O, |vs| vs.arm_voltage.map(|v| (v as f32) / 1000.0))
.line("Battery Voltage [V]", G, |vs| vs.battery_voltage.map(|v| (v as f32) / 1000.0))
.line("Current [A]", O1, |vs| vs.current.map(|v| (v as f32) / 1000.0))
.line("Charge Voltage [V]", B, |vs| vs.charge_voltage.map(|v| (v as f32) / 1000.0));

let runtime_plot = PlotState::new("Runtime", (Some(0.0), Some(100.0)), shared_plot.clone())
.line("CPU Util. [%]", O, |vs| vs.cpu_utilization.map(|u| u as f32))
.line("Heap Util. [%]", G, |vs| vs.heap_utilization.map(|u| u as f32));
.line("CPU Util. [%]", O, |vs| vs.cpu_utilization.map(|u| u as f32));

let signal_plot = PlotState::new("Signal", (Some(-100.0), Some(10.0)), shared_plot.clone())
.line("GCS RSSI [dBm]", B, |vs| vs.gcs_lora_rssi.map(|x| x as f32 / -2.0))
.line("GCS Signal RSSI [dBm]", B1, |vs| vs.gcs_lora_rssi_signal.map(|x| x as f32 / -2.0))
.line("GCS SNR [dB]", C, |vs| vs.gcs_lora_snr.map(|x| x as f32 / 4.0))
.line("Vehicle RSSI [dBm]", P, |vs| vs.vehicle_lora_rssi.map(|x| x as f32 / -2.0))
.line("Vehicle RSSI [dBm]", P, |vs| vs.lora_rssi.map(|x| x as f32 / -2.0))
.line("HDOP", R, |vs| vs.hdop.map(|x| x as f32 / 100.0))
.line("# Satellites", G, |vs| vs.num_satellites.map(|x| x as f32));

Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ mod file;
mod gui;
pub mod settings;
mod simulation;
mod state;
mod telemetry_ext;

pub use crate::gui::*;
Expand Down
6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ use crc::{Crc, CRC_16_IBM_SDLC};
use log::*;

use mithril::telemetry::*;
use state::VehicleState;

mod data_source;
mod file;
mod gui;
mod settings;
mod simulation;
mod state;
mod telemetry_ext;

use crate::data_source::serial::{self, *};
Expand Down Expand Up @@ -347,8 +345,8 @@ fn bin2kml(
.split_mut(|b| *b == 0x00)
.filter_map(|b| postcard::from_bytes_cobs::<DownlinkMessage>(b).ok())
.map(|msg| msg.into())
.filter(|vs: &VehicleState| vs.longitude.is_some() && vs.latitude.is_some() && vs.altitude_gps.is_some())
.map(|vs| (vs.longitude.unwrap(), vs.latitude.unwrap(), vs.altitude_gps.unwrap()))
.filter(|vs: &VehicleState| vs.longitude.is_some() && vs.latitude.is_some() && vs.altitude_gps_asl.is_some())
.map(|vs| (vs.longitude.unwrap(), vs.latitude.unwrap(), vs.altitude_gps_asl.unwrap()))
.map(|(ln, lt, alt)| format!(" {},{},{}", ln, lt, alt + 100.0))
.collect();

Expand Down
32 changes: 15 additions & 17 deletions src/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use mithril::settings::*;
use mithril::state_estimation::*;
use mithril::telemetry::*;

use crate::state::VehicleState;

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

#[cfg(any(target_os = "windows", target_os = "android"))]
Expand Down Expand Up @@ -165,7 +163,7 @@ impl SimulationState {

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

// Skip the first known states without raw sensor data
Expand Down Expand Up @@ -295,8 +293,8 @@ impl SimulationState {
break;
}

if next.altitude_gps.is_some() && next.latitude.is_some() && next.longitude.is_some() {
self.altitude = next.altitude_gps.unwrap();
if next.altitude_gps_asl.is_some() && next.latitude.is_some() && next.longitude.is_some() {
self.altitude = next.altitude_gps_asl.unwrap();
self.latitude = next.latitude.unwrap() as f64;
self.longitude = next.longitude.unwrap() as f64;
}
Expand Down Expand Up @@ -433,7 +431,7 @@ impl SimulationState {

// update state estimation with sampled sensor values
self.state_estimator.update(
self.time,
std::num::Wrapping(self.time),
self.mode,
self.gyroscope,
self.accelerometer1,
Expand All @@ -457,11 +455,11 @@ impl From<&SimulationState> for VehicleState {
time: ss.time,
mode: Some(ss.mode),
orientation: ss.state_estimator.orientation,
altitude: Some(ss.state_estimator.altitude_asl()),
altitude_asl: Some(ss.state_estimator.altitude_asl()),
altitude_baro: ss.altitude_baro,
altitude_ground: Some(ss.state_estimator.altitude_ground),
altitude_max: Some(ss.state_estimator.altitude_max),
altitude_gps: Some(ss.altitude), // TODO
altitude_ground_asl: Some(ss.state_estimator.altitude_ground),
apogee_asl: Some(ss.state_estimator.altitude_max),
altitude_gps_asl: Some(ss.altitude), // TODO
latitude: Some(ss.latitude as f32),
longitude: Some(ss.longitude as f32),
vertical_speed: Some(ss.state_estimator.vertical_speed()),
Expand All @@ -481,11 +479,11 @@ impl From<&SimulationState> for VehicleState {
time: ss.time,
mode: Some(ss.mode),
orientation: ss.state_estimator.orientation,
altitude: Some(ss.state_estimator.altitude_asl()), // TODO
altitude_asl: Some(ss.state_estimator.altitude_asl()), // TODO
altitude_baro: ss.altitude_baro,
altitude_ground: Some(ss.altitude_ground),
altitude_max: Some(ss.state_estimator.altitude_max),
altitude_gps: Some(ss.altitude), // TODO
altitude_ground_asl: Some(ss.altitude_ground),
apogee_asl: Some(ss.state_estimator.altitude_max),
altitude_gps_asl: Some(ss.altitude), // TODO
latitude: Some(ss.latitude as f32),
longitude: Some(ss.longitude as f32),
vertical_speed: Some(ss.state_estimator.vertical_speed()),
Expand All @@ -498,11 +496,11 @@ impl From<&SimulationState> for VehicleState {
accelerometer1: ss.accelerometer1,
accelerometer2: ss.accelerometer2,
magnetometer: ss.magnetometer,
battery_voltage: Some(8.4),
battery_voltage: Some(8400),
arm_voltage: Some(if ss.time >= (ss.settings.sim_start_delay - 5000) {
8.4
8400
} else {
0.0
0
}),
true_orientation: Some(ss.orientation),
true_vertical_accel: Some(ss.acceleration.z),
Expand Down
Loading

0 comments on commit a324d19

Please sign in to comment.