hlrc / client / python / tools / dump_relative_gazetargets.py @ b3694223
History | View | Annotate | Download (5.395 KB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
import roslib |
3 |
import rospy |
4 |
import math |
5 |
import tf |
6 |
import errno |
7 |
from humotion.msg import * |
8 |
import array |
9 |
|
10 |
"""
|
11 |
small code snippet to dump humotion relative gaze targets
|
12 |
combined with the current gaze direction from tf
|
13 |
"""
|
14 |
USE_MEKA_TOPICS=True
|
15 |
|
16 |
#joint targets
|
17 |
if USE_MEKA_TOPICS:
|
18 |
humotion_gaze_topic = "/meka/humotion/gaze/target"
|
19 |
tf_topic_base = "upper"
|
20 |
tf_topic_gaze_l = "shell"
|
21 |
tf_topic_gaze_r = "shell"
|
22 |
from trajectory_msgs.msg import * |
23 |
topic_joint_target = "/meka_roscontrol/head_position_trajectory_controller/command"
|
24 |
else:
|
25 |
humotion_gaze_topic = "/flobi/humotion/gaze/target"
|
26 |
tf_topic_base = "BASE_LINK"
|
27 |
tf_topic_gaze_l = "EYES_LEFT_UD_VIRTUAL_LINK"
|
28 |
tf_topic_gaze_r = "EYES_RIGHT_UD_VIRTUAL_LINK"
|
29 |
from xsc3_server.msg import * |
30 |
topic_joint_target = "/xsc3/custom_joint_state"
|
31 |
|
32 |
|
33 |
target_neck_pan = 0.0
|
34 |
target_neck_tilt = 0.0
|
35 |
target_eyes_tilt = 0.0
|
36 |
target_eye_l_pan = 0.0
|
37 |
target_eye_r_pan = 0.0
|
38 |
|
39 |
def fetch_gaze_from_tf(timestamp): |
40 |
try:
|
41 |
#fetch right eye
|
42 |
(trans,rot) = listener.lookupTransform(tf_topic_base, tf_topic_gaze_r, timestamp) |
43 |
euler = tf.transformations.euler_from_quaternion(rot, axes='sxyz')
|
44 |
pan_r = -euler[2]
|
45 |
tilt_r = -euler[1]
|
46 |
#fetch left eye
|
47 |
(trans,rot) = listener.lookupTransform(tf_topic_base, tf_topic_gaze_l, timestamp) |
48 |
euler = tf.transformations.euler_from_quaternion(rot, axes='sxyz')
|
49 |
pan_l = -euler[2]
|
50 |
tilt_l = -euler[1]
|
51 |
#combine to overall gaze direction:
|
52 |
pan = (pan_l + pan_r) / 2.0 * 180.0 / math.pi |
53 |
tilt = (tilt_l + tilt_r) / 2.0 * 180.0 / math.pi |
54 |
return (pan, tilt)
|
55 |
|
56 |
except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
|
57 |
print("> WARNING: no transformation %s-->%s at %s found" % (tf_topic_base, tf_topic_gaze_l, timestamp))
|
58 |
return (0.0, 0.0) |
59 |
|
60 |
|
61 |
def incoming_target_position_meka(msg): |
62 |
global target_neck_pan, target_neck_tilt, target_eyes_tilt, target_eye_l_pan, target_eye_r_pan
|
63 |
#print("> incoming target")
|
64 |
for x in range(len(msg.joint_names)): |
65 |
if (msg.joint_names[x] == "head_j0"): |
66 |
target_neck_tilt = msg.points[0].positions[x]
|
67 |
elif (msg.joint_names[x] == "head_j1"): |
68 |
target_neck_pan = msg.points[0].positions[x]
|
69 |
target_neck_tilt = 0.0
|
70 |
target_eyes_tilt = 0.0
|
71 |
target_eye_l_pan = 0.0
|
72 |
target_eye_r_pan = 0.0
|
73 |
|
74 |
|
75 |
|
76 |
def incoming_target_position_flobi(msg): |
77 |
global target_neck_pan, target_neck_tilt, target_eyes_tilt, target_eye_l_pan, target_eye_r_pan
|
78 |
#python 2.x interprets int8_t as string, reconvert this to an array:
|
79 |
ids = list(array.array("B", msg.id)) |
80 |
id = ids[0]
|
81 |
#print("> incoming target for joint %d" % (id))
|
82 |
if (id == 0x01): |
83 |
target_neck_pan = - 180.0/math.pi * msg.target_position[0] |
84 |
elif (id == 0x02): |
85 |
target_neck_tilt = 180.0/math.pi * msg.target_position[0] |
86 |
elif (id == 0x04): |
87 |
target_eyes_tilt = 180.0/math.pi * msg.target_position[0] |
88 |
elif (id == 0x05): |
89 |
target_eye_l_pan = 180.0/math.pi * msg.target_position[0] |
90 |
elif (id == 0x06): |
91 |
target_eye_r_pan = 180.0/math.pi * msg.target_position[0] |
92 |
|
93 |
def incoming_humotion_gaze_target(msg): |
94 |
print("> incoming gaze @%f" % (msg.gaze_timestamp.to_sec()))
|
95 |
|
96 |
if (msg.gaze_type == gaze.GAZETYPE_RELATIVE):
|
97 |
#find out the overall gaze state using TF:
|
98 |
(tf_pan, tf_tilt) = fetch_gaze_from_tf(msg.gaze_timestamp) |
99 |
#relative targets
|
100 |
rel_pan = msg.pan |
101 |
rel_tilt = msg.tilt |
102 |
#combined:
|
103 |
target_pan = tf_pan + rel_pan |
104 |
target_tilt = tf_tilt + rel_tilt |
105 |
#current position:
|
106 |
now = rospy.get_rostime() |
107 |
(now_pan, now_tilt) = fetch_gaze_from_tf(rospy.Time(0))
|
108 |
now_pan |
109 |
print("%f %f %f %f %f %f %f %f %f %f %f %f %f #DUMP" % (
|
110 |
now.to_sec(), |
111 |
now_pan, |
112 |
now_tilt, |
113 |
msg.gaze_timestamp.to_sec(), |
114 |
rel_pan, |
115 |
rel_tilt, |
116 |
target_pan, |
117 |
target_tilt, |
118 |
target_neck_pan, |
119 |
target_neck_tilt, |
120 |
target_eyes_tilt, |
121 |
target_eye_l_pan, |
122 |
target_eye_r_pan, |
123 |
)) |
124 |
#print("pan = %f, tilt=%f" % (pan* 180.0 / math.pi,tilt* 180.0 / math.pi))
|
125 |
|
126 |
else:
|
127 |
print("> NOT dumping absolute targets...")
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
if __name__ == '__main__': |
133 |
new_transform = False
|
134 |
rospy.init_node('dump_relative_gazetarget')
|
135 |
|
136 |
#humotion gaze subscriber
|
137 |
print("> setting up subscriber on %s" % (humotion_gaze_topic))
|
138 |
rospy.Subscriber(humotion_gaze_topic, gaze, incoming_humotion_gaze_target) |
139 |
|
140 |
#target angles
|
141 |
print("> setting up subscriber on %s" % (topic_joint_target))
|
142 |
if USE_MEKA_TOPICS:
|
143 |
rospy.Subscriber(topic_joint_target,JointTrajectory, incoming_target_position_meka) |
144 |
else:
|
145 |
rospy.Subscriber(topic_joint_target, xsc3_server.msg.joint_state, incoming_target_position_flobi) |
146 |
|
147 |
#tf listener
|
148 |
listener = tf.TransformListener() |
149 |
|
150 |
print("> waiting for transform...")
|
151 |
try:
|
152 |
listener.waitForTransform(tf_topic_base, tf_topic_gaze_r, rospy.Time(), rospy.Duration(4.0))
|
153 |
except tf.Exception:
|
154 |
print("> ERROR: timed out while waiting for transform %s->%s to become available\n" % (tf_topic_base, tf_topic_gaze_r))
|
155 |
exit(errno.ETIMEDOUT)
|
156 |
print("> first transform arrived, will start now...")
|
157 |
|
158 |
rate = rospy.Rate(50.0)
|
159 |
|
160 |
while not rospy.is_shutdown(): |
161 |
|
162 |
rate.sleep() |
163 |
|