Go to the documentation of this file.00001
00002 from actions.ActionSet import ActionSet
00003 from actions.Pr2MoveHeadAction import Pr2MoveHeadAction
00004 from actions.Pr2MoveLeftArmAction import Pr2MoveLeftArmAction
00005 from actions.Pr2MoveLeftGripperAction import Pr2MoveLeftGripperAction
00006 from actions.Pr2MoveRightArmAction import Pr2MoveRightArmAction
00007 from actions.Pr2MoveRightGripperAction import Pr2MoveRightGripperAction
00008 from actions.Pr2MoveTorsoAction import Pr2MoveTorsoAction
00009
00010 """ Contains methods to convert between Pose messages and actions """
00011
00012 def pose_to_actionset(pose):
00013 """ Takes the given museum_msgs/Pose message and converts it into an ActionSet
00014
00015 It is assumed that pose values are between 0 and 1. They will be scaled """
00016
00017 head = Pr2MoveHeadAction()
00018 head_vals = [pose.head.head_pan, pose.head.head_tilt]
00019 _set_transformed_data(head, head_vals)
00020
00021 torso = Pr2MoveTorsoAction()
00022 torso_vals = [pose.torso]
00023 _set_transformed_data(torso, torso_vals)
00024
00025 def arm_data(arm):
00026 return [
00027 arm.shoulder_pan,
00028 arm.shoulder_lift,
00029 arm.upper_arm_roll,
00030 arm.elbow_flex,
00031 arm.forearm_roll,
00032 arm.wrist_flex,
00033 arm.wrist_roll
00034 ]
00035
00036 larm = Pr2MoveLeftArmAction()
00037 larm_vals = arm_data(pose.left)
00038 _set_transformed_data(larm, larm_vals)
00039
00040 lgrip = Pr2MoveLeftGripperAction()
00041 lgrip_vals = [pose.left.gripper]
00042 _set_transformed_data(lgrip, lgrip_vals)
00043
00044 rarm = Pr2MoveRightArmAction()
00045 rarm_vals = arm_data(pose.right)
00046 _set_transformed_data(rarm, rarm_vals)
00047
00048 rgrip = Pr2MoveRightGripperAction()
00049 rgrip_vals = [pose.right.gripper]
00050 _set_transformed_data(rgrip, rgrip_vals)
00051
00052 actionset = ActionSet()
00053 actionset.add_action(head)
00054 actionset.add_action(torso)
00055 actionset.add_action(larm)
00056 actionset.add_action(lgrip)
00057 actionset.add_action(rarm)
00058 actionset.add_action(rgrip)
00059
00060 actionset.set_duration(_transform_value(pose.duration, 0.5, 5.0))
00061
00062 print actionset.to_string()
00063
00064 return actionset
00065
00066
00067
00068
00069 def scaled_pose_to_actionset(pose):
00070 """ Takes the given museum_msgs/Pose message and converts it into an ActionSet
00071
00072 It is assumed that pose values are already scaled """
00073
00074 head = Pr2MoveHeadAction()
00075 head.set_values([pose.head.head_pan, pose.head.head_tilt])
00076
00077 torso = Pr2MoveTorsoAction()
00078 torso.set_values([pose.torso])
00079
00080 def arm_data(arm):
00081 return [
00082 arm.shoulder_pan,
00083 arm.shoulder_lift,
00084 arm.upper_arm_roll,
00085 arm.elbow_flex,
00086 arm.forearm_roll,
00087 arm.wrist_flex,
00088 arm.wrist_roll
00089 ]
00090
00091 larm = Pr2MoveLeftArmAction()
00092 larm.set_values(arm_data(pose.left))
00093
00094 lgrip = Pr2MoveLeftGripperAction()
00095 lgrip.set_values([pose.left.gripper])
00096
00097 rarm = Pr2MoveRightArmAction()
00098 rarm.set_values(arm_data(pose.right))
00099
00100 rgrip = Pr2MoveRightGripperAction()
00101 rgrip.set_values([pose.right.gripper])
00102
00103 duration = pose.duration
00104
00105 actionset = ActionSet()
00106 actionset.add_action(head)
00107 actionset.add_action(torso)
00108 actionset.add_action(larm)
00109 actionset.add_action(lgrip)
00110 actionset.add_action(rarm)
00111 actionset.add_action(rgrip)
00112 actionset.set_duration(duration)
00113
00114 return actionset
00115
00116 def _set_transformed_data(action, data):
00117 action.set_values(_transform_data(action, data))
00118
00119 def _transform_data(action, data):
00120 assert(len(action._joints) == len(data))
00121 transformed = []
00122 for index, joint in enumerate(action._joints):
00123 min_value = joint['min']
00124 max_value = joint['max']
00125 value = _transform_value(data[index], min_value, max_value)
00126 transformed.append(value)
00127 return transformed
00128
00129 def _transform_value(value, min_value, max_value):
00130 assert(value >= -1 and value <= 1)
00131 value = (value + 1.0) / 2.0
00132 value = min_value + value * (max_value - min_value)
00133 assert(value >= min_value)
00134 assert(value <= max_value)
00135 return value