00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 import roslib
00039
00040 roslib.load_manifest('nao_remote')
00041 import rospy
00042 import sys
00043
00044 import rospy.rostime
00045 from rospy.rostime import Duration
00046
00047 import actionlib
00048 from actionlib_msgs.msg import GoalStatus
00049 import nao_msgs.msg
00050 from nao_msgs.msg import JointTrajectoryGoal, JointTrajectoryAction, BodyPoseAction, BodyPoseGoal
00051 from trajectory_msgs.msg import JointTrajectory, JointTrajectoryPoint
00052
00053 from sensor_msgs.msg import JointState
00054 from std_msgs.msg import String
00055 from std_msgs.msg import Bool
00056 from std_srvs.srv import Empty
00057
00058 class PoseManager():
00059 def __init__(self):
00060
00061 rospy.init_node('pose_manager')
00062
00063 self.poseLibrary = dict()
00064 self.readInPoses()
00065 self.poseServer = actionlib.SimpleActionServer("body_pose", BodyPoseAction,
00066 execute_cb=self.executeBodyPose,
00067 auto_start=False)
00068 self.trajectoryClient = actionlib.SimpleActionClient("joint_trajectory", JointTrajectoryAction)
00069 if self.trajectoryClient.wait_for_server(rospy.Duration(3.0)):
00070 try:
00071 rospy.wait_for_service("stop_walk_srv", timeout=2.0)
00072 self.stopWalkSrv = rospy.ServiceProxy("stop_walk_srv", Empty)
00073 except:
00074 rospy.logwarn("stop_walk_srv not available, pose_manager will not stop the walker before executing a trajectory. "
00075 +"This is normal if there is no nao_walker running.")
00076 self.stopWalkSrv = None
00077 self.poseServer.start()
00078
00079 rospy.loginfo("pose_manager running, offering poses: %s", self.poseLibrary.keys());
00080
00081 else:
00082 rospy.logfatal("Could not connect to required \"joint_trajectory\" action server, is the nao_controller node running?");
00083 rospy.signal_shutdown("Required component missing");
00084
00085
00086
00087
00088 def readInPoses(self):
00089 poses = rospy.get_param('~poses')
00090 rospy.loginfo("Found %d poses on the param server", len(poses))
00091
00092 for key,value in poses.iteritems():
00093 try:
00094
00095 trajectory = JointTrajectory()
00096 trajectory.joint_names = value["joint_names"]
00097 point = JointTrajectoryPoint()
00098 point.time_from_start = Duration(value["time_from_start"])
00099 point.positions = value["positions"]
00100 trajectory.points = [point]
00101 self.poseLibrary[key] = trajectory
00102 except:
00103 rospy.logwarn("Could not parse pose \"%s\" from the param server:", key);
00104 rospy.logwarn(sys.exc_info())
00105
00106
00107 if not "crouch" in self.poseLibrary:
00108 trajectory = JointTrajectory()
00109 trajectory.joint_names = ["Body"]
00110 point = JointTrajectoryPoint()
00111 point.time_from_start = Duration(1.5)
00112 point.positions = [0.0,0.0,
00113 1.545, 0.33, -1.57, -0.486, 0.0, 0.0,
00114 -0.3, 0.057, -0.744, 2.192, -1.122, -0.035,
00115 -0.3, 0.057, -0.744, 2.192, -1.122, -0.035,
00116 1.545, -0.33, 1.57, 0.486, 0.0, 0.0]
00117 trajectory.points = [point]
00118 self.poseLibrary["crouch"] = trajectory;
00119
00120
00121 def executeBodyPose(self, goal):
00122 if not goal.pose_name in self.poseLibrary:
00123 rospy.loginfo("Pose %s not in library, reload library from parameters..." % goal.pose_name)
00124 self.readInPoses()
00125
00126 if goal.pose_name in self.poseLibrary:
00127 rospy.loginfo("Executing body pose %s...", goal.pose_name);
00128 if not self.stopWalkSrv is None:
00129 self.stopWalkSrv()
00130
00131 trajGoal = JointTrajectoryGoal()
00132
00133 trajGoal.trajectory = self.poseLibrary[goal.pose_name]
00134 timeout = trajGoal.trajectory.points[-1].time_from_start + Duration(1.0)
00135 trajGoal.trajectory.header.stamp = rospy.Time.now()
00136
00137 self.trajectoryClient.send_goal_and_wait(trajGoal, timeout)
00138 if self.trajectoryClient.get_state() != GoalStatus.SUCCEEDED:
00139 self.poseServer.set_aborted(text="JointTrajectory action did not succeed (timeout?)");
00140
00141 self.poseServer.set_succeeded()
00142 rospy.loginfo("Done.");
00143
00144 else:
00145 msg = "pose \""+goal.pose_name+"\" not in pose_manager's library";
00146 rospy.logwarn("%s", msg)
00147 self.poseServer.set_aborted(text=str(msg));
00148
00149
00150
00151 if __name__ == '__main__':
00152
00153 manager = PoseManager()
00154 rospy.spin()
00155 exit(0)