Dateien nach "tutel" hochladen

This commit is contained in:
2024-03-10 20:28:00 +01:00
parent db3a0cff37
commit be3d08b05a
3 changed files with 126 additions and 79 deletions
+39 -36
View File
@@ -15,7 +15,7 @@ from cv_bridge import CvBridge, CvBridgeError
from geometry_msgs.msg import Twist
TARGET = 0
DEPTH = 470
DEPTH = 460
POSITIONS = ["left", "right", "middle"]
DEVICE = 'dev/ttyACM'
BAUD = 9600
@@ -32,7 +32,8 @@ class tutel():
self.arucoParams = cv2.aruco.DetectorParameters_create()
#minimal depth we recognize, still testing for values!
self.maxMinDepth = DEPTH
self.min_threshold = DEPTH
self.max_threshold = 1200
#cv related
#rgb related
@@ -108,7 +109,7 @@ class tutel():
#cv2.rotate(depth_image, cv2.ROTATE_180, depth_image)
if self.depth_counter == 0:
#pass
#self.last_depth = 10.0
self.last_depth = 10.0
x = (0,0)
y = (0,0)
#x and y are swapped, because
@@ -123,45 +124,47 @@ class tutel():
if self.bottom_left[1] < self.bottom_right[1]: y = (self.bottom_left[1],self.bottom_right[1])
else: y = (self.bottom_right[1],self.bottom_left[1])
depth_values = []
M = ((x[0] + (x[1]-x[0])//2), y[0]+(y[1]-y[0]//2))
S = ((M[0]-10),(M[1]-10))
E = ((M[0]+10),(M[1]+10)
#for i in range(x[0], x[1]):
#self.number_of_pixels += 1
#for j in range(y[0], y[1]):
#if depth_image[j][i] > self.maxMinDepth:
#depth_values.append(depth_image[j][i])
#print(M, S, E)
for i in range(S[0], E[0]+1):
for j in range(S[1], E[1]+1):
print(depth_image[j][i])
if depth_image[j][i] > self.maxMinDepth and depth_image[j][i] < 1200:
depth_values.append(depth_image[j][i])
#for i inrange(x[0], x[1]):
#if depth_image[y[1]][i] > self.maxMinDepth:
#depth_values.append(depth_image[y[0]][i])
if len(depth_values)>0 or self.last_depth == 10:
self.last_depth = max(depth_values)
print(max(depth_values))
pixels = (abs(self.top_left[0]-self.top_right[0]), abs(self.top_left[1] - self.bottom_left[1]), abs(self.top_right[1]-self.bottom_right[1]), abs(self.bottom_right[0]-self.bottom_left[0]))
self.pixel_length = max(pixels)
self.pixel_depth = ((self.pixel_length/2858.4636643479)**(-1/1.0056840993))*10
#self.pixel_depth = (self.pixel_depth + ((((self.x[1] - self.x[0] + self.y[1] - self.y[0]) / 2)/2914.29872964688)**(-1/1.01590290349592))*10)/2
depth_values = []
M = ((x[0] + (x[1]-x[0])//2), (y[0] + (y[1]-y[0])//2))
depth_values.append(depth_image[M[1]][M[0]])
#for i in range(self.x[0], self.x[1]):
#for j in range(self.y[0], self.y[1]):
#if depth_image[j][i] > self.min_threshold and depth_image[j][i] < self.max_threshold and depth_image[j][i] < self.pixel_depth:
#depth_values.append(depth_image[j][i])
#print(self.pixel_depth, depth_values, self.last_depth)
if self.pixel_depth > self.max_threshold:
if self.pixel_depth < self.last_depth or self.last_depth == 10:
self.last_depth = self.pixel_depth
else:
self.last_depth = self.last_depth
else:
self.last_depth = self.last_depth
if len(depth_values)>0 and ((max(depth_values) > self.min_threshold and max(depth_values) < self.last_depth) or self.last_depth == 10):
self.last_depth = max(depth_values)
elif (len(depth_values)>0 and max(depth_values) < self.min_threshold) or len(depth_values)<1:
self.last_depth = self.pixel_depth
else:
self.last_depth = self.last_depth
lengths = [abs(self.top_left[0]-self.top_right[0]), abs(self.top_left[1] - self.bottom_left[1]), abs(self.top_right[1]-self.bottom_right[1]), abs(self.bottom_right[0]-self.bottom_left[0])]
#pixel_length = (abs(self.top_left[0]-self.top_right[0]) + abs(self.top_left[1] - self.bottom_left[1]) + abs(self.top_right[1]-self.bottom_right[1]) + abs(self.bottom_right[0]-self.bottom_left[0]))/4
pixel_length = max(lengths)
middle_coordinates = ((x[0] + (x[1]-x[0])//2),(y[0]+(y[1]-y[0]//2)))
print(self.last_depth, pixel_length, middle_coordinates)
#self.number_of_operations = 0
#self.number_of_pixels = 0
M_Kante = (M[0], y[0])
print(pixel_length, M_Kante)
self.number_of_operations = 0
self.number_of_pixels = 0
pixel_length = 0
#cv2.imshow("Tutel_Depth", depth_image)
#if self.last_depth < self.maxMinDepth and self.last_depth != 0:
# self.depth_counter += 1
# if self.depth_counter > 0:
# self.depth_state = False
# self.last_depth = 10.0
# self.depth_counter = 0
#else:
# self.depth_counter = 0
if self.last_depth < self.min_threshold and self.last_depth != 0:
self.depth_counter += 1
if self.depth_counter > 0:
self.depth_state = False
self.last_depth = 10.0
self.depth_counter = 0
else:
self.depth_counter = 0
def markDetected(self, image, corners):
+84 -39
View File
@@ -9,14 +9,15 @@ import time
import numpy as np
import serial
import subprocess
import math
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
from geometry_msgs.msg import Twist
TARGET = -1
DEPTH_CAMERA_MIN_THRESHOLD = 470
DEPTH_CAMERA_MAX_THRESHOLD = 750
DEPTH_CAMERA_MIN_THRESHOLD = 530
DEPTH_CAMERA_MAX_THRESHOLD = 1000
POSITIONS = ["left", "right", "middle"]
DEVICE = '/dev/ttyACM'
BAUD = 115200
@@ -24,6 +25,9 @@ BAUD = 115200
WHITELIST_OBJECT = [1, 6, 123]
WHITELIST_PLACE = [234, 497, 666]
SEARCH = ["no", "object", "place"]
HEIGHT_CAMERA = 310
ARM_LENGTH = 385 #not the real arm length, its the max length from camera to object where our arm can reach the object
CAMERA_TO_ARM = 125
class tutel():
@@ -44,17 +48,25 @@ class tutel():
self.min_threshold = DEPTH_CAMERA_MIN_THRESHOLD
#Serial Pipeline init
self.com = self.init_serial()
#+self.com=3
#self.com = self.init_serial()
self.com=3
self.device = DEVICE+str(self.com)
self.baud = BAUD
self.board = serial.Serial(self.device, self.baud, timeout = 1)
time.sleep(5)
answer =''
self.board.write(bytes("0","utf-8"))
while answer != b'1':
answer = self.board.readline()
print(answer)
self.board.write(bytes("0 0 0", "utf-8"))
while self.board.in_waiting < 3:
self.routine = True
self.board.reset_input_buffer()
self.board.write(bytes("1 1 1", "utf-8"))
while self.board.in_waiting < 3:
self.routine = True
self.board.reset_input_buffer()
#answer =''
#self.board.write(bytes("0","utf-8"))
#while answer != b'1':
#answer = self.board.readline()
#print(answer)
#Tutel_control
self.pub = rospy.Publisher(tutel_topic, Twist, queue_size = 1)
@@ -120,7 +132,7 @@ class tutel():
def callback_rgb(self, data):
try:
rgb_image = self.rgb_bridge.imgmsg_to_cv2(data, "bgr8")
if self.verbose == "window" or self.verbose == "both": cv2.rotate(rgb_image, cv2.ROTATE_180, rgb_image)
#if self.verbose == "window" or self.verbose == "both": cv2.rotate(rgb_image, cv2.ROTATE_180, rgb_image)
(self.h, self.w) = rgb_image.shape[:2]
except CvBridgeError as e:
print(e)
@@ -227,8 +239,8 @@ class tutel():
#if self.verbose == "terminal" or self.verbose == "both": print("Routine initialised")
#some additional info on screen
if self.verbose == "window" or self.verbose == "both":
id_str = "IDs: " + str(ids) + " Target_ID:" + str(self.target_id)
cv2.putText(rgb_image, id_str, (10,30), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8)
#id_str = "IDs: " + str(ids) + " Target_ID:" + str(self.target_id)
#cv2.putText(rgb_image, id_str, (10,30), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8)
depth_str = "Depth_State:" + str(self.depth_state) + " Depth:" + str(self.last_depth)
cv2.putText(rgb_image, depth_str, (10,60), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8)
info_str = "Search_State: " + str(self.search)
@@ -242,7 +254,7 @@ class tutel():
def callback_depth(self, data):
if self.depth_state:
depth_image = self.depth_bridge.imgmsg_to_cv2(data, "passthrough")
if self.verbose == "window" or self.verbose == "both": cv2.rotate(depth_image, cv2.ROTATE_180, depth_image)
#if self.verbose == "window" or self.verbose == "both": cv2.rotate(depth_image, cv2.ROTATE_180, depth_image)
#x and y are swapped, because
print(str(self.top_left[0]) + " " + str(self.top_right[0]) + " " + str(self.top_left[1]) + " " + str(self.bottom_right[1]))
if (self.top_left[0] - self.bottom_left[0]) in range (-10,10):
@@ -266,10 +278,15 @@ class tutel():
self.pixel_depth = ((self.pixel_length/2858.4636643479)**(-1/1.0056840993))*10
#self.pixel_depth = (self.pixel_depth + ((((self.x[1] - self.x[0] + self.y[1] - self.y[0]) / 2)/2914.29872964688)**(-1/1.01590290349592))*10)/2
depth_values = []
for i in range(self.x[0], self.x[1]):
for j in range(self.y[0], self.y[1]):
if depth_image[j][i] > self.min_threshold and depth_image[j][i] < self.max_threshold and depth_image[j][i] < self.pixel_depth:
depth_values.append(depth_image[j][i])
#M = ((self.x[0] + (self.x[1]-self.x[0])//2), (self.y[0] + (self.y[1]-self.y[0])//2))
#depth_values.append(depth_image[M[1]][M[0]])
#for i in range(self.x[0], self.x[1]):
#for j in range(self.y[0], self.y[1]):
#if depth_image[j][i] > self.min_threshold and depth_image[j][i] < self.max_threshold and depth_image[j][i] < self.pixel_depth:
#depth_values.append(depth_image[j][i])
for i in range(self.x[0], self.x[1]+1):
if depth_image[self.y[0]][i] > self.min_threshold and depth_image[self.y[0]][i] < self.max_threshold and depth_image[self.y[0]][i] < self.pixel_depth:
depth_values.append(depth_image[self.y[0]][i])
print(self.pixel_depth, depth_values, self.last_depth)
if self.pixel_depth > self.max_threshold:
if self.pixel_depth < self.last_depth or self.last_depth == 10:
@@ -284,7 +301,7 @@ class tutel():
else:
self.last_depth = self.last_depth
if self.verbose == "terminal" or self.verbose == "both": print(self.depth_counter, self.last_depth)
if self.last_depth < self.min_threshold and self.last_depth != 0:
if (self.last_depth**2 - HEIGHT_CAMERA**2) < ARM_LENGTH**2:
self.depth_counter += 1
if self.depth_counter > 0:
self.t.linear.x = 0.0 * self.vel_fac
@@ -293,22 +310,25 @@ class tutel():
if self.verbose == "terminal" or self.verbose == "both": print(self.last_depth)
self.routine = True
if self.verbose == "terminal" or self.verbose == "both": print("Only a few cm")
self.t.linear.x = 0.1 * self.vel_fac
self.t.angular.z = 0.0 * self.vel_fac
#self.t.linear.x = 0.1 * self.vel_fac
#self.t.angular.z = 0.0 * self.vel_fac
self.position = POSITIONS[2]
self.depth_counter = 0
self.depth_state = False
self.last_depth = 10.0
start = time.time()
while (time.time() - start) < 0.55:
self.pub.publish(self.t)
self.t.linear.x = 0.0 * self.vel_fac
self.pub.publish(self.t)
#self.last_depth = 10.0
#start = time.time()
#while (time.time() - start) < 0.55:
# self.pub.publish(self.t)
#self.t.linear.x = 0.0 * self.vel_fac
#self.pub.publish(self.t)
if self.target_id in WHITELIST_OBJECT:
print("Grab")
x_coordinate = math.sqrt(self.last_depth**2 - HEIGHT_CAMERA**2) - CAMERA_TO_ARM
self.last_id = self.target_id
self.target_id = TARGET
self.move_arm()
self.move_arm(x_coordinate)
else:
print("Release")
self.last_id = TARGET
self.target_id = TARGET
self.release()
@@ -411,28 +431,53 @@ class tutel():
return image
def move_arm(self):
def move_arm(self, x):
grabbed =''
print("I AM HERE")
x_coordinate = int(x) + 40
print(x_coordinate)
#self.board.flush()
self.board.write(bytes("0 0 0", "utf-8"))
self.board.flush()
self.board.write(bytes("250 0 -145", "utf-8"))
time.sleep(1)
self.board.reset_output_buffer()
while self.board.in_waiting < 6:
#grabbed = self.board.read()
#print(grabbed)
pass
self.board.reset_input_buffer();
self.board.reset_output_buffer()
coordinates = str(x_coordinate) + " 0 -115"
print(coordinates)
self.board.write(bytes(coordinates, "utf-8"))
self.board.flush()
self.board.reset_output_buffer()
print("Messages send!")
if self.verbose == "terminal" or self.verbose == "both": print("move arm")
while grabbed != b'2\r\n':
grabbed = self.board.readline()
print(grabbed)
while self.board.in_waiting < 6:
#grabbed = self.board.read()
#print(grabbed)
pass
self.board.reset_input_buffer()
self.board.write(bytes("1 1 1","utf-8"))
self.board.flush()
self.board.reset_output_buffer()
while self.board.in_waiting < 6:
pass
self.board.reset_input_buffer()
self.search = SEARCH[2]
self.routine =False
print(self.routine)
def release(self):
released = ''
#released = ''
self.board.flush()
self.board.write(bytes("3", "utf-8"))
time.sleep(1)
self.board.write(bytes("-1 -1 -1", "utf-8"))
time.sleep(15)
if self.verbose == "terminal" or self.verbose == "both": print("release object")
while released != b"4\r\n":
released = self.board.readline()
print(released)
#while released != b"4\r\n":
# released = self.board.readline()
# print(released)
self.search = SEARCH[1]
self.routine = False
print(self.routine)
@@ -24,7 +24,6 @@ BAUD = 115200
WHITELIST_OBJECT = [1, 6, 123]
WHITELIST_PLACE = [234, 497, 666]
SEARCH = ["no", "object", "place"]
PIXEL_THRESH = 28
class tutel():
@@ -262,14 +261,14 @@ class tutel():
#self.pixel_depth = ((self.pixel_length/2914.29872964688)**(-1/1.01590290349592))*10 #*10 to get the depth value in mm as the depth image delivers them in mm
#self.pixel_length = (abs(self.top_left[0]-self.top_right[0]) + abs(self.top_left[1] - self.bottom_left[1]) + abs(self.top_right[1]-self.bottom_right[1]) + abs(self.bottom_right[0]-self.bottom_left[0]))/4
#self.pixel_depth = ((self.pixel_length/2675.4910012317)**(-1/0.993434027))*10
pixels = (abs(self.top_left[0]-self.top_right[0]), abs(self.top_left[1] - self.bottom_left[1]), abs(self.top_right[1]-self.bottom_right[1]), abs(self.bottom_right[0]-self.bottom_left[0]))
self.pixel_length = max(pixels)
#pixels = (abs(self.top_left[0]-self.top_right[0]), abs(self.top_left[1] - self.bottom_left[1]), abs(self.top_right[1]-self.bottom_right[1]), abs(self.bottom_right[0]-self.bottom_left[0]))
#self.pixel_length = max(pixels)
#self.pixel_depth = ((self.pixel_length/2858.4636643479)**(-1/1.0056840993))*10
#self.pixel_depth = (self.pixel_depth + ((((self.x[1] - self.x[0] + self.y[1] - self.y[0]) / 2)/2914.29872964688)**(-1/1.01590290349592))*10)/2
depth_values = []
for i in range(self.x[0], self.x[1]):
for j in range(self.y[0], self.y[1]):
if self.pixel_length >= PIXEL_THRESH and depth_image[j][i] > self.min_threshold:
if depth_image[j][i] > self.min_threshold:
depth_values.append(depth_image[j][i])
print(self.pixel_depth, depth_values, self.last_depth)
self.last_depth = max(depth_values)