def __init__(self): group = MoveGroupCommander("arm") #group.set_orientation_tolerance([0.3,0.3,0,3]) p = PoseStamped() p.header.frame_id = "/katana_base_link" p.pose.position.x = 0.4287 #p.pose.position.y = -0.0847 p.pose.position.y = 0.0 p.pose.position.z = 0.4492 q = quaternion_from_euler(2, 0, 0) p.pose.orientation.x = q[0] p.pose.orientation.y = q[1] p.pose.orientation.z = q[2] p.pose.orientation.w = q[3] print "Planning frame: ", group.get_planning_frame() print "Pose reference frame: ", group.get_pose_reference_frame() #group.set_pose_reference_frame("katana_base_link") print "RPy target: 0,0,0" #group.set_rpy_target([0, 0, 0],"katana_gripper_tool_frame") #group.set_position_target([0.16,0,0.40], "katana_gripper_tool_frame") group.set_pose_target(p, "katana_gripper_tool_frame") group.go() print "Current rpy: ", group.get_current_rpy( "katana_gripper_tool_frame")
def __init__(self): group = MoveGroupCommander("arm") #group.set_orientation_tolerance([0.3,0.3,0,3]) p = PoseStamped() p.header.frame_id = "/katana_base_link" p.pose.position.x = 0.4287 #p.pose.position.y = -0.0847 p.pose.position.y = 0.0 p.pose.position.z = 0.4492 q = quaternion_from_euler(2, 0, 0) p.pose.orientation.x = q[0] p.pose.orientation.y = q[1] p.pose.orientation.z = q[2] p.pose.orientation.w = q[3] print "Planning frame: " ,group.get_planning_frame() print "Pose reference frame: ",group.get_pose_reference_frame() #group.set_pose_reference_frame("katana_base_link") print "RPy target: 0,0,0" #group.set_rpy_target([0, 0, 0],"katana_gripper_tool_frame") #group.set_position_target([0.16,0,0.40], "katana_gripper_tool_frame") group.set_pose_target(p, "katana_gripper_tool_frame") group.go() print "Current rpy: " , group.get_current_rpy("katana_gripper_tool_frame")
class WarehousePlanner(object): def __init__(self): rospy.init_node('moveit_warehouse_planner', anonymous=True) self.scene = PlanningSceneInterface() self.robot = RobotCommander() rospy.sleep(2) group_id = rospy.get_param("~arm_group_name") self.eef_step = rospy.get_param("~eef_step", 0.01) self.jump_threshold = rospy.get_param("~jump_threshold", 1000) self.group = MoveGroupCommander(group_id) # self._add_ground() self._robot_name = self.robot._r.get_robot_name() self._robot_link = self.group.get_end_effector_link() self._robot_frame = self.group.get_pose_reference_frame() self._min_wp_fraction = rospy.get_param("~min_waypoint_fraction", 0.9) rospy.sleep(4) rospy.loginfo("Waiting for warehouse services...") rospy.wait_for_service('moveit_warehouse_services/list_robot_states') rospy.wait_for_service('moveit_warehouse_services/get_robot_state') rospy.wait_for_service('moveit_warehouse_services/has_robot_state') rospy.wait_for_service('/compute_fk') self._list_states = rospy.ServiceProxy( 'moveit_warehouse_services/list_robot_states', ListStates) self._get_state = rospy.ServiceProxy( 'moveit_warehouse_services/get_robot_state', GetState) self._has_state = rospy.ServiceProxy( 'moveit_warehouse_services/has_robot_state', HasState) self._forward_k = rospy.ServiceProxy('compute_fk', GetPositionFK) rospy.loginfo("Service proxies connected") self._tr_frm_list_srv = rospy.Service('plan_trajectory_from_list', PlanTrajectoryFromList, self._plan_from_list_cb) self._tr_frm_prfx_srv = rospy.Service('plan_trajectory_from_prefix', PlanTrajectoryFromPrefix, self._plan_from_prefix_cb) self._execute_tr_srv = rospy.Service('execute_planned_trajectory', ExecutePlannedTrajectory, self._execute_plan_cb) self.__plan = None def get_waypoint_names_by_prefix(self, prefix): regex = "^" + str(prefix) + ".*" waypoint_names = self._list_states(regex, self._robot_name).states return waypoint_names def get_pose_from_state(self, state): header = Header() fk_link_names = [self._robot_link] header.frame_id = self._robot_frame response = self._forward_k(header, fk_link_names, state) return response.pose_stamped[0].pose def get_cartesian_waypoints(self, waypoint_names): waypoints = [] waypoints.append(self.group.get_current_pose().pose) for name in waypoint_names: if self._has_state(name, self._robot_name).exists: robot_state = self._get_state(name, "").state waypoints.append(self.get_pose_from_state(robot_state)) else: rospy.logerr("Waypoint %s doesn't exist for robot %s.", name, self._robot_name) return waypoints def _add_ground(self): p = geometry_msgs.msg.PoseStamped() p.header.frame_id = self.robot.get_planning_frame() p.pose.position.x = 0 p.pose.position.y = 0 # offset such that the box is below the dome p.pose.position.z = -0.11 p.pose.orientation.x = 0 p.pose.orientation.y = 0 p.pose.orientation.z = 0 p.pose.orientation.w = 1 self.scene.add_box("ground", p, (3, 3, 0.01)) rospy.sleep(1) def plan_from_filter(self, prefix): waypoint_names = self.get_waypoint_names_by_prefix(prefix) waypoint_names.sort() if 0 == len(waypoint_names): rospy.logerr( "No waypoints found for robot %s with prefix %s. " + "Can't make trajectory :(", self._robot_name, str(prefix)) return False rospy.loginfo( "Creating trajectory with %d waypoints selected by " + "prefix %s.", len(waypoint_names), str(prefix)) return self.plan_from_list(waypoint_names) def plan_from_list(self, waypoint_names): self.group.clear_pose_targets() waypoints = self.get_cartesian_waypoints(waypoint_names) if len(waypoints) != len(waypoint_names) + 1: # +1 because current position is used as first waypiont. rospy.logerr("Not all waypoints existed, not executing.") return False (plan, fraction) = self.group.compute_cartesian_path(waypoints, self.eef_step, self.jump_threshold) if fraction < self._min_wp_fraction: rospy.logerr( "Only managed to generate trajectory through" + "%f of waypoints. Not executing", fraction) return False self.__plan = plan return True def _plan_from_list_cb(self, request): # check all exist self.__plan = None rospy.loginfo("Creating trajectory from points given: %s", ",".join(request.waypoint_names)) return self.plan_from_list(request.waypoint_names) def _plan_from_prefix_cb(self, request): self.__plan = None rospy.loginfo("Creating trajectory from points filtered by prefix %s", request.prefix) return self.plan_from_filter(request.prefix) def _execute_plan_cb(self, request): if self.__plan is None: rospy.logerr("No plan stored!") return False rospy.loginfo("Executing stored plan") response = self.group.execute(self.__plan) self.__plan = None return response
class PR2Greeter: def __init__(self, debug=False, online = True, robot_frame="odom_combined"): self._tf = TransformListener() self._online = online # self.snd_handle = SoundClient() if self._online: #self._interface = ROSpeexInterface() #self._interface.init() self._speech_client = SpeechSynthesisClient_NICT() else: self.snd_handle = SoundClient() rospy.sleep(1) self.say('Hello world!') rospy.sleep(1) self._debug = debug self._robot_frame = robot_frame self._point_sub = rospy.Subscriber('nearest_face', PointStamped, self.face_cb) self._head_action_cl = actionlib.SimpleActionClient('/head_traj_controller/point_head_action', pr2_controllers_msgs.msg.PointHeadAction) self._torso_action_cl = actionlib.SimpleActionClient('/torso_controller/position_joint_action', pr2_controllers_msgs.msg.SingleJointPositionAction) self._left_arm = MoveGroupCommander("left_arm") self._right_arm = MoveGroupCommander("right_arm") print "r.f.: " + self._left_arm.get_pose_reference_frame() self.face = None # self.face_from = rospy.Time(0) self.face_last_dist = 0 self.last_invited_at = rospy.Time(0) self._person_prob_left = 0 self.l_home_pose = [0.283, 0.295, 0.537, -1.646, 0.468, -1.735] self.l_wave_1 = [-0.1, 0.6, 1.15, -1.7, -0.97, -1.6] self.l_wave_2 = [-0.1, 0.6, 1.15, 1.7, -0.97, 1.6] self.r_home_pose = [0.124, -0.481, 0.439, -1.548, 0.36, -0.035] self.r_advert = [0.521, -0.508, 0.845, -1.548, 0.36, -0.035] self.no_face_random_delay = None self._initialized = False self._timer = rospy.Timer(rospy.Duration(1.0), self.timer) self._move_buff = Queue.Queue() self._head_buff = Queue.Queue() self._move_thread = threading.Thread(target=self.movements) self._move_thread.daemon = True self._move_thread.start() self._head_thread = threading.Thread(target=self.head) self._head_thread.daemon = True self._head_thread.start() self.new_face = False self.face_last_dist = 0.0 self.face_counter = 0 self.actions = [self.stretchingAction, self.introduceAction, self.waveHandAction, self.lookAroundAction, self.lookAroundAction, self.lookAroundAction, self.advertAction, self.numberOfFacesAction] self.goodbye_strings = ["Thanks for stopping by.", "Enjoy the event.", "See you later!", "Have a nice day!"] self.invite_strings = ["Hello. It's nice to see you.", "Come here and take some flyer.", "I hope you are enjoying the event."] rospy.loginfo("Ready") def say(self, text): if self._online: #self._interface.say(text, 'en', 'nict') data = self._speech_client.request(text, 'en') try: tmp_file = open('output_tmp.dat', 'wb') tmp_file.write(data) tmp_file.close() # play sound subprocess.check_output(['ffplay', 'output_tmp.dat', '-autoexit', '-nodisp'], stderr=subprocess.STDOUT) except IOError: rospy.logerr('file io error.') except OSError: rospy.logerr('ffplay is not installed.') except subprocess.CalledProcessError: rospy.logerr('ffplay return error value.') except: rospy.logerr('unknown exception.') else: self.snd_handle.say(text) def getRandomFromArray(self, arr): idx = random.randint(0, len(arr) - 1) return arr[idx] def stretchingAction(self): self.say("I'm bit tired. Let's do some stretching.") self._torso_action_cl.wait_for_server() goal = pr2_controllers_msgs.msg.SingleJointPositionGoal() goal.position = 0.195 goal.max_velocity = 0.5 try: self._torso_action_cl.send_goal(goal) self._torso_action_cl.wait_for_result(rospy.Duration.from_sec(10.0)) except: rospy.logerr("torso action error (up)") # TODO move arms self.say("I feel much better now!") goal.position = 0.0 try: self._torso_action_cl.send_goal(goal) self._torso_action_cl.wait_for_result(rospy.Duration.from_sec(10.0)) except: rospy.logerr("torso action error (down)") def numberOfFacesAction(self): string = "Today I already saw " + str(self.face_counter) + "face" if self.face_counter != 1: string = string + "s" string = string + "." self.say(string) rospy.sleep(1) def advertAction(self): self.say("Hello. Here are some posters for you.") self.go(self._right_arm, self.r_advert) rospy.sleep(1) def introduceAction(self): self.say("Hello. I'm PR2 robot. Come here to check me.") rospy.sleep(1) def waveHandAction(self): self.say("I'm here. Please come to see me.") rand = random.randint(1, 3) for _ in range(rand): self.wave() self.go(self._left_arm, self.l_home_pose) rospy.loginfo("Waving") rospy.sleep(1) def lookAroundAction(self): self.say("I'm looking for somebody. Please come closer.") p = PointStamped() p.header.stamp = rospy.Time.now() p.header.frame_id = "/base_link" p.point.x = 5.0 sign = random.choice([-1, 1]) p.point.y = sign * random.uniform(1.5, 0.5) p.point.z = random.uniform(1.7, 0.2) self._head_buff.put((copy.deepcopy(p), 0.1, True)) p.point.y = -1 * sign * random.uniform(1.5, 0.5) p.point.z = random.uniform(1.7, 0.2) self._head_buff.put((copy.deepcopy(p), 0.1, True)) p.point.y = sign * random.uniform(1.5, 0.5) p.point.z = random.uniform(1.7, 0.2) self._head_buff.put((copy.deepcopy(p), 0.1, True)) p.point.y = -1 * sign * random.uniform(1.5, 0.5) p.point.z = random.uniform(1.7, 0.2) self._head_buff.put((copy.deepcopy(p), 0.1, True)) rospy.loginfo("Looking around") rospy.sleep(1) def getPointDist(self, pt): assert(self.face is not None) # fist, get my position p = PoseStamped() p.header.frame_id = "base_link" p.header.stamp = rospy.Time.now() - rospy.Duration(0.5) p.pose.position.x = 0 p.pose.position.y = 0 p.pose.position.z = 0 p.pose.orientation.x = 0 p.pose.orientation.y = 0 p.pose.orientation.z = 0 p.pose.orientation.w = 1 try: self._tf.waitForTransform(p.header.frame_id, self._robot_frame, p.header.stamp, rospy.Duration(2)) p = self._tf.transformPose(self._robot_frame, p) except: rospy.logerr("TF error!") return None return sqrt(pow(p.pose.position.x - pt.point.x, 2) + pow(p.pose.position.y - pt.point.y, 2) + pow(p.pose.position.z - pt.point.z, 2)) def getPoseStamped(self, group, c): assert(len(c) == 6) p = PoseStamped() p.header.frame_id = "base_link" p.header.stamp = rospy.Time.now() - rospy.Duration(0.5) p.pose.position.x = c[0] p.pose.position.y = c[1] p.pose.position.z = c[2] quat = tf.transformations.quaternion_from_euler(c[3], c[4], c[5]) p.pose.orientation.x = quat[0] p.pose.orientation.y = quat[1] p.pose.orientation.z = quat[2] p.pose.orientation.w = quat[3] try: self._tf.waitForTransform(p.header.frame_id, group.get_pose_reference_frame(), p.header.stamp, rospy.Duration(2)) p = self._tf.transformPose(group.get_pose_reference_frame(), p) except: rospy.logerr("TF error!") return None return p def go(self, group, where): self._move_buff.put((group, where)) def wave(self): self.go(self._left_arm, self.l_wave_1) self.go(self._left_arm, self.l_wave_2) self.go(self._left_arm, self.l_wave_1) def head(self): self._head_action_cl.wait_for_server() while not rospy.is_shutdown(): (target, vel, imp) = self._head_buff.get() # we don't need to block it here self._head_buff.task_done() if (not imp) and (not self._head_buff.empty()): print "skipping head goal" # head target can be skipped (follow only the latest one) continue # print "head point goal" # print target # point PR2's head there (http://wiki.ros.org/pr2_controllers/Tutorials/Moving%20the%20Head) goal = pr2_controllers_msgs.msg.PointHeadGoal() goal.target = target # goal.min_duration = rospy.Duration(3.0) goal.max_velocity = vel # goal.pointing_frame = "high_def_frame" goal.pointing_frame = "head_mount_kinect_rgb_link" goal.pointing_axis.x = 1 goal.pointing_axis.y = 0 goal.pointing_axis.z = 0 try: self._head_action_cl.send_goal(goal) self._head_action_cl.wait_for_result(rospy.Duration.from_sec(5.0)) except: rospy.logerr("head action error") #self._head_buff.task_done() def movements(self): while not rospy.is_shutdown(): (group, where) = self._move_buff.get() group.set_start_state_to_current_state() p = self.getPoseStamped(group, where) if p is None: self._move_buff.task_done() continue group.set_pose_target(p) group.set_goal_tolerance(0.1) group.allow_replanning(True) self._move_buff.task_done() group.go(wait=True) def timer(self, event): if self._initialized is False: rospy.loginfo("Moving arms to home positions") self.init_head() self.go(self._left_arm, self.l_home_pose) self.go(self._right_arm, self.r_home_pose) self._move_buff.join() self.say("I'm ready for a great job.") self._initialized = True if self.face is None: if (self.no_face_random_delay is None): delay = random.uniform(30, 10) self.no_face_random_delay = rospy.Time.now() + rospy.Duration(delay) rospy.loginfo("Random delay: " + str(delay)) return else: if rospy.Time.now() < self.no_face_random_delay: return self.init_head() self.go(self._left_arm, self.l_home_pose) self.go(self._right_arm, self.r_home_pose) rospy.loginfo("Starting selected action") action = self.getRandomFromArray(self.actions) action() delay = random.uniform(30, 10) self.no_face_random_delay = rospy.Time.now() + rospy.Duration(delay) return else: self.no_face_random_delay = None if self.new_face and (self.last_invited_at + rospy.Duration(10) < rospy.Time.now()): self.last_invited_at = rospy.Time.now() self.new_face = False rospy.loginfo("new person") self.face_counter = self.face_counter + 1 # cd = getPointDist(self.face) # TODO decide action based on distance ? self.go(self._left_arm, self.l_home_pose) # if a person is too close, dont move hand? self.go(self._right_arm, self.r_advert) string = self.getRandomFromArray(self.invite_strings) self.say(string) # TODO wait some min. time + say something # after 20 seconds of no detected face, let's continue if self.face.header.stamp + rospy.Duration(10) < rospy.Time.now(): rospy.loginfo("person left") string = self.getRandomFromArray(self.goodbye_strings) self.say(string) self.init_head() self.go(self._left_arm, self.l_home_pose) self.go(self._right_arm, self.r_home_pose) self.face = None return self._head_buff.put((copy.deepcopy(self.face), 0.4, False)) def init_head(self): p = PointStamped() p.header.stamp = rospy.Time.now() p.header.frame_id = "/base_link" p.point.x = 2.0 p.point.y = 0.0 p.point.z = 1.7 self._head_buff.put((p, 0.1, True)) def face_cb(self, point): # transform point try: self._tf.waitForTransform(point.header.frame_id, self._robot_frame, point.header.stamp, rospy.Duration(2)) pt = self._tf.transformPoint(self._robot_frame, point) except: rospy.logerr("Transform error") return if self.face is not None: cd = self.getPointDist(pt) # current distance dd = fabs(self.face_last_dist - cd) # change in distance if dd < 0.5: self.face.header = pt.header # filter x,y,z values a bit self.face.point = pt.point #self.face.point.x = (self.face.point.x + pt.point.x) / 2 #self.face.point.y = (self.face.point.y + pt.point.y) / 2 #self.face.point.z = (self.face.point.z + pt.point.z) / 2 else: if self._person_prob_left < 2: self._person_prob_left += 1 else: self._person_prob_left = 0 print "new face ddist: " + str(dd) self.new_face = True self.face = pt self.face_last_dist = cd else: self._person_prob_left = 0 self.new_face = True self.face = pt
larmg = MoveGroupCommander("left_arm") larm_init_pose = Pose() larm_init_pose.position.x = 0.325 larm_init_pose.position.y = 0.182 larm_init_pose.position.z = 0.067 larm_init_pose.orientation.x = 0.0 larm_init_pose.orientation.y = -0.707 larm_init_pose.orientation.z = 0.0 larm_init_pose.orientation.w = 0.707 larmg.set_pose_target(larm_init_pose) larmg.go() # Right Arm group = MoveGroupCommander("right_arm") initial_reference_frame = group.get_pose_reference_frame() rospy.loginfo( "Initial Reference Frame: {}".format(initial_reference_frame)) initial_pose = group.get_current_pose() rospy.loginfo("Initial Pose:\n{}".format(initial_pose)) # Relative Target Pose target_pose = Pose() target_pose.position.x = 0.4 target_pose.orientation.w = 1.0 # Relative Target with PoseStamped rospy.loginfo("Using PoseStamped") target_posestamped = PoseStamped()
class SrRobotCommander(object): """ Base class for hand and arm commanders """ def __init__(self, name): """ Initialize MoveGroupCommander object @param name - name of the MoveIt group """ self._name = name self._move_group_commander = MoveGroupCommander(name) self._robot_commander = RobotCommander() self._robot_name = self._robot_commander._r.get_robot_name() self.refresh_named_targets() self._warehouse_name_get_srv = rospy.ServiceProxy( "get_robot_state", GetState) self._planning_scene = PlanningSceneInterface() self._joint_states_lock = threading.Lock() self._joint_states_listener = \ rospy.Subscriber("joint_states", JointState, self._joint_states_callback, queue_size=1) self._joints_position = {} self._joints_velocity = {} self._joints_effort = {} self._joints_state = None self._clients = {} self.__plan = None self._controllers = {} rospy.wait_for_service('compute_ik') self._compute_ik = rospy.ServiceProxy('compute_ik', GetPositionIK) self._forward_k = rospy.ServiceProxy('compute_fk', GetPositionFK) controller_list_param = rospy.get_param("/move_group/controller_list") # create dictionary with name of controllers and corresponding joints self._controllers = { item["name"]: item["joints"] for item in controller_list_param } self._set_up_action_client(self._controllers) self.tf_buffer = tf2_ros.Buffer() self.listener = tf2_ros.TransformListener(self.tf_buffer) threading.Thread(None, rospy.spin) def set_planner_id(self, planner_id): self._move_group_commander.set_planner_id(planner_id) def set_num_planning_attempts(self, num_planning_attempts): self._move_group_commander.set_num_planning_attempts( num_planning_attempts) def set_planning_time(self, seconds): self._move_group_commander.set_planning_time(seconds) def get_end_effector_pose_from_named_state(self, name): state = self._warehouse_name_get_srv(name, self._robot_name).state return self.get_end_effector_pose_from_state(state) def get_end_effector_pose_from_state(self, state): header = Header() fk_link_names = [self._move_group_commander.get_end_effector_link()] header.frame_id = self._move_group_commander.get_pose_reference_frame() response = self._forward_k(header, fk_link_names, state) return response.pose_stamped[0] def get_planning_frame(self): return self._move_group_commander.get_planning_frame() def set_pose_reference_frame(self, reference_frame): self._move_group_commander.set_pose_reference_frame(reference_frame) def get_group_name(self): return self._name def refresh_named_targets(self): self._srdf_names = self.__get_srdf_names() self._warehouse_names = self.__get_warehouse_names() def set_max_velocity_scaling_factor(self, value): self._move_group_commander.set_max_velocity_scaling_factor(value) def set_max_acceleration_scaling_factor(self, value): self._move_group_commander.set_max_acceleration_scaling_factor(value) def allow_looking(self, value): self._move_group_commander.allow_looking(value) def allow_replanning(self, value): self._move_group_commander.allow_replanning(value) def execute(self): """ Executes the last plan made. """ if self.check_plan_is_valid(): self._move_group_commander.execute(self.__plan) self.__plan = None else: rospy.logwarn("No plans were made, not executing anything.") def execute_plan(self, plan): if self.check_given_plan_is_valid(plan): self._move_group_commander.execute(plan) self.__plan = None else: rospy.logwarn("Plan is not valid, not executing anything.") def move_to_joint_value_target(self, joint_states, wait=True, angle_degrees=False): """ Set target of the robot's links and moves to it. @param joint_states - dictionary with joint name and value. It can contain only joints values of which need to be changed. @param wait - should method wait for movement end or not @param angle_degrees - are joint_states in degrees or not """ joint_states_cpy = copy.deepcopy(joint_states) if angle_degrees: joint_states_cpy.update( (joint, radians(i)) for joint, i in joint_states_cpy.items()) self._move_group_commander.set_start_state_to_current_state() self._move_group_commander.set_joint_value_target(joint_states_cpy) self._move_group_commander.go(wait=wait) def plan_to_joint_value_target(self, joint_states, angle_degrees=False): """ Set target of the robot's links and plans. @param joint_states - dictionary with joint name and value. It can contain only joints values of which need to be changed. @param angle_degrees - are joint_states in degrees or not This is a blocking method. """ joint_states_cpy = copy.deepcopy(joint_states) if angle_degrees: joint_states_cpy.update( (joint, radians(i)) for joint, i in joint_states_cpy.items()) self._move_group_commander.set_start_state_to_current_state() self._move_group_commander.set_joint_value_target(joint_states_cpy) self.__plan = self._move_group_commander.plan() return self.__plan def check_plan_is_valid(self): """ Checks if current plan contains a valid trajectory """ return (self.__plan is not None and len(self.__plan.joint_trajectory.points) > 0) def check_given_plan_is_valid(self, plan): """ Checks if given plan contains a valid trajectory """ return (plan is not None and len(plan.joint_trajectory.points) > 0) def get_robot_name(self): return self._robot_name def named_target_in_srdf(self, name): return name in self._srdf_names def set_named_target(self, name): if name in self._srdf_names: self._move_group_commander.set_named_target(name) elif (name in self._warehouse_names): response = self._warehouse_name_get_srv(name, self._robot_name) active_names = self._move_group_commander._g.get_active_joints() joints = response.state.joint_state.name positions = response.state.joint_state.position js = {} for n, this_name in enumerate(joints): if this_name in active_names: js[this_name] = positions[n] self._move_group_commander.set_joint_value_target(js) else: rospy.logerr("Unknown named state '%s'..." % name) return False return True def get_named_target_joint_values(self, name): output = dict() if (name in self._srdf_names): output = self._move_group_commander.\ _g.get_named_target_values(str(name)) elif (name in self._warehouse_names): js = self._warehouse_name_get_srv( name, self._robot_name).state.joint_state for x, n in enumerate(js.name): if n in self._move_group_commander._g.get_joints(): output[n] = js.position[x] else: rospy.logerr("No target named %s" % name) return None return output def get_end_effector_link(self): return self._move_group_commander.get_end_effector_link() def get_current_pose(self, reference_frame=None): """ Get the current pose of the end effector. @param reference_frame - The desired reference frame in which end effector pose should be returned. If none is passed, it will use the planning frame as reference. @return geometry_msgs.msg.Pose() - current pose of the end effector """ if reference_frame is not None: try: trans = self.tf_buffer.lookup_transform( reference_frame, self._move_group_commander.get_end_effector_link(), rospy.Time(0), rospy.Duration(5.0)) current_pose = geometry_msgs.msg.Pose() current_pose.position.x = trans.transform.translation.x current_pose.position.y = trans.transform.translation.y current_pose.position.z = trans.transform.translation.z current_pose.orientation.x = trans.transform.rotation.x current_pose.orientation.y = trans.transform.rotation.y current_pose.orientation.z = trans.transform.rotation.z current_pose.orientation.w = trans.transform.rotation.w return current_pose except (tf2_ros.LookupException, tf2_ros.ConnectivityException, tf2_ros.ExtrapolationException): rospy.logwarn( "Couldn't get the pose from " + self._move_group_commander.get_end_effector_link() + " in " + reference_frame + " reference frame") return None else: return self._move_group_commander.get_current_pose().pose def get_current_state(self): """ Get the current joint state of the group being used. @return a dictionary with the joint names as keys and current joint values """ joint_names = self._move_group_commander._g.get_active_joints() joint_values = self._move_group_commander._g.get_current_joint_values() return dict(zip(joint_names, joint_values)) def get_current_state_bounded(self): """ Get the current joint state of the group being used, enforcing that they are within each joint limits. @return a dictionary with the joint names as keys and current joint values """ current = self._move_group_commander._g.get_current_state_bounded() names = self._move_group_commander._g.get_active_joints() output = {n: current[n] for n in names if n in current} return output def get_robot_state_bounded(self): return self._move_group_commander._g.get_current_state_bounded() def move_to_named_target(self, name, wait=True): """ Set target of the robot's links and moves to it @param name - name of the target pose defined in SRDF @param wait - should method wait for movement end or not """ self._move_group_commander.set_start_state_to_current_state() if self.set_named_target(name): self._move_group_commander.go(wait=wait) def plan_to_named_target(self, name): """ Set target of the robot's links and plans This is a blocking method. @param name - name of the target pose defined in SRDF """ self._move_group_commander.set_start_state_to_current_state() if self.set_named_target(name): self.__plan = self._move_group_commander.plan() def __get_warehouse_names(self): try: list_srv = rospy.ServiceProxy("list_robot_states", ListStates) return list_srv("", self._robot_name).states except rospy.ServiceException as exc: rospy.logwarn("Couldn't access warehouse: " + str(exc)) return list() def _reset_plan(self): self.__plan = None def _set_plan(self, plan): self.__plan = plan def __get_srdf_names(self): return self._move_group_commander._g.get_named_targets() def get_named_targets(self): """ Get the complete list of named targets, from SRDF as well as warehouse poses if available. @return list of strings containing names of targets. """ return self._srdf_names + self._warehouse_names def get_joints_position(self): """ Returns joints position @return - dictionary with joints positions """ with self._joint_states_lock: return self._joints_position def get_joints_velocity(self): """ Returns joints velocities @return - dictionary with joints velocities """ with self._joint_states_lock: return self._joints_velocity def _get_joints_effort(self): """ Returns joints effort @return - dictionary with joints efforts """ with self._joint_states_lock: return self._joints_effort def get_joints_state(self): """ Returns joints state @return - JointState message """ with self._joint_states_lock: return self._joints_state def run_joint_trajectory(self, joint_trajectory): """ Moves robot through all joint states with specified timeouts @param joint_trajectory - JointTrajectory class object. Represents trajectory of the joints which would be executed. """ plan = RobotTrajectory() plan.joint_trajectory = joint_trajectory self._move_group_commander.execute(plan) def make_named_trajectory(self, trajectory): """ Makes joint value trajectory from specified by named poses (either from SRDF or from warehouse) @param trajectory - list of waypoints, each waypoint is a dict with the following elements (n.b either name or joint_angles is required) - name -> the name of the way point - joint_angles -> a dict of joint names and angles - interpolate_time -> time to move from last wp - pause_time -> time to wait at this wp - degrees -> set to true if joint_angles is specified in degrees. Assumed false if absent. """ current = self.get_current_state_bounded() joint_trajectory = JointTrajectory() joint_names = current.keys() joint_trajectory.joint_names = joint_names start = JointTrajectoryPoint() start.positions = current.values() start.time_from_start = rospy.Duration.from_sec(0.001) joint_trajectory.points.append(start) time_from_start = 0.0 for wp in trajectory: joint_positions = None if 'name' in wp.keys(): joint_positions = self.get_named_target_joint_values( wp['name']) elif 'joint_angles' in wp.keys(): joint_positions = copy.deepcopy(wp['joint_angles']) if 'degrees' in wp.keys() and wp['degrees']: for joint, angle in joint_positions.iteritems(): joint_positions[joint] = radians(angle) if joint_positions is None: rospy.logerr( "Invalid waypoint. Must contain valid name for named target or dict of joint angles." ) return None new_positions = {} for n in joint_names: new_positions[n] = joint_positions[ n] if n in joint_positions else current[n] trajectory_point = JointTrajectoryPoint() trajectory_point.positions = [ new_positions[n] for n in joint_names ] current = new_positions time_from_start += wp['interpolate_time'] trajectory_point.time_from_start = rospy.Duration.from_sec( time_from_start) joint_trajectory.points.append(trajectory_point) if 'pause_time' in wp and wp['pause_time'] > 0: extra = JointTrajectoryPoint() extra.positions = trajectory_point.positions time_from_start += wp['pause_time'] extra.time_from_start = rospy.Duration.from_sec( time_from_start) joint_trajectory.points.append(extra) return joint_trajectory def send_stop_trajectory_unsafe(self): """ Sends a trajectory of all active joints at their current position. This stops the robot. """ current = self.get_current_state_bounded() trajectory_point = JointTrajectoryPoint() trajectory_point.positions = current.values() trajectory_point.time_from_start = rospy.Duration.from_sec(0.1) trajectory = JointTrajectory() trajectory.points.append(trajectory_point) trajectory.joint_names = current.keys() self.run_joint_trajectory_unsafe(trajectory) def run_named_trajectory_unsafe(self, trajectory, wait=False): """ Moves robot through trajectory specified by named poses, either from SRDF or from warehouse. Runs trajectory directly via contoller. @param trajectory - list of waypoints, each waypoint is a dict with the following elements: - name -> the name of the way point - interpolate_time -> time to move from last wp - pause_time -> time to wait at this wp """ joint_trajectory = self.make_named_trajectory(trajectory) if joint_trajectory is not None: self.run_joint_trajectory_unsafe(joint_trajectory, wait) def run_named_trajectory(self, trajectory): """ Moves robot through trajectory specified by named poses, either from SRDF or from warehouse. Runs trajectory via moveit. @param trajectory - list of waypoints, each waypoint is a dict with the following elements: - name -> the name of the way point - interpolate_time -> time to move from last wp - pause_time -> time to wait at this wp """ joint_trajectory = self.make_named_trajectory(trajectory) if joint_trajectory is not None: self.run_joint_trajectory(joint_trajectory) def move_to_position_target(self, xyz, end_effector_link="", wait=True): """ Specify a target position for the end-effector and moves to it @param xyz - new position of end-effector @param end_effector_link - name of the end effector link @param wait - should method wait for movement end or not """ self._move_group_commander.set_start_state_to_current_state() self._move_group_commander.set_position_target(xyz, end_effector_link) self._move_group_commander.go(wait=wait) def plan_to_position_target(self, xyz, end_effector_link=""): """ Specify a target position for the end-effector and plans. This is a blocking method. @param xyz - new position of end-effector @param end_effector_link - name of the end effector link """ self._move_group_commander.set_start_state_to_current_state() self._move_group_commander.set_position_target(xyz, end_effector_link) self.__plan = self._move_group_commander.plan() def move_to_pose_target(self, pose, end_effector_link="", wait=True): """ Specify a target pose for the end-effector and moves to it @param pose - new pose of end-effector: a Pose message, a PoseStamped message or a list of 6 floats: [x, y, z, rot_x, rot_y, rot_z] or a list of 7 floats [x, y, z, qx, qy, qz, qw] @param end_effector_link - name of the end effector link @param wait - should method wait for movement end or not """ self._move_group_commander.set_start_state_to_current_state() self._move_group_commander.set_pose_target(pose, end_effector_link) self._move_group_commander.go(wait=wait) def plan_to_pose_target(self, pose, end_effector_link="", alternative_method=False): """ Specify a target pose for the end-effector and plans. This is a blocking method. @param pose - new pose of end-effector: a Pose message, a PoseStamped message or a list of 6 floats: [x, y, z, rot_x, rot_y, rot_z] or a list of 7 floats [x, y, z, qx, qy, qz, qw] @param end_effector_link - name of the end effector link @param alternative_method - use set_joint_value_target instead of set_pose_target """ self._move_group_commander.set_start_state_to_current_state() if alternative_method: self._move_group_commander.set_joint_value_target( pose, end_effector_link) else: self._move_group_commander.set_pose_target(pose, end_effector_link) self.__plan = self._move_group_commander.plan() return self.__plan def _joint_states_callback(self, joint_state): """ The callback function for the topic joint_states. It will store the received joint position, velocity and efforts information into dictionaries @param joint_state - the message containing the joints data. """ with self._joint_states_lock: self._joints_state = joint_state self._joints_position = { n: p for n, p in zip(joint_state.name, joint_state.position) } self._joints_velocity = { n: v for n, v in zip(joint_state.name, joint_state.velocity) } self._joints_effort = { n: v for n, v in zip(joint_state.name, joint_state.effort) } def _set_up_action_client(self, controller_list): """ Sets up an action client to communicate with the trajectory controller """ self._action_running = {} for controller_name in controller_list.keys(): self._action_running[controller_name] = False service_name = controller_name + "/follow_joint_trajectory" self._clients[controller_name] = SimpleActionClient( service_name, FollowJointTrajectoryAction) if self._clients[controller_name].wait_for_server( timeout=rospy.Duration(4)) is False: err_msg = 'Failed to connect to action server ({}) in 4 sec'.format( service_name) rospy.logwarn(err_msg) def move_to_joint_value_target_unsafe(self, joint_states, time=0.002, wait=True, angle_degrees=False): """ Set target of the robot's links and moves to it. @param joint_states - dictionary with joint name and value. It can contain only joints values of which need to be changed. @param time - time in s (counting from now) for the robot to reach the target (it needs to be greater than 0.0 for it not to be rejected by the trajectory controller) @param wait - should method wait for movement end or not @param angle_degrees - are joint_states in degrees or not """ # self._update_default_trajectory() # self._set_targets_to_default_trajectory(joint_states) goals = {} joint_states_cpy = copy.deepcopy(joint_states) if angle_degrees: joint_states_cpy.update( (joint, radians(i)) for joint, i in joint_states_cpy.items()) for controller in self._controllers: controller_joints = self._controllers[controller] goal = FollowJointTrajectoryGoal() goal.trajectory.joint_names = [] point = JointTrajectoryPoint() point.positions = [] for x in joint_states_cpy.keys(): if x in controller_joints: goal.trajectory.joint_names.append(x) point.positions.append(joint_states_cpy[x]) point.time_from_start = rospy.Duration.from_sec(time) goal.trajectory.points = [point] goals[controller] = goal self._call_action(goals) if not wait: return for i in self._clients.keys(): if not self._clients[i].wait_for_result(): rospy.loginfo("Trajectory not completed") def action_is_running(self, controller=None): if controller is not None: return self._action_running[controller] for controller_running in self._action_running.values(): if controller_running: return True return False def _action_done_cb(self, controller, terminal_state, result): self._action_running[controller] = False def _call_action(self, goals): for client in self._clients: self._action_running[client] = True self._clients[client].send_goal( goals[client], lambda terminal_state, result: self._action_done_cb( client, terminal_state, result)) def run_joint_trajectory_unsafe(self, joint_trajectory, wait=True): """ Moves robot through all joint states with specified timeouts @param joint_trajectory - JointTrajectory class object. Represents trajectory of the joints which would be executed. @param wait - should method wait for movement end or not """ goals = {} for controller in self._controllers: controller_joints = self._controllers[controller] goal = FollowJointTrajectoryGoal() goal.trajectory = copy.deepcopy(joint_trajectory) indices_of_joints_in_this_controller = [] for i, joint in enumerate(joint_trajectory.joint_names): if joint in controller_joints: indices_of_joints_in_this_controller.append(i) goal.trajectory.joint_names = [ joint_trajectory.joint_names[i] for i in indices_of_joints_in_this_controller ] for point in goal.trajectory.points: if point.positions: point.positions = [ point.positions[i] for i in indices_of_joints_in_this_controller ] if point.velocities: point.velocities = [ point.velocities[i] for i in indices_of_joints_in_this_controller ] if point.effort: point.effort = [ point.effort[i] for i in indices_of_joints_in_this_controller ] goals[controller] = goal self._call_action(goals) if not wait: return for i in self._clients.keys(): if not self._clients[i].wait_for_result(): rospy.loginfo("Trajectory not completed") def plan_to_waypoints_target(self, waypoints, reference_frame=None, eef_step=0.005, jump_threshold=0.0): """ Specify a set of waypoints for the end-effector and plans. This is a blocking method. @param reference_frame - the reference frame in which the waypoints are given @param waypoints - an array of poses of end-effector @param eef_step - configurations are computed for every eef_step meters @param jump_threshold - maximum distance in configuration space between consecutive points in the resulting path """ old_frame = self._move_group_commander.get_pose_reference_frame() if reference_frame is not None: self.set_pose_reference_frame(reference_frame) (self.__plan, fraction) = self._move_group_commander.compute_cartesian_path( waypoints, eef_step, jump_threshold) self.set_pose_reference_frame(old_frame) def set_teach_mode(self, teach): """ Activates/deactivates the teach mode for the robot. Activation: stops the the trajectory controllers for the robot, and sets it to teach mode. Deactivation: stops the teach mode and starts trajectory controllers for the robot. Currently this method blocks for a few seconds when called on a hand, while the hand parameters are reloaded. @param teach - bool to activate or deactivate teach mode """ if teach: mode = RobotTeachModeRequest.TEACH_MODE else: mode = RobotTeachModeRequest.TRAJECTORY_MODE self.change_teach_mode(mode, self._name) def move_to_trajectory_start(self, trajectory, wait=True): """ Make and execute a plan from the current state to the first state in an pre-existing trajectory @param trajectory - moveit_msgs/JointTrajectory @param wait - Bool to specify if movement should block untill finished. """ if len(trajectory.points) <= 0: rospy.logerr("Trajectory has no points in it, can't reverse...") return None first_point = trajectory.points[0] end_state = dict(zip(trajectory.joint_names, first_point.positions)) self.move_to_joint_value_target(end_state, wait=wait) @staticmethod def change_teach_mode(mode, robot): teach_mode_client = rospy.ServiceProxy('/teach_mode', RobotTeachMode) req = RobotTeachModeRequest() req.teach_mode = mode req.robot = robot try: resp = teach_mode_client(req) if resp.result == RobotTeachModeResponse.ERROR: rospy.logerr("Failed to change robot %s to mode %d", robot, mode) else: rospy.loginfo("Changed robot %s to mode %d Result = %d", robot, mode, resp.result) except rospy.ServiceException: rospy.logerr("Failed to call service teach_mode") def get_ik(self, target_pose, avoid_collisions=False, joint_states=None): """ Computes the inverse kinematics for a given pose. It returns a JointState @param target_pose - A given pose of type PoseStamped @param avoid_collisions - Find an IK solution that avoids collisions. By default, this is false """ service_request = PositionIKRequest() service_request.group_name = self._name service_request.ik_link_name = self._move_group_commander.get_end_effector_link( ) service_request.pose_stamped = target_pose service_request.timeout.secs = 0.5 service_request.avoid_collisions = avoid_collisions if joint_states is None: service_request.robot_state.joint_state = self.get_joints_state() else: service_request.robot_state.joint_state = joint_states try: resp = self._compute_ik(ik_request=service_request) # Check if error_code.val is SUCCESS=1 if resp.error_code.val != 1: if resp.error_code.val == -10: rospy.logerr("Unreachable point: Start state in collision") elif resp.error_code.val == -12: rospy.logerr("Unreachable point: Goal state in collision") elif resp.error_code.val == -31: rospy.logerr("Unreachable point: No IK solution") else: rospy.logerr("Unreachable point (error: %s)" % resp.error_code) return else: return resp.solution.joint_state except rospy.ServiceException, e: rospy.logerr("Service call failed: %s" % e)
class PR2Greeter: def __init__(self,debug = False, robot_frame = "odom_combined"): self._tf = TransformListener() self.snd_handle = SoundClient() rospy.sleep(1) self.snd_handle.say('Hello world!') rospy.sleep(1) self._debug = debug self._robot_frame = robot_frame self._point_sub = rospy.Subscriber('nearest_face', PointStamped, self.face_cb) self._head_action_cl = actionlib.SimpleActionClient('/head_traj_controller/point_head_action', pr2_controllers_msgs.msg.PointHeadAction) self._left_arm = MoveGroupCommander("left_arm") self._right_arm = MoveGroupCommander("right_arm") print "r.f.: " + self._left_arm.get_pose_reference_frame() self.face = None self.face_from = rospy.Time(0) self.face_last_dist = 0 self.l_home_pose = [0.283, 0.295, 0.537, -1.646, 0.468, -1.735] self.l_wave_1 = [-0.1, 0.6, 1.15, -1.7, -0.97, -1.6] self.l_wave_2 = [-0.1, 0.6, 1.15, 1.7, -0.97, 1.6] self.r_home_pose = [0.124, -0.481, 0.439, -1.548, 0.36, -0.035] self.r_advert = [0.521, -0.508, 0.845, -1.548, 0.36, -0.035] self.no_face_random_delay = None self._initialized = False self._timer = rospy.Timer(rospy.Duration(1.0), self.timer) self._move_buff = Queue.Queue() self._head_buff = Queue.Queue() self._move_thread = threading.Thread(target=self.movements) self._move_thread.daemon = True self._move_thread.start() self._head_thread = threading.Thread(target=self.head) self._head_thread.daemon = True self._head_thread.start() self.new_face = False self.face_last_dist = 0.0 self.face_counter = 0 self.actions = [self.introduceAction, self.waveHandAction, self.lookAroundAction, self.lookAroundAction, self.lookAroundAction, self.advertAction, self.numberOfFacesAction] self.goodbye_strings = ["Thanks for stopping by.","Enjoy the event.","See you!", "Have a nice day!"] self.invite_strings = ["Hello. It's nice to see you.","Come here and take some flyer.", "I hope you are enjoying the event."] rospy.loginfo("Ready") def getRandomFromArray(self, arr): idx = random.randint(0,len(arr)-1) return arr[idx] def numberOfFacesAction(self): self.snd_handle.say("Today I already saw " + str(self.face_counter) + " faces.") rospy.sleep(1) def advertAction(self): self.snd_handle.say("Hello. Here are some posters for you.") rospy.sleep(1) self.go(self._right_arm, self.r_advert) def introduceAction(self): self.snd_handle.say("Hello. I'm PR2 robot. Come here to check me.") rospy.sleep(1) def waveHandAction(self): self.snd_handle.say("I'm here. Please come to see me.") rospy.sleep(1) rand = random.randint(1,3) for _ in range(rand): self.wave() self.go(self._left_arm, self.l_home_pose) rospy.loginfo("Waving") def lookAroundAction(self): self.snd_handle.say("I'm looking for somebody. Please come closer.") rospy.sleep(1) p = PointStamped() p.header.stamp = rospy.Time.now() p.header.frame_id = "/base_link" p.point.x = 2.0 sign = random.choice([-1, 1]) p.point.y = sign*random.uniform(1.5, 0.5) p.point.z = random.uniform(1.7, 0.2) self._head_buff.put(copy.deepcopy(p)) p.point.y = -1*sign*random.uniform(1.5, 0.5) p.point.z = random.uniform(1.7, 0.2) self._head_buff.put(copy.deepcopy(p)) p.point.y = sign*random.uniform(1.5, 0.5) p.point.z = random.uniform(1.7, 0.2) self._head_buff.put(copy.deepcopy(p)) p.point.y = -1*sign*random.uniform(1.5, 0.5) p.point.z = random.uniform(1.7, 0.2) self._head_buff.put(copy.deepcopy(p)) rospy.loginfo("Looking around") def getPointDist(self,pt): assert(self.face is not None) # fist, get my position p = PoseStamped() p.header.frame_id = "base_link" p.header.stamp = rospy.Time.now() - rospy.Duration(0.5) p.pose.position.x = 0 p.pose.position.y = 0 p.pose.position.z = 0 p.pose.orientation.x = 0 p.pose.orientation.y = 0 p.pose.orientation.z = 0 p.pose.orientation.w = 1 try: self._tf.waitForTransform(p.header.frame_id, self._robot_frame, p.header.stamp, rospy.Duration(2)) p = self._tf.transformPose(self._robot_frame, p) except: rospy.logerr("TF error!") return None return sqrt(pow(p.pose.position.x - pt.point.x, 2) + pow(p.pose.position.y - pt.point.y, 2) + pow(p.pose.position.z - pt.point.z, 2)) def getPoseStamped(self, group, c): assert(len(c)==6) p = PoseStamped() p.header.frame_id = "base_link" p.header.stamp = rospy.Time.now() - rospy.Duration(0.5) p.pose.position.x = c[0] p.pose.position.y = c[1] p.pose.position.z = c[2] quat = tf.transformations.quaternion_from_euler(c[3], c[4], c[5]) p.pose.orientation.x = quat[0] p.pose.orientation.y = quat[1] p.pose.orientation.z = quat[2] p.pose.orientation.w = quat[3] try: self._tf.waitForTransform(p.header.frame_id, group.get_pose_reference_frame(), p.header.stamp, rospy.Duration(2)) p = self._tf.transformPose(group.get_pose_reference_frame(), p) except: rospy.logerr("TF error!") return None return p def go(self,group,where): self._move_buff.put((group,where)) def wave(self): self.go(self._left_arm, self.l_wave_1) self.go(self._left_arm, self.l_wave_2) self.go(self._left_arm, self.l_wave_1) def head(self): self._head_action_cl.wait_for_server() while not rospy.is_shutdown(): target = self._head_buff.get() #print "head point goal" #print target # point PR2's head there (http://wiki.ros.org/pr2_controllers/Tutorials/Moving%20the%20Head) goal = pr2_controllers_msgs.msg.PointHeadGoal() goal.target = target goal.pointing_frame = "high_def_frame" goal.pointing_axis.x = 1 goal.pointing_axis.y = 0 goal.pointing_axis.z = 0 self._head_action_cl.send_goal(goal) self._head_action_cl.wait_for_result(rospy.Duration.from_sec(5.0)) self._head_buff.task_done() def movements(self): while not rospy.is_shutdown(): (group, where) = self._move_buff.get() group.set_start_state_to_current_state() p = self.getPoseStamped(group, where) if p is None: self._move_buff.task_done() continue group.set_pose_target(p) self._move_buff.task_done() group.go(wait = True) def timer(self,event): if self._initialized is False: rospy.loginfo("Moving arms to home positions") self.init_head() self.go(self._left_arm, self.l_home_pose) self.go(self._right_arm, self.r_home_pose) self._move_buff.join() self.snd_handle.say("I'm ready for a great job.") self._initialized = True if self.face is None: if (self.no_face_random_delay is None): delay = random.uniform(20, 5) self.no_face_random_delay = rospy.Time.now() + rospy.Duration(delay) rospy.loginfo("Random delay: " + str(delay)) return else: if rospy.Time.now() < self.no_face_random_delay: return self.init_head() self.go(self._left_arm, self.l_home_pose) self.go(self._right_arm, self.r_home_pose) rospy.loginfo("Starting selected action") action = self.getRandomFromArray(self.actions) action() delay = random.uniform(30, 5) self.no_face_random_delay = rospy.Time.now() + rospy.Duration(delay) return else: self.no_face_random_delay = None if self.new_face: self.face_counter = self.face_counter + 1 self.new_face = False #cd = getPointDist(self.face) # TODO decide action based on distance ? self.go(self._left_arm, self.l_home_pose) self.go(self._right_arm, self.r_advert) string = self.getRandomFromArray(self.invite_strings) self.snd_handle.say(string) # TODO wait some min. time + say something # after 20 seconds of no detected face, let's continue if self.face.header.stamp + rospy.Duration(20) < rospy.Time.now(): string = self.getRandomFromArray(self.goodbye_strings) self.snd_handle.say(string) self.init_head() self.go(self._left_arm, self.l_home_pose) self.go(self._right_arm, self.r_home_pose) self.face = None return self._head_buff.put(self.face) def init_head(self): p = PointStamped() p.header.stamp = rospy.Time.now() p.header.frame_id = "/base_link" p.point.x = 2.0 p.point.y = 0.0 p.point.z = 1.7 self._head_buff.put(p) def face_cb(self,point): # transform point try: self._tf.waitForTransform(point.header.frame_id, self._robot_frame, point.header.stamp, rospy.Duration(2)) pt = self._tf.transformPoint(self._robot_frame, point) except: rospy.logerr("Transform error") return if self.face is not None: cd = self.getPointDist(pt) # current distance dd = fabs(self.face_last_dist - cd) # change in distance if dd < 0.2: self.face.header = pt.header # filter x,y,z values a bit self.face.point.x = (15*self.face.point.x + pt.point.x)/16 self.face.point.y = (15*self.face.point.y + pt.point.y)/16 self.face.point.z = (15*self.face.point.z + pt.point.z)/16 else: self.new_face = True self.face = pt self.face_last_dist = cd else: self.new_face = True self.face = pt if self.face_from == rospy.Time(0): #self.snd_handle.say('Hello. Come closer.') rospy.loginfo("New face.") self.face_from = self.face.header.stamp
if __name__=='__main__': roscpp_initialize(sys.argv) rospy.init_node('moveit_py_demo', anonymous=True) scene = PlanningSceneInterface() robot = RobotCommander() group = MoveGroupCommander("arm") print "============ Robot Groups:" print robot.get_group_names() print "============ Robot Links for arm:" print robot.get_link_names("arm") print "============ Robot Links for gripper:" print robot.get_link_names("gripper") print group.get_end_effector_link() print group.get_pose_reference_frame() print "============ Printing robot state" #print robot.get_current_state() print "============" tl = tf.TransformListener() rospy.sleep(1) waypoints = [] # start with the current pose waypoints.append(group.get_current_pose().pose) print waypoints[0] currentPose = PoseStamped() currentPose.header.frame_id = group.get_pose_reference_frame() currentPose.pose = waypoints[0]
import rospy from moveit_commander import RobotCommander, PlanningSceneInterface, MoveGroupCommander, conversions from geometry_msgs.msg import Pose, PoseStamped from moveit_msgs.msg import RobotTrajectory, Grasp if __name__ == '__main__': rospy.init_node('moveit_py_demo', anonymous=True) scene = PlanningSceneInterface() robot = MoveGroupCommander("arm_1") rospy.sleep(1) currentrf = robot.get_pose_reference_frame() robot.set_pose_reference_frame(currentrf) newpose = robot.get_current_pose().pose a = robot.get_current_pose().pose newposerpy = robot.get_current_rpy() newpose.position.x = newpose.position.x - delta_x newpose.position.x = newpose.position.y - delta_y newpose.position.x = newpose.position.z - delta_z robot.set_pose_target(newpose) planned = robot.plan() robot.execute(planned) rospy.sleep(2) newpose = robot.get_current_pose().pose