Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration tests #58

Merged
merged 6 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/data/config/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sensor:
beam_diameter: 120.0 # μm
middle_output: 9400
response_time: 15.0 # ms
tolerance: 0.6
tolerance: 2.6
not_in_range_output: 18800
trace:
min_measure_count: 5
Expand Down
2 changes: 1 addition & 1 deletion server/data/config/production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sensor:
beam_diameter: 120 # μm
middle_output: 9400
response_time: 10.0 # ms
tolerance: 0.6
tolerance: 2.6
not_in_range_output: 18800
trace:
min_measure_count: 10
Expand Down
157 changes: 38 additions & 119 deletions server/server/measure/estimate.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
import paho.mqtt.client as mqtt
from server.listener import status
from server.config import (
GCODE_PATH,
LISTENER_LOG_TOPIC,
MODEL_PATH,
MQTT_BROKER_URL,
MQTT_PASSWORD,
MQTT_USERNAME,
get_config,
)
from time import time
from server.mark.edge import (
import_edge_results,
delete_edge_results,
)
from server.mark.gcode import get_gcode_filename
from .sensor import get_sensor_data, sensor_output_to_mm
from server.model import get_model_data
import numpy as np
from .gcode import (
load_gcode,
get_start_end_points_from_line_number,
)
from .mtconnect import MtctDataChecker, update_mtct_latency
from server.mark import arc, pair
from server.mark.trace import (
Expand All @@ -31,7 +21,6 @@
from server.mark.trace import import_trace_line_results
import logging
from scipy.ndimage import uniform_filter1d
import trimesh

logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s: %(message)s")
logger = logging.getLogger(__name__)
Expand All @@ -42,146 +31,74 @@ def __init__(self, mysql_config: dict, model_id: int, process_id: int):
self.mysql_config = mysql_config
self.model_id = model_id
self.process_id = process_id
self.conf = get_config()

self.mtct_data_checker = MtctDataChecker(mysql_config, model_id, process_id)
self.mtct_lines = self.mtct_data_checker.estimate_timestamps_from_mtct_data()
# self.mtct_lines = self.mtct_data_checker.adjust_delays(mtct_lines)
sensor_data = get_sensor_data(process_id, mysql_config)
self.np_sensor_data = np.array(sensor_data)

model_row = get_model_data(model_id)
filename = model_row[1]
self.stl_filepath = f"{MODEL_PATH}/{filename}"
self.mesh = trimesh.load(self.stl_filepath)
process_status = status.get_process_status(mysql_config, process_id)
self.offset = (process_status[4], process_status[5], process_status[6])
gcode_filename = get_gcode_filename(filename)
gcode_file_path = f"{GCODE_PATH}/{gcode_filename}"
self.gcode = load_gcode(gcode_file_path)
self.np_sensor_data = np.array(self.mtct_data_checker.sensor_data)
self.first_line_for_tracing = self.mtct_data_checker.first_line_for_tracing

def get_edge_results(self, start_timestamp, end_timestamp, line):
"""
Estimate the exact coordinate of the edge from mtconnect data and
sensor data using timestamp
"""
# feedrate = row[7] # feedrate from MTConnect is not accurate
(start, end, feedrate) = get_start_end_points_from_line_number(self.gcode, line)

# get sensor data that is between start and end timestamp
for sensor_row in self.np_sensor_data:
sensor_timestamp = sensor_row[2]
sensor_output = sensor_row[3]
if start_timestamp <= sensor_timestamp <= end_timestamp:
(
sensor_output_valid,
edge_ids,
) = self.mtct_data_checker.validate_sensor_output(sensor_output, line)
if not sensor_output_valid:
def get_tracing_mtct_lines(self):
for mtct_line in self.mtct_lines:
line_number = mtct_line[0]
start_timestamp = mtct_line[1]
end_timestamp = mtct_line[2]
if (
self.first_line_for_tracing
and line_number > self.first_line_for_tracing - 2
):
if line_number % 2 == 0:
# Not measuring
continue
edge_coord = self.mtct_data_checker.sensor_timestamp_to_coord(
start_timestamp,
sensor_timestamp,
start,
end,
feedrate,
)
"""
when edges are overlapping, multiple edges can be measured
for a single line
"""
# edge_ids = get_edge_id_from_line_number(
# self.mysql_config, self.model_id, line
# )

# ignore the rest of the sensor data
# multiple edges can be measured due to the following reasons:
# - noise (can be reduced by increasing the sensor threshold)
# - sensor restart
# - timestamp is not accurate
results = []
measured_z = round(sensor_output_to_mm(sensor_output), 3)
for edge_id in edge_ids:
results.append(
(
edge_id,
self.process_id,
edge_coord[0],
edge_coord[1],
measured_z,
)
)
return results
yield (line_number, start_timestamp, end_timestamp)

def get_trace_line_result(self, start_timestamp, end_timestamp, line):
"""
Estimate the approximate distance between the workpiece and the sensor
"""
distance_results = []
# get sensor data that is between start and end timestamp
for sensor_row in self.np_sensor_data:
sensor_timestamp = sensor_row[2]
if start_timestamp <= sensor_timestamp <= end_timestamp:
distance_results.append(sensor_row[3])
sensor_data_during_line = self.np_sensor_data[
np.logical_and(
self.np_sensor_data[:, 2] >= start_timestamp,
self.np_sensor_data[:, 2] <= end_timestamp,
)
]
distance_results = sensor_data_during_line[:, 3].astype(float)

# Apply moving average filter to remove fluctuations
filtered_distance_results = uniform_filter1d(distance_results, size=5)

average_sensor_output = np.mean(filtered_distance_results)

if len(filtered_distance_results) == 0:
return
average_sensor_output = np.mean(filtered_distance_results)

trace_line_id = get_trace_line_id_from_line_number(
self.mysql_config, self.model_id, line
)
if trace_line_id is None:
logger.warning(f"trace_line_id is None for line {line}")
return
return [trace_line_id, self.process_id, average_sensor_output]

def compute_updating_data(self):
current_line = 0
edge_update_list = []
trace_line_update_list = []
for mtct_line in self.mtct_lines:
line_number = mtct_line[0]
start_timestamp = mtct_line[1]
end_timestamp = mtct_line[2]

for (
line_number,
start_timestamp,
end_timestamp,
) in self.get_tracing_mtct_lines():
if line_number == current_line:
continue

# Edge detection
if (
not self.first_line_for_tracing
or line_number < self.first_line_for_tracing - 2
):
if line_number % 2 != 0:
# Not measuring
continue

edge_results = self.get_edge_results(
start_timestamp, end_timestamp, line_number
)
if edge_results:
edge_update_list += edge_results

# waiting
elif line_number == self.first_line_for_tracing - 2:
continue

# tracing
else:
if line_number % 2 == 0:
# Not measuring
continue
trace_line_result = self.get_trace_line_result(
start_timestamp, end_timestamp, line_number
)
if trace_line_result:
trace_line_update_list.append(trace_line_result)
trace_line_result = self.get_trace_line_result(
start_timestamp, end_timestamp, line_number
)
if trace_line_result:
trace_line_update_list.append(trace_line_result)

current_line = line_number

return edge_update_list, trace_line_update_list
return self.mtct_data_checker.edge_results, trace_line_update_list


def update_data_after_measurement(
Expand Down Expand Up @@ -258,7 +175,9 @@ def recompute(mysql_config: dict, process_id: int):
process_data = status.get_process_status(mysql_config, process_id)
model_id = process_data[1]
result = Result(mysql_config, model_id, process_id)
compute_start_time = time()
edge_update_list, trace_line_update_list = result.compute_updating_data()
logger.info(f"compute_updating_data() took {time() - compute_start_time} seconds")
logger.info("compute_updating_data() done")

edge_count = len(edge_update_list)
Expand Down
Loading
Loading