Statistics
| Branch: | Tag: | Revision:

hlrc / client / python / hlrc_client / RobotController.py @ fc4357ee

History | View | Annotate | Download (6.291 KB)

1
"""
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
import sys
30
import errno
31

    
32
class RobotController:
33
    def __init__(self, mw_name, scope, loglevel=logging.WARNING, timeout=None):
34
        """initialise
35
        :param mw_name: which mw to use, currentyl ROS and RSB are supported
36
        :param scope: base scope we want to listen on
37
        :param  loglevel: optional log level
38
        """
39
        self.logger = logging.getLogger(__name__)
40

    
41
        # create nice and actually usable formatter and add it to the handler
42
        self.config_logger(loglevel)
43

    
44
        # store
45
        self.scope = scope
46
        self.mw = mw_name
47
        self.loglevel = loglevel
48

    
49
        self.middleware = None
50

    
51
        if (self.mw.upper() == "RSB"):
52
            self.logger.info("creating new middleware connection via RSB")
53
            try:
54
                from MiddlewareRSB import MiddlewareRSB
55
            except ImportError, e:
56
                self.logger.error("failed to import RSB or the necessary data types: {}".format(e))
57
                sys.exit(errno.EINVAL)
58

    
59
            # import worked, safe to intantiate RSB mw now
60
            self.middleware = MiddlewareRSB(self.scope, self.loglevel)
61

    
62
        elif (self.mw.upper() == "ROS"):
63
            self.logger.info("creating new middleware connection via ROS")
64
            try:
65
                from MiddlewareROS import MiddlewareROS
66
            except ImportError, e:
67
                self.logger.error("failed to import ROS or the necessary data types: {}".format(e))
68
                sys.exit(errno.EINVAL)
69

    
70
            # import worked, safe to instantiate RSB mw now
71
            self.middleware = MiddlewareROS(self.scope, self.loglevel, timeout)
72
        else:
73
            self.logger.error("invalid middleware requested (%s). supported: {ROS, RSB}\n\n" % (self.mw))
74
            sys.exit(errno.EINVAL)
75

    
76
    def __del__(self):
77
        """destructor
78
        """
79
        try: # if constructor failed, logger attribute might be missing
80
            self.logger.debug("destructor of RobotController called")
81
        except AttributeError:
82
            pass
83

    
84
    def is_running(self):
85
        return self.middleware.is_running()
86

    
87
    def config_logger(self, level):
88
        """initialise a nice logger formatting
89
        :param  level: log level
90
        """
91
        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
        """set the current emotion
100
        :param robot_emotion: a RobotEmotion object
101
        :param blocking: should this call block during execution?
102
        """
103
        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
        """set the default emotion
108
        :param robot_emotion: a RobotEmotion object
109
        :param blocking: should this call block during execution?
110
        """
111
        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
        """set a gaze target
116
        :param robot_gaze: a RobotGaze object
117
        :param blocking: should this call block during execution?
118
        """
119

    
120
        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_lookat_target(self, x, y, z, blocking=False, frame_id='', roll=0.0):
124
        """set a gaze at a Cartesian position
125
        :param x,y,z: position to look at (in eyes frame or frame_id)
126
        :param roll: side-ways motion of head
127
        :param blocking: True if this call should block until execution finished on robot
128
        """
129
        self.logger.debug("set_lookat_target(%s) %s" % ((x,y,z), "BLOCKING" if blocking else "NON_BLOCKING"))
130
        self.middleware.set_lookat_target(x, y, z, blocking, frame_id, roll)
131

    
132
    def set_mouth_target(self, robot_mouth, blocking=False):
133
        """set a mouth target
134
        :param robot_mouth: a RobotMouth object
135
        :param blocking: should this call block during execution?
136
        """
137
        self.logger.debug("set_mouth_target(%s) %s" % (robot_mouth, ("BLOCKING" if blocking else "NON_BLOCKING")))
138
        self.middleware.set_mouth_target(robot_mouth, blocking)
139

    
140
    def set_head_animation(self, robot_animation, blocking=False):
141
        """set a head animation
142
        :param robot_animation: a RobotAnimation object
143
        :param blocking: should this call block during execution?
144
        """
145
        self.logger.debug("set_head_animation(%s) %s" % (robot_animation, ("BLOCKING" if blocking else "NON_BLOCKING")))
146
        self.middleware.set_head_animation(robot_animation, blocking)
147

    
148
    def set_speak(self, text, blocking=False):
149
        """request the robot to say something using tts
150
        :param text: text to synthesize
151
        :param blocking: should this call block during execution?
152
        """
153
        self.logger.debug("set_speak(%s) %s" % (text, ("BLOCKING" if blocking else "NON_BLOCKING")))
154
        self.middleware.set_speak(text, blocking)