Statistics
| Branch: | Tag: | Revision:

hlrc / client / python / tools / dump_relative_gazetargets.py @ master

History | View | Annotate | Download (5.395 KB)

1 6a233066 Simon Schulz
#!/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 c1e27eb0 Simon Schulz
import array
9 6a233066 Simon Schulz
10
"""
11
small code snippet to dump humotion relative gaze targets
12
combined with the current gaze direction from tf
13
"""
14 e832c8ef Sebastian Meyer zu Borgsen
USE_MEKA_TOPICS=True
15 6a233066 Simon Schulz
16 c1e27eb0 Simon Schulz
#joint targets
17
if USE_MEKA_TOPICS:
18 e832c8ef Sebastian Meyer zu Borgsen
    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 c1e27eb0 Simon Schulz
    topic_joint_target = "/meka_roscontrol/head_position_trajectory_controller/command"
24
else:
25 e832c8ef Sebastian Meyer zu Borgsen
    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 c1e27eb0 Simon Schulz
    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 6a233066 Simon Schulz
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 c1e27eb0 Simon Schulz
61
def incoming_target_position_meka(msg):
62 e832c8ef Sebastian Meyer zu Borgsen
    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 c1e27eb0 Simon Schulz
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 04cf2b6f Sebastian Meyer zu Borgsen
        target_neck_pan = - 180.0/math.pi * msg.target_position[0]
84 c1e27eb0 Simon Schulz
    elif (id == 0x02):
85 04cf2b6f Sebastian Meyer zu Borgsen
        target_neck_tilt =  180.0/math.pi *  msg.target_position[0]
86 c1e27eb0 Simon Schulz
    elif (id == 0x04):
87 04cf2b6f Sebastian Meyer zu Borgsen
        target_eyes_tilt =  180.0/math.pi * msg.target_position[0]
88 c1e27eb0 Simon Schulz
    elif (id == 0x05):
89 04cf2b6f Sebastian Meyer zu Borgsen
        target_eye_l_pan =  180.0/math.pi * msg.target_position[0]
90 c1e27eb0 Simon Schulz
    elif (id == 0x06):
91 04cf2b6f Sebastian Meyer zu Borgsen
        target_eye_r_pan =  180.0/math.pi * msg.target_position[0]
92 c1e27eb0 Simon Schulz
93 6a233066 Simon Schulz
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 c1e27eb0 Simon Schulz
        print("%f %f %f %f %f %f %f %f %f %f %f %f %f #DUMP" % (
110 6a233066 Simon Schulz
            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 c1e27eb0 Simon Schulz
            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 6a233066 Simon Schulz
            ))
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 c1e27eb0 Simon Schulz
    #target angles
141
    print("> setting up subscriber on %s" % (topic_joint_target))
142
    if USE_MEKA_TOPICS:
143 e832c8ef Sebastian Meyer zu Borgsen
        rospy.Subscriber(topic_joint_target,JointTrajectory, incoming_target_position_meka)
144 c1e27eb0 Simon Schulz
    else:
145
        rospy.Subscriber(topic_joint_target, xsc3_server.msg.joint_state, incoming_target_position_flobi)
146 6a233066 Simon Schulz
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()