Skip to main content

moving_link/
moving_link.rs

1//! Demonstrates a moving aircraft communicating with a stationary ground site.
2
3use c3mesh::{
4    ChannelConfig, ChannelState, DeviceConfig, DeviceKind, DistanceChannel, DistanceRateModel,
5    MobilityModel, NetworkConfig, NetworkEvent, Position3D, SimTime, Simulator, Velocity3D,
6};
7
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let network = NetworkConfig {
10        devices: vec![
11            DeviceConfig {
12                id: "aircraft".into(),
13                kind: DeviceKind::Source {
14                    egress: "radio".into(),
15                },
16                mobility: MobilityModel::Linear {
17                    position_at_epoch: Position3D::new(100_000.0, 0.0, 10_000.0),
18                    velocity_mps: Velocity3D::new(-150.0, 0.0, 0.0),
19                    epoch_ns: 0,
20                },
21                interference: vec![],
22            },
23            DeviceConfig {
24                id: "ground_station".into(),
25                kind: DeviceKind::Sink,
26                mobility: MobilityModel::Static {
27                    position: Position3D::ORIGIN,
28                },
29                interference: vec![],
30            },
31        ],
32        channels: vec![ChannelConfig {
33            id: "radio".into(),
34            endpoints: ["aircraft".into(), "ground_station".into()],
35            bit_rate_bps: 10_000_000,
36            propagation_delay_ns: 50_000,
37            state: ChannelState::Operational,
38            distance: Some(DistanceChannel {
39                propagation_speed_mps: 299_792_458.0,
40                max_range_m: 120_000.0,
41                rate_model: DistanceRateModel::Linear {
42                    full_rate_distance_m: 20_000.0,
43                    minimum_bit_rate_bps: 1_000_000,
44                },
45            }),
46            radio: None,
47        }],
48    };
49
50    let mut simulator = Simulator::new(network)?;
51    simulator.schedule_send(SimTime::ZERO, "aircraft", "ground_station", vec![0; 1_000])?;
52    simulator.schedule_send(
53        SimTime::from_nanos(300_000_000_000),
54        "aircraft",
55        "ground_station",
56        vec![0; 1_000],
57    )?;
58    simulator.schedule_send(
59        SimTime::from_nanos(1_500_000_000_000),
60        "aircraft",
61        "ground_station",
62        vec![0; 1_000],
63    )?;
64
65    simulator.run_with(|event| match event {
66        NetworkEvent::TransmissionStarted {
67            at,
68            distance_mm,
69            effective_bit_rate_bps,
70            ..
71        } => println!(
72            "{at}: distance {:.1} km, rate {:.2} Mbit/s",
73            distance_mm.unwrap_or_default() as f64 / 1_000_000.0,
74            *effective_bit_rate_bps as f64 / 1_000_000.0,
75        ),
76        NetworkEvent::PacketDropped { at, reason, .. } => {
77            println!("{at}: packet dropped: {reason:?}");
78        }
79        _ => {}
80    })?;
81    Ok(())
82}