Skip to content

Commit

Permalink
add noise
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichiroaoki committed Oct 29, 2023
1 parent 5e24e75 commit e56aa1c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
9 changes: 5 additions & 4 deletions cnceye/edge/find.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"
Expand Down Expand Up @@ -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
"""
Expand Down
13 changes: 11 additions & 2 deletions scripts/simple_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -14,14 +15,18 @@

# x, y, z, distance
data = []
current_data = 100000
threshold = 100

# Ensure the object has a mesh
assert obj.type == "MESH"


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):
Expand All @@ -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
Expand All @@ -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
Expand Down
Binary file modified tests/fixtures/db/listener.db
Binary file not shown.
18 changes: 18 additions & 0 deletions tests/test_script_utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e56aa1c

Please sign in to comment.