|
| 1 | +use color_eyre::Result; |
| 2 | +use linear_algebra::Vector2; |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | + |
| 5 | +use ball_filter::BallFilter as BallFiltering; |
| 6 | +use context_attribute::context; |
| 7 | +use coordinate_systems::Ground; |
| 8 | +use framework::{MainOutput, PerceptionInput}; |
| 9 | +use projection::camera_matrices::CameraMatrices; |
| 10 | +use types::{ |
| 11 | + ball_detection::BallPercept, |
| 12 | + ball_position::{BallPosition, HypotheticalBallPosition}, |
| 13 | + cycle_time::CycleTime, |
| 14 | + field_dimensions::FieldDimensions, |
| 15 | +}; |
| 16 | + |
| 17 | +#[derive(Deserialize, Serialize)] |
| 18 | +pub struct BallFilter { |
| 19 | + ball_filter: BallFiltering, |
| 20 | + last_ball_position: Option<BallPosition<Ground>>, |
| 21 | +} |
| 22 | + |
| 23 | +#[context] |
| 24 | +pub struct CreationContext {} |
| 25 | + |
| 26 | +#[context] |
| 27 | +pub struct CycleContext { |
| 28 | + cycle_time: Input<CycleTime, "cycle_time">, |
| 29 | + ball_percepts_from_vision: PerceptionInput<Option<Vec<BallPercept>>, "VisionBottom", "balls?">, |
| 30 | +} |
| 31 | + |
| 32 | +#[context] |
| 33 | +#[derive(Default)] |
| 34 | +pub struct MainOutputs { |
| 35 | + pub ball_position: MainOutput<Option<BallPosition<Ground>>>, |
| 36 | + pub hypothetical_ball_positions: MainOutput<Vec<HypotheticalBallPosition<Ground>>>, |
| 37 | +} |
| 38 | + |
| 39 | +impl BallFilter { |
| 40 | + pub fn new(_context: CreationContext) -> Result<Self> { |
| 41 | + Ok(Self { |
| 42 | + ball_filter: Default::default(), |
| 43 | + last_ball_position: None, |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + pub fn cycle(&mut self, mut context: CycleContext) -> Result<MainOutputs> { |
| 48 | + let filtered_ball = context |
| 49 | + .ball_percepts_from_vision |
| 50 | + .persistent |
| 51 | + .last_entry() |
| 52 | + .and_then(|entry| entry.get().last().cloned()??.last()); |
| 53 | + |
| 54 | + let filtered_ball = match (filtered_ball, self.last_ball_position) { |
| 55 | + (Some(ball_percepts), _) => Some(BallPosition { |
| 56 | + position: ball_percepts.percept_in_ground.mean.into(), |
| 57 | + velocity: Vector2::zeros(), |
| 58 | + last_seen: context.cycle_time.start_time, |
| 59 | + }), |
| 60 | + (None, Some(last_ball_position)) => Some(last_ball_position), |
| 61 | + _ => None, |
| 62 | + }; |
| 63 | + |
| 64 | + Ok(MainOutputs { |
| 65 | + ball_position: filtered_ball.into(), |
| 66 | + hypothetical_ball_positions: Vec::new().into(), |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
0 commit comments