Skip to content

Commit 9bebd50

Browse files
committed
task'd
1 parent 77c6477 commit 9bebd50

2 files changed

Lines changed: 58 additions & 16 deletions

File tree

yggdrasil/src/vision/scan_grid.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn mean_and_std(data: &[f32]) -> (f32, f32) {
138138
(mean, variance.sqrt())
139139
}
140140

141-
#[derive(Debug)]
141+
#[derive(Debug, Clone)]
142142
pub struct Line {
143143
pub x: i32,
144144
pub y_max: i32,
@@ -160,6 +160,19 @@ pub struct ScanGrid<T: CameraLocation> {
160160
pub low_res_step: usize,
161161
}
162162

163+
impl<T: CameraLocation> Clone for ScanGrid<T> {
164+
fn clone(&self) -> Self {
165+
Self {
166+
image: self.image.clone(),
167+
y: self.y.clone(),
168+
lines: self.lines.clone(),
169+
field_limit: self.field_limit.clone(),
170+
low_res_start: self.low_res_start.clone(),
171+
low_res_step: self.low_res_step.clone(),
172+
}
173+
}
174+
}
175+
163176
pub fn init_top_scan_grid(mut commands: Commands, image: Res<Image<Top>>) {
164177
commands.insert_resource(ScanGrid {
165178
image: image.clone(),

yggdrasil/src/vision/scan_lines.rs

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{ops::Deref, sync::Arc};
1+
use std::{marker::PhantomData, ops::Deref, sync::Arc};
22

33
use crate::{
44
core::debug::{
@@ -15,11 +15,15 @@ use super::{
1515
field_boundary::FieldBoundary,
1616
scan_grid::{FieldColorApproximate, ScanGrid},
1717
};
18-
use bevy::prelude::*;
18+
use bevy::{
19+
prelude::*,
20+
tasks::{AsyncComputeTaskPool, Task},
21+
};
1922

2023
use heimdall::{Bottom, CameraLocation, CameraPosition, Top, YuvPixel, YuyvImage};
2124
use nalgebra::Point2;
2225
use serde::{Deserialize, Serialize};
26+
use tasks::{CommandsExt, conditions::task_finished};
2327
use yggdrasil_rerun_comms::protocol::control::FieldColorConfig;
2428

2529
#[derive(Resource, Debug, Clone, Serialize, Deserialize)]
@@ -100,12 +104,14 @@ impl Plugin for ScanLinesPlugin {
100104
.add_systems(
101105
Update,
102106
(
103-
update_scan_lines::<Top>
107+
spawn_scan_lines_task::<Top>
104108
.after(super::scan_grid::update_top_scan_grid)
105-
.run_if(resource_exists_and_changed::<ScanGrid<Top>>),
106-
update_scan_lines::<Bottom>
109+
.run_if(resource_exists_and_changed::<ScanGrid<Top>>)
110+
.run_if(task_finished::<ScanLines<Top>>),
111+
spawn_scan_lines_task::<Bottom>
107112
.after(super::scan_grid::update_bottom_scan_grid)
108-
.run_if(resource_exists_and_changed::<ScanGrid<Bottom>>),
113+
.run_if(resource_exists_and_changed::<ScanGrid<Bottom>>)
114+
.run_if(task_finished::<ScanLines<Bottom>>),
109115
),
110116
)
111117
.add_named_debug_systems(
@@ -174,6 +180,29 @@ impl<T: CameraLocation> ScanLines<T> {
174180
}
175181
}
176182

183+
fn spawn_scan_lines_task<T: CameraLocation>(
184+
mut commands: Commands,
185+
cfg: Res<ScanLinesConfig>,
186+
image: Res<Image<T>>,
187+
grid: Res<ScanGrid<T>>,
188+
field_boundary: Res<FieldBoundary>,
189+
) {
190+
commands
191+
.prepare_task(tasks::TaskPool::AsyncCompute)
192+
.to_resource()
193+
.spawn({
194+
let cfg = cfg.clone();
195+
let image = image.clone();
196+
let grid = grid.clone();
197+
let field_boundary = field_boundary.clone();
198+
199+
async move {
200+
// Run the scan lines generation in a separate task
201+
Some(get_scan_lines(&cfg, image, &grid, &field_boundary))
202+
}
203+
});
204+
}
205+
177206
/// A set of classified scanline regions.
178207
#[derive(Debug, Default)]
179208
pub struct ScanLine {
@@ -640,15 +669,15 @@ pub fn init_scan_lines<T: CameraLocation>(mut commands: Commands, image: Res<Ima
640669
commands.insert_resource(scan_lines);
641670
}
642671

643-
pub fn update_scan_lines<T: CameraLocation>(
644-
config: Res<ScanLinesConfig>,
645-
mut scan_lines: ResMut<ScanLines<T>>,
646-
image: Res<Image<T>>,
647-
scan_grid: Res<ScanGrid<T>>,
648-
field_boundary: Res<FieldBoundary>,
649-
) {
650-
*scan_lines = get_scan_lines(&config, image.clone(), &scan_grid, &field_boundary);
651-
}
672+
// pub fn update_scan_lines<T: CameraLocation>(
673+
// config: Res<ScanLinesConfig>,
674+
// mut scan_lines: ResMut<ScanLines<T>>,
675+
// image: Res<Image<T>>,
676+
// scan_grid: Res<ScanGrid<T>>,
677+
// field_boundary: Res<FieldBoundary>,
678+
// ) {
679+
// *scan_lines = get_scan_lines(&config, image.clone(), &scan_grid, &field_boundary);
680+
// }
652681

653682
/// Find the edge of a region in a scanline.
654683
///

0 commit comments

Comments
 (0)