hlrc / client / python / hlrc_client / RobotController.py @ 860e3253
History | View | Annotate | Download (5.272 KB)
1 | 0c286af0 | Simon Schulz | """
|
---|---|---|---|
2 | This file is part of hlrc
|
||
3 |
|
||
4 | Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
|
||
5 | http://opensource.cit-ec.de/projects/hlrc
|
||
6 |
|
||
7 | This file may be licensed under the terms of the
|
||
8 | GNU General Public License Version 3 (the ``GPL''),
|
||
9 | or (at your option) any later version.
|
||
10 |
|
||
11 | Software distributed under the License is distributed
|
||
12 | on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
|
||
13 | express or implied. See the GPL for the specific language
|
||
14 | governing rights and limitations.
|
||
15 |
|
||
16 | You should have received a copy of the GPL along with this
|
||
17 | program. If not, go to http://www.gnu.org/licenses/gpl.html
|
||
18 | or write to the Free Software Foundation, Inc.,
|
||
19 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
20 |
|
||
21 | The development of this software was supported by the
|
||
22 | Excellence Cluster EXC 277 Cognitive Interaction Technology.
|
||
23 | The Excellence Cluster EXC 277 is a grant of the Deutsche
|
||
24 | Forschungsgemeinschaft (DFG) in the context of the German
|
||
25 | Excellence Initiative.
|
||
26 | """
|
||
27 | |||
28 | import logging |
||
29 | ba6302b1 | Simon Schulz | try:
|
30 | eb9b1b16 | fl | import rsb |
31 | import rst |
||
32 | import rstsandbox |
||
33 | from rst.robot.EmotionState_pb2 import EmotionState |
||
34 | from rst.robot.Animation_pb2 import Animation |
||
35 | from rst.robot.GazeTarget_pb2 import GazeTarget |
||
36 | from rst.robot.MouthTarget_pb2 import MouthTarget |
||
37 | except ImportError, e: |
||
38 | print("Warning, RSB import error: ")
|
||
39 | print e
|
||
40 | RSB_SUPPORT = False
|
||
41 | ba6302b1 | Simon Schulz | else:
|
42 | from MiddlewareRSB import * |
||
43 | RSB_SUPPORT = True
|
||
44 | |||
45 | 0c286af0 | Simon Schulz | from MiddlewareROS import * |
46 | ba6302b1 | Simon Schulz | import sys |
47 | 0c286af0 | Simon Schulz | |
48 | class RobotController: |
||
49 | def __init__(self, mw_name, scope, loglevel=logging.WARNING): |
||
50 | f8fa1217 | Simon Schulz | """initialise
|
51 | :param mw_name: which mw to use, currentyl ROS and RSB are supported
|
||
52 | :param scope: base scope we want to listen on
|
||
53 | :param loglevel: optional log level
|
||
54 | """
|
||
55 | 0c286af0 | Simon Schulz | self.logger = logging.getLogger(__name__)
|
56 | |||
57 | # create nice and actually usable formatter and add it to the handler
|
||
58 | self.config_logger(loglevel)
|
||
59 | |||
60 | f8fa1217 | Simon Schulz | #store
|
61 | 0c286af0 | Simon Schulz | self.scope = scope
|
62 | f8fa1217 | Simon Schulz | self.mw = mw_name
|
63 | self.loglevel = loglevel
|
||
64 | 0c286af0 | Simon Schulz | |
65 | f8fa1217 | Simon Schulz | self.middleware = None |
66 | |||
67 | |||
68 | if (self.mw.upper() == "RSB"): |
||
69 | ba6302b1 | Simon Schulz | if (not RSB_SUPPORT): |
70 | self.logger.error("ERROR: no RSB support detected") |
||
71 | sys.exit(errno.EINVAL) |
||
72 | else:
|
||
73 | self.logger.info("creating new middleware connection via RSB") |
||
74 | self.middleware = MiddlewareRSB(self.scope, self.loglevel) |
||
75 | f8fa1217 | Simon Schulz | elif (self.mw.upper() == "ROS"): |
76 | 0c286af0 | Simon Schulz | self.logger.info("creating new middleware connection via ROS") |
77 | f8fa1217 | Simon Schulz | self.middleware = MiddlewareROS(self.scope, self.loglevel) |
78 | 0c286af0 | Simon Schulz | else:
|
79 | f8fa1217 | Simon Schulz | self.logger.error("invalid middleware requested (%s). supported: {ROS, RSB}\n\n" % (self.mw)) |
80 | 0c286af0 | Simon Schulz | sys.exit(errno.EINVAL) |
81 | |||
82 | f8fa1217 | Simon Schulz | def __del__(self): |
83 | """destructor
|
||
84 | """
|
||
85 | self.logger.debug("destructor of RobotController called") |
||
86 | |||
87 | 0c286af0 | Simon Schulz | def config_logger(self, level): |
88 | f8fa1217 | Simon Schulz | """initialise a nice logger formatting
|
89 | :param level: log level
|
||
90 | """
|
||
91 | 0c286af0 | Simon Schulz | formatter = logging.Formatter('%(asctime)s %(name)-30s %(levelname)-8s > %(message)s')
|
92 | ch = logging.StreamHandler() |
||
93 | #ch.setLevel(level)
|
||
94 | ch.setFormatter(formatter) |
||
95 | self.logger.setLevel(level)
|
||
96 | self.logger.addHandler(ch)
|
||
97 | |||
98 | def set_current_emotion(self, robot_emotion, blocking = False): |
||
99 | f8fa1217 | Simon Schulz | """set the current emotion
|
100 | :param robot_emotion: a RobotEmotion object
|
||
101 | :param blocking: should this call block during execution?
|
||
102 | """
|
||
103 | 0c286af0 | Simon Schulz | self.logger.debug("set_current_emotion(%s) %s" % (robot_emotion, ("BLOCKING" if blocking else "NON_BLOCKING"))) |
104 | self.middleware.set_current_emotion(robot_emotion, blocking)
|
||
105 | |||
106 | def set_default_emotion(self, robot_emotion, blocking = False): |
||
107 | f8fa1217 | Simon Schulz | """set the default emotion
|
108 | :param robot_emotion: a RobotEmotion object
|
||
109 | :param blocking: should this call block during execution?
|
||
110 | """
|
||
111 | 0c286af0 | Simon Schulz | self.logger.debug("set_default_emotion(%s) %s" % (robot_emotion, ("BLOCKING" if blocking else "NON_BLOCKING"))) |
112 | self.middleware.set_default_emotion(robot_emotion, blocking)
|
||
113 | |||
114 | def set_gaze_target(self, robot_gaze, blocking = False): |
||
115 | f8fa1217 | Simon Schulz | """set a gaze target
|
116 | :param robot_gaze: a RobotGaze object
|
||
117 | :param blocking: should this call block during execution?
|
||
118 | """
|
||
119 | |||
120 | 0c286af0 | Simon Schulz | self.logger.debug("set_gaze_target(%s) %s" % (robot_gaze, ("BLOCKING" if blocking else "NON_BLOCKING"))) |
121 | self.middleware.set_gaze_target(robot_gaze, blocking)
|
||
122 | |||
123 | def set_mouth_target(self, robot_mouth, blocking = False): |
||
124 | f8fa1217 | Simon Schulz | """set a mouth target
|
125 | :param robot_mouth: a RobotMouth object
|
||
126 | :param blocking: should this call block during execution?
|
||
127 | """
|
||
128 | 0c286af0 | Simon Schulz | self.logger.debug("set_mouth_target(%s) %s" % (robot_mouth, ("BLOCKING" if blocking else "NON_BLOCKING"))) |
129 | self.middleware.set_mouth_target(robot_mouth, blocking)
|
||
130 | |||
131 | def set_head_animation(self, robot_animation, blocking = False): |
||
132 | f8fa1217 | Simon Schulz | """set a head animation
|
133 | :param robot_animation: a RobotAnimation object
|
||
134 | :param blocking: should this call block during execution?
|
||
135 | """
|
||
136 | 0c286af0 | Simon Schulz | self.logger.debug("set_head_animation(%s) %s" % (robot_animation, ("BLOCKING" if blocking else "NON_BLOCKING"))) |
137 | self.middleware.set_head_animation(robot_animation, blocking)
|
||
138 | |||
139 | def set_speak(self, text, blocking = False): |
||
140 | f8fa1217 | Simon Schulz | """request the robot to say something using tts
|
141 | :param text: text to synthesize
|
||
142 | :param blocking: should this call block during execution?
|
||
143 | """
|
||
144 | 0c286af0 | Simon Schulz | self.logger.debug("set_speak(%s) %s" % (text, ("BLOCKING" if blocking else "NON_BLOCKING"))) |
145 | self.middleware.set_speak(text, blocking)
|
||
146 | |||
147 | f8fa1217 | Simon Schulz | #def get_gaze_target(self):
|
148 | # result = self.middleware.get_gaze_target()
|
||
149 | # self.logger.debug("get_gaze_target() returned %s" % (result))
|
||
150 | # return self.middleware.get_gaze_target() |