Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.797 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
import sys
30 64f5c90e Simon Schulz
import errno
31 0c286af0 Simon Schulz
32
class RobotController:
33 6304bfc1 Robert Haschke
    def __init__(self, mw_name, scope, loglevel=logging.WARNING, timeout=None):
34 70c54617 Simon Schulz
        """initialise
35 d81c4f8b Matthias Barth
        :param mw_name: which mw to use, currentyl ROS is supported
36 70c54617 Simon Schulz
        :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 465c7ad7 Florian Lier
        # store
45 70c54617 Simon Schulz
        self.scope = scope
46
        self.mw = mw_name
47
        self.loglevel = loglevel
48
49
        self.middleware = None
50
51 d81c4f8b Matthias Barth
        if (self.mw.upper() == "ROS"):
52 70c54617 Simon Schulz
            self.logger.info("creating new middleware connection via ROS")
53
            try:
54 836f314c Robert Haschke
                from .MiddlewareROS import MiddlewareROS
55
            except ImportError as e:
56 70c54617 Simon Schulz
                self.logger.error("failed to import ROS or the necessary data types: {}".format(e))
57
                sys.exit(errno.EINVAL)
58
59 d81c4f8b Matthias Barth
            # import worked, safe to instantiate ROS mw now
60 6304bfc1 Robert Haschke
            self.middleware = MiddlewareROS(self.scope, self.loglevel, timeout)
61 70c54617 Simon Schulz
        else:
62 d81c4f8b Matthias Barth
            self.logger.error("invalid middleware requested (%s). supported: {ROS}\n\n" % (self.mw))
63 70c54617 Simon Schulz
            sys.exit(errno.EINVAL)
64
65
    def __del__(self):
66
        """destructor
67
        """
68 6304bfc1 Robert Haschke
        try: # if constructor failed, logger attribute might be missing
69
            self.logger.debug("destructor of RobotController called")
70
        except AttributeError:
71
            pass
72 70c54617 Simon Schulz
73 02fc76ae Simon Schulz
    def is_running(self):
74
        return self.middleware.is_running()
75
76 70c54617 Simon Schulz
    def config_logger(self, level):
77
        """initialise a nice logger formatting
78
        :param  level: log level
79
        """
80
        formatter = logging.Formatter('%(asctime)s %(name)-30s %(levelname)-8s > %(message)s')
81
        ch = logging.StreamHandler()
82 465c7ad7 Florian Lier
        # ch.setLevel(level)
83 70c54617 Simon Schulz
        ch.setFormatter(formatter)
84
        self.logger.setLevel(level)
85
        self.logger.addHandler(ch)
86
87 465c7ad7 Florian Lier
    def set_current_emotion(self, robot_emotion, blocking=False):
88 70c54617 Simon Schulz
        """set the current emotion
89
        :param robot_emotion: a RobotEmotion object
90
        :param blocking: should this call block during execution?
91
        """
92
        self.logger.debug("set_current_emotion(%s) %s" % (robot_emotion, ("BLOCKING" if blocking else "NON_BLOCKING")))
93
        self.middleware.set_current_emotion(robot_emotion, blocking)
94
95 465c7ad7 Florian Lier
    def set_default_emotion(self, robot_emotion, blocking=False):
96 70c54617 Simon Schulz
        """set the default emotion
97
        :param robot_emotion: a RobotEmotion object
98
        :param blocking: should this call block during execution?
99
        """
100
        self.logger.debug("set_default_emotion(%s) %s" % (robot_emotion, ("BLOCKING" if blocking else "NON_BLOCKING")))
101
        self.middleware.set_default_emotion(robot_emotion, blocking)
102
103 465c7ad7 Florian Lier
    def set_gaze_target(self, robot_gaze, blocking=False):
104 70c54617 Simon Schulz
        """set a gaze target
105
        :param robot_gaze: a RobotGaze object
106
        :param blocking: should this call block during execution?
107
        """
108
109
        self.logger.debug("set_gaze_target(%s) %s" % (robot_gaze, ("BLOCKING" if blocking else "NON_BLOCKING")))
110
        self.middleware.set_gaze_target(robot_gaze, blocking)
111
112 465c7ad7 Florian Lier
    def set_lookat_target(self, x, y, z, blocking=False, frame_id='', roll=0.0):
113
        """set a gaze at a Cartesian position
114
        :param x,y,z: position to look at (in eyes frame or frame_id)
115
        :param roll: side-ways motion of head
116
        :param blocking: True if this call should block until execution finished on robot
117
        """
118
        self.logger.debug("set_lookat_target(%s) %s" % ((x,y,z), "BLOCKING" if blocking else "NON_BLOCKING"))
119
        self.middleware.set_lookat_target(x, y, z, blocking, frame_id, roll)
120
121
    def set_mouth_target(self, robot_mouth, blocking=False):
122 70c54617 Simon Schulz
        """set a mouth target
123
        :param robot_mouth: a RobotMouth object
124
        :param blocking: should this call block during execution?
125
        """
126
        self.logger.debug("set_mouth_target(%s) %s" % (robot_mouth, ("BLOCKING" if blocking else "NON_BLOCKING")))
127
        self.middleware.set_mouth_target(robot_mouth, blocking)
128
129 465c7ad7 Florian Lier
    def set_head_animation(self, robot_animation, blocking=False):
130 70c54617 Simon Schulz
        """set a head animation
131
        :param robot_animation: a RobotAnimation object
132
        :param blocking: should this call block during execution?
133
        """
134
        self.logger.debug("set_head_animation(%s) %s" % (robot_animation, ("BLOCKING" if blocking else "NON_BLOCKING")))
135
        self.middleware.set_head_animation(robot_animation, blocking)
136
137 465c7ad7 Florian Lier
    def set_speak(self, text, blocking=False):
138 70c54617 Simon Schulz
        """request the robot to say something using tts
139
        :param text: text to synthesize
140
        :param blocking: should this call block during execution?
141
        """
142
        self.logger.debug("set_speak(%s) %s" % (text, ("BLOCKING" if blocking else "NON_BLOCKING")))
143
        self.middleware.set_speak(text, blocking)