update
Browse files
app.py
CHANGED
|
@@ -132,6 +132,7 @@ def on_connect(client, userdata, flags, rc):
|
|
| 132 |
logger.info(f"Connected with result code {rc}")
|
| 133 |
if rc == 0:
|
| 134 |
serial = os.getenv("PRINTER_SERIAL", "0309CA471800852")
|
|
|
|
| 135 |
response_topic = f"bambu_a1_mini/response/{serial}"
|
| 136 |
client.subscribe(response_topic)
|
| 137 |
logger.info(f"Subscribed to {response_topic}")
|
|
@@ -163,6 +164,27 @@ def send_camera_command(square_id: str):
|
|
| 163 |
logger.info(f"Camera command sent for square {square_id}")
|
| 164 |
return "Camera command sent successfully"
|
| 165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
# Initialize MQTT client
|
| 167 |
mqtt_client = create_client(HOST, PORT, USERNAME, PASSWORD)
|
| 168 |
|
|
@@ -184,9 +206,7 @@ def update_printer_status():
|
|
| 184 |
f"Last Update: {latest_data['update_time']}"
|
| 185 |
)
|
| 186 |
|
| 187 |
-
def analyze_print(image,
|
| 188 |
-
nozzle_temp, print_speed, layer_height,
|
| 189 |
-
flow_rate, retraction_distance, fan_speed):
|
| 190 |
"""Analyze print quality and return evaluation results"""
|
| 191 |
if image is None:
|
| 192 |
return None, 0, 0, 0, 0, 0, 0, 0, 0
|
|
@@ -194,10 +214,8 @@ def analyze_print(image,
|
|
| 194 |
# Package current parameters
|
| 195 |
current_params = {
|
| 196 |
'nozzle_temp': float(nozzle_temp),
|
|
|
|
| 197 |
'print_speed': float(print_speed),
|
| 198 |
-
'layer_height': float(layer_height),
|
| 199 |
-
'flow_rate': float(flow_rate),
|
| 200 |
-
'retraction_distance': float(retraction_distance),
|
| 201 |
'fan_speed': float(fan_speed)
|
| 202 |
}
|
| 203 |
|
|
@@ -255,27 +273,32 @@ with gr.Blocks(title="Bambu A1 Mini Print Analysis") as demo:
|
|
| 255 |
with gr.Row():
|
| 256 |
with gr.Column():
|
| 257 |
# Print parameter inputs
|
| 258 |
-
nozzle_temp = gr.Slider(
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
fan_speed = gr.Slider(
|
| 274 |
-
|
| 275 |
-
|
|
|
|
|
|
|
| 276 |
|
| 277 |
-
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
| 279 |
|
| 280 |
with gr.Column():
|
| 281 |
# Results visualization
|
|
@@ -320,17 +343,13 @@ with gr.Blocks(title="Bambu A1 Mini Print Analysis") as demo:
|
|
| 320 |
send_print_command(dict(
|
| 321 |
nozzle_temp=params[0], # nozzle_temp slider
|
| 322 |
print_speed=params[1], # print_speed slider
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
retraction_distance=params[4], # retraction_distance slider
|
| 326 |
-
fan_speed=params[5], # fan_speed slider
|
| 327 |
-
bed_temp=60 # use default value, because there is no slider for bed_temp
|
| 328 |
))
|
| 329 |
),
|
| 330 |
inputs=[
|
| 331 |
captured_image,
|
| 332 |
-
nozzle_temp,
|
| 333 |
-
flow_rate, retraction_distance, fan_speed
|
| 334 |
],
|
| 335 |
outputs=[
|
| 336 |
result_image,
|
|
@@ -362,6 +381,13 @@ with gr.Blocks(title="Bambu A1 Mini Print Analysis") as demo:
|
|
| 362 |
|
| 363 |
threading.Thread(target=auto_refresh, daemon=True).start()
|
| 364 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
def handle_print_workflow(params):
|
| 366 |
"""Coordinate print and capture workflow"""
|
| 367 |
try:
|
|
|
|
| 132 |
logger.info(f"Connected with result code {rc}")
|
| 133 |
if rc == 0:
|
| 134 |
serial = os.getenv("PRINTER_SERIAL", "0309CA471800852")
|
| 135 |
+
request_topic = f"bambu_a1_mini/request/{serial}"
|
| 136 |
response_topic = f"bambu_a1_mini/response/{serial}"
|
| 137 |
client.subscribe(response_topic)
|
| 138 |
logger.info(f"Subscribed to {response_topic}")
|
|
|
|
| 164 |
logger.info(f"Camera command sent for square {square_id}")
|
| 165 |
return "Camera command sent successfully"
|
| 166 |
|
| 167 |
+
def send_print_parameters(nozzle_temp, bed_temp, print_speed, fan_speed):
|
| 168 |
+
"""Send new print parameters to printer"""
|
| 169 |
+
try:
|
| 170 |
+
params = {
|
| 171 |
+
'nozzle_temp': nozzle_temp,
|
| 172 |
+
'bed_temp': bed_temp,
|
| 173 |
+
'print_speed': print_speed,
|
| 174 |
+
'fan_speed': fan_speed
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
serial = os.getenv("PRINTER_SERIAL", "0309CA471800852")
|
| 178 |
+
request_topic = f"bambu_a1_mini/request/{serial}"
|
| 179 |
+
mqtt_client.publish(request_topic, json.dumps({
|
| 180 |
+
'command': 'set_parameters',
|
| 181 |
+
'parameters': params
|
| 182 |
+
}))
|
| 183 |
+
|
| 184 |
+
return "Parameters sent successfully"
|
| 185 |
+
except Exception as e:
|
| 186 |
+
return f"Error sending parameters: {e}"
|
| 187 |
+
|
| 188 |
# Initialize MQTT client
|
| 189 |
mqtt_client = create_client(HOST, PORT, USERNAME, PASSWORD)
|
| 190 |
|
|
|
|
| 206 |
f"Last Update: {latest_data['update_time']}"
|
| 207 |
)
|
| 208 |
|
| 209 |
+
def analyze_print(image, nozzle_temp, bed_temp, print_speed, fan_speed):
|
|
|
|
|
|
|
| 210 |
"""Analyze print quality and return evaluation results"""
|
| 211 |
if image is None:
|
| 212 |
return None, 0, 0, 0, 0, 0, 0, 0, 0
|
|
|
|
| 214 |
# Package current parameters
|
| 215 |
current_params = {
|
| 216 |
'nozzle_temp': float(nozzle_temp),
|
| 217 |
+
'bed_temp': float(bed_temp),
|
| 218 |
'print_speed': float(print_speed),
|
|
|
|
|
|
|
|
|
|
| 219 |
'fan_speed': float(fan_speed)
|
| 220 |
}
|
| 221 |
|
|
|
|
| 273 |
with gr.Row():
|
| 274 |
with gr.Column():
|
| 275 |
# Print parameter inputs
|
| 276 |
+
nozzle_temp = gr.Slider(
|
| 277 |
+
minimum=180, maximum=250, step=1,
|
| 278 |
+
value=200,
|
| 279 |
+
label="Nozzle Temperature (°C)"
|
| 280 |
+
)
|
| 281 |
+
bed_temp = gr.Slider(
|
| 282 |
+
minimum=40, maximum=100, step=1,
|
| 283 |
+
value=60,
|
| 284 |
+
label="Bed Temperature (°C)"
|
| 285 |
+
)
|
| 286 |
+
print_speed = gr.Slider(
|
| 287 |
+
minimum=20, maximum=150, step=1,
|
| 288 |
+
value=60,
|
| 289 |
+
label="Print Speed (mm/s)"
|
| 290 |
+
)
|
| 291 |
+
fan_speed = gr.Slider(
|
| 292 |
+
minimum=0, maximum=100, step=1,
|
| 293 |
+
value=100,
|
| 294 |
+
label="Fan Speed (%)"
|
| 295 |
+
)
|
| 296 |
|
| 297 |
+
# Buttons
|
| 298 |
+
with gr.Row():
|
| 299 |
+
capture_btn = gr.Button("Capture Image")
|
| 300 |
+
analyze_btn = gr.Button("Analyze Print")
|
| 301 |
+
send_params_btn = gr.Button("Send Print Parameters") # 新增按钮
|
| 302 |
|
| 303 |
with gr.Column():
|
| 304 |
# Results visualization
|
|
|
|
| 343 |
send_print_command(dict(
|
| 344 |
nozzle_temp=params[0], # nozzle_temp slider
|
| 345 |
print_speed=params[1], # print_speed slider
|
| 346 |
+
bed_temp=params[1], # bed_temp slider
|
| 347 |
+
fan_speed=params[2], # fan_speed slider
|
|
|
|
|
|
|
|
|
|
| 348 |
))
|
| 349 |
),
|
| 350 |
inputs=[
|
| 351 |
captured_image,
|
| 352 |
+
nozzle_temp, bed_temp, print_speed, fan_speed
|
|
|
|
| 353 |
],
|
| 354 |
outputs=[
|
| 355 |
result_image,
|
|
|
|
| 381 |
|
| 382 |
threading.Thread(target=auto_refresh, daemon=True).start()
|
| 383 |
|
| 384 |
+
# 连接按钮
|
| 385 |
+
send_params_btn.click(
|
| 386 |
+
fn=send_print_parameters,
|
| 387 |
+
inputs=[nozzle_temp, bed_temp, print_speed, fan_speed],
|
| 388 |
+
outputs=[current_job_status]
|
| 389 |
+
)
|
| 390 |
+
|
| 391 |
def handle_print_workflow(params):
|
| 392 |
"""Coordinate print and capture workflow"""
|
| 393 |
try:
|