File size: 8,017 Bytes
5f9a692 |
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 |
import gradio as gr
import numpy as np
import plotly.graph_objects as go
from dataclasses import dataclass
@dataclass
class Project:
name: str
difficulty: str
components: list
description: str
model_data: dict
class VirtualLab3D:
def __init__(self):
self.projects = {
'basic': self._load_basic_projects(),
'intermediate': self._load_intermediate_projects(),
'advanced': self._load_advanced_projects()
}
def _load_basic_projects(self):
return {
'led_circuit': {
'name': 'LED Circuit',
'components': ['LED', 'Resistor', 'Battery'],
'steps': ['Connect resistor to LED', 'Add power source'],
'model': self._create_led_model()
},
'voltage_divider': {
'name': 'Voltage Divider',
'components': ['Resistor x2', 'Power Source'],
'steps': ['Series connection', 'Measure output voltage'],
'model': self._create_voltage_divider_model()
}
}
def _load_intermediate_projects(self):
return {
'arduino_robot': {
'name': 'Arduino Robot Arm',
'components': ['Arduino Uno', 'Servo Motors x3', 'Power Supply'],
'steps': ['Servo wiring', 'Arduino programming', '3D assembly'],
'model': self._create_robot_model()
},
'oscilloscope': {
'name': 'Digital Oscilloscope',
'components': ['MCU', 'ADC', 'Display', 'Amplifier'],
'steps': ['Signal conditioning', 'ADC setup', 'Display interface'],
'model': self._create_oscilloscope_model()
}
}
def _load_advanced_projects(self):
return {
'smart_grid': {
'name': 'Smart Grid Simulator',
'components': ['Power Sources', 'Load Banks', 'Smart Meters'],
'steps': ['Grid topology', 'Power flow analysis', 'Control system'],
'model': self._create_smart_grid_model()
},
'quantum_circuit': {
'name': 'Quantum Circuit Simulator',
'components': ['Quantum Gates', 'Qubits', 'Measurement Units'],
'steps': ['Circuit design', 'Quantum operations', 'State analysis'],
'model': self._create_quantum_model()
}
}
def _create_led_model(self):
# Example 3D model data for an LED circuit
return {
'name': 'LED Circuit',
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
'faces': np.array([[0, 1, 2], [0, 2, 3]])
}
def _create_voltage_divider_model(self):
# Example 3D model data for a voltage divider
return {
'name': 'Voltage Divider',
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
'faces': np.array([[0, 1, 2], [0, 2, 3]])
}
def _create_robot_model(self):
# Example 3D model data for a robot arm
return {
'name': 'Arduino Robot Arm',
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
'faces': np.array([[0, 1, 2], [0, 2, 3]])
}
def _create_oscilloscope_model(self):
# Example 3D model data for an oscilloscope
return {
'name': 'Digital Oscilloscope',
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
'faces': np.array([[0, 1, 2], [0, 2, 3]])
}
def _create_smart_grid_model(self):
# Example 3D model data for a smart grid
return {
'name': 'Smart Grid Simulator',
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
'faces': np.array([[0, 1, 2], [0, 2, 3]])
}
def _create_quantum_model(self):
# Example 3D model data for a quantum circuit
return {
'name': 'Quantum Circuit Simulator',
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
'faces': np.array([[0, 1, 2], [0, 2, 3]])
}
def _create_3d_model(self, data):
fig = go.Figure(data=[
go.Mesh3d(
x=data['vertices'][:, 0],
y=data['vertices'][:, 1],
z=data['vertices'][:, 2],
i=data['faces'][:, 0],
j=data['faces'][:, 1],
k=data['faces'][:, 2],
color='lightblue',
opacity=0.7
)
])
fig.update_layout(
scene=dict(
aspectmode='data'
),
title=data['name']
)
return fig
def get_project_info(self, difficulty, project_name):
project = self.projects[difficulty][project_name]
model_data = self._create_3d_model(project['model'])
return {
'name': project['name'],
'components': project['components'],
'steps': project['steps'],
'model': model_data,
'difficulty': difficulty
}
def simulate_project(self, project_data, parameters):
# Simulation logic based on project type
results = {
'voltage': [],
'current': [],
'power': [],
'temperature': []
}
t = np.linspace(0, 10, 100)
if project_data['difficulty'] == 'basic':
results['voltage'] = np.sin(t)
results['current'] = np.cos(t)
elif project_data['difficulty'] == 'intermediate':
results['voltage'] = np.sin(2*t)
results['current'] = np.cos(2*t)
results['temperature'] = 20 + 5*np.sin(t/2)
else:
results['voltage'] = np.sin(3*t)
results['current'] = np.cos(3*t)
results['power'] = np.sin(3*t) * np.cos(3*t)
results['temperature'] = 25 + 10*np.sin(t/3)
return results
def create_interface():
def process_project(difficulty, project_name, parameters):
lab = VirtualLab3D()
project_info = lab.get_project_info(difficulty, project_name)
simulation = lab.simulate_project(project_info, parameters)
return (
project_info['model'],
f"""
π Project: {project_info['name']}
π§ Components Required:
{', '.join(project_info['components'])}
π Implementation Steps:
{', '.join(project_info['steps'])}
π Simulation Results:
- Peak Voltage: {max(simulation['voltage']):.2f}V
- Max Current: {max(simulation['current']):.2f}A
- Power Usage: {np.mean(simulation.get('power', [0])):.2f}W
π― Learning Objectives:
1. Circuit design principles
2. Component integration
3. System analysis
4. Troubleshooting skills
"""
)
iface = gr.Interface(
fn=process_project,
inputs=[
gr.Dropdown(choices=["basic", "intermediate", "advanced"], label="Difficulty Level"),
gr.Dropdown(choices=["led_circuit", "voltage_divider", "arduino_robot", "oscilloscope", "smart_grid", "quantum_circuit"], label="Project Type"),
gr.Slider(minimum=0, maximum=100, label="Simulation Parameters")
],
outputs=[
gr.Plot(label="3D Model Visualization"),
gr.Textbox(label="Project Details and Analysis")
],
title="π¬ 3D Virtual Electrical Engineering Lab",
description="Experiment with various electrical projects in a 3D virtual environment"
)
return iface
if __name__ == "__main__":
interface = create_interface()
interface.launch(share=True) |