Skip to content

Commit

Permalink
Simulator Generator (#1024)
Browse files Browse the repository at this point in the history
* It wörks

* Always produce all additional outputs

* Clean up & clippening

* Remove manually maintained cycler

* Fix scenario files

* Add basic documentation for simulator

* Reflect new simulator location in CI

* std::Mutex -> parking_lot::Mutex

* Rename cycler2 -> cycler

* Remove leftover clippy allow
  • Loading branch information
knoellle authored Jun 3, 2024
1 parent 97e63f6 commit b94fc16
Show file tree
Hide file tree
Showing 46 changed files with 423 additions and 936 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ jobs:
strategy:
fail-fast: true
matrix:
target: [imagine, nao, replayer, webots]
target: [behavior_simulator, imagine, nao, replayer, webots]
profile: [release, dev]
runs-on:
- self-hosted
Expand All @@ -129,7 +129,6 @@ jobs:
[
aliveness,
annotato,
behavior_simulator,
camera_matrix_extractor,
depp,
fanta,
Expand Down
78 changes: 40 additions & 38 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [
"crates/geometry",
"crates/hardware",
"crates/hulk",
"crates/hulk_behavior_simulator",
"crates/hulk_imagine",
"crates/hulk_manifest",
"crates/hulk_nao",
Expand All @@ -45,7 +46,6 @@ members = [
"crates/vision",
"crates/walking_engine",
"tools/annotato",
"tools/behavior_simulator",
"tools/camera_matrix_extractor",
"tools/depp",
"tools/fanta",
Expand Down
4 changes: 2 additions & 2 deletions crates/code_generation/src/cyclers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn generate_database_struct() -> TokenStream {
path_serde::PathSerialize,
path_serde::PathIntrospect,
)]
pub(crate) struct Database {
pub struct Database {
pub main_outputs: MainOutputs,
pub additional_outputs: AdditionalOutputs,
}
Expand Down Expand Up @@ -130,7 +130,7 @@ fn generate_struct(cycler: &Cycler, cyclers: &Cyclers, mode: CyclerMode) -> Toke
own_sender: buffered_watch::Sender<Database>,
own_subscribed_outputs_receiver: buffered_watch::Receiver<std::collections::HashSet<String>>,
parameters_receiver: buffered_watch::Receiver<crate::structs::Parameters>,
cycler_state: crate::structs::#module_name::CyclerState,
pub cycler_state: crate::structs::#module_name::CyclerState,
#realtime_inputs
#input_output_fields
#node_fields
Expand Down
1 change: 0 additions & 1 deletion crates/control/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub mod button_filter;
pub mod camera_matrix_calculator;
pub mod center_of_mass_provider;
pub mod dribble_path_planner;
pub mod fake_data;
pub mod fall_state_estimation;
pub mod foot_bumper_filter;
pub mod game_controller_filter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "behavior_simulator"
name = "hulk_behavior_simulator"
version = "0.1.0"
edition.workspace = true
license.workspace = true
homepage.workspace = true

[[bin]]
name = "behavior_simulator"
name = "hulk_behavior_simulator"

[dependencies]
bincode = { workspace = true }
Expand All @@ -15,6 +15,7 @@ chrono = { workspace = true }
clap = { workspace = true }
color-eyre = { workspace = true }
communication = { workspace = true }
context_attribute = { workspace = true }
control = { workspace = true }
coordinate_systems = { workspace = true }
ctrlc = { workspace = true }
Expand All @@ -32,6 +33,7 @@ parking_lot = { workspace = true }
path_serde = { workspace = true }
projection = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
spl_network = { workspace = true }
spl_network_messages = { workspace = true }
tokio = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@ fn main() -> Result<()> {
name: "Control",
kind: CyclerKind::RealTime,
instances: vec![""],
setup_nodes: vec!["control::fake_data"],
setup_nodes: vec!["crate::fake_data"],
nodes: vec![
"control::active_vision",
"control::ball_state_composer",
"control::behavior::node",
"control::referee_position_provider",
"control::game_controller_state_filter",
"control::kick_selector",
"control::kick_target_provider",
"control::motion::look_around",
"control::motion::motion_selector",
"control::referee_pose_detection_filter",
"control::referee_position_provider",
"control::role_assignment",
"control::rule_obstacle_composer",
"control::search_suggestor",
Expand All @@ -41,7 +40,7 @@ fn main() -> Result<()> {
kind: CyclerKind::Perception,
instances: vec![""],
setup_nodes: vec!["spl_network::message_receiver"],
nodes: vec![],
nodes: vec!["spl_network::message_filter"],
},
],
};
Expand Down Expand Up @@ -77,7 +76,7 @@ fn main() -> Result<()> {
paths
.write_to_file("behavior_files.rs")
.wrap_err("failed to write generated tests to file")?;
generate(&cyclers, &structs, ExecutionMode::None)
generate(&cyclers, &structs, ExecutionMode::Run)
.write_to_file("generated_code.rs")
.wrap_err("failed to write generated code to file")
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use types::{
sensor_data::SensorData,
};

use crate::interfake::FakeDataInterface;

#[derive(Deserialize, Serialize)]
pub struct FakeData {}

Expand All @@ -37,6 +39,7 @@ pub struct CreationContext {
#[context]
#[allow(dead_code)]
pub struct CycleContext {
hardware_interface: HardwareInterface,
glance_angle: Parameter<f32, "look_at.glance_angle">,
}

Expand All @@ -51,6 +54,7 @@ pub struct MainOutputs {
pub game_controller_address: MainOutput<Option<SocketAddr>>,
pub has_ground_contact: MainOutput<bool>,
pub hulk_messages: MainOutput<Vec<HulkMessage>>,
pub is_referee_initial_pose_detected: MainOutput<bool>,
pub hypothetical_ball_positions: MainOutput<Vec<HypotheticalBallPosition<Ground>>>,
pub is_localization_converged: MainOutput<bool>,
pub obstacles: MainOutput<Vec<Obstacle>>,
Expand All @@ -67,7 +71,35 @@ impl FakeData {
Ok(Self {})
}

pub fn cycle(&mut self, _context: CycleContext) -> Result<MainOutputs> {
Ok(MainOutputs::default())
pub fn cycle(&mut self, context: CycleContext<impl FakeDataInterface>) -> Result<MainOutputs> {
let mut receiver = context
.hardware_interface
.get_last_database_receiver()
.lock();
let last_database = &receiver.borrow_and_mark_as_seen().main_outputs;
Ok(MainOutputs {
ball_position: last_database.ball_position.into(),
cycle_time: last_database.cycle_time.into(),
fall_state: last_database.fall_state.into(),
filtered_whistle: last_database.filtered_whistle.clone().into(),
game_controller_state: last_database.game_controller_state.into(),
game_controller_address: last_database.game_controller_address.into(),
has_ground_contact: last_database.has_ground_contact.into(),
hulk_messages: last_database.hulk_messages.clone().into(),
is_referee_initial_pose_detected: last_database.is_referee_initial_pose_detected.into(),
hypothetical_ball_positions: last_database.hypothetical_ball_positions.clone().into(),
is_localization_converged: last_database.is_localization_converged.into(),
obstacles: last_database.obstacles.clone().into(),
penalty_shot_direction: last_database.penalty_shot_direction.into(),
primary_state: last_database.primary_state.into(),
ground_to_field: last_database.ground_to_field.into(),
sensor_data: last_database.sensor_data.clone().into(),
stand_up_front_estimated_remaining_duration: last_database
.stand_up_front_estimated_remaining_duration
.into(),
stand_up_back_estimated_remaining_duration: last_database
.stand_up_back_estimated_remaining_duration
.into(),
})
}
}
82 changes: 82 additions & 0 deletions crates/hulk_behavior_simulator/src/interfake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use std::{
mem::take,
sync::Arc,
time::{SystemTime, UNIX_EPOCH},
};

use parking_lot::Mutex;

use buffered_watch::{Receiver, Sender};
use color_eyre::Result;
use hardware::{NetworkInterface, RecordingInterface, TimeInterface};
use types::messages::{IncomingMessage, OutgoingMessage};

use crate::{cyclers::control::Database, HardwareInterface};

pub struct Interfake {
time: SystemTime,
messages: Arc<Mutex<Vec<OutgoingMessage>>>,
last_database_receiver: Mutex<Receiver<Database>>,
last_database_sender: Mutex<Sender<Database>>,
}

impl Default for Interfake {
fn default() -> Self {
let (last_database_sender, last_database_receiver) =
buffered_watch::channel(Default::default());
Self {
time: UNIX_EPOCH,
messages: Default::default(),
last_database_receiver: Mutex::new(last_database_receiver),
last_database_sender: Mutex::new(last_database_sender),
}
}
}

impl NetworkInterface for Interfake {
fn read_from_network(&self) -> Result<IncomingMessage> {
unimplemented!()
}

fn write_to_network(&self, message: OutgoingMessage) -> Result<()> {
self.messages.lock().push(message);
Ok(())
}
}

impl RecordingInterface for Interfake {
fn should_record(&self) -> bool {
false
}

fn set_whether_to_record(&self, _enable: bool) {}
}

impl TimeInterface for Interfake {
fn get_now(&self) -> SystemTime {
self.time
}
}

pub trait FakeDataInterface {
fn get_last_database_receiver(&self) -> &Mutex<Receiver<Database>>;
fn get_last_database_sender(&self) -> &Mutex<Sender<Database>>;
}

impl FakeDataInterface for Interfake {
fn get_last_database_receiver(&self) -> &Mutex<Receiver<Database>> {
&self.last_database_receiver
}

fn get_last_database_sender(&self) -> &Mutex<Sender<Database>> {
&self.last_database_sender
}
}

impl Interfake {
pub fn take_outgoing_messages(&self) -> Vec<OutgoingMessage> {
take(&mut self.messages.lock())
}
}

impl HardwareInterface for Interfake {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use hardware::{NetworkInterface, RecordingInterface, TimeInterface};
use interfake::FakeDataInterface;

pub mod cycler;
pub mod fake_data;
pub mod interfake;
pub mod robot;
pub mod server;
Expand All @@ -9,4 +10,7 @@ pub mod state;

include!(concat!(env!("OUT_DIR"), "/generated_code.rs"));

pub trait HardwareInterface: TimeInterface + NetworkInterface + RecordingInterface {}
pub trait HardwareInterface:
TimeInterface + NetworkInterface + RecordingInterface + FakeDataInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use fern::{Dispatch, InitError};
use log::LevelFilter;
use tokio_util::sync::CancellationToken;

use behavior_simulator::{server, simulator::Simulator};
use hulk_behavior_simulator::{server, simulator::Simulator};

#[derive(Parser)]
enum Arguments {
Expand Down
Loading

0 comments on commit b94fc16

Please sign in to comment.