hlrc / client / python / hlrc_client / hlrc_play_animation.py @ ebf5b65f
History | View | Annotate | Download (2.842 KB)
1 | 0c286af0 | Simon Schulz | #!/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.Animation_pb2 import Animation |
||
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_animation(): |
||
18 | def __init__(self, _base_scope): |
||
19 | #print "> registering rst converter"
|
||
20 | converter = rsb.converter.ProtocolBufferConverter(messageClass = Animation) |
||
21 | rsb.converter.registerGlobalConverter(converter) |
||
22 | self.server = None |
||
23 | self.set_scope(_base_scope);
|
||
24 | |||
25 | def __del__(self): |
||
26 | if (not self.server is None): |
||
27 | self.server.deactivate()
|
||
28 | |||
29 | def set_scope(self, scope): |
||
30 | self.base_scope = str(scope) #NOTE: str() is important here, scope is a qstring (?) and gets deleted during call |
||
31 | print "> setting scope to '%s'" % self.base_scope |
||
32 | if (not self.server is None): |
||
33 | self.server.deactivate()
|
||
34 | try:
|
||
35 | self.server = rsb.createRemoteServer(self.base_scope + '/set') |
||
36 | except ValueError: |
||
37 | print "> invalid scope given. server deactivated" |
||
38 | self.server.deactivate()
|
||
39 | |||
40 | def trigger_animation(self, ani_id, repetitions, duration_each, scale, blocking): |
||
41 | if (self.server is None): |
||
42 | print("> invalid server")
|
||
43 | return
|
||
44 | #create animation & fill it with values:
|
||
45 | ani = Animation() |
||
46 | |||
47 | #select ani
|
||
48 | ani.target = ani_id |
||
49 | ani.repetitions = repetitions |
||
50 | ani.duration_each = duration_each |
||
51 | ani.scale = scale |
||
52 | |||
53 | if (blocking):
|
||
54 | #blocking:
|
||
55 | print "> calling the animation rpc (blocking until we finished talking)..." |
||
56 | print '> server reply: "%s"' % self.server.animation(ani) |
||
57 | else:
|
||
58 | print "> calling the animation rpc (NON-BLOCKING)..." |
||
59 | return self.server.animation.async(ani) |
||
60 | #we can block here for a incoming result with a timeout in s
|
||
61 | #print '> server reply: "%s"' % future.get(timeout = 10);
|
||
62 | |||
63 | print "> blocking call done" |
||
64 | |||
65 | def main(): |
||
66 | if (len(sys.argv) != 5): |
||
67 | print "> usage: %s <base scope> <animation id> <dur each> <repetitions>\n> example: %s /flobi1 1 1000 1" % (sys.argv[0] , sys.argv[0]) |
||
68 | sys.exit(0)
|
||
69 | |||
70 | # Pacify logger.
|
||
71 | #logging.basicConfig()
|
||
72 | scope = sys.argv[1]
|
||
73 | ani_id = int(sys.argv[2]) |
||
74 | dur = int(sys.argv[3]) |
||
75 | rep = int(sys.argv[4]) |
||
76 | scale = 1.0
|
||
77 | |||
78 | hani = hlrc_animation(scope) |
||
79 | hani.trigger_animation(ani_id, rep, dur, scale, 1)
|
||
80 | |||
81 | if __name__ == '__main__': |
||
82 | main() |
||
83 |