hollywood/example_actors/one_dim_robot/
model.rs

1use std::fmt::Debug;
2use std::fmt::Display;
3
4/// A generic value with a timestamp.
5#[derive(Clone, Debug, Default)]
6pub struct Stamped<T: Clone + Debug> {
7    /// Timestamp of the value.
8    pub time: f64,
9    /// Monotonic sequence counter
10    pub seq: u64,
11    /// The value.
12    pub value: T,
13}
14
15impl<T: Clone + Debug> Stamped<T> {
16    /// Creates a new value with a timestamp.
17    pub fn from_stamp_counter_and_value(time: f64, seq: u64, value: &T) -> Self {
18        Self {
19            time,
20            seq,
21            value: value.clone(),
22        }
23    }
24}
25
26impl<T: Display + Clone + Debug> Display for Stamped<T> {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        write!(f, "@{} {}", self.time, self.value)
29    }
30}
31
32/// A robot in a one dimensional world.
33#[derive(Clone, Debug, Default)]
34pub struct Robot {
35    /// Position of the robot.
36    pub position: f64,
37    /// Velocity of the robot.
38    pub velocity: f64,
39}
40
41impl Display for Robot {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        write!(
44            f,
45            "position: {}, velocity: {})",
46            self.position, self.velocity
47        )
48    }
49}
50
51/// A one dimensional world model.
52pub struct WorldModel {}
53
54impl WorldModel {
55    /// Boundary of the world in the East.
56    pub const BOUNDARY_EAST: f64 = 45.0;
57}
58
59/// A range measurement model.
60pub struct RangeMeasurementModel {}
61
62impl RangeMeasurementModel {
63    /// Standard deviation of the range measurement.
64    pub const RANGE_STD_DEV: f64 = 0.1;
65
66    /// Range measurement generative model.
67    pub fn range(&self, robot_position: f64) -> f64 {
68        if robot_position > WorldModel::BOUNDARY_EAST {
69            panic!("not implemented");
70        } else {
71            WorldModel::BOUNDARY_EAST - robot_position
72        }
73    }
74
75    /// Derivative of the range measurement w.r.t. the robot position.
76    pub fn dx_range(&self) -> f64 {
77        -1.0
78    }
79}