Skip to content

Commit

Permalink
add mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichiroaoki committed Aug 8, 2023
1 parent 4b55223 commit 3175698
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

- name: Run MySQL
run: |
docker-compose up -d mysql
working-directory: .

- name: Run image
uses: abatilo/actions-poetry@v2
with:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# cnceye
![Test](https://github.com/OpenCMM/cnceye/actions/workflows/ci.yml/badge.svg)

An image-based CMM (Coordinate Measuring Machine) system for CNC machine tools

## Simulation with Blender
Expand Down
25 changes: 25 additions & 0 deletions cnceye/cmm/single.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from cnceye.line import get_lines, Line
from cnceye.vertex import get_vertices
from typing import List
import mysql.connector
from mysql.connector.errors import IntegrityError
from cnceye.config import MYSQL_CONFIG


class SingleImage:
Expand Down Expand Up @@ -61,3 +64,25 @@ def vertex(self, distance: float) -> Coordinate:

x, y = vertices[0][0]
return self.from_opencv_coord(distance, (x, y))

def add_real_coordinate(self, distance):
point_id = self.center.unique_key()
real_coordinate = self.vertex(distance)

cnx = mysql.connector.connect(**MYSQL_CONFIG, database="coord")
cursor = cnx.cursor()

update_query = """
UPDATE point
SET rx = %s, ry = %s, rz = %s
WHERE point_id = %s
"""
try:
data = (real_coordinate.x, real_coordinate.y, real_coordinate.z, point_id)
cursor.execute(update_query, data)
except IntegrityError:
print("Error: unable to update points")

cnx.commit()
cursor.close()
cnx.close()
20 changes: 20 additions & 0 deletions cnceye/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os

# For github actions
CI_MYSQL_CONFIG = dict(
host="localhost",
port=3306,
user="root",
password="root",
)


MYSQL_CONFIG = dict(
host="raspberrypi.local",
port=3306,
user="yuchi",
password="raspberrypi",
)

if os.environ.get("CI"):
MYSQL_CONFIG = CI_MYSQL_CONFIG
4 changes: 3 additions & 1 deletion cnceye/coordinate/coord.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np


class Coordinate(np.ndarray):
def __new__(cls, x, y, z):
obj = np.array([x, y, z], dtype=float).view(cls)
Expand Down Expand Up @@ -28,3 +27,6 @@ def __truediv__(self, other):

def distance_to(self, other) -> float:
return np.linalg.norm(self - other)

def unique_key(self) -> str:
return f"{self.x},{self.y},{self.z}"
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: "3.9"
services:
mysql:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: coord
ports:
- 3306:3306
volumes:
- ./mysql/init.sql:/docker-entrypoint-initdb.d/init.sql
64 changes: 63 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ readme = "README.md"
[tool.poetry.dependencies]
python = "^3.11"
opencv-python-headless = "^4.8.0.74"
mysql-connector-python = "^8.1.0"


[tool.poetry.group.dev.dependencies]
Expand Down
12 changes: 12 additions & 0 deletions scripts/coordinates.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-50.0,-65.0,10.0
-50.0,65.0,10.0
-25.0,28.0,10.0
-25.0,48.0,10.0
-20.0,23.0,10.0
-20.0,53.0,10.0
20.0,23.0,10.0
20.0,53.0,10.0
25.0,28.0,10.0
25.0,48.0,10.0
50.0,-65.0,10.0
50.0,65.0,10.0
19 changes: 10 additions & 9 deletions scripts/create_test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,25 @@ def render_image(output_path, camera_position, camera_rotation, light_position):

# Example usage
if __name__ == "__main__":
# set unit to millimeters
bpy.context.scene.unit_settings.system = "METRIC"
bpy.context.scene.unit_settings.length_unit = "MILLIMETERS"

# Define camera and light positions
camera_position_start = (-0.05, 0.025, 0.06)
camera_rotation = (0.0, 0.0, 0.0)
light_position = (0, 0, 0.1)

# Create a folder to save the rendered images
output_folder = "/path/to/output_images"
os.makedirs(output_folder, exist_ok=True)

# Render images with different camera and light positions
index = 0
for i in range(6):
for j in range(6):
camera_position = (
camera_position_start[0] + j * 0.02,
camera_position_start[1] - i * 0.01,
camera_position_start[2],
)
with open("coordinates.txt") as f:
for line in f:
xyz = line.strip().split(",")
x, y, z = [float(i)/1000 for i in xyz]
camera_position = (x, y, 0.0105)
light_position = (x, y, 0.1)
output_path = os.path.join(output_folder, f"image_{index}.png")
render_image(output_path, camera_position, camera_rotation, light_position)
index += 1
Expand Down
9 changes: 9 additions & 0 deletions tests/test_coord.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,12 @@ def test_distance_to():

assert coord1.distance_to(coord2) == 3.0
assert coord1.distance_to(coord3) == 5.196152422706632


def test_unique_key():
coord1 = Coordinate(1.0, 2.0, 3.0)
coord1_id = coord1.unique_key()
assert coord1_id == "1.0,2.0,3.0"
coord2 = Coordinate(1.0, 5.0, 3.0)
coord2_id = coord2.unique_key()
assert coord2_id == "1.0,5.0,3.0"
49 changes: 10 additions & 39 deletions tests/test_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,26 @@
from cnceye.cmm import SingleImage
from cnceye.camera import Camera
from cnceye.coordinate import Coordinate
from cnceye.circle import get_circles
from tests.utils import diff_in_micron

focal_length = 50.0 # mm
camera_height = 60.0 # mm
camera_height = 10.5 # mm
object_height = 10.0 # mm
distance = camera_height - object_height
sensor_width = 36.0 # mm
camera = Camera(focal_length, sensor_width)


def test_opencv_coord_0():
image = cv2.imread("tests/fixtures/output_images/image_0.png")
center_coordinates = Coordinate(-50.0, 25.0, 60.0)
def test_image_0():
image = cv2.imread("tests/fixtures/images/image_0.png")
center_coordinates = Coordinate(-50.0, 65.0, camera_height)

cmm = SingleImage(image, center_coordinates, camera)
circles = get_circles(image, 100, 50, 100, 200)
(x_pixel, y_pixel, r_pixel) = circles[0][0]

# check radius is close to expected radius
expected_radius = 3.0 # mm
radius_from_img_in_mm = cmm.from_pixel_length(distance, r_pixel)
radius_diff_in_micron = diff_in_micron(expected_radius, radius_from_img_in_mm)
print(f"radius: {radius_diff_in_micron:.2f} μm")
assert radius_diff_in_micron < 100.0

# check center is close to expected center
expected_circle_center = Coordinate(-48.0, 23.0, 10.0)
circle_center_from_img_in_mm = cmm.from_opencv_coord(distance, (x_pixel, y_pixel))
x_from_img_in_mm = circle_center_from_img_in_mm.x
y_from_img_in_mm = circle_center_from_img_in_mm.y

x_diff_in_micro = diff_in_micron(expected_circle_center.x, x_from_img_in_mm)
vertex = cmm.vertex(distance)
x_diff_in_micro = diff_in_micron(-50.0, vertex.x)
print(f"x: {x_diff_in_micro:.2f} μm")
assert x_diff_in_micro < 100.0

y_diff_in_micron = diff_in_micron(expected_circle_center.y, y_from_img_in_mm)
print(f"y: {y_diff_in_micron:.2f} μm")
assert y_diff_in_micron < 100.0

lines = cmm.lines(distance)
assert len(lines) > 0
expected_corner = Coordinate(-53.0, 28.0, 10.0)
corner = lines[0].start

x_diff_in_micro = diff_in_micron(expected_corner.x, corner.x)
print(f"x: {x_diff_in_micro:.2f} μm")
assert x_diff_in_micro < 100.0

y_diff_in_micron = diff_in_micron(expected_corner.y, corner.y)
assert x_diff_in_micro < 1.0
y_diff_in_micron = diff_in_micron(65.0, vertex.y)
print(f"y: {y_diff_in_micron:.2f} μm")
assert y_diff_in_micron < 100.0
assert y_diff_in_micron < 1.0
cmm.add_real_coordinate(distance)

0 comments on commit 3175698

Please sign in to comment.