2025-01-17 14:14:28 +00:00
|
|
|
from PyQt5.QtGui import QMovie
|
|
|
|
|
import logging
|
2025-01-23 14:35:10 +00:00
|
|
|
from src.ui.test_test import Test_Test
|
2025-01-22 11:27:15 +00:00
|
|
|
import time
|
2025-01-24 09:23:49 +00:00
|
|
|
from PyQt5.QtWidgets import QMessageBox, QDialog, QApplication
|
2025-01-22 14:21:08 +00:00
|
|
|
from PyQt5.QtCore import QByteArray
|
2025-01-17 14:14:28 +00:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Test_Pipe_Cutter(Test_Test):
|
|
|
|
|
"""
|
|
|
|
|
A class to manage and operate a pipe cutter machine in a test environment.
|
|
|
|
|
"""
|
|
|
|
|
|
2025-01-24 07:23:45 +00:00
|
|
|
def __init__(self, config, components, recipe=None, run_once=False,reset_on_start=True, enable_override=False, bench_name="generic", step=None,):
|
2025-01-22 11:27:15 +00:00
|
|
|
super().__init__(components=components, recipe=recipe, step=step, run_once=run_once,reset_on_start=reset_on_start, enable_override=enable_override,)
|
2025-01-23 14:35:10 +00:00
|
|
|
self.end_cut = None
|
2025-01-21 14:44:47 +00:00
|
|
|
self.config = config
|
2025-01-17 14:14:28 +00:00
|
|
|
self.bench_name = bench_name
|
2025-01-22 11:27:15 +00:00
|
|
|
self.step=step
|
|
|
|
|
self.current_cut_length = 0
|
|
|
|
|
self.get_connection = None
|
2025-01-21 14:44:47 +00:00
|
|
|
|
2025-01-22 14:21:08 +00:00
|
|
|
gif_path = "config/instruction_images/st-ten-10/pipe.gif"
|
|
|
|
|
self.movie = QMovie(gif_path,QByteArray(), self)
|
2025-01-22 11:27:15 +00:00
|
|
|
self.pipe_gif.setMovie(self.movie)
|
|
|
|
|
self.movie.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.pipe_gif.setMovie(self.movie)
|
2025-01-17 14:14:28 +00:00
|
|
|
|
|
|
|
|
def start(self, recipe=None, step=None, pieces=None):
|
2025-01-24 09:23:49 +00:00
|
|
|
"""
|
|
|
|
|
Iterate through pipe cutter statuses, handling each status appropriately.
|
|
|
|
|
"""
|
2025-01-22 11:27:15 +00:00
|
|
|
self.get_connection = self.components["pipe_cutter"].out.connect(self.get)
|
2025-01-21 14:44:47 +00:00
|
|
|
self.components["pipe_cutter"].resume()
|
2025-01-22 11:27:15 +00:00
|
|
|
|
2025-01-24 09:23:49 +00:00
|
|
|
# Call the parent start process
|
2025-01-22 11:27:15 +00:00
|
|
|
show = super().start(recipe=recipe, step=step, pieces=pieces)
|
|
|
|
|
if show is False:
|
|
|
|
|
return show
|
|
|
|
|
|
2025-01-24 09:23:49 +00:00
|
|
|
# Read the current status of the pipe cutter
|
|
|
|
|
self.current_status = self.components["pipe_cutter"].read(register=766)
|
|
|
|
|
status_actions = {
|
|
|
|
|
102: "ready for operation",
|
|
|
|
|
103: "running",
|
|
|
|
|
105: "emergency",
|
|
|
|
|
100: "need to reset"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for idx, status in enumerate([self.current_status]): # Iterate over the current status
|
|
|
|
|
print(f"Processing status {status_actions.get(status, 'unknown')} at index {idx}")
|
|
|
|
|
|
|
|
|
|
if status == 102: # Ready for operation
|
|
|
|
|
print(f"Status {status}: Starting the cutting process.")
|
|
|
|
|
self.start_cutting(recipe=recipe, step=step)
|
|
|
|
|
|
|
|
|
|
elif status == 103: # Running
|
|
|
|
|
print(f"Status {status}: The pipe cutter is running. Monitoring the system.")
|
|
|
|
|
self.display_text(text="TAGLIO CORRUGATO IN CORSO", bg_color="yellow")
|
|
|
|
|
|
|
|
|
|
elif status == 105: # Emergency
|
|
|
|
|
print(f"Status {status}: Emergency detected! Resetting the system.")
|
|
|
|
|
try:
|
|
|
|
|
self.display_text(text="EMERGENZA INTERNEVUTA", bg_color="red")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Failed to handle the emergency state: {e}")
|
|
|
|
|
|
|
|
|
|
elif status == 100: # Need to reset
|
|
|
|
|
print(f"Status {status}: The system requires resetting. Performing reset.")
|
|
|
|
|
try:
|
|
|
|
|
self.reset()
|
|
|
|
|
self.display_text(text="RESET IN CORSO", bg_color="yellow")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Failed to reset the system: {e}")
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
# Handle unknown statuses
|
|
|
|
|
print(f"Unknown status: {status}. Skipping operation.")
|
|
|
|
|
logger.warning(f"Unknown pipe cutter status {status} detected.")
|
|
|
|
|
|
|
|
|
|
# Final steps after all statuses are processed
|
|
|
|
|
if step is not None:
|
2025-01-17 14:14:28 +00:00
|
|
|
self.stop_cutting()
|
2025-01-23 10:15:13 +00:00
|
|
|
self.stop()
|
2025-01-24 09:23:49 +00:00
|
|
|
|
2025-01-22 11:27:15 +00:00
|
|
|
return show
|
2025-01-17 14:14:28 +00:00
|
|
|
|
2025-01-24 09:23:49 +00:00
|
|
|
def start_cutting(self, recipe=None, step=None):
|
2025-01-17 14:14:28 +00:00
|
|
|
"""
|
2025-01-24 09:23:49 +00:00
|
|
|
Perform the pipe cutting process when in the 'ready for operation' state (102).
|
2025-01-17 14:14:28 +00:00
|
|
|
"""
|
2025-01-24 09:23:49 +00:00
|
|
|
self.lenght = self.step.spec["lenght"]
|
|
|
|
|
self.diameter = self.step.spec["diameter"]
|
|
|
|
|
try:
|
2025-01-23 09:58:14 +00:00
|
|
|
self.components["pipe_cutter"].write_total_length(self.lenght)
|
|
|
|
|
self.components["pipe_cutter"].write_od_of_pipe(self.diameter)
|
2025-01-22 11:27:15 +00:00
|
|
|
time.sleep(1)
|
2025-01-24 09:23:49 +00:00
|
|
|
self.components["pipe_cutter"].write_bit_with_delay(600, 0, 100)
|
|
|
|
|
print(f"Status 102: Pipe cutting process started successfully.")
|
|
|
|
|
except Exception as e:
|
2025-01-17 14:14:28 +00:00
|
|
|
logger.error(f"Failed to start the pipe cutting process: {e}")
|
|
|
|
|
|
|
|
|
|
def stop_cutting(self):
|
|
|
|
|
"""
|
|
|
|
|
Stop the pipe cutting process and deactivate the pipe cutter component.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
2025-01-21 14:44:47 +00:00
|
|
|
self.components["pipe_cutter"].write_bit_with_delay(600, 2, 100)
|
2025-01-23 14:35:10 +00:00
|
|
|
self.end_cut = True
|
2025-01-17 14:14:28 +00:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Failed to stop the pipe cutting process: {e}")
|
|
|
|
|
|
2025-01-22 11:27:15 +00:00
|
|
|
def stop(self):
|
|
|
|
|
self.components["pipe_cutter"].pause()
|
|
|
|
|
self.disconnect(self.get_connection)
|
2025-01-24 09:23:49 +00:00
|
|
|
self.display_text(text="TAGLIO INTERROTTO", bg_color="yellow")
|
2025-01-24 07:21:22 +00:00
|
|
|
super().stop()
|
2025-01-23 10:15:13 +00:00
|
|
|
|
|
|
|
|
|
2025-01-22 11:27:15 +00:00
|
|
|
|
2025-01-17 14:14:28 +00:00
|
|
|
def reset_machine(self):
|
|
|
|
|
"""
|
|
|
|
|
Reset the pipe cutter machine to its initial state.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
2025-01-21 14:44:47 +00:00
|
|
|
self.components["pipe_cutter"].write_bit_with_delay(600, 1, 100)
|
2025-01-17 14:14:28 +00:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Failed to reset the pipe cutter machine: {e}")
|
|
|
|
|
|
2025-01-23 14:35:10 +00:00
|
|
|
def get(self, data=None, override=False):
|
|
|
|
|
if self.done: # avoid processing if completed
|
|
|
|
|
return
|
|
|
|
|
if data is None or data[-1] is None:
|
|
|
|
|
super().get(None, override=override)
|
|
|
|
|
return
|
|
|
|
|
data = data[-1]
|
|
|
|
|
if self.end_cut is True:
|
2025-01-23 15:15:49 +00:00
|
|
|
ok = True
|
2025-01-23 14:35:10 +00:00
|
|
|
else:
|
|
|
|
|
ok = None
|
|
|
|
|
|
|
|
|
|
results = {"ok": ok}
|
|
|
|
|
super().get([{
|
|
|
|
|
"time": data.get("time", None),
|
|
|
|
|
"results": results
|
2025-01-24 09:23:49 +00:00
|
|
|
}], override=override, fail=ok is False)
|
|
|
|
|
|
|
|
|
|
def display_text(self,text="", bg_color=None,text_color=None):
|
|
|
|
|
if self.parent_assembly_widget is not None:
|
|
|
|
|
self.parent_assembly_widget().set_text(text=text, bg_color=bg_color,text_color=text_color)
|
|
|
|
|
QApplication.processEvents()
|
|
|
|
|
time.sleep(0.3)
|
|
|
|
|
QApplication.processEvents()
|