Dateien nach "dennis_nodes/tutel_nodes_max_value_pure_variations" hochladen

This commit is contained in:
2024-03-04 16:07:48 +01:00
parent de36b09bce
commit bdde39d6d0
@@ -49,13 +49,12 @@ class tutel():
self.device = DEVICE
self.baud = BAUD
self.board = serial.Serial(self.device, self.baud, timeout = 1)
#answer =''
#while answer != '1337':
# self.board.write(100)
# time.sleep(0.05)
# answer = self.board.readline()#.decode("utf-8")#.rstrip()
# print(answer)
time.sleep(5)
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)
@@ -121,7 +120,7 @@ class tutel():
def callback_rgb(self, data):
try:
rgb_image = self.rgb_bridge.imgmsg_to_cv2(data, "bgr8")
if self.verbose is "window" or self.verbose is "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)
@@ -133,7 +132,7 @@ class tutel():
# self.fresh_start = False
# self.search_time = time.time()
if not self.routine:
if self.verbose is "terminal" or self.verbose is "both": print("NO SEARCH JUST DETECT")
if self.verbose == "terminal" or self.verbose == "both": print("NO SEARCH JUST DETECT")
(corners, ids, rejected) = cv2.aruco.detectMarkers(rgb_image, self.arucoDict, parameters = self.arucoParams)
if corners:
for id_x in ids:
@@ -142,19 +141,19 @@ class tutel():
self.search = SEARCH[0]
#print("TARGET ID = " + str(self.target_id))
break
if self.search is "object":
if self.verbose is "terminal" or self.verbose is "both": print("SEARCH TIME")
if self.search == "object":
if self.verbose == "terminal" or self.verbose == "both": print("SEARCH TIME")
if (time.time() - self.search_time) < 31.415925:
if self.verbose is "terminal" or self.verbose is "both": print("LOOKING AROUND")
if self.verbose == "terminal" or self.verbose == "both": print("LOOKING AROUND")
self.t.linear.x = 0.00 * self.vel_fac
self.t.angular.z = 0.2 * self.vel_fac
else:
if self.verbose is "terminal" or self.verbose is "both":print("TIMES UP")
if self.verbose == "terminal" or self.verbose == "both":print("TIMES UP")
self.t.linear.x = 0.2 * self.vel_fac
self.t.angular.z = 0.0 * self.vel_fac
self.search_time = time.time()
self.pub.publish(self.t)
if self.verbose is "terminal" or self.verbose is "both": print("DID I FIND THE ANSWER TO ALL MY QUESTIONS OR GRAVENIMAGE OF ME?")
if self.verbose == "terminal" or self.verbose == "both": print("DID I FIND THE ANSWER TO ALL MY QUESTIONS OR GRAVENIMAGE OF ME?")
(corners, ids, rejected) = cv2.aruco.detectMarkers(rgb_image, self.arucoDict, parameters = self.arucoParams)
if corners:
for id_x in ids:
@@ -162,9 +161,9 @@ class tutel():
self.target_id = id_x
self.search = SEARCH[0]
break
elif self.search is "no":
elif self.search == "no":
if corners:
if self.verbose is "terminal" or self.verbose is "both": print("I SAW SOMETHING")
if self.verbose == "terminal" or self.verbose == "both": print("I SAW SOMETHING")
x = np.where(ids == self.target_id)
if len(x[0]) > 0:
fitting_corners = corners[x[0][0]]
@@ -173,37 +172,35 @@ class tutel():
elif(self.lastConernerTime - time.time()) < 1./2:
self.depth_state = False
if self.position is POSITIONS[0]:
if self.verbose is "terminal" or self.verbose is "both": print("I NEED TO GO LEFT!")
if self.verbose == "terminal" or self.verbose == "both": print("I NEED TO GO LEFT!")
cv2.rectangle(rgb_image, (0,0), (20, self.h), (0,0,255), cv2.FILLED)
self.t.linear.x = 0.0 * self.vel_fac
self.t.angular.z = 0.2 * self.vel_fac
self.pub.publish(self.t)
elif self.position is POSITIONS[1]:
if self.verbose is "terminal" or self.verbose is "both": print("I NEED TO GO RIGHT")
if self.verbose == "terminal" or self.verbose == "both": print("I NEED TO GO RIGHT")
cv2.rectangle(rgb_image, (self.w-20,0), (self.w, self.h), (0,0,255), cv2.FILLED)
self.t.linear.x = 0.0 * self.vel_fac
self.t.angular.z = -0.2 * self.vel_fac
self.pub.publish(self.t)
elif self.search is "place":
if self.verbose is "terminal" or self.verbose is "both": print("SEARCH TIME")
elif self.search == "place":
if self.verbose == "terminal" or self.verbose == "both": print("SEARCH TIME")
if (time.time() - self.search_time) < 31.415925:
if self.verbose is "terminal" or self.verbose is "both": print("LOOKING AROUND")
if self.verbose == "terminal" or self.verbose == "both": print("LOOKING AROUND")
self.t.linear.x = 0.00 * self.vel_fac
self.t.angular.z = 0.2 * self.vel_fac
else:
if self.verbose is "terminal" or self.verbose is "both": print("TIMES UP")
if self.verbose == "terminal" or self.verbose == "both": print("TIMES UP")
self.t.linear.x = 0.2 * self.vel_fac
self.t.angular.z = 0.0 * self.vel_fac
self.search_time = time.time()
self.pub.publish(self.t)
if self.verbose is "terminal" or self.verbose is "both": print("DID I FIND THE ANSWER TO ALL MY QUESTIONS OR GRAVENIMAGE OF ME?")
if self.verbose == "terminal" or self.verbose == "both": print("DID I FIND THE ANSWER TO ALL MY QUESTIONS OR GRAVENIMAGE OF ME?")
(corners, ids, rejected) = cv2.aruco.detectMarkers(rgb_image, self.arucoDict, parameters = self.arucoParams)
if corners:
for id_x in ids:
if id_x in WHITELIST_PLACE:
if (last_id = WHITELIST_OBJECT[0] and id_x = WHITELIST_PLACE[0]) or
(last_id = WIHTELIST_OBJECT[1] and id_x = WHITELIST_PLACE[1]) or
(last_id = WHITELIST_OBJECT[2] and id_x = WHITELIST_PLACE[2]):
if (self.last_id == WHITELIST_OBJECT[0] and id_x == WHITELIST_PLACE[0]) or (self.last_id == WHITELIST_OBJECT[1] and id_x == WHITELIST_PLACE[1]) or (self.last_id == WHITELIST_OBJECT[2] and id_x == WHITELIST_PLACE[2]):
self.target_id = id_x
self.search = SEARCH[0]
break
@@ -218,9 +215,9 @@ class tutel():
self.target_id = id_x
self.search = SEARCH[0]
break
if self.verbose is "terminal" or self.verbose is "both": print("Routine initialised")
#if self.verbose == "terminal" or self.verbose == "both": print("Routine initialised")
#some additional info on screen
if self.verbose is "window" or self.verbose is "both":
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)
depth_str = "Depth_State:" + str(self.depth_state) + " Depth:" + str(self.last_depth)
@@ -229,14 +226,14 @@ class tutel():
cv2.putText(rgb_image, info_str, (10,90), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8)
self.rate.sleep()
if self.verbose is "window" or self.verbose is "both": cv2.imshow("Tutel", rgb_image)
if self.verbose == "window" or self.verbose == "both": cv2.imshow("Tutel", rgb_image)
cv2.waitKey(1)
def callback_depth(self, data):
if self.depth_state:
depth_image = self.depth_bridge.imgmsg_to_cv2(data, "passthrough")
if self.verbose is "window" or self.verbose is "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):
@@ -277,13 +274,16 @@ class tutel():
self.last_depth = self.pixel_depth
else:
self.last_depth = self.last_depth
if self.verbose is "terminal" or self.verbose is "both": print(self.depth_counter, 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:
self.depth_counter += 1
if self.depth_counter > 1:
if self.verbose is "terminal" or self.verbose is "both": print(self.last_depth)
if self.depth_counter > 0:
self.t.linear.x = 0.0 * self.vel_fac
self.t.angular.z = 0.0 * self.vel_fac
self.pub.publish(self.t)
if self.verbose == "terminal" or self.verbose == "both": print(self.last_depth)
self.routine = True
if self.verbose is "terminal" or self.verbose is "both": print("Only a few cm")
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.position = POSITIONS[2]
@@ -291,12 +291,12 @@ class tutel():
self.depth_state = False
self.last_depth = 10.0
start = time.time()
while (time.time() - start) < 0.6:
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.move_arm()
if self.verbose is "terminal" or self.verbose is "both": print("We reached our destination")
if self.verbose == "terminal" or self.verbose == "both": print("We reached our destination")
else:
self.depth_counter = 0
@@ -351,11 +351,11 @@ class tutel():
#self.pub.publish(self.t)
if self.last_depth > 700:
if self.x[1] < self.w//2 - 30:
if self.verbose is "terminal" or self.verbose is "both": print("LEFT")
if self.verbose == "terminal" or self.verbose == "both": print("LEFT")
self.t.linear.x = 0.0
self.t.angular.z = 0.15
elif self.x[0] > self.w//2 + 30:
if self.verbose is "terminal" or self.verbose is "both": print("RIGHT")
if self.verbose == "terminal" or self.verbose == "both": print("RIGHT")
self.t.linear.x = 0.0
self.t.angular.z = -0.15
else:
@@ -364,11 +364,11 @@ class tutel():
else:
marker_x = ((self.x[0] + (self.x[1]-self.x[0])//2 - 5), (self.x[0] + (self.x[1]-self.x[0])//2 + 5))
if marker_x[1] < self.w//2:
if self.verbose is "terminal" or self.verbose is "both": print("LEFT")
if self.verbose == "terminal" or self.verbose == "both": print("LEFT")
self.t.linear.x = 0.0
self.t.angular.z = 0.15
elif marker_x[0] > self.w//2:
if self.verbose is "terminal" or self.verbose is "both": print("RIGHT")
if self.verbose == "terminal" or self.verbose == "both": print("RIGHT")
self.t.linear.x = 0.0
self.t.angular.z = -0.15
else:
@@ -384,10 +384,17 @@ class tutel():
def move_arm(self):
self.board.write("250 0 -145".encode("utf-8"))
grabbed =''
self.board.flush()
self.board.write(bytes("250 0 -145", "utf-8"))
time.sleep(1)
if self.verbose is "terminal" or self.verbose is "both": print("move arm")
if self.verbose == "terminal" or self.verbose == "both": print("move arm")
while grabbed != b'2\r\n':
grabbed = self.board.readline()
print(grabbed)
self.search = SEARCH[2]
self.routine =False
print(self.routine)
def main():
if len(sys.argv) < 4 or len(sys.argv) > 5:
@@ -396,8 +403,8 @@ def main():
return 1
elif len(sys.argv) == 4:
ic = tutel(str(sys.argv[1]),str(sys.argv[2]),str(sys.argv[3]))
elif len(sys.argv) == 5:
ic = tutel(str(sys.argv[1]), str(sys.argv[2]), str(sys.argv[3]), str(sys.argv[4])
else:
ic = tutel(str(sys.argv[1]), str(sys.argv[2]), str(sys.argv[3]), str(sys.argv[4]))
try:
rospy.spin()
except KeyboardInterrupt: