Statistics
| Branch: | Tag: | Revision:

hlrc / client / python / hlrc_client / hlrc_set_emotion.py @ 62d50515

History | View | Annotate | Download (2.555 KB)

1
#!/usr/bin/python
2
#PYTHONPATH="/opt/ros/groovy/lib/python2.7/dist-packages:/vol/csra/releases/nightly/lib/python2.7/:/vol/csra/releases/nightly/lib/python2.7/site-packages/
3
import sys
4
import logging
5
import errno
6

    
7
try:
8
    import rsb
9
    import rsb.converter
10
    import rst
11
    import rstsandbox
12
    from rst.robot.EmotionState_pb2 import EmotionState
13
except ImportError as exception:
14
    sys.stderr.write("ImportError: {}\n> HINT: try to export PYTHONPATH=$PYTHONPATH:$YOUR_PREFIX/lib/python2.7/site-packages/\n\n".format(exception))
15
    sys.exit(errno.ENOPKG)
16

    
17
class hlrc_emotion():
18
    def __init__(self, _base_scope):
19
        #print "> registering rst converter"
20
        converter = rsb.converter.ProtocolBufferConverter(messageClass = EmotionState)
21
        rsb.converter.registerGlobalConverter(converter)
22
        self.set_scope( _base_scope);
23

    
24
    def set_scope(self, scope):
25
        self.base_scope = str(scope) #NOTE: str() is important here, scope is a qstring (?) and gets deleted during call
26
        print "> setting scope to '%s'" % self.base_scope
27
        try:
28
            self.server = rsb.createRemoteServer(self.base_scope + '/set')
29
        except ValueError:
30
            print "> invalid scope given. server deactivated"
31
            self.server.deactivate()
32

    
33
    def set_emotion(self, emotion_id, duration_each, blocking):
34
        if (self.server is None):
35
            print("> invalid server")
36
            return
37

    
38
        #create emotion & fill it with values:
39
        em = EmotionState()
40

    
41
        #select ani
42
        em.value    = emotion_id
43
        em.duration = duration_each
44

    
45
        with rsb.createRemoteServer(self.base_scope + '/set') as server:
46
            if (blocking):
47
                #blocking:
48
                print "> calling the emotion rpc (blocking until we finished talking)..."
49
                print '> server reply: "%s"' % server.currentEmotion(em)
50
            else:
51
               print "> calling the animation rpc (NON-BLOCKING)..."
52
               future = server.currentEmotion.async(em)
53
               #we can block here for a incoming result with a timeout in s
54
               #print '> server reply: "%s"' % future.get(timeout = 10);
55

    
56
        print "> done"
57
    
58
def main():
59
    if (len(sys.argv) != 4):
60
        print "> usage: %s <base scope> <emotion id> <duration>\n>     example: %s /flobi1 1 1000" % (sys.argv[0] , sys.argv[0])
61
        sys.exit(0)
62

    
63
    # Pacify logger.
64
    #logging.basicConfig()
65
    em_id = int(sys.argv[2])
66
    dur    = int(sys.argv[3])
67
    base   = sys.argv[1]
68

    
69
    hlrc_emotion(base).set_emotion(em_id, dur, 1)
70

    
71
if __name__ == '__main__':
72
    main()
73

    
74