def __init__(self, route_idx = 0): self.detection_id = -1 self.idx = 6 self.lr = tf.TransformListener() self.br = tf.TransformBroadcaster() self.apriltag_sub = rospy.Subscriber("/apriltags/detections", AprilTagDetections, self.apriltag_callback, queue_size = 1) self.velcmd_pub = rospy.Publisher('/cmdvel',WheelCmdVel,queue_size=1) wcv = WheelCmdVel() wcv.desiredGripperPos = gripperOpen self.velcmd_pub.publish(wcv) self.idx6cnt= 0 self.stage9 = 0 self.thread = threading.Thread(target = self.navi_loop) self.thread.start() rospy.sleep(1)
def navi_loop(self): generic_target_pose2d = [0.4, 0, np.pi] #target_pose2d = [0.25, 0, np.pi] rate = rospy.Rate(100) # 100hz wcv = WheelCmdVel() wcv.desiredGripperPos = gripperOpen arrived = False arrived_position = False self.print_id = -1 while not rospy.is_shutdown() : if self.idx >= 10: break # get robot pose robot_pose3d = lookupTransform(self.lr, '/map', '/robot_base') target_pose2d = generic_target_pose2d #7 close gripper if self.idx == 7: rospy.sleep(10) wcv.desiredGripperPos = gripperClose self.velcmd_pub.publish(wcv) self.idx = self.idx + 1 #8 elif self.idx == 8: # retreat print 'idx =', self.idx wcv.desiredWV_R = -0.1 wcv.desiredWV_L = -0.1 self.velcmd_pub.publish(wcv) rospy.sleep(5.5) wcv.desiredWV_R = -0.3 wcv.desiredWV_L = -0.1 self.velcmd_pub.publish(wcv) rospy.sleep(4.5) wcv.desiredWV_R = 0.1 wcv.desiredWV_L = 0.15 self.velcmd_pub.publish(wcv) rospy.sleep(1) wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) self.idx = self.idx + 1 elif self.idx == 9: # go straight wcv.desiredWV_R = 0.1 wcv.desiredWV_L = 0.1 self.velcmd_pub.publish(wcv) self.idx = self.idx + 1 rospy.sleep(18) wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) rate.sleep()
def navi_loop(self): generic_target_pose2d = [0.4, 0, np.pi] #target_pose2d = [0.25, 0, np.pi] rate = rospy.Rate(100) # 100hz wcv = WheelCmdVel() wcv.desiredGripperPos = gripperOpen arrived = False arrived_position = False self.print_id = -1 while not rospy.is_shutdown(): if self.idx >= 10: break # get robot pose robot_pose3d = lookupTransform(self.lr, '/map', '/robot_base') target_pose2d = generic_target_pose2d #7 close gripper if self.idx == 7: rospy.sleep(1) wcv.desiredGripperPos = gripperClose self.velcmd_pub.publish(wcv) self.idx = self.idx + 1 #8 elif self.idx == 8: # retreat print 'idx =', self.idx wcv.desiredWV_R = -0.1 wcv.desiredWV_L = -0.1 self.velcmd_pub.publish(wcv) rospy.sleep(2) wcv.desiredWV_R = -0.3 wcv.desiredWV_L = -0.1 self.velcmd_pub.publish(wcv) rospy.sleep(2.5) wcv.desiredWV_R = 0 wcv.desiredWV_L = 0.1 self.velcmd_pub.publish(wcv) rospy.sleep(4) wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) self.idx = self.idx + 1 elif self.idx == 9: # go straight wcv.desiredWV_R = 0.1 wcv.desiredWV_L = 0.1 self.velcmd_pub.publish(wcv) self.idx = self.idx + 1 rospy.sleep(18) wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) elif (self.idx == 5 and self.detection_id == 4): if robot_pose3d is None: print_id = 0 if print_id != self.print_id: self.print_id = print_id print '1. Tag not in view, Stop' wcv.desiredWV_R = 0 # right, left wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) rate.sleep() continue robot_position2d = robot_pose3d[0:2] target_position2d = target_pose2d[0:2] robot_yaw = tfm.euler_from_quaternion(robot_pose3d[3:7])[2] robot_pose2d = robot_position2d + [robot_yaw] # 2. navigation policy # 2.1 if in the target, stop # 2.2 else if close to target position, turn to the target orientation # 2.3 else if in the correct heading, go straight to the target position, # 2.4 else turn in the direction of the target position pos_delta = np.array(target_position2d) - np.array( robot_position2d) robot_heading_vec = np.array( [np.cos(robot_yaw), np.sin(robot_yaw)]) heading_err_cross = cross2d( robot_heading_vec, pos_delta / np.linalg.norm(pos_delta)) # print 'robot_position2d', robot_position2d, 'target_position2d', target_position2d # print 'pos_delta', pos_delta # print 'robot_yaw', robot_yaw # print 'norm delta', np.linalg.norm( pos_delta ), 'diffrad', diffrad(robot_yaw, target_pose2d[2]) # print 'heading_err_cross', heading_err_cross if arrived or (np.linalg.norm(pos_delta) < 0.08 and np.fabs( diffrad(robot_yaw, target_pose2d[2])) < 0.05): print_id = 21 if print_id != self.print_id: self.print_id = print_id print 'Case 2.1 Stop' wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 arrived = True elif np.linalg.norm(pos_delta) < 0.08: arrived_position = True if diffrad(robot_yaw, target_pose2d[2]) > 0: print_id = 221 if print_id != self.print_id: self.print_id = print_id print 'Case 2.2.1 Turn right slowly' wcv.desiredWV_R = -0.05 wcv.desiredWV_L = 0.05 else: print_id = 222 if print_id != self.print_id: self.print_id = print_id print 'Case 2.2.2 Turn left slowly' wcv.desiredWV_R = 0.05 wcv.desiredWV_L = -0.05 elif arrived_position or np.fabs(heading_err_cross) < 0.2: print_id = 23 if print_id != self.print_id: self.print_id = print_id print 'Case 2.3 Straight forward' wcv.desiredWV_R = 0.1 wcv.desiredWV_L = 0.1 else: if heading_err_cross < 0: print_id = 241 if print_id != self.print_id: self.print_id = print_id print 'Case 2.4.1 Turn right' wcv.desiredWV_R = -0.05 wcv.desiredWV_L = 0.05 else: print_id = 242 if print_id != self.print_id: self.print_id = print_id print 'Case 2.4.2 Turn left' wcv.desiredWV_R = 0.05 wcv.desiredWV_L = -0.05 self.velcmd_pub.publish(wcv) if arrived: print 'old idx', self.idx, 'new idx', self.idx + 1 self.idx = self.idx + 1 arrived = False arrived_position = False rospy.sleep(3) rate.sleep()
def navi_loop(self): first_turn_point_id6_route0 = [0.95, 0.28, -3.00] # apriltag id = 6 first_turn_point_id6_route1 = [0.38, 0.09, 3.076] # apriltag id = 6 stop_first_turn_lo_route0 = [2.38, 0.84, -2.75] # apriltag id = 4 stop_first_turn_up_route0 = [2.30, 0.86, -3.03] # apriltag id = 4 stop_first_turn_lo_route1 = [2.14, -0.45, 2.95] # apriltag id = 4 stop_first_turn_up_route1 = [2.01, 0.34, 3.10] # apriltag id = 4 generic_target_pose2d = [0.4, 0, np.pi] #target_pose2d = [0.25, 0, np.pi] rate = rospy.Rate(100) # 100hz wcv = WheelCmdVel() wcv.desiredGripperPos = gripperOpen arrived = False arrived_position = False self.print_id = -1 while not rospy.is_shutdown(): print 'self.idx', self.idx if self.idx >= 11: break if self.idx <= 0: # haven't found obstacle rospy.sleep(2) continue # 0. set target target_pose2d = generic_target_pose2d # 1. get robot pose robot_pose3d = lookupTransform(self.lr, '/map', '/robot_base') # idx == 1, start from initial position if self.idx == 1: if self.route_idx == 0: self.idx = self.idx + 1 # go straight wcv.desiredWV_R = 0.2 wcv.desiredWV_L = 0.2 self.velcmd_pub.publish(wcv) rospy.sleep(1) # stop wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) rospy.sleep(1) # turn right a few degrees wcv.desiredWV_R = 0.1 wcv.desiredWV_L = 0.2 self.velcmd_pub.publish(wcv) rospy.sleep(5.5) # stop wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) rospy.sleep(1) # go straight wcv.desiredWV_R = 0.5 wcv.desiredWV_L = 0.5 self.velcmd_pub.publish(wcv) rospy.sleep(4) # stop wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) rospy.sleep(1) elif self.route_idx == 1: self.idx = self.idx + 1 # go straight wcv.desiredWV_R = 0.3 wcv.desiredWV_L = 0.3 self.velcmd_pub.publish(wcv) rospy.sleep(4.5) # stop wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) rospy.sleep(1) # turn right a few degrees wcv.desiredWV_R = -0.1 wcv.desiredWV_L = 0.1 self.velcmd_pub.publish(wcv) rospy.sleep(3) # stop wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) rospy.sleep(1) # go straight wcv.desiredWV_R = 0.5 wcv.desiredWV_L = 0.5 self.velcmd_pub.publish(wcv) rospy.sleep(3) #was3 # stop wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) rospy.sleep(1) elif self.idx == 2: self.idx = self.idx + 1 # idx == 2 or 5, go to human or target elif (self.idx == 5 and self.detection_id == 3) \ or (self.idx == 10 and self.detection_id == 4): if robot_pose3d is None: print_id = 0 if print_id != self.print_id: self.print_id = print_id print '1. Tag not in view, Stop' wcv.desiredWV_R = 0 # right, left wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) rate.sleep() continue robot_position2d = robot_pose3d[0:2] target_position2d = target_pose2d[0:2] robot_yaw = tfm.euler_from_quaternion(robot_pose3d[3:7])[2] robot_pose2d = robot_position2d + [robot_yaw] # 2. navigation policy # 2.1 if in the target, stop # 2.2 else if close to target position, turn to the target orientation # 2.3 else if in the correct heading, go straight to the target position, # 2.4 else turn in the direction of the target position pos_delta = np.array(target_position2d) - np.array( robot_position2d) robot_heading_vec = np.array( [np.cos(robot_yaw), np.sin(robot_yaw)]) heading_err_cross = cross2d( robot_heading_vec, pos_delta / np.linalg.norm(pos_delta)) # print 'robot_position2d', robot_position2d, 'target_position2d', target_position2d # print 'pos_delta', pos_delta # print 'robot_yaw', robot_yaw # print 'norm delta', np.linalg.norm( pos_delta ), 'diffrad', diffrad(robot_yaw, target_pose2d[2]) # print 'heading_err_cross', heading_err_cross if arrived or (np.linalg.norm(pos_delta) < 0.08 and np.fabs( diffrad(robot_yaw, target_pose2d[2])) < 0.05): print_id = 21 if print_id != self.print_id: self.print_id = print_id print 'Case 2.1 Stop' wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 arrived = True elif np.linalg.norm(pos_delta) < 0.08: arrived_position = True if diffrad(robot_yaw, target_pose2d[2]) > 0: print_id = 221 if print_id != self.print_id: self.print_id = print_id print 'Case 2.2.1 Turn right slowly' wcv.desiredWV_R = -0.05 wcv.desiredWV_L = 0.05 else: print_id = 222 if print_id != self.print_id: self.print_id = print_id print 'Case 2.2.2 Turn left slowly' wcv.desiredWV_R = 0.05 wcv.desiredWV_L = -0.05 elif arrived_position or np.fabs(heading_err_cross) < 0.2: print_id = 23 if print_id != self.print_id: self.print_id = print_id print 'Case 2.3 Straight forward' wcv.desiredWV_R = 0.1 wcv.desiredWV_L = 0.1 else: if heading_err_cross < 0: print_id = 241 if print_id != self.print_id: self.print_id = print_id print 'Case 2.4.1 Turn right' wcv.desiredWV_R = -0.05 wcv.desiredWV_L = 0.05 else: print_id = 242 if print_id != self.print_id: self.print_id = print_id print 'Case 2.4.2 Turn left' wcv.desiredWV_R = 0.05 wcv.desiredWV_L = -0.05 self.velcmd_pub.publish(wcv) if arrived: print 'old idx', self.idx, 'new idx', self.idx + 1 self.idx = self.idx + 1 arrived = False arrived_position = False rospy.sleep(3) # idx == 3, wait for human signal # apriltag id == 12: left # apriltag_id == 14: middle # apriltag_id == 16: right elif self.idx == 3: if self.detection_id == 12: self.bottle = 0 self.idx = self.idx + 1 elif self.detection_id == 14: self.bottle = 1 self.idx = self.idx + 1 elif self.detection_id == 16: self.bottle = 2 self.idx = self.idx + 1 # idx == 4, turn left and go to target elif self.idx == 4: if self.detection_id == 3: print_id = 4002 if print_id != self.print_id: self.print_id = print_id print 'idx4 finished' wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 arrived = True else: print_id = 4001 if print_id != self.print_id: self.print_id = print_id print 'Turn left slowly' wcv.desiredWV_R = 0.05 wcv.desiredWV_L = -0.05 self.velcmd_pub.publish(wcv) if arrived: self.idx = self.idx + 1 arrived = False arrived_position = False rospy.sleep(1) # 6 go to bottle elif self.idx == 6: print 'idx =', self.idx if self.bottle == 0: #left wcv.desiredWV_R = -0.3 wcv.desiredWV_L = -0.1 self.velcmd_pub.publish(wcv) rospy.sleep(2) wcv.desiredWV_R = 0.3 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) rospy.sleep(1.8) ''' wcv.desiredWV_R = 0.05 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) rospy.sleep(1) ''' wcv.desiredWV_R = 0.1 wcv.desiredWV_L = 0.1 self.velcmd_pub.publish(wcv) rospy.sleep(3.8) wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) self.idx = self.idx + 1 elif self.bottle == 1: # middle wcv.desiredWV_R = 0.05 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) rospy.sleep(3) wcv.desiredWV_R = 0.05 wcv.desiredWV_L = 0.05 self.velcmd_pub.publish(wcv) self.idx = self.idx + 1 rospy.sleep(3.6) wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) elif self.bottle == 2: # right wcv.desiredWV_R = 0.05 wcv.desiredWV_L = 0.05 self.velcmd_pub.publish(wcv) self.idx = self.idx + 1 rospy.sleep(6) wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) # left #7 close gripper elif self.idx == 7: wcv.desiredGripperPos = girpperClose self.velcmd_pub.publish(wcv) self.idx = self.idx + 1 rospy.sleep(2) self.idx6cnt = 0 #8 elif self.idx == 8: # retreat print 'idx =', self.idx if self.bottle == 0: #left if self.route_idx == 0: wcv.desiredWV_R = -0.3 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) rospy.sleep(2) self.idx = self.idx + 1 wcv.desiredWV_R = -0.1 wcv.desiredWV_L = -0.1 #self.velcmd_pub.publish(wcv) rospy.sleep(2) # left wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) elif self.route_idx == 1: wcv.desiredWV_R = -0.3 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) rospy.sleep(2) self.idx = self.idx + 1 wcv.desiredWV_R = -0.1 wcv.desiredWV_L = -0.1 #self.velcmd_pub.publish(wcv) rospy.sleep(1) # left wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) elif self.bottle == 1: #middle wcv.desiredWV_R = -0.3 wcv.desiredWV_L = -0.1 self.velcmd_pub.publish(wcv) self.idx = self.idx + 1 rospy.sleep(5) #middle wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) elif self.bottle == 2: #right wcv.desiredWV_R = -0.3 wcv.desiredWV_L = -0.1 self.velcmd_pub.publish(wcv) self.idx = self.idx + 1 rospy.sleep(4) wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) elif self.idx == 9: # turn right print 'idx =', self.idx if self.bottle == 0: #left if self.route_idx == 0: wcv.desiredWV_R = 0 wcv.desiredWV_L = 0.2 self.velcmd_pub.publish(wcv) self.idx = self.idx + 1 rospy.sleep(3) # left wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) elif self.route_idx == 1: wcv.desiredWV_R = 0 wcv.desiredWV_L = 0.2 self.velcmd_pub.publish(wcv) rospy.sleep(3) # left if self.detection_id != 4: wcv.desiredWV_R = 0 wcv.desiredWV_L = 0.05 self.velcmd_pub.publish(wcv) else: self.idx = self.idx + 1 elif self.bottle == 1: #middle wcv.desiredWV_R = 0 wcv.desiredWV_L = 0.2 self.velcmd_pub.publish(wcv) self.idx = self.idx + 1 rospy.sleep(5.5) # middle wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) elif self.bottle == 2: #right wcv.desiredWV_R = 0 wcv.desiredWV_L = 0.2 self.velcmd_pub.publish(wcv) self.idx = self.idx + 1 rospy.sleep(5) wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) ''' elif self.idx == 10: # go straight print 'idx =', self.idx wcv.desiredWV_R = 0.3 wcv.desiredWV_L = 0.3 self.velcmd_pub.publish(wcv) self.idx = self.idx + 1 rospy.sleep(4.5) wcv.desiredWV_R = 0 wcv.desiredWV_L = 0 self.velcmd_pub.publish(wcv) ''' rate.sleep()