diff --git a/dennis_nodes/tutel.py b/dennis_nodes/tutel.py index c6c5098..236d1dc 100644 --- a/dennis_nodes/tutel.py +++ b/dennis_nodes/tutel.py @@ -16,6 +16,7 @@ from geometry_msgs.msg import Twist TARGET = 0 DEPTH = 400 +POSITIONS = ["left", "right", "middle"] DEVICE = 'dev/ttyACM' BAUD = 9600 @@ -62,13 +63,13 @@ class tutel(): #cv related #rgb related - self.leftright = 0 + self.position = POSITIONS[-1] self.lastConernerTime = time.time() self.rgb_bridge = CvBridge() - self.rState = 0 + self.search = True #depth related self.depth_bridge = CvBridge() - self.depth_point(-1, -1) + self.depth_state = False self.last_depth = 10 self.depth_counter = 0 @@ -81,27 +82,149 @@ class tutel(): #Sub init self.rgb_sub = rospy.Subscriber(astra_rgb_topic, Image, self.callback_rgb) self.depth_sub = rospy.Subscriber(astra_depth_topic, Image, self.callback_depth) - + + #finding specific port for arduino communication def init_serial(self): - pass - + for com in range(0,4): + subprocess.run(["fuser", "/dev/ttyACM"+str(com), "-k"]) + try: + port = DEVICE+str(com) + baud = BAUD + board = serial.Serial(port, baud) + board.close() + break + except: + pass + return com + + #we wait for both publishers, camera topics, to send information def wait_publishers(self, astra_rgb_topic, astra_depth_topic): while True: if any(astra_rgb_topic in topic_list for topic_list in rospy.get_published_topics()) and any(astra_depth_topic in topic_list for topic_list in rospy.get_published_topics()): break print("Waiting for {} and {} to start.".format(astra_rgb_topic, astra_depth_topic)) rospy.sleep(1) + + def callback_rgb(self, data): + try: + rgb_image = self.rgb_bridge.imgmsg_to_cv2(data, "bgr8") + (self.h, self.w) = rgb_image.shape[:2] + except CvBridgeError as e: + print(e) + + (corners, ids, rejected) = cv2.aruco.detectMarkers(rgb_image, self.arucoDict, parameters = self.arucoParams) + + if self.search: + self.t.linear.x = 0.0 * self.vel_fac + self.t.angular.z = 0.4 * self.vel_fac + self.pub.publish(self.t) + if corners: + self.target_id = ids[0] + self.search = False + else: + if corners: + x = np.where(ids == self.target_id) + if len(x[0]) > 0: + fitting_corners = corners(x[0][0]) + rgb_image = self.markDetected(rgb_image, fitting_corners) + self.lastConernerTime = time.time() + elif(self.lastConernerTime . time.time()) < 1./2: + self.depth_state = False + if self.position is POSITIONS[0]: + 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.3 * self.vel_fac + self.pub.publish(self.t) + elif self.position is POSITIONS[1]: + 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.3 * self.vel_fac + self.pub.publish(self.t) + + #some additional info on screen + 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 = "Resolution: " + str(self.w) + "x" + str(self.h) + " Search_State: " + str(self.search) + " Max_Min_Depth: " + str(self.maxMinDepth) + cv2.putText(rgb_image, info_str, (10,90), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8) + + self.rate.sleep() + 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.depth_counter == 0: + self.last_depth = 0 + self.number_of_operations = 0 + #x and y are swapped, because + for x in range(self.top_left.x, self.top_right.x+1): + for y in range(self.top_left.y, self.bottom_left.y+1): + self.last_depth += depth_image[y][x] + self.number_of_operations += 1 + for y in range(self.top_left.y, self.top_right.y+1): + for x in range(self.top_left.x, self.bottom_left.x+1): + self.last_depth += depth_image[y][x] + self.number_of_operations += 1 + self.last_depth /= number_of_operations + if self.last_depth < self.maxMinDepth and self.last_depth != 0: + self.depth_counter += 1 + if self.depth_counter > 4: + print("Reached our destination") + self.t.linear.x = 0.0 * self.vel_fac + self.t.angular.z = 0.0 * self.vel_fac + self.pub.publish(t) + self.position = POSITIONS[2] + self.move_arm(self) + else: + self.depth_counter = 0 + + + def markDetected(self, image, corners): + thresh_left = self.w//2 - 100 + thresh_right = self.w//2 + 100 + for markerCorner in corners: + (top_left, top_right, bottom_right, bottom_left) = markerCorner.reshape((4,2)) + top_left = (int(top_left[0]), int(top_left[1])) + top_right = (int(top_right[0]), int(top_right[1])) + bottom_right = (int(bottom_right[0]), int(bottom_right[1])) + bottom_left = (int(bottom_left[0]), int(bottom_left[1])) + + cv2.line(image, top_left, bottom_right, (0,255,0), 2) + cv2.line(image, top_right, bottom_right, (0,255,0), 2) + + self.depth_state = True + + self.last_marker_position = ((top_left), (bottom_right)) + if ((top_left[0] > thresh_left and top_left[0] < thresh_right) or + (top_right[0] > thresh_left and top_right[0] < thresh_right) or + (bottom_left[0] > thresh_left and bottom_left[0] < thresh_right) or + (bottom_right[0] > thresh_left and bottom_right[0] < thresh_right)): + self.t.linear.x = 0.1 * self.vel_fac + self.t.angular.z = 0.0 * self.vel_fac + self.pub.publish(self.t) + elif(top_right[0] < thresh_left or + bottom_right[0] < thresh_left): + self.t.linear.x = 0.0 * self.vel_fac + self.t.angular.z = 0.3 * self.vel_fac + self.pub.publish(self.t) + elif(top_left[0] > thresh_right or + bottom_left[0] > thresh_right): + self.t.linear.x = 0.0 * self.vel_fac + self.t.angular.z = -0.3 * self.vel_fac + self.pub.publish(self.t) + if int(self.w / 2) > top_left[0]: + self.position = POSITIONS[0] + elif self.w - int(self.w / 2) < top_left[0]: + self.position = POSITIONS[1] + + return img + + + def move_arm(self): + self.s.write('1'.encode('utf-8')) + time.sleep(1) \ No newline at end of file