Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.715 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=False
15

    
16
humotion_gaze_topic = "/flobi/humotion/gaze/target"
17
tf_topic_base   = "BASE_LINK"
18
tf_topic_gaze_l = "EYES_LEFT_UD_VIRTUAL_LINK"
19
tf_topic_gaze_r = "EYES_RIGHT_UD_VIRTUAL_LINK"
20

    
21
#joint targets
22
if USE_MEKA_TOPICS:
23
    from trajectory.msg import *
24
    topic_joint_target = "/meka_roscontrol/head_position_trajectory_controller/command"
25
else:
26
    from xsc3_server.msg import *
27
    topic_joint_target = "/xsc3/custom_joint_state"
28

    
29

    
30
target_neck_pan = 0.0
31
target_neck_tilt = 0.0
32
target_eyes_tilt = 0.0
33
target_eye_l_pan = 0.0
34
target_eye_r_pan = 0.0
35

    
36
def fetch_gaze_from_tf(timestamp):
37
    try:
38
        #fetch right eye
39
        (trans,rot) = listener.lookupTransform(tf_topic_base, tf_topic_gaze_r, timestamp)
40
        euler  = tf.transformations.euler_from_quaternion(rot, axes='sxyz')
41
        pan_r  = -euler[2]
42
        tilt_r = -euler[1]
43
        #fetch left eye
44
        (trans,rot) = listener.lookupTransform(tf_topic_base, tf_topic_gaze_l, timestamp)
45
        euler  = tf.transformations.euler_from_quaternion(rot, axes='sxyz')
46
        pan_l  = -euler[2]
47
        tilt_l = -euler[1]
48
        #combine to overall gaze direction:
49
        pan  = (pan_l + pan_r) / 2.0 * 180.0 / math.pi
50
        tilt = (tilt_l + tilt_r) / 2.0 * 180.0 / math.pi
51
        return (pan, tilt)
52

    
53
    except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
54
        print("> WARNING: no transformation %s-->%s at %s found" % (tf_topic_base, tf_topic_gaze_l, timestamp))
55
        return (0.0, 0.0)
56

    
57

    
58
def incoming_target_position_meka(msg):
59
    print("> incoming target")
60
    print msg
61

    
62

    
63
def incoming_target_position_flobi(msg):
64
    global target_neck_pan, target_neck_tilt, target_eyes_tilt, target_eye_l_pan, target_eye_r_pan
65
    #python 2.x interprets int8_t as string, reconvert this to an array:
66
    ids = list(array.array("B", msg.id))
67
    id = ids[0]
68
    #print("> incoming target for joint %d" % (id))
69
    if (id == 0x01):
70
        target_neck_pan = msg.target_position[0]
71
    elif (id == 0x02):
72
        target_neck_tilt = msg.target_position[0]
73
    elif (id == 0x04):
74
        target_eyes_tilt = msg.target_position[0]
75
    elif (id == 0x05):
76
        target_eye_l_pan = msg.target_position[0]
77
    elif (id == 0x06):
78
        target_eye_r_pan = msg.target_position[0]
79

    
80
def incoming_humotion_gaze_target(msg):
81
    print("> incoming gaze @%f" % (msg.gaze_timestamp.to_sec()))
82

    
83
    if (msg.gaze_type == gaze.GAZETYPE_RELATIVE):
84
        #find out the overall gaze state using TF:
85
        (tf_pan, tf_tilt) = fetch_gaze_from_tf(msg.gaze_timestamp)
86
        #relative targets
87
        rel_pan  = msg.pan
88
        rel_tilt = msg.tilt
89
        #combined:
90
        target_pan  = tf_pan + rel_pan
91
        target_tilt = tf_tilt + rel_tilt
92
        #current position:
93
        now = rospy.get_rostime()
94
        (now_pan, now_tilt) = fetch_gaze_from_tf(rospy.Time(0))
95
        now_pan
96
        print("%f %f %f %f %f %f %f %f %f %f %f %f %f #DUMP" % (
97
            now.to_sec(),
98
            now_pan,
99
            now_tilt,
100
            msg.gaze_timestamp.to_sec(),
101
            rel_pan,
102
            rel_tilt,
103
            target_pan,
104
            target_tilt,
105
            target_neck_pan,
106
            target_neck_tilt,
107
            target_eyes_tilt,
108
            target_eye_l_pan,
109
            target_eye_r_pan,
110
            ))
111
        #print("pan = %f, tilt=%f" % (pan* 180.0 / math.pi,tilt* 180.0 / math.pi))
112

    
113
    else:
114
        print("> NOT dumping absolute targets...")
115

    
116

    
117

    
118

    
119
if __name__ == '__main__':
120
    new_transform = False
121
    rospy.init_node('dump_relative_gazetarget')
122

    
123
    #humotion gaze subscriber
124
    print("> setting up subscriber on %s" % (humotion_gaze_topic))
125
    rospy.Subscriber(humotion_gaze_topic, gaze, incoming_humotion_gaze_target)
126

    
127
    #target angles
128
    print("> setting up subscriber on %s" % (topic_joint_target))
129
    if USE_MEKA_TOPICS:
130
        rospy.Subscriber(humotion_gaze_topic, JointTrajectory, incoming_target_position_meka)
131
    else:
132
        rospy.Subscriber(topic_joint_target, xsc3_server.msg.joint_state, incoming_target_position_flobi)
133

    
134
    #tf listener
135
    listener = tf.TransformListener()
136

    
137
    print("> waiting for transform...")
138
    try:
139
        listener.waitForTransform(tf_topic_base, tf_topic_gaze_r, rospy.Time(), rospy.Duration(4.0))
140
    except tf.Exception:
141
        print("> ERROR: timed out while waiting for transform %s->%s to become available\n" % (tf_topic_base, tf_topic_gaze_r))
142
        exit(errno.ETIMEDOUT)
143
    print("> first transform arrived, will start now...")
144

    
145
    rate = rospy.Rate(50.0)
146

    
147
    while not rospy.is_shutdown():
148

    
149
        rate.sleep()
150