sm_interface.py
Go to the documentation of this file.
00001 """
00002    Copyright 2013 Southwest Research Institute
00003 
00004    Licensed under the Apache License, Version 2.0 (the "License");
00005    you may not use this file except in compliance with the License.
00006    You may obtain a copy of the License at
00007 
00008      http://www.apache.org/licenses/LICENSE-2.0
00009 
00010    Unless required by applicable law or agreed to in writing, software
00011    distributed under the License is distributed on an "AS IS" BASIS,
00012    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013    See the License for the specific language governing permissions and
00014    limitations under the License.
00015 """
00016 
00017 import roslib
00018 roslib.load_manifest('mtconnect_example_gui')
00019 
00020 import os
00021 import rospy
00022 
00023 from qt_gui.plugin import Plugin
00024 from python_qt_binding import loadUi
00025 from python_qt_binding.QtGui import QWidget
00026 
00027 
00028 from mtconnect_example_msgs.srv import StateMachineCmd
00029 from mtconnect_example_msgs.msg import StateMachineStatus
00030 
00031 class SMInterfaceGUI(Plugin):
00032 
00033     def __init__(self, context):
00034         super(SMInterfaceGUI, self).__init__(context)
00035         # Give QObjects reasonable names
00036         self.setObjectName('SMInterfaceGUI')
00037 
00038         # Process standalone plugin command-line arguments
00039         from argparse import ArgumentParser
00040         parser = ArgumentParser()
00041         # Add argument(s) to the parser.
00042         parser.add_argument("-q", "--quiet", action="store_true",
00043                       dest="quiet",
00044                       help="Put plugin in silent mode")
00045         args, unknowns = parser.parse_known_args(context.argv())
00046         if not args.quiet:
00047             print 'arguments: ', args
00048             print 'unknowns: ', unknowns
00049 
00050         # Create QWidget
00051         self._widget = QWidget()
00052         # Get path to UI file which is a sibling of this file
00053         # in this example the .ui and .py file are in the same folder
00054         ui_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'sm_interface.ui')
00055         # Extend the widget with all attributes and children from UI file
00056         loadUi(ui_file, self._widget)
00057         # Give QObjects reasonable names
00058         self._widget.setObjectName('SMInterfaceUi')
00059         # Show _widget.windowTitle on left-top of each plugin (when 
00060         # it's set in _widget). This is useful when you open multiple 
00061         # plugins at once. Also if you open multiple instances of your 
00062         # plugin at once, these lines add number to make it easy to 
00063         # tell from pane to pane.
00064         if context.serial_number() > 1:
00065             self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % context.serial_number()))
00066         # Add widget to the user interface
00067         context.add_widget(self._widget)
00068         
00069         # Custom code begins here
00070         self._widget.start_button.clicked[bool].connect(self.__handle_start_clicked)
00071         self._widget.stop_button.clicked[bool].connect(self.__handle_stop_clicked)
00072         self._widget.reset_button.clicked[bool].connect(self.__handle_reset_clicked)
00073         
00074         self.cmd_service = rospy.ServiceProxy('external_command', StateMachineCmd)
00075         self.sm_status_sub = rospy.Subscriber('state_machine_status', StateMachineStatus, self.__sm_status_callback)
00076 
00077     def shutdown_plugin(self):
00078         self.sm_status_sub.unregister()
00079         # TODO unregister all publishers here
00080         pass
00081 
00082     def save_settings(self, plugin_settings, instance_settings):
00083         # TODO save intrinsic configuration, usually using:
00084         # instance_settings.set_value(k, v)
00085         pass
00086 
00087     def restore_settings(self, plugin_settings, instance_settings):
00088         # TODO restore intrinsic configuration, usually using:
00089         # v = instance_settings.value(k)
00090         pass
00091         
00092         
00093     def __handle_start_clicked(self, checked):
00094         self.cmd_service(2)
00095         
00096     def __handle_stop_clicked(self, checked):
00097         self.cmd_service(3)
00098         
00099     def __handle_reset_clicked(self, checked):
00100         self.cmd_service(1)
00101 
00102     def __sm_status_callback(self, msg):
00103         self._widget.state_name.setText(msg.state_name + '(' + str(msg.state) + ')')
00104         
00105         
00106     #def trigger_configuration(self):
00107         # Comment in to signal that the plugin has a way to configure it
00108         # Usually used to open a configuration dialog


mtconnect_example_gui
Author(s): Shaun M. Edwards
autogenerated on Mon Jan 6 2014 11:31:22