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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
from dataclasses import dataclass
|
| 5 |
+
|
| 6 |
+
@dataclass
|
| 7 |
+
class Project:
|
| 8 |
+
name: str
|
| 9 |
+
difficulty: str
|
| 10 |
+
components: list
|
| 11 |
+
description: str
|
| 12 |
+
model_data: dict
|
| 13 |
+
|
| 14 |
+
class VirtualLab3D:
|
| 15 |
+
def __init__(self):
|
| 16 |
+
self.projects = {
|
| 17 |
+
'basic': self._load_basic_projects(),
|
| 18 |
+
'intermediate': self._load_intermediate_projects(),
|
| 19 |
+
'advanced': self._load_advanced_projects()
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
def _load_basic_projects(self):
|
| 23 |
+
return {
|
| 24 |
+
'led_circuit': {
|
| 25 |
+
'name': 'LED Circuit',
|
| 26 |
+
'components': ['LED', 'Resistor', 'Battery'],
|
| 27 |
+
'steps': ['Connect resistor to LED', 'Add power source'],
|
| 28 |
+
'model': self._create_led_model()
|
| 29 |
+
},
|
| 30 |
+
'voltage_divider': {
|
| 31 |
+
'name': 'Voltage Divider',
|
| 32 |
+
'components': ['Resistor x2', 'Power Source'],
|
| 33 |
+
'steps': ['Series connection', 'Measure output voltage'],
|
| 34 |
+
'model': self._create_voltage_divider_model()
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
def _load_intermediate_projects(self):
|
| 39 |
+
return {
|
| 40 |
+
'arduino_robot': {
|
| 41 |
+
'name': 'Arduino Robot Arm',
|
| 42 |
+
'components': ['Arduino Uno', 'Servo Motors x3', 'Power Supply'],
|
| 43 |
+
'steps': ['Servo wiring', 'Arduino programming', '3D assembly'],
|
| 44 |
+
'model': self._create_robot_model()
|
| 45 |
+
},
|
| 46 |
+
'oscilloscope': {
|
| 47 |
+
'name': 'Digital Oscilloscope',
|
| 48 |
+
'components': ['MCU', 'ADC', 'Display', 'Amplifier'],
|
| 49 |
+
'steps': ['Signal conditioning', 'ADC setup', 'Display interface'],
|
| 50 |
+
'model': self._create_oscilloscope_model()
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
def _load_advanced_projects(self):
|
| 55 |
+
return {
|
| 56 |
+
'smart_grid': {
|
| 57 |
+
'name': 'Smart Grid Simulator',
|
| 58 |
+
'components': ['Power Sources', 'Load Banks', 'Smart Meters'],
|
| 59 |
+
'steps': ['Grid topology', 'Power flow analysis', 'Control system'],
|
| 60 |
+
'model': self._create_smart_grid_model()
|
| 61 |
+
},
|
| 62 |
+
'quantum_circuit': {
|
| 63 |
+
'name': 'Quantum Circuit Simulator',
|
| 64 |
+
'components': ['Quantum Gates', 'Qubits', 'Measurement Units'],
|
| 65 |
+
'steps': ['Circuit design', 'Quantum operations', 'State analysis'],
|
| 66 |
+
'model': self._create_quantum_model()
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
def _create_led_model(self):
|
| 71 |
+
# Example 3D model data for an LED circuit
|
| 72 |
+
return {
|
| 73 |
+
'name': 'LED Circuit',
|
| 74 |
+
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
|
| 75 |
+
'faces': np.array([[0, 1, 2], [0, 2, 3]])
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
def _create_voltage_divider_model(self):
|
| 79 |
+
# Example 3D model data for a voltage divider
|
| 80 |
+
return {
|
| 81 |
+
'name': 'Voltage Divider',
|
| 82 |
+
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
|
| 83 |
+
'faces': np.array([[0, 1, 2], [0, 2, 3]])
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
def _create_robot_model(self):
|
| 87 |
+
# Example 3D model data for a robot arm
|
| 88 |
+
return {
|
| 89 |
+
'name': 'Arduino Robot Arm',
|
| 90 |
+
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
|
| 91 |
+
'faces': np.array([[0, 1, 2], [0, 2, 3]])
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
def _create_oscilloscope_model(self):
|
| 95 |
+
# Example 3D model data for an oscilloscope
|
| 96 |
+
return {
|
| 97 |
+
'name': 'Digital Oscilloscope',
|
| 98 |
+
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
|
| 99 |
+
'faces': np.array([[0, 1, 2], [0, 2, 3]])
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
def _create_smart_grid_model(self):
|
| 103 |
+
# Example 3D model data for a smart grid
|
| 104 |
+
return {
|
| 105 |
+
'name': 'Smart Grid Simulator',
|
| 106 |
+
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
|
| 107 |
+
'faces': np.array([[0, 1, 2], [0, 2, 3]])
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
def _create_quantum_model(self):
|
| 111 |
+
# Example 3D model data for a quantum circuit
|
| 112 |
+
return {
|
| 113 |
+
'name': 'Quantum Circuit Simulator',
|
| 114 |
+
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
|
| 115 |
+
'faces': np.array([[0, 1, 2], [0, 2, 3]])
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
def _create_3d_model(self, data):
|
| 119 |
+
fig = go.Figure(data=[
|
| 120 |
+
go.Mesh3d(
|
| 121 |
+
x=data['vertices'][:, 0],
|
| 122 |
+
y=data['vertices'][:, 1],
|
| 123 |
+
z=data['vertices'][:, 2],
|
| 124 |
+
i=data['faces'][:, 0],
|
| 125 |
+
j=data['faces'][:, 1],
|
| 126 |
+
k=data['faces'][:, 2],
|
| 127 |
+
color='lightblue',
|
| 128 |
+
opacity=0.7
|
| 129 |
+
)
|
| 130 |
+
])
|
| 131 |
+
fig.update_layout(
|
| 132 |
+
scene=dict(
|
| 133 |
+
aspectmode='data'
|
| 134 |
+
),
|
| 135 |
+
title=data['name']
|
| 136 |
+
)
|
| 137 |
+
return fig
|
| 138 |
+
|
| 139 |
+
def get_project_info(self, difficulty, project_name):
|
| 140 |
+
project = self.projects[difficulty][project_name]
|
| 141 |
+
model_data = self._create_3d_model(project['model'])
|
| 142 |
+
return {
|
| 143 |
+
'name': project['name'],
|
| 144 |
+
'components': project['components'],
|
| 145 |
+
'steps': project['steps'],
|
| 146 |
+
'model': model_data,
|
| 147 |
+
'difficulty': difficulty
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
def simulate_project(self, project_data, parameters):
|
| 151 |
+
# Simulation logic based on project type
|
| 152 |
+
results = {
|
| 153 |
+
'voltage': [],
|
| 154 |
+
'current': [],
|
| 155 |
+
'power': [],
|
| 156 |
+
'temperature': []
|
| 157 |
+
}
|
| 158 |
+
t = np.linspace(0, 10, 100)
|
| 159 |
+
|
| 160 |
+
if project_data['difficulty'] == 'basic':
|
| 161 |
+
results['voltage'] = np.sin(t)
|
| 162 |
+
results['current'] = np.cos(t)
|
| 163 |
+
elif project_data['difficulty'] == 'intermediate':
|
| 164 |
+
results['voltage'] = np.sin(2*t)
|
| 165 |
+
results['current'] = np.cos(2*t)
|
| 166 |
+
results['temperature'] = 20 + 5*np.sin(t/2)
|
| 167 |
+
else:
|
| 168 |
+
results['voltage'] = np.sin(3*t)
|
| 169 |
+
results['current'] = np.cos(3*t)
|
| 170 |
+
results['power'] = np.sin(3*t) * np.cos(3*t)
|
| 171 |
+
results['temperature'] = 25 + 10*np.sin(t/3)
|
| 172 |
+
|
| 173 |
+
return results
|
| 174 |
+
|
| 175 |
+
def create_interface():
|
| 176 |
+
def process_project(difficulty, project_name, parameters):
|
| 177 |
+
lab = VirtualLab3D()
|
| 178 |
+
project_info = lab.get_project_info(difficulty, project_name)
|
| 179 |
+
simulation = lab.simulate_project(project_info, parameters)
|
| 180 |
+
|
| 181 |
+
return (
|
| 182 |
+
project_info['model'],
|
| 183 |
+
f"""
|
| 184 |
+
π Project: {project_info['name']}
|
| 185 |
+
|
| 186 |
+
π§ Components Required:
|
| 187 |
+
{', '.join(project_info['components'])}
|
| 188 |
+
|
| 189 |
+
π Implementation Steps:
|
| 190 |
+
{', '.join(project_info['steps'])}
|
| 191 |
+
|
| 192 |
+
π Simulation Results:
|
| 193 |
+
- Peak Voltage: {max(simulation['voltage']):.2f}V
|
| 194 |
+
- Max Current: {max(simulation['current']):.2f}A
|
| 195 |
+
- Power Usage: {np.mean(simulation.get('power', [0])):.2f}W
|
| 196 |
+
|
| 197 |
+
π― Learning Objectives:
|
| 198 |
+
1. Circuit design principles
|
| 199 |
+
2. Component integration
|
| 200 |
+
3. System analysis
|
| 201 |
+
4. Troubleshooting skills
|
| 202 |
+
"""
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
iface = gr.Interface(
|
| 206 |
+
fn=process_project,
|
| 207 |
+
inputs=[
|
| 208 |
+
gr.Dropdown(choices=["basic", "intermediate", "advanced"], label="Difficulty Level"),
|
| 209 |
+
gr.Dropdown(choices=["led_circuit", "voltage_divider", "arduino_robot", "oscilloscope", "smart_grid", "quantum_circuit"], label="Project Type"),
|
| 210 |
+
gr.Slider(minimum=0, maximum=100, label="Simulation Parameters")
|
| 211 |
+
],
|
| 212 |
+
outputs=[
|
| 213 |
+
gr.Plot(label="3D Model Visualization"),
|
| 214 |
+
gr.Textbox(label="Project Details and Analysis")
|
| 215 |
+
],
|
| 216 |
+
title="π¬ 3D Virtual Electrical Engineering Lab",
|
| 217 |
+
description="Experiment with various electrical projects in a 3D virtual environment"
|
| 218 |
+
)
|
| 219 |
+
return iface
|
| 220 |
+
|
| 221 |
+
if __name__ == "__main__":
|
| 222 |
+
interface = create_interface()
|
| 223 |
+
interface.launch(share=True)
|