Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
158 lines (136 loc) · 4.58 KB
/
Copy pathmain.py
File metadata and controls
158 lines (136 loc) · 4.58 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
"""
AetherSwarm CLI & Entry Point
=============================
Main entry point for running the swarm simulation and dashboard.
"""
import argparse
import logging
import sys
import os
# Add parent to path for direct script execution
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import time
import json
from aether_swarm.simulator.simulator import SwarmSimulator, SCENARIOS
def main():
parser = argparse.ArgumentParser(
description="AetherSwarm - Edge-Native Autonomous Swarm Robotics Platform",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python main.py --scenario disaster_response --duration 60
python main.py --scenario flocking_demo --dashboard
python main.py --nodes 10 --packet-loss 0.3
"""
)
parser.add_argument(
"--scenario", "-s",
type=str,
default="flocking_demo",
choices=list(SCENARIOS.keys()),
help="Pre-configured mission scenario"
)
parser.add_argument(
"--duration", "-d",
type=float,
default=60.0,
help="Simulation duration in seconds (0 = indefinite)"
)
parser.add_argument(
"--nodes", "-n",
type=int,
default=0,
help="Override total node count"
)
parser.add_argument(
"--packet-loss", "-p",
type=float,
default=None,
help="Network packet loss rate (0.0-1.0)"
)
parser.add_argument(
"--dashboard",
action="store_true",
help="Launch web dashboard at http://localhost:8765"
)
parser.add_argument(
"--dashboard-port",
type=int,
default=8765,
help="Dashboard port (default: 8765)"
)
parser.add_argument(
"--verbose", "-v",
action="store_true",
help="Enable debug logging"
)
parser.add_argument(
"--status-interval",
type=float,
default=2.0,
help="Status print interval in seconds"
)
args = parser.parse_args()
# Setup logging
log_level = logging.DEBUG if args.verbose else logging.INFO
logging.basicConfig(
level=log_level,
format='%(asctime)s [%(levelname)s] %(name)s: %(message)s',
datefmt='%H:%M:%S'
)
logger = logging.getLogger("AetherSwarm")
print("""
================================================================
AETHERSWARM v1.0.0
Edge-Native Autonomous Swarm Robotics
================================================================
""")
# Create simulator directly (so we can pass it to dashboard)
logger.info(f"Starting scenario: {args.scenario}")
sim = SwarmSimulator(scenario=args.scenario)
if args.packet_loss is not None:
sim.packet_loss = args.packet_loss
logger.info(f"Packet loss set to {args.packet_loss}")
# Start dashboard in background if requested
dashboard_thread = None
dashboard_port = args.dashboard_port
if args.dashboard:
import threading
from aether_swarm.dashboard.server import start_dashboard, set_simulator
# Inject simulator into dashboard before starting
set_simulator(sim)
dashboard_thread = threading.Thread(
target=start_dashboard,
args=(dashboard_port,),
daemon=False, # Non-daemon: stays alive after simulation ends
name="dashboard"
)
dashboard_thread.start()
# Let uvicorn start up
time.sleep(1)
print(f"\n Dashboard: http://localhost:{dashboard_port}\n")
# Run simulation loop
sim.start()
try:
start_time = time.time()
while time.time() - start_time < args.duration:
sim._simulation_time = time.time() - sim._start_time
sim.print_status()
time.sleep(args.status_interval)
except KeyboardInterrupt:
print("\n[SIM] Simulation interrupted by user")
finally:
sim.stop()
print("\n[SIM] Simulation ended. Final Statistics:")
print(json.dumps(sim.get_stats(), indent=2, default=str))
# Keep dashboard alive so user can inspect final state
if dashboard_thread and dashboard_thread.is_alive():
print(f"\n Dashboard remains open at http://localhost:{dashboard_port}")
print(" Press Ctrl+C to shut down.\n")
try:
while dashboard_thread.is_alive():
dashboard_thread.join(1)
except KeyboardInterrupt:
print("\nShutting down dashboard...\n")
if __name__ == "__main__":
main()