Skip to content

Commit

Permalink
Vision tuner panel: add save button (#1404)
Browse files Browse the repository at this point in the history
* Add save button to vision tuner

* Sort rc24_indoor/default.json for readable diffs when using the save button
  • Loading branch information
rmburg authored Jul 21, 2024
1 parent 86d18af commit ba9a9fb
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 24 deletions.
14 changes: 14 additions & 0 deletions crates/parameters/src/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,27 @@ pub struct Scope {
}

impl Scope {
pub fn default_location() -> Self {
Self {
location: Location::All,
id: Id::All,
}
}

pub fn default_head() -> Self {
Self {
location: Location::All,
id: Id::Head,
}
}

pub fn current_location() -> Self {
Self {
location: Location::Current,
id: Id::All,
}
}

pub fn current_head() -> Self {
Self {
location: Location::Current,
Expand Down
44 changes: 22 additions & 22 deletions etc/parameters/rc24_indoor/default.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
{
"image_segmenter": {
"vision_top": {
"vertical_edge_threshold": 10
},
"vision_bottom": {
"vertical_edge_threshold": 15,
"vertical_median_mode": "FivePixels"
}
},
"line_detection": {
"vision_top": {
"allowed_projected_segment_length": {
"start": 0.02
},
"gradient_alignment": -0.8
},
"vision_bottom": {
"gradient_alignment": -0.8
}
},
"field_color_detection": {
"vision_bottom": {
"saturation": {
"start": 95,
"end": 255
"end": 255,
"start": 95
}
},
"vision_top": {
Expand All @@ -36,5 +16,25 @@
"start": 63
}
}
},
"image_segmenter": {
"vision_bottom": {
"vertical_edge_threshold": 15,
"vertical_median_mode": "FivePixels"
},
"vision_top": {
"vertical_edge_threshold": 10
}
},
"line_detection": {
"vision_bottom": {
"gradient_alignment": -0.8
},
"vision_top": {
"allowed_projected_segment_length": {
"start": 0.02
},
"gradient_alignment": -0.8
}
}
}
60 changes: 58 additions & 2 deletions tools/twix/src/panels/vision_tuner.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use std::{ops::RangeInclusive, sync::Arc};

use color_eyre::Result;
use color_eyre::{eyre::OptionExt, Result};
use communication::messages::TextOrBinary;
use eframe::{
egui::{Grid, Response, Slider, Ui, Widget},
emath::Numeric,
};
use log::error;
use parameters::directory::Scope;
use serde::Serialize;
use serde_json::{to_value, Value};

use types::{field_color::FieldColorParameters, image_segments::Direction};

use crate::{nao::Nao, panel::Panel, value_buffer::BufferHandle};
use crate::{log_error::LogError, nao::Nao, panel::Panel, value_buffer::BufferHandle};

use super::image::cycler_selector::{VisionCycler, VisionCyclerSelector};

Expand Down Expand Up @@ -75,6 +76,58 @@ impl VisionTunerPanel {
}
ui.end_row();
}

fn save_field_color_parameters(&self, scope: Scope) -> Result<()> {
let cycler = self.cycler.as_snake_case_path();

let parameters = self
.field_color_detection
.get_last_value()?
.ok_or_eyre("unable to retrieve parameters, nothing was saved.")?;

let value = to_value(parameters).unwrap();

self.nao
.store_parameters(&format!("field_color_detection.{cycler}"), value, scope)?;

Ok(())
}

fn save_image_segmenter_parameters(&self, scope: Scope) -> Result<()> {
let cycler = self.cycler.as_snake_case_path();

let horizontal_edge_threshold = self
.horizontal_edge_threshold
.get_last_value()?
.ok_or_eyre("unable to retrieve horizontal_edge_threshold, nothing was saved.")?;
let vertical_edge_threshold = self
.vertical_edge_threshold
.get_last_value()?
.ok_or_eyre("unable to retrieve vertical_edge_threshold, nothing was saved.")?;

let horizontal_edge_threshold_value = to_value(horizontal_edge_threshold).unwrap();
let vertical_edge_threshold_value = to_value(vertical_edge_threshold).unwrap();

self.nao.store_parameters(
&format!("image_segmenter.{cycler}.horizontal_edge_threshold"),
horizontal_edge_threshold_value,
scope,
)?;
self.nao.store_parameters(
&format!("image_segmenter.{cycler}.vertical_edge_threshold"),
vertical_edge_threshold_value,
scope,
)?;

Ok(())
}

fn save(&self, scope: Scope) -> Result<()> {
self.save_field_color_parameters(scope)?;
self.save_image_segmenter_parameters(scope)?;

Ok(())
}
}

impl Panel for VisionTunerPanel {
Expand Down Expand Up @@ -112,6 +165,9 @@ impl Widget for &mut VisionTunerPanel {
if cycler_selector.ui(ui).changed() {
self.resubscribe();
}
if ui.button("Save to current location").clicked() {
self.save(Scope::current_location()).log_err();
}
});
ui.separator();
let cycler = self.cycler.as_snake_case_path();
Expand Down

0 comments on commit ba9a9fb

Please sign in to comment.