-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMechanicus_HAND_TRACKER.py
144 lines (121 loc) · 5.47 KB
/
Mechanicus_HAND_TRACKER.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
136
137
138
139
140
141
142
143
144
import cv2
import numpy as np
import time
import serial
import threading
import sys
# Define your serial port
serial_port = 'COM3' # Change this to your CNC machine's serial port
class CNCControlApp:
def __init__(self):
# Initialize default values for the green point and current quadrant
self.green_x = None
self.green_y = None
self.current_quadrant = "center" # Initialize as "center"
# Check the connection when the app starts
if not self.check_connection():
print("Connection Error", "Unable to establish a connection to the CNC machine.")
self.root.destroy() # Close the app if there's no connection
# Perform a 10 mm move left and right for testing
self.send_gcode("G91") # Switch to relative mode
self.send_gcode("G1 X10 F500") # Move right by 10 mm
time.sleep(2) # Wait for 2 seconds
self.send_gcode("G1 X-10 F500") # Move left by 10 mm
# Start the coordinate update loop
threading.Thread(target=self.run).start()
def check_connection(self):
# Try to establish a connection with the CNC machine
try:
ser = serial.Serial(serial_port, 115200)
ser.close()
return True
except serial.SerialException as e:
print(f"Serial Error: {e}")
sys.exit(1) # Terminate the script if there's no connection
def detect_green_point(self, frame):
lower_green = np.array([35, 70, 70])
upper_green = np.array([90, 255, 255])
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_frame, lower_green, upper_green)
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if contours:
largest_contour = max(contours, key=cv2.contourArea)
moments = cv2.moments(largest_contour)
if moments["m00"] != 0:
self.green_x = int(moments["m10"] / moments["m00"])
self.green_y = int(moments["m01"] / moments["m00"])
else:
self.green_x = None
self.green_y = None
else:
self.green_x = None
self.green_y = None
def track_hand(self):
if self.green_x is not None and self.green_y is not None:
video_width, video_height = 480, 480 # Adjust these values based on your camera resolution
cnc_width, cnc_height = 400, 400 # in millimeters
step_size = 2.0
speed = 500 # F500 G-code speed
# Determine the current quadrant based on the position of the green point
if self.green_x < video_width / 2:
if self.green_y < video_height / 2:
self.current_quadrant = "top_left"
else:
self.current_quadrant = "bottom_left"
else:
if self.green_y < video_height / 2:
self.current_quadrant = "top_right"
else:
self.current_quadrant = "bottom_right"
# Calculate the movement steps based on the quadrant
if self.current_quadrant == "top_left":
x_step = -step_size
y_step = step_size
elif self.current_quadrant == "top_right":
x_step = step_size
y_step = step_size
elif self.current_quadrant == "bottom_left":
x_step = -step_size
y_step = -step_size
elif self.current_quadrant == "bottom_right":
x_step = step_size
y_step = -step_size
else:
x_step = 0
y_step = 0 # Center quadrant, no movement
# Generate and send G-code command
gcode_command = f"G1 X{x_step} Y{y_step} F{speed}"
print("Generated G-code command:", gcode_command) # Print the generated G-code
self.send_gcode(gcode_command)
def send_gcode(self, gcode_line):
try:
ser = serial.Serial(serial_port, 115200)
ser.write(gcode_line.encode())
ser.close()
except serial.SerialException as e:
print(f"Serial Error: {e}")
def run(self):
video_capture = cv2.VideoCapture(1) # Change the argument to specify the video source (0 for webcam)
while True:
ret, frame = video_capture.read()
if not ret:
break
# Detect the green point
self.detect_green_point(frame)
#self.send_gcode("G91")
# Track the hand and generate G-code
self.track_hand()
# Display the frame with the green point
cv2.circle(frame, (self.green_x, self.green_y), 5, (0, 255, 0), -1)
cv2.imshow("Green Point Tracking", frame)
# Stop CNC motion when no green point is detected
if self.green_x is None or self.green_y is None:
#print("No green point detected. Stopping CNC motion.")
self.send_gcode("M112") # Send an emergency stop command
# Exit the loop when the 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
app = CNCControlApp()