Skip to content

Commit

Permalink
ピトー管のプラホの値表示
Browse files Browse the repository at this point in the history
  • Loading branch information
xsuz committed Jul 27, 2024
1 parent 61aa95a commit 5c71088
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 25 deletions.
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use ui::{
tachometer::TachUI,
vane::VaneUI,
AppUI,
power_est::PowerEstimaterUI
};

use crate::parse::Parser;
Expand Down Expand Up @@ -54,6 +55,8 @@ struct MeisterApp {
tach: TachUI,
vane: VaneUI,

pwrest: PowerEstimaterUI

// menu: FlightMenu,
}

Expand All @@ -75,6 +78,8 @@ impl Default for MeisterApp {
tach: TachUI::new(),
vane: VaneUI::new(),

pwrest: PowerEstimaterUI::new()

// menu: FlightMenu::new(),
}
}
Expand Down Expand Up @@ -126,6 +131,8 @@ impl eframe::App for MeisterApp {
self.tach.update(&mut self.parser, ctx);
self.vane.update(&mut self.parser, ctx);

self.pwrest.update(&mut self.parser, ctx);

// 生存確認
let t = chrono::Utc::now().timestamp_millis();

Expand Down
13 changes: 12 additions & 1 deletion src/parse/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fs::OpenOptions;
use std::io::prelude::*;

use crate::parse::{decode_cobs, UltraSonicData,BarometerData,GPSData, Data, IMUData, ServoData,PitotData,TachData};
use std::fmt::Debug;

Expand Down Expand Up @@ -34,6 +35,7 @@ pub struct Parser {
alt_data: Vec<(UltraSonicData,i64)>,
barometer_data: [Vec<(BarometerData,i64)>;2],
pitot_data:Vec<(PitotData,i64)>,
pitot1_data:Vec<(PitotData,i64)>,
vane_data:Vec<(VaneData,i64)>,
tac_data: [Vec<(TachData,i64)>;2],
port: Option<Box<dyn serialport::SerialPort>>,
Expand All @@ -53,6 +55,7 @@ impl Parser {
alt_data: Vec::new(),
barometer_data: [Vec::new(),Vec::new()],
pitot_data:Vec::new(),
pitot1_data: Vec::new(),
gps_data: Vec::new(),
vane_data: Vec::new(),
tac_data: [Vec::new(),Vec::new()],
Expand Down Expand Up @@ -108,6 +111,10 @@ impl Parser {
pub fn get_pitot_data(&self)->&Vec<(PitotData,i64)>{
&self.pitot_data
}

pub fn get_pitot1_data(&self)->&Vec<(PitotData,i64)>{
&self.pitot_data
}
pub fn get_gps_data(&self) -> &Vec<(GPSData,i64)> {
&self.gps_data
}
Expand Down Expand Up @@ -157,7 +164,11 @@ impl Parser {
}
}
0x30 => {
parse_data(&mut self.pitot_data,&decoded,timestamp);
if decoded[0]==0x31{
parse_data(&mut self.pitot_data,&decoded,timestamp);
}else{
parse_data(&mut self.pitot1_data,&decoded,timestamp);
}
}
0x40 => {
parse_data(&mut self.imu[(decoded[0] & 0x0f) as usize], &decoded,timestamp);
Expand Down
36 changes: 20 additions & 16 deletions src/ui/altitude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ use eframe::egui;

pub struct AltitudeUI {
offset: f32,
raw_alt: f32,
}

impl AltitudeUI {
pub fn new() -> Self {
Self {
offset: -1.2,
raw_alt: 0.0,
}
Self { offset: -1.2 }
}
}

Expand All @@ -27,13 +23,20 @@ impl AppUI for AltitudeUI {
// uint8_t id;
// float diff_alt_from_10;
// }
ui.add(egui::DragValue::new(&mut self.raw_alt));
if ui.button("Send diff altidude from 10m").clicked() {
let mut bytes: [u8; 8] = [0; 8];
bytes[0] = 0xD5; // message id
BigEndian::write_f32(&mut bytes[4..8], self.raw_alt - 10.0);
data.write(&bytes.to_vec());
self.offset = -self.raw_alt + 10.0;

if let Some((barometer_data, _)) = data.get_barometer_data(1).last() {
let p0 = barometer_data.pressure;

if let Some((barometer_data0, _)) = data.get_barometer_data(0).last() {
if ui.button("Send diff altidude from 10m").clicked() {
let mut bytes: [u8; 8] = [0; 8];
bytes[0] = 0xD5; // message id
let raw_alt=44330.0 * (1.0 - (barometer_data0.pressure / p0).powf(1.0 / 5.255));
BigEndian::write_f32(&mut bytes[4..8], raw_alt - 10.0);
data.write(&bytes.to_vec());
self.offset = -raw_alt + 10.0;
}
}
}
});
egui::CentralPanel::default().show_inside(ui, |ui| {
Expand All @@ -55,12 +58,12 @@ impl AppUI for AltitudeUI {
));
ui.heading(format!(
"altitude:\t{:2.2}m\ttimestamp:\t{}ms",
44330.0 * (1.0 - (barometer_data0.pressure / p0).powf(1.0 / 5.255)),
44330.0 * (1.0 - (barometer_data0.pressure / p0).powf(1.0 / 5.255)) +self.offset,
barometer_data0.timestamp
));
}
}

egui_plot::Plot::new("Altitude")
.legend(egui_plot::Legend::default())
.show(ui, |plt_ui| {
Expand All @@ -80,7 +83,7 @@ impl AppUI for AltitudeUI {
}
if data.get_barometer_data(0).len() > 100 {
let point_barometer: egui_plot::PlotPoints = data
.get_barometer_data(0)
.get_barometer_data(0)[data.get_barometer_data(1).len() - 100..]
.iter()
.map(|(baro0_data, baro0_time)| {
if let Some((baro1_data, _)) = data
Expand All @@ -94,7 +97,8 @@ impl AppUI for AltitudeUI {
* (1.0
- (baro0_data.pressure / baro1_data.pressure)
.powf(1.0 / 5.255))
as f64,
as f64
+ (self.offset as f64),
]
} else {
[
Expand Down
2 changes: 2 additions & 0 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub mod servo;
pub mod tachometer;
pub mod vane;

pub mod power_est;

pub trait AppUI{
fn update(&mut self,data:&mut crate::parse::Parser,ctx:&eframe::egui::Context);
}
10 changes: 9 additions & 1 deletion src/ui/pitot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ impl PitotUI {

impl super::AppUI for PitotUI {
fn update(&mut self, data: &mut crate::parse::Parser, ctx: &eframe::egui::Context) {
if let Some((pitot_data, _)) = data.get_pitot1_data().last() {
egui::Window::new("Pitot1").vscroll(true).show(ctx, |ui| {
ui.heading(format!(
"IAS:\t{:2.2}m/s\ttimestamp:\t{}ms",
pitot_data.velocity, pitot_data.timestamp
));
});
}
if let Some((pitot_data, _)) = data.get_pitot_data().last() {
egui::Window::new("Pitot").vscroll(true).show(ctx, |ui| {
ui.heading(format!(
Expand All @@ -27,7 +35,7 @@ impl super::AppUI for PitotUI {
.collect();

let point_cas: egui_plot::PlotPoints = data
.get_pitot_data()
.get_pitot_data()[data.get_pitot_data().len() - 100..]
.iter()
.map(|(pitot_data, pitot_timestamp)| {
let c3 = 2.0e-5;
Expand Down
155 changes: 148 additions & 7 deletions src/ui/power_est.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,162 @@ use eframe::egui;

use super::AppUI;

pub struct PowerEstimaterUI {}
pub struct PowerEstimaterUI {
visible: bool,
}

impl PowerEstimaterUI {
pub fn new() -> Self {
Self {}
Self { visible: true }
}
}

impl AppUI for PowerEstimaterUI {
fn update(&mut self, data: &mut crate::parse::Parser, ctx: &eframe::egui::Context) {
egui::Window::new("PowerEstimater").vscroll(true).show(ctx, |ui| {
let link_group_id=ui.id().with("power_est");
ui.horizontal(|ui|{
Plot::new("PowerEstimate")
egui::Window::new("PowerEstimater")
.vscroll(true)
.show(ctx, |ui| {
let link_group_id = ui.id().with("power_est");
ui.checkbox(&mut self.visible, "visible");
if self.visible {
ui.horizontal(|ui| {
//Pitot

egui_plot::Plot::new("velocity")
.legend(egui_plot::Legend::default())
.link_axis(link_group_id, true, false)
.show(ui, |plt_ui| {
let point_ias: egui_plot::PlotPoints = data
.get_pitot_data()
.iter()
.map(|(_data, utc)| [*utc as f64, _data.velocity as f64])
.collect();

let point_cas: egui_plot::PlotPoints = data
.get_pitot_data()
.iter()
.map(|(pitot_data, pitot_timestamp)| {
let c3 = 2.0e-5;
let c2 = 0.0032;
let c1 = 1.0073;
let c0 = 1.0073;

let mut c = c0;

if let Some((vane_data, _)) = data
.get_vane_data()
.iter()
.rfind(|(_, vane_timestamp)| {
vane_timestamp <= pitot_timestamp
})
{
c = c0
+ c1 * vane_data.angle
+ c2 * vane_data.angle * vane_data.angle
+ c3 * vane_data.angle
* vane_data.angle
* vane_data.angle;
}
[*pitot_timestamp as f64, (c * pitot_data.velocity) as f64]
})
.collect();
plt_ui.line(
egui_plot::Line::new(point_ias)
.color(egui::Color32::from_rgb(255, 0, 0))
.name("IAS")
.fill(0.0),
);
plt_ui.line(
egui_plot::Line::new(point_cas)
.color(egui::Color32::from_rgb(0, 255, 0))
.name("CAS")
.fill(0.0),
);
});

// Alt

egui_plot::Plot::new("pwr_esr_altitude")
.legend(egui_plot::Legend::default())
.show(ui, |plt_ui| {
if data.get_ultra_sonic_data().len() > 100 {
let point_ultra_sonic: egui_plot::PlotPoints = data
.get_ultra_sonic_data()
[data.get_ultra_sonic_data().len() - 100..]
.iter()
.map(|(data, time)| [*time as f64, data.altitude as f64])
.collect();

plt_ui.line(
egui_plot::Line::new(point_ultra_sonic)
.color(egui::Color32::from_rgb(0, 64, 255))
.name("ultra sonic")
.fill(0.0),
);
}
if data.get_barometer_data(0).len() > 100 {
let p0=101300.0;
let point_barometer: egui_plot::PlotPoints = data
.get_barometer_data(0)
.iter()
.map(|(baro0_data, baro0_time)| {
if let Some((baro1_data, _)) = data
.get_barometer_data(1)
.iter()
.rfind(|(_, baro1_time)| baro1_time <= baro0_time)
{
[
*baro0_time as f64,
44330.0
* (1.0
- (baro0_data.pressure
/ baro1_data.pressure)
.powf(1.0 / 5.255))
as f64
+ (1.2 as f64),
]
} else {
[
*baro0_time as f64,
44330.0
* (1.0
- (baro0_data.pressure / p0)
.powf(1.0 / 5.255))
as f64,
]
}
})
.collect();
plt_ui.line(
egui_plot::Line::new(point_barometer)
.color(egui::Color32::from_rgb(255, 64, 0))
.name("barometer")
.fill(0.0),
);
}
});

// Tachometer

egui_plot::Plot::new("power_est_tach")
.legend(egui_plot::Legend::default())
.link_axis(link_group_id, true, false)
.show(ui, |plt_ui| {
let point_cadence: egui_plot::PlotPoints = data
.get_tach_data(1)
.iter()
.map(|(_data, utc)| [*utc as f64, _data.cadence as f64])
.collect();
plt_ui.line(
egui_plot::Line::new(point_cadence)
.color(egui::Color32::from_rgb(255, 0, 0))
.name("cadence")
.fill(0.0),
);
});

});
}
});
});
}
}

0 comments on commit 5c71088

Please sign in to comment.