-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mechanicus_LASER_SHAPE_TRACING copy 6.py
135 lines (106 loc) · 4.4 KB
/
Mechanicus_LASER_SHAPE_TRACING copy 6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import cv2
import numpy as np
import serial
from PIL import Image, ImageTk
import tkinter as tk
import time
# Define the dimensions of the video feed and CNC workspace
video_width, video_height = 480, 480
cnc_width, cnc_height = 200, 200 # in millimeters
# Function to scale the video coordinates to CNC coordinates
def scale_coordinates(x, y):
# Calculate the scaling factors
x_scale = cnc_width / video_width
y_scale = cnc_height / video_height
# Apply scaling to the coordinates
scaled_x = x * x_scale
scaled_y = y * y_scale
return scaled_x, scaled_y
# Initialize a list to store G-code commands in the buffer
gcode_buffer = []
# Function to send G-code commands to control the CNC
def send_gcode(gcode_line):
# Open serial port
ser = serial.Serial('COM4', 115200)
# Send the G-code command to the CNC
ser.write(gcode_line.encode())
# Function to move the CNC to a specific position
def move_to_position(x, y, speed):
# Limit movement within the 400 x 400 mm area
x = max(0, min(x, 400))
y = max(0, min(y, 400))
# Construct the G-code command for movement
gcode_line = f"G1 X{x} Y{y} F{speed}\n"
# Add the G-code command to the buffer
gcode_buffer.append(gcode_line)
# Check if the buffer has reached the maximum size (5)
if len(gcode_buffer) >= 1:
# Send the buffered commands and wait for confirmation
for cmd in gcode_buffer:
send_gcode(cmd)
# Add logic here to wait for confirmation (e.g., feedback from CNC)
# You can use serial communication to receive confirmation
# Once confirmed, remove the command from the buffer
gcode_buffer.clear() # Clear the buffer after sending
def tracer():
WINDOW = "480x480"
# Open the webcam
# Capture a frame from the webcam
cap = cv2.VideoCapture(1)
root = tk.Tk()
root.geometry(WINDOW) # Set the window size to 480x480
label = tk.Label(root)
label.pack()
# Initialize the blue point (centered) coordinates
center_x = video_width // 2
center_y = video_height // 2
while True:
frame, green_x, green_y = process_frame(cap)
if frame is None:
break
if green_x is not None and green_y is not None:
# Calculate the required motion to follow the green point
dx = -int(center_x - green_x)/6
dy = -int(center_y - green_y)/6
print (dx,dy)
# Move the CNC by the calculated movement
move_to_position(dx, dy, speed=9000)
# Crop the frame to 480x480
frame = frame[:480, :480]
# Convert the frame to a PIL Image
img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
# Convert the resized image to a Tkinter-compatible format
img_tk = ImageTk.PhotoImage(image=img)
# Update the label with the resized image
label.config(image=img_tk)
label.image = img_tk
# Wait for 0.2 seconds before updating again
root.update()
time.sleep(0.00001)
cap.release() # Release the webcam when done
root.destroy() # Destroy the Tkinter window when done
def process_frame(cap):
ret, frame = cap.read()
if not ret:
return None, None, None # Return None for frame and coordinates
# Define the color range for detecting the green point
lower_green = np.array([35, 70, 70])
upper_green = np.array([90, 255, 255])
# Convert the frame to the HSV color space
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Create a mask to isolate the green point
mask = cv2.inRange(hsv_frame, lower_green, upper_green)
# Find contours in the mask
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if contours:
# Get the largest green contour (assuming it's the target point)
largest_contour = max(contours, key=cv2.contourArea)
moments = cv2.moments(largest_contour)
if moments["m00"] != 0:
# Calculate the center of the green point
green_x = int(moments["m10"] / moments["m00"])
green_y = int(moments["m01"] / moments["m00"])
return frame, green_x, green_y
return frame, None, None # Return frame with no green point
time.sleep(0.0001)
tracer()