Skip to content

Commit f26dd5b

Browse files
committed
procedural coinflip
1 parent d8b4c28 commit f26dd5b

5 files changed

Lines changed: 65 additions & 6 deletions

File tree

.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use flake ./#noFHS

src/config/coinflip.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
#[derive(Debug, Serialize, Deserialize)]
4+
pub struct Config {
5+
pub depth: f32,
6+
pub angle_correction: f32,
7+
pub true_count: u32,
8+
}
9+
10+
impl Default for Config {
11+
fn default() -> Self {
12+
Self {
13+
depth: -1.25,
14+
angle_correction: 15.0,
15+
true_count: 4,
16+
}
17+
}
18+
}

src/config/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod bin;
2+
pub mod coinflip;
23
pub mod gate;
34
pub mod octagon;
45
pub mod path_align;
@@ -76,6 +77,7 @@ pub struct Missions {
7677
pub slalom: slalom::Config,
7778
pub bin: bin::Config,
7879
pub octagon: octagon::Config,
80+
pub coinflip: coinflip::Config,
7981
}
8082

8183
#[derive(Debug, Serialize, Deserialize)]

src/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use sw8s_rust_lib::{
2020
circle_buoy::{
2121
buoy_circle_sequence, buoy_circle_sequence_blind, buoy_circle_sequence_model,
2222
},
23-
coinflip::coinflip,
23+
coinflip::{coinflip, coinflip_procedural},
2424
example::{initial_descent, pid_test},
2525
fire_torpedo::{FireLeftTorpedo, FireRightTorpedo},
2626
gate::{gate_run_complex, gate_run_naive, gate_run_procedural, gate_run_testing},
@@ -467,7 +467,12 @@ async fn run_mission(mission: &str, cancel: CancellationToken) -> Result<()> {
467467
FireLeftTorpedo::new(static_context().await).execute().await;
468468
Ok(())
469469
}
470-
"coinflip" => ctwrap!(coinflip(static_context().await).execute()),
470+
"coinflip" => {
471+
ctwrap!(coinflip_procedural(
472+
static_context().await,
473+
&config.missions.coinflip
474+
))
475+
}
471476
// Just stall out forever
472477
"forever" | "infinite" => loop {
473478
while control_board().await.raw_speed_set([0.0; 8]).await.is_err() {}

src/missions/coinflip.rs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use tokio_serial::SerialStream;
44

55
use crate::{
66
act_nest,
7+
config::coinflip::Config,
78
missions::{
89
extra::AlwaysTrue,
910
meb::WaitArm,
@@ -33,6 +34,7 @@ pub async fn coinflip_procedural<
3334
Con: Send + Sync + GetControlBoard<WriteHalf<SerialStream>> + GetMainElectronicsBoard + FrontCamIO,
3435
>(
3536
context: &Con,
37+
config: &Config,
3638
) {
3739
#[cfg(feature = "logging")]
3840
logln!("Starting path align");
@@ -51,10 +53,17 @@ pub async fn coinflip_procedural<
5153
}
5254
};
5355

56+
let DEPTH = config.depth;
57+
5458
let _ = cb
55-
.stability_2_speed_set(0.0, 0.0, 0.0, 0.0, initial_yaw, 0.0)
59+
.stability_2_speed_set(0.0, 0.0, 0.0, 0.0, initial_yaw, DEPTH)
5660
.await;
5761

62+
let mut true_count = 0;
63+
let max_true_count = config.true_count;
64+
let mut target_yaw = initial_yaw;
65+
let angle_correction = config.angle_correction;
66+
5867
loop {
5968
#[allow(unused_variables)]
6069
let detections = vision.execute().await.unwrap_or_else(|e| {
@@ -63,10 +72,33 @@ pub async fn coinflip_procedural<
6372
vec![]
6473
});
6574

66-
let _gate = detections
75+
let gate = detections
6776
.iter()
6877
.filter(|d| matches!(d.class().identifier, Target::Gate))
6978
.collect_vec();
79+
let shark = detections
80+
.iter()
81+
.filter(|d| matches!(d.class().identifier, Target::Shark))
82+
.collect_vec();
83+
let sawfish = detections
84+
.iter()
85+
.filter(|d| matches!(d.class().identifier, Target::Sawfish))
86+
.collect_vec();
87+
88+
if (gate.len() > 0 || shark.len() > 0 || sawfish.len() > 0) {
89+
if true_count > max_true_count {
90+
let _ = cb
91+
.stability_2_speed_set(0.0, 0.0, 0.0, 0.0, initial_yaw, DEPTH)
92+
.await;
93+
} else {
94+
true_count += 1;
95+
}
96+
} else {
97+
target_yaw += angle_correction;
98+
let _ = cb
99+
.stability_2_speed_set(0.0, 0.0, 0.0, 0.0, target_yaw, DEPTH)
100+
.await;
101+
}
70102
}
71103
}
72104

@@ -113,10 +145,11 @@ pub fn coinflip<
113145
DetectTarget::<Target, YoloClass<Target>, Offset2D<f64>>::new(Target::Gate),
114146
DetectTarget::<Target, YoloClass<Target>, Offset2D<f64>>::new(Target::Middle),
115147
DetectTarget::<Target, YoloClass<Target>, Offset2D<f64>>::new(Target::LeftPole),
116-
DetectTarget::<Target, YoloClass<Target>, Offset2D<f64>>::new(Target::RightPole),
148+
DetectTarget::<Target, YoloClass<Target>, Offset2D<f64>>::new(
149+
Target::RightPole
150+
),
117151
DetectTarget::<Target, YoloClass<Target>, Offset2D<f64>>::new(Target::Shark),
118152
DetectTarget::<Target, YoloClass<Target>, Offset2D<f64>>::new(Target::Sawfish),
119-
120153
),
121154
CountTrue::new(TRUE_COUNT),
122155
),

0 commit comments

Comments
 (0)