-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigital_twin_demo.rs
More file actions
196 lines (167 loc) Β· 6.13 KB
/
digital_twin_demo.rs
File metadata and controls
196 lines (167 loc) Β· 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//! Digital twin demonstration.
//!
//! This example shows how to create a digital twin environment with entities,
//! simulate physics, and visualize the scene.
use std::sync::Arc;
use std::time::Duration;
use chrono::Utc;
use digital_twin::{
Camera, CollisionShape, DigitalTwinModel, Entity, EntityState, EntityType,
OperationalStatus, PhysicsConfig, PhysicsEngine, PhysicalProperties,
Projection, Scene, Simple2DRenderer, VisualizationManager,
};
use uuid::Uuid;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("=== Digital Twin Demo ===");
// 1. Create a digital twin model
println!("1. Creating digital twin model...");
let mut model = DigitalTwinModel::new("Factory Floor");
// Add entities
let robot_id = Uuid::new_v4();
let sensor_id = Uuid::new_v4();
let machine_id = Uuid::new_v4();
let robot = Entity::new(robot_id, EntityType::Robot, "Robot-001");
let sensor = Entity::new(sensor_id, EntityType::Sensor, "Temperature-Sensor-01");
let machine = Entity::new(machine_id, EntityType::Machine, "CNC-Machine-05");
model.add_entity(robot);
model.add_entity(sensor);
model.add_entity(machine);
// Add a relationship
model.add_relationship(
robot_id,
machine_id,
"controls".to_string(),
None,
);
println!(" Model created with {} entities, {} relationships",
model.entities().len(), model.relationships().len());
// 2. Create entity states
println!("2. Creating entity states...");
let robot_state = EntityState {
entity_id: robot_id,
timestamp: Utc::now(),
position: Some((0.0, 0.0, 0.5)),
orientation: Some((0.0, 0.0, 0.0, 1.0)),
velocity: Some((0.1, 0.0, 0.0)),
angular_velocity: Some((0.0, 0.0, 0.05)),
status: OperationalStatus::Operational,
battery_level: Some(0.85),
temperature: Some(35.5),
custom_state: Default::default(),
};
let sensor_state = EntityState {
entity_id: sensor_id,
timestamp: Utc::now(),
position: Some((2.0, 1.5, 1.0)),
orientation: Some((0.0, 0.0, 0.0, 1.0)),
velocity: Some((0.0, 0.0, 0.0)),
angular_velocity: None,
status: OperationalStatus::Operational,
battery_level: Some(0.95),
temperature: Some(22.5),
custom_state: Default::default(),
};
let machine_state = EntityState {
entity_id: machine_id,
timestamp: Utc::now(),
position: Some((1.5, -1.0, 0.0)),
orientation: Some((0.0, 0.0, 0.0, 1.0)),
velocity: Some((0.0, 0.0, 0.0)),
angular_velocity: None,
status: OperationalStatus::Maintenance,
battery_level: None,
temperature: Some(45.0),
custom_state: Default::default(),
};
// 3. Physics simulation
println!("3. Setting up physics simulation...");
let physics_config = PhysicsConfig::default();
let mut physics_engine = PhysicsEngine::new(physics_config);
// Add physical properties for robot
let robot_props = PhysicalProperties {
mass: 50.0,
collision_shape: CollisionShape::Box {
width: 0.8,
height: 1.2,
depth: 0.6,
},
friction: 0.7,
restitution: 0.3,
drag_coefficient: 0.5,
buoyancy: 0.0,
};
physics_engine.add_entity(robot_id, robot_props, robot_state.clone());
// Add sensor (static)
let sensor_props = PhysicalProperties {
mass: 0.5,
collision_shape: CollisionShape::Sphere { radius: 0.1 },
friction: 0.2,
restitution: 0.1,
drag_coefficient: 0.8,
buoyancy: 0.0,
};
physics_engine.add_entity(sensor_id, sensor_props, sensor_state.clone());
// Add machine (static)
let machine_props = PhysicalProperties {
mass: 500.0,
collision_shape: CollisionShape::Box {
width: 2.0,
height: 1.5,
depth: 1.0,
},
friction: 0.9,
restitution: 0.1,
drag_coefficient: 1.2,
buoyancy: 0.0,
};
physics_engine.add_entity(machine_id, machine_props, machine_state.clone());
println!(" Physics engine ready with {} entities", physics_engine.entity_count());
// 4. Visualization
println!("4. Setting up visualization...");
let renderer = Simple2DRenderer::new(800, 600);
let mut viz = VisualizationManager::new(renderer);
viz.init()?;
// Add entities to visualization scene
let robot_entity = model.get_entity(robot_id).unwrap();
let sensor_entity = model.get_entity(sensor_id).unwrap();
let machine_entity = model.get_entity(machine_id).unwrap();
viz.add_entity(robot_entity, &robot_state);
viz.add_entity(sensor_entity, &sensor_state);
viz.add_entity(machine_entity, &machine_state);
// Configure camera
viz.camera_mut().move_to(5.0, 5.0, 5.0);
viz.camera_mut().look_at(0.0, 0.0, 0.0);
viz.camera_mut().viewport_width = 800;
viz.camera_mut().viewport_height = 600;
// 5. Simulation loop
println!("5. Running simulation loop (5 steps)...");
for step in 0..5 {
println!(" Step {}:", step + 1);
// Update physics
physics_engine.step(0.1); // 100 ms time step
let updated_states = physics_engine.get_states();
// Update visualization
viz.update_scene(&updated_states);
viz.render_frame()?;
// Capture and show frame info (just metadata)
let frame = viz.capture_frame()?;
println!(" Frame size: {} bytes ({}x{} RGBA)",
frame.len(),
viz.camera().viewport_width,
viz.camera().viewport_height);
// Print entity positions
for state in &updated_states {
if let Some(pos) = state.position {
println!(" Entity {} at ({:.2}, {:.2}, {:.2})",
state.entity_id, pos.0, pos.1, pos.2);
}
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
// 6. Cleanup
println!("6. Cleaning up...");
viz.stop()?;
println!("=== Demo completed successfully ===");
Ok(())
}