From a000276c221628abc4972610e76dea9b5cbd9bf4 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Sat, 8 Mar 2025 15:13:39 -0500 Subject: [PATCH] added auto stub --- interface/src/auto.rs | 110 ++++++++++++++++++++++++++++++++++++++++++ interface/src/main.rs | 1 + 2 files changed, 111 insertions(+) create mode 100644 interface/src/auto.rs diff --git a/interface/src/auto.rs b/interface/src/auto.rs new file mode 100644 index 0000000..64cf59f --- /dev/null +++ b/interface/src/auto.rs @@ -0,0 +1,110 @@ +use std::{collections::VecDeque, ops::{Div, Sub}}; + +use common::{CamState, ControlPacket, SensorData}; +use anyhow::Result; + +struct AutoConfig { + turn_gain: f32, + auto_fire_distance: f32, +} +const AUTO_GAP: i16 = 140; /// mm tofs must drop to count as a hit +const AUTO_SELF_OCCLUSION: u16 = 130; /// minimum + +struct AutoInterface { +} + +impl AutoInterface { + /// change active command, fails if not in control + fn run_command(&self, command: ControlPacket) -> Result<()> {unimplemented!()} + fn sensor_data(&self) -> SensorData {unimplemented!()} + fn cam_state(&self) -> CamState {unimplemented!()} + async fn sensor_update(&self) -> SensorData {unimplemented!()} + /// disable auto + fn disable(&self) {unimplemented!()} + /// request auto enable, fails if the driver does not grant it + fn enable(&self) -> Result<()> {unimplemented!()} + fn enabled(&self) -> bool {unimplemented!()} + fn auto_conf(&self) -> AutoConfig {unimplemented!()} + fn send_message(&self, message: String) {unimplemented!()} +} + +struct Configurable { + name: &'static str, + default: f32, + min: f32, + max: f32, +} + +trait Auto { + /// entrypoint + async fn run(interface: &AutoInterface); + /// register + fn default_configs() -> [Configurable]; + +} + +async fn auto (interface: &AutoInterface) { + let mut tof_l = Stats::new(); + let mut tof_r = Stats::new(); + loop { + let data = interface.sensor_update().await; + let cam = interface.cam_state(); + let CamState::Charged = cam else { + continue; + }; + + let Some(latest_tof_l) = data.tof_l else { + continue; + }; + tof_l.update(latest_tof_l as i16); + let Some(latest_tof_r) = data.tof_r else { + continue; + }; + tof_r.update(latest_tof_r as i16); + + let detection = |latest: u16, delta: i16| { + delta < AUTO_GAP && latest > AUTO_SELF_OCCLUSION + }; + + if detection(latest_tof_l, tof_l.delta()) || detection(latest_tof_l, tof_l.delta()) { + if let Ok(()) = interface.enable() { + println!("found, now seek") + } + } + } +} + + +struct Stats { + table: VecDeque +} + +impl Stats { + pub fn new() -> Self { + Self { table: VecDeque::new() } + + } + + const MAX_ELEMENTS: usize = 3; + + pub fn update(&mut self, elem: T) { + self.table.push_front(elem); + if self.table.len() > Self::MAX_ELEMENTS { + self.table.pop_back(); + } + } +} + +impl + Copy + From> Stats { + pub fn delta(&self) -> T { + *self.table.get(0).unwrap_or(&T::from(0)) - *self.table.get(1).unwrap_or(&T::from(0)) + } + + pub fn max(&self) -> T { + *self.table.iter().max().unwrap_or(&T::from(0)) + } + + pub fn min(&self) -> T { + *self.table.iter().max().unwrap_or(&T::from(0)) + } +} diff --git a/interface/src/main.rs b/interface/src/main.rs index ce49c5e..1aa3d10 100644 --- a/interface/src/main.rs +++ b/interface/src/main.rs @@ -15,6 +15,7 @@ use tokio::{io::{AsyncReadExt, AsyncWriteExt, BufWriter, WriteHalf}, net::{tcp:: mod gui; mod storage_dir; mod combatlog; +mod auto; pub const POWER_THRESHOLD: AtomicF32 = AtomicF32::new(DEFAULT_VOLUME_THRESHOLD);