node.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 import rospy
00019 import time
00020 import os
00021 import rosnode
00022 import roslaunch
00023 import subprocess
00024 from roslaunch import main as ros_launch
00025 
00026 from roslib.packages import get_pkg_dir
00027 
00028 from python_qt_binding.QtGui import *
00029 from python_qt_binding.QtCore import *
00030 
00031 class NodeItem:
00032     
00033     NONE = 'None'
00034     RUNNING = 'Running'
00035     ABARTED = 'Aborted'
00036     SHUTDOWN = 'Shutdown'
00037     
00038     NODE_EXCEPTION = ['/rosout']
00039     
00040     def __init__(self, node_uri, node_name):
00041         
00042         rsc = os.path.join(get_pkg_dir('airbus_plugin_node_manager'),'resources')
00043         
00044         self._icon_node_start = QIcon(rsc+'/start.png')
00045         self._icon_node_stop = QIcon(rsc+'/stop.png')
00046         
00047         self.uri = QLabel(node_uri)
00048         self.uri.setContentsMargins(0,0,10,0)
00049         self.uri.setMinimumHeight(40)
00050         
00051         self.name = QLabel(node_name)
00052         self.name.setContentsMargins(0,0,10,0)
00053         self.name.setMinimumHeight(40)
00054         
00055         self.status = QLabel(self.RUNNING)
00056         self.status.setStyleSheet('qproperty-alignment: AlignCenter;')
00057         self.status.setMinimumSize(QSize(100,40))
00058         
00059         self.ping = QLabel('...')
00060         self.ping.setStyleSheet("qproperty-alignment: AlignCenter;")
00061         
00062         self.button_start_stop = QPushButton()
00063         self.button_start_stop.setIcon(self._icon_node_stop)
00064         self.button_start_stop.setIconSize(QSize(30,30))
00065         self.button_start_stop.setFixedSize(QSize(100,40))
00066         
00067         self.button_start_stop_widget = self.setup_start_stop_button(self.button_start_stop)
00068         
00069         if node_name not in self.NODE_EXCEPTION:
00070             self.button_start_stop.clicked.connect(self.start_stop_slot)
00071             self.button_start_stop.setEnabled(False)
00072         
00073         self.current_status = self.NONE
00074         
00075     def setup_start_stop_button(obj_ui):
00076         
00077         widget = QWidget()
00078         
00079         layout = QHBoxLayout(widget)
00080         layout.setSpacing(6)
00081         layout.setContentsMargins(0, 0, 0, 0)
00082         spacer_left = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
00083         spacer_right = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
00084         
00085         layout.addItem(spacer_left)
00086         layout.addWidget(obj_ui)
00087         layout.addItem(spacer_right)
00088         
00089         return widget
00090         
00091     def start_stop_slot(self):
00092         
00093         self.button_start_stop.setEnabled(False)
00094         
00095         if self.current_status == self.RUNNING:
00096             self.stop_node()
00097         else:
00098             self.start_node()
00099         
00100     def start_node(self):
00101         rospy.loginfo('%s::started()'%self.name.text())
00102         
00103         launch_file = self.name.text().replace('/','')
00104         launch_file += '.launch'
00105         subprocess.Popen(['roslaunch',
00106                           'node_launchers',
00107                           launch_file])
00108         
00109     def stop_node(self):
00110         rospy.loginfo('%s::stoped()'%self.name.text())
00111         rosnode._rosnode_cmd_kill(['fake','fake',self.name.text()])
00112         
00113     def refresh(self, status, ping=None):
00114         
00115         if ping is not None:
00116             self.ping.setText(str("%.3f"%ping))
00117         else:
00118             self.ping.setText('...')
00119         
00120         if status != self.current_status:
00121             self.current_status = status
00122             self.button_start_stop.setEnabled(True)
00123             self.status.setText(self.current_status)
00124             if self.current_status == self.RUNNING:
00125                 self.status.setStyleSheet("background:rgb(0,255,0);")
00126                 self.button_start_stop.setIcon(self._icon_node_stop)
00127             elif self.current_status == self.ABARTED:
00128                 self.status.setStyleSheet("background:rgb(255,0,0);")
00129                 self.button_start_stop.setIcon(self._icon_node_start)
00130             elif self.current_status == self.SHUTDOWN:
00131                 self.status.setStyleSheet("background:rgb(255,255,0);")
00132                 self.button_start_stop.setIcon(self._icon_node_start)
00133             else:
00134                 self.status.setStyleSheet("background:rgb(255,255,255);")
00135                 self.status.setText('Unknown')
00136         
00137 #End of file


airbus_plugin_node_manager
Author(s): Matignon Martin
autogenerated on Thu Jun 6 2019 17:59:23