-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
313 lines (255 loc) · 8.12 KB
/
main.py
File metadata and controls
313 lines (255 loc) · 8.12 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python3
"""
Main entry point for the Laser Diode Test Equipment Simulation Environment.
This application provides a high-fidelity simulation of laser diode test automation equipment
featuring:
- 6-axis stage for precision positioning
- 3-axis gantry for chip loading
- 3-axis probe stage with press/unpress functionality
- Real-time 3D visualization
- JSON-based command interface
- Advanced collision detection and safety systems
Usage:
python main.py [options]
Options:
--config FILE Load configuration from FILE
--headless Run without GUI (for testing)
--debug Enable debug logging
--help Show this help message
Author: Simulation Team
Version: 1.0
"""
import sys
import argparse
import logging
import os
from pathlib import Path
# Add the project root to Python path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
from PyQt5.QtWidgets import QApplication, QMessageBox
from PyQt5.QtCore import Qt, QSettings
from PyQt5.QtGui import QIcon
from gui.main_window import MainWindow
from core.system_controller import SystemController
from config.settings_manager import get_settings_manager
def setup_logging(debug=False):
"""Setup logging configuration."""
level = logging.DEBUG if debug else logging.INFO
# Create logs directory if it doesn't exist
logs_dir = project_root / "logs"
logs_dir.mkdir(exist_ok=True)
# Configure logging
logging.basicConfig(
level=level,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(logs_dir / "simulation.log"),
logging.StreamHandler(sys.stdout) if debug else logging.NullHandler()
]
)
return logging.getLogger(__name__)
def parse_arguments():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description="Laser Diode Test Equipment Simulation Environment",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python main.py # Run with GUI
python main.py --config test_config.json # Load custom configuration
python main.py --headless # Run without GUI (for testing)
python main.py --debug # Enable debug logging
JSON command examples:
{
"command_id": "ST0001",
"command_type": "MOVE",
"target_stage": "six_axis",
"parameters": {
"position": {
"position": {
"x": 10500,
"y": 5200,
"z": 100
}
},
"velocity": 50000,
"acceleration": 500000
},
"execution": {
"mode": "immediate",
"priority": 5,
"timeout": 30.0
}
}
"""
)
parser.add_argument(
'--config',
type=str,
help='Load configuration from JSON file'
)
parser.add_argument(
'--headless',
action='store_true',
help='Run without GUI (for automated testing)'
)
parser.add_argument(
'--debug',
action='store_true',
help='Enable debug logging'
)
parser.add_argument(
'--version',
action='version',
version='Laser Diode Test Equipment Simulation v1.0'
)
return parser.parse_args()
def check_dependencies():
"""Check if required dependencies are available."""
missing = []
try:
import PyQt5
except ImportError:
missing.append("PyQt5")
try:
import numpy
except ImportError:
missing.append("NumPy")
try:
import pyvista
except ImportError:
print("Warning: PyVista not available. 3D visualization will be disabled.")
if missing:
print(f"Error: Missing required dependencies: {', '.join(missing)}")
print("\nTo install dependencies, run:")
print("pip install -r requirements.txt")
return False
return True
def load_configuration(config_file: str = None):
"""Load application configuration."""
settings_manager = get_settings_manager()
if config_file:
try:
# Import configuration
success = settings_manager.import_settings(config_file)
if success:
print(f"Configuration loaded from {config_file}")
else:
print(f"Failed to load configuration from {config_file}")
except Exception as e:
print(f"Error loading configuration: {e}")
# Validate settings
validation = settings_manager.validate_settings()
if not validation['valid']:
print("Configuration validation errors:")
for issue in validation['issues']:
print(f" - {issue}")
return False
return True
def run_headless_test():
"""Run simulation in headless mode for testing."""
logger = logging.getLogger(__name__)
logger.info("Running simulation in headless mode")
# Initialize system controller
system_controller = SystemController()
system_controller.start()
# Run test sequence
test_commands = [
{
"command_id": "HM0001",
"command_type": "HOME",
"target_stage": None
},
{
"command_id": "ST0002",
"command_type": "MOVE",
"target_stage": "six_axis",
"parameters": {
"position": {
"position": {
"x": 10000,
"y": 5000,
"z": 1000
}
}
}
},
{
"command_id": "GT0003",
"command_type": "GET_STATUS"
}
]
# Execute test commands
for command in test_commands:
logger.info(f"Executing command: {command['command_id']}")
result = system_controller.execute_command(command)
logger.info(f"Result: {result}")
# Wait for completion
import time
time.sleep(1)
# Cleanup
system_controller.stop()
logger.info("Headless test completed")
def main():
"""Main application entry point."""
# Parse command line arguments
args = parse_arguments()
# Setup logging
logger = setup_logging(args.debug)
# Check dependencies
if not check_dependencies():
sys.exit(1)
# Load configuration
if not load_configuration(args.config):
logger.error("Configuration loading failed")
sys.exit(1)
# Check if running headless
if args.headless:
run_headless_test()
return
# Initialize Qt application
app = QApplication(sys.argv)
app.setApplicationName("Laser Diode Test Equipment Simulation")
app.setApplicationVersion("1.0")
app.setOrganizationName("Test Equipment Simulation")
# Set application style
app.setStyle('Fusion')
# Enable high DPI support
app.setAttribute(Qt.AA_EnableHighDpiScaling)
app.setAttribute(Qt.AA_UseHighDpiPixmaps)
# Create and show main window
try:
main_window = MainWindow()
main_window.show()
# Show welcome message
QMessageBox.information(
main_window,
"Welcome",
"Laser Diode Test Equipment Simulation\n\n"
"This is a high-fidelity simulation environment for\n"
"laser diode test automation equipment.\n\n"
"For help, see the Help > About menu."
)
logger.info("Application started successfully")
except Exception as e:
logger.error(f"Failed to create main window: {e}")
QMessageBox.critical(
None,
"Startup Error",
f"Failed to start the application:\n{e}"
)
sys.exit(1)
# Run application event loop
try:
exit_code = app.exec_()
logger.info("Application shutting down")
sys.exit(exit_code)
except KeyboardInterrupt:
logger.info("Application interrupted by user")
sys.exit(0)
except Exception as e:
logger.error(f"Unexpected error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()