plugin.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2015 Airbus
00004 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00005 #
00006 # Licensed under the Apache License, Version 2.0 (the "License");
00007 # you may not use this file except in compliance with the License.
00008 # You may obtain a copy of the License at
00009 #
00010 #   http://www.apache.org/licenses/LICENSE-2.0
00011 #
00012 # Unless required by applicable law or agreed to in writing, software
00013 # distributed under the License is distributed on an "AS IS" BASIS,
00014 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 # See the License for the specific language governing permissions and
00016 # limitations under the License.
00017 
00018 
00019 from rosgraph_msgs.msg import Log
00020 import rospkg
00021 import rospy
00022 
00023 from python_qt_binding.QtCore import QMutex, QMutexLocker, QTimer
00024 from python_qt_binding.QtGui import QWidget, QGridLayout
00025 
00026 from rqt_console.console_settings_dialog import ConsoleSettingsDialog
00027 from rqt_console.console_widget import ConsoleWidget
00028 from rqt_console.message import Message
00029 from rqt_console.message_data_model import MessageDataModel
00030 from rqt_console.message_proxy_model import MessageProxyModel
00031 
00032 from airbus_cobot_gui import Plugin, ControlMode
00033 
00034 class PluginLogManager(Plugin):
00035     """
00036     rqt_console plugin's main class. Handles communication with ros_gui and contains
00037     callbacks to handle incoming message
00038     """
00039     def __init__(self, context):
00040         Plugin.__init__(self, context)
00041         
00042     def onCreate(self, param):
00043         
00044         layout = QGridLayout(self)
00045         layout.setContentsMargins(2, 2, 2, 2)
00046         
00047         self._rospack = rospkg.RosPack()
00048         
00049         self._model = MessageDataModel()
00050         self._proxy_model = MessageProxyModel()
00051         self._proxy_model.setSourceModel(self._model)
00052         
00053         self.__widget = ConsoleWidget(self._proxy_model, self._rospack)
00054         
00055         layout.addWidget(self.__widget, 0, 0, 0, 0)
00056         
00057         # queue to store incoming data which get flushed periodically to the model
00058         # required since QSortProxyModel can not handle a high insert rate
00059         self._message_queue = []
00060         self._mutex = QMutex()
00061         self._timer = QTimer()
00062         self._timer.timeout.connect(self.insert_messages)
00063         self._timer.start(100)
00064          
00065         self._subscriber = None
00066         self._topic = '/rosout_agg'
00067         self._subscribe(self._topic)
00068         
00069     def queue_message(self, log_msg):
00070         """
00071         Callback for adding an incomming message to the queue.
00072         """
00073         if not self.__widget._paused:
00074             msg = PluginLogManager.convert_rosgraph_log_message(log_msg)
00075             with QMutexLocker(self._mutex):
00076                 self._message_queue.append(msg)
00077  
00078     @staticmethod
00079     def convert_rosgraph_log_message(log_msg):
00080         msg = Message()
00081         msg.set_stamp_format('hh:mm:ss.ZZZ (yyyy-MM-dd)')
00082         msg.message = log_msg.msg
00083         msg.severity = log_msg.level
00084         msg.node = log_msg.name
00085         msg.stamp = (log_msg.header.stamp.secs, log_msg.header.stamp.nsecs)
00086         msg.topics = sorted(log_msg.topics)
00087         msg.location = log_msg.file + ':' + log_msg.function + ':' + str(log_msg.line)
00088         return msg
00089      
00090     def insert_messages(self):
00091         """
00092         Callback for flushing incoming Log messages from the queue to the model.
00093         """
00094         with QMutexLocker(self._mutex):
00095             msgs = self._message_queue
00096             self._message_queue = []
00097         if msgs:
00098             self._model.insert_rows(msgs)
00099              
00100     def shutdown_plugin(self):
00101         self._subscriber.unregister()
00102         self._timer.stop()
00103         self.__widget.cleanup_browsers_on_close()
00104          
00105     def save_settings(self, plugin_settings, instance_settings):
00106         self.__widget.save_settings(plugin_settings, instance_settings)
00107          
00108     def restore_settings(self, plugin_settings, instance_settings):
00109         self.__widget.restore_settings(plugin_settings, instance_settings)
00110          
00111     def trigger_configuration(self):
00112         topics = [t for t in rospy.get_published_topics() if t[1] == 'rosgraph_msgs/Log']
00113         topics.sort(key=lambda tup: tup[0])
00114         dialog = ConsoleSettingsDialog(topics, self._rospack)
00115         (topic, message_limit) = dialog.query(self._topic, self._model.get_message_limit())
00116         if topic != self._topic:
00117             self._subscribe(topic)
00118         if message_limit != self._model.get_message_limit():
00119             self._model.set_message_limit(message_limit)
00120      
00121     def _subscribe(self, topic):
00122         if self._subscriber:
00123             self._subscriber.unregister()
00124         self._subscriber = rospy.Subscriber(topic, Log, self.queue_message)
00125         self._currenttopic = topic
00126         
00127     def onStart(self):
00128         pass
00129         
00130     def onPause(self):
00131         pass
00132     
00133     def onResume(self):
00134         pass
00135         
00136     def onControlModeChanged(self, mode):
00137         
00138         if mode == ControlMode.AUTOMATIC:
00139             self.setEnabled(False)
00140         else:
00141             self.setEnabled(True)
00142         
00143     def onUserChanged(self, user_info):
00144         pass
00145     
00146     def onTranslate(self, lng):
00147         pass
00148     
00149     def onEmergencyStop(self, state):
00150         pass
00151         
00152     def onDestroy(self):
00153         self.shutdown_plugin()


airbus_plugin_log_manager
Author(s): Matignon Martin
autogenerated on Thu Jun 6 2019 17:59:22