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

Ver #18

Merged
merged 6 commits into from
Oct 29, 2023
Merged

Ver #18

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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
poetry run ruff --format=github --target-version=py37 .

- name: Run the automated tests
run: poetry run pytest -v
run: poetry run pytest -v --cov=cnceye tests

blender:
name: Blender
Expand Down
22 changes: 10 additions & 12 deletions cnceye/edge/find.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import mysql.connector
from mysql.connector.errors import IntegrityError

from cnceye.config import MYSQL_CONFIG


def find_edge(filepath: str, minimal_diff: float = 5.0):
# read csv
Expand All @@ -22,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 = MYSQL_CONFIG, 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 @@ -105,11 +101,11 @@ def find_lines(filepath: str, edge_count: int, minimal_diff: float = 5.0):
return lines


def get_edge_data(mysql_config=MYSQL_CONFIG):
def get_edge_data(model_id: int, mysql_config):
cnx = mysql.connector.connect(**mysql_config, database="coord")
cursor = cnx.cursor()
query = "SELECT id,x,y,z FROM edge"
cursor.execute(query)
query = "SELECT id,x,y,z FROM edge WHERE model_id = %s"
cursor.execute(query, (model_id,))
edges = cursor.fetchall()
cursor.close()
cnx.close()
Expand All @@ -135,7 +131,7 @@ def identify_close_edge(edges, measured_edges, distance_threshold=2.5):
return update_list


def add_measured_edge_coord(edge_list: list, mysql_config=MYSQL_CONFIG):
def add_measured_edge_coord(edge_list: list, mysql_config: dict):
cnx = mysql.connector.connect(**mysql_config, database="coord")
cursor = cnx.cursor()
query = "UPDATE edge SET rx = %s, ry = %s, rz = %s WHERE id = %s"
Expand All @@ -148,12 +144,14 @@ def add_measured_edge_coord(edge_list: list, mysql_config=MYSQL_CONFIG):
cnx.close()


def process_edges(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
"""
measured_edges = find_edges(mysql_config, minimal_diff)
edge_data = get_edge_data(mysql_config)
measured_edges = find_edges(process_id, mysql_config, minimal_diff)
edge_data = get_edge_data(model_id, mysql_config)
update_list = identify_close_edge(edge_data, measured_edges)
add_measured_edge_coord(update_list, mysql_config)
edge_count = len(edge_data)
Expand Down
160 changes: 83 additions & 77 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cnceye"
version = "0.4.2"
version = "0.4.3"
description = "CMM python library"
license = "MIT"
authors = ["yuichiroaoki <[email protected]>"]
Expand All @@ -11,13 +11,13 @@ keywords = ["cnc", "cmm", "milling", "gcode"]
[tool.poetry.dependencies]
python = ">=3.11,<3.13"
mysql-connector-python = "^8.1.0"
scipy = "^1.11.1"


[tool.poetry.group.dev.dependencies]
ruff = "^0.0.280"
pytest = "^7.4.0"
black = "^23.7.0"
pytest-cov = "^4.1.0"

[build-system]
requires = ["poetry-core"]
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
File renamed without changes.
Binary file modified tests/fixtures/db/listener.db
Binary file not shown.
10 changes: 5 additions & 5 deletions tests/test_find.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from cnceye.config import MYSQL_CONFIG
from tests.config import MYSQL_CONFIG
from cnceye.edge import find
import mysql.connector
import sqlite3
Expand Down Expand Up @@ -31,7 +31,7 @@ def copy_sqlite_db_to_mysql():

def test_find_edges():
copy_sqlite_db_to_mysql()
measured_edges = find.find_edges(process_id)
measured_edges = find.find_edges(process_id, MYSQL_CONFIG)
assert len(measured_edges) > 32


Expand All @@ -49,7 +49,7 @@ def test_find_edge_from_sqlite():


def test_add_measured_edge_coord():
measured_edges = find.find_edges(process_id)
measured_edges = find.find_edges(process_id, MYSQL_CONFIG)
edge_data = [
(1, -50.0, -21.667, 10.0),
(2, -50.0, 21.667, 10.0),
Expand Down Expand Up @@ -83,9 +83,9 @@ def test_add_measured_edge_coord():
(30, -4.5, -22.203, 10.0),
(31, -4.5, -37.791, 10.0),
]
# edge_data = find.get_edge_data()
# edge_data = find.get_edge_data(1)
update_list = find.identify_close_edge(edge_data, measured_edges)
find.add_measured_edge_coord(update_list)
find.add_measured_edge_coord(update_list, MYSQL_CONFIG)


def test_check_if_edge_is_found():
Expand Down
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