diff --git a/cnceye/edge/find.py b/cnceye/edge/find.py index f043198..bf35636 100644 --- a/cnceye/edge/find.py +++ b/cnceye/edge/find.py @@ -3,6 +3,7 @@ import mysql.connector from mysql.connector.errors import IntegrityError + def find_edge(filepath: str, minimal_diff: float = 5.0): # read csv with open(filepath, newline="") as csvfile: @@ -19,9 +20,7 @@ def find_edge(filepath: str, minimal_diff: float = 5.0): previous_distance = distance -def find_edges( - process_id: int, mysql_config: dict, minimal_diff: float = 5.0 -): +def find_edges(process_id: int, mysql_config: dict, minimal_diff: float = 5.0): cnx = mysql.connector.connect(**mysql_config, database="coord") cursor = cnx.cursor() query = "SELECT * FROM sensor WHERE process_id = %s" @@ -145,7 +144,9 @@ def add_measured_edge_coord(edge_list: list, mysql_config): cnx.close() -def process_edges(model_id: int, process_id: int, mysql_config: dict, minimal_diff: float = 5.0) -> int: +def process_edges( + model_id: int, process_id: int, mysql_config: dict, minimal_diff: float = 5.0 +) -> int: """ Identify the edges from the sensor data and add the coordinates to the database """ diff --git a/scripts/simple_measurement.py b/scripts/simple_measurement.py index 805b161..c8c5afc 100644 --- a/scripts/simple_measurement.py +++ b/scripts/simple_measurement.py @@ -6,6 +6,7 @@ import sqlite3 import csv import sys +import random # Get the active object (the 3D model) obj = bpy.data.objects["test-part"] @@ -14,6 +15,8 @@ # x, y, z, distance data = [] +current_data = 100000 +threshold = 100 # Ensure the object has a mesh assert obj.type == "MESH" @@ -21,7 +24,9 @@ def distance_to_analog_output(distance: float): distance = distance * 1000 * 135 # m to mm, distance to analog output - return float(round(distance)) + # add noise + distance = distance + (distance * 0.001 * (2 * random.random() - 1)) + return round(distance) def load_gcode(filepath: str): @@ -40,6 +45,7 @@ def move_start_point(_start_point, xyz, feedrate: float): feedrate: float (mm/min) sensor response time is 10ms """ + global current_data one_step_distance = feedrate / 1000 / 60 * 0.01 # m/step destination = Vector(tuple([x / 1000 for x in xyz])) total_distance_to_move = (destination - _start_point).length @@ -57,7 +63,10 @@ def move_start_point(_start_point, xyz, feedrate: float): xyz = [_start_point.x, _start_point.y, _start_point.z] xyz = [round(x * 1000, 3) for x in xyz] - data.append([*xyz, distance_to_analog_output(distance)]) + _sensor_data = distance_to_analog_output(distance) + if abs(_sensor_data - current_data) > threshold: + data.append([*xyz, _sensor_data]) + current_data = _sensor_data _start_point = _start_point + move_vector return destination # _start_point not may not become exactly the destination diff --git a/tests/fixtures/db/listener.db b/tests/fixtures/db/listener.db index dc765bd..695a568 100644 Binary files a/tests/fixtures/db/listener.db and b/tests/fixtures/db/listener.db differ diff --git a/tests/test_script_utils.py b/tests/test_script_utils.py new file mode 100644 index 0000000..0b47072 --- /dev/null +++ b/tests/test_script_utils.py @@ -0,0 +1,18 @@ +import random + + +def distance_to_analog_output(distance: float): + """ + distance: float in m + """ + distance = distance * 1000 * 135 # m to mm, distance to analog output + # add noise + distance = distance + (distance * 0.001 * (2 * random.random() - 1)) + return round(distance) + + +def test_distance_to_analog_output(): + distance_in_mm = 10.5 + analog_output = distance_to_analog_output(distance_in_mm / 1000) + assert analog_output > 0 + assert analog_output < 19000