Go to the documentation of this file.00001 """
00002 Copyright 2017 Shaun Edwards
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 os
00018 import rospy
00019 import rospkg
00020
00021 from threading import Thread
00022 from qt_gui.plugin import Plugin
00023 from python_qt_binding import loadUi
00024 from python_qt_binding.QtCore import Qt, QThread
00025 from python_qt_binding.QtGui import QWidget, QPalette
00026 from std_srvs.srv import Trigger
00027 from packml_msgs.srv import Transition
00028 from packml_msgs.srv import TransitionRequest
00029 from packml_msgs.msg import Status
00030 from packml_msgs.msg import State
00031 from packml_msgs.msg import Mode
00032
00033 class Packml(Plugin):
00034
00035 def __init__(self, context):
00036 super(Packml, self).__init__(context)
00037 self.setObjectName('Packml')
00038
00039 from argparse import ArgumentParser
00040 parser = ArgumentParser()
00041
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
00051 self._widget = QWidget()
00052 ui_file = os.path.join(rospkg.RosPack().get_path('packml_gui'), 'resource', 'packml.ui')
00053 loadUi(ui_file, self._widget)
00054 self._widget.setObjectName('Packml')
00055
00056 if context.serial_number() > 1:
00057 self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % context.serial_number()))
00058
00059 context.add_widget(self._widget)
00060
00061
00062 self._widget.reset_button.clicked[bool].connect(self.__handle_reset_clicked)
00063 self._widget.start_button.clicked[bool].connect(self.__handle_start_clicked)
00064 self._widget.stop_button.clicked[bool].connect(self.__handle_stop_clicked)
00065 self._widget.clear_button.clicked[bool].connect(self.__handle_clear_clicked)
00066 self._widget.hold_button.clicked[bool].connect(self.__handle_hold_clicked)
00067 self._widget.unhold_button.clicked[bool].connect(self.__handle_unhold_clicked)
00068 self._widget.suspend_button.clicked[bool].connect(self.__handle_suspend_clicked)
00069 self._widget.unsuspend_button.clicked[bool].connect(self.__handle_unsuspend_clicked)
00070 self._widget.abort_button.clicked[bool].connect(self.__handle_abort_clicked)
00071
00072 self._service_thread = Thread(target=self.wait_for_services, args=())
00073 self._service_thread.start()
00074
00075 self._status_sub = rospy.Subscriber('packml/status', Status, self.status_callback)
00076
00077 def disable_all_buttons(self):
00078 self._widget.clear_button.setEnabled(False)
00079 self._widget.reset_button.setEnabled(False)
00080 self._widget.start_button.setEnabled(False)
00081 self._widget.stop_button.setEnabled(False)
00082 self._widget.hold_button.setEnabled(False)
00083 self._widget.suspend_button.setEnabled(False)
00084 self._widget.unhold_button.setEnabled(False)
00085 self._widget.unsuspend_button.setEnabled(False)
00086 self._widget.abort_button.setEnabled(False)
00087
00088 def set_message_text(self, text):
00089 self._widget.message_box.setText("Message: " + text)
00090
00091 def status_callback(self, msg):
00092 self.update_button_states(msg.state.val)
00093 self.update_status_fields(msg)
00094
00095
00096 def update_button_states(self, state):
00097 self.disable_all_buttons()
00098 if state == State.ABORTED:
00099 self._widget.clear_button.setEnabled(True)
00100 elif state == State.STOPPED:
00101 self._widget.reset_button.setEnabled(True)
00102 elif state == State.IDLE:
00103 self._widget.start_button.setEnabled(True)
00104 elif state == State.EXECUTE:
00105 self._widget.hold_button.setEnabled(True)
00106 self._widget.suspend_button.setEnabled(True)
00107 elif state == State.HELD:
00108 self._widget.unhold_button.setEnabled(True)
00109 elif state == State.SUSPENDED:
00110 self._widget.unsuspend_button.setEnabled(True)
00111 elif state == State.COMPLETE:
00112 self._widget.reset_button.setEnabled(True)
00113
00114 if state != State.STOPPED and \
00115 state != State.STOPPING and \
00116 state != State.ABORTED and \
00117 state != State.ABORTING and \
00118 state != State.CLEARING:
00119 self._widget.stop_button.setEnabled(True)
00120
00121
00122 if state != State.ABORTED and \
00123 state != State.ABORTING:
00124 self._widget.abort_button.setEnabled(True)
00125
00126 def update_status_fields(self, msg):
00127 self.update_state_field(msg.state.val)
00128 self._widget.substate.setText(str(msg.sub_state))
00129 self.update_mode_field(msg.mode.val)
00130 self._widget.error_code.setText(str(msg.error))
00131 self._widget.suberror_code.setText(str(msg.sub_error))
00132
00133
00134 def update_state_field(self, state):
00135 if state == State.UNDEFINED:
00136 self._widget.state_name.setText("UNDEFINED")
00137 elif state == State.OFF:
00138 self._widget.state_name.setText("OFF")
00139 elif state == State.STOPPED:
00140 self._widget.state_name.setText("STOPPED")
00141 elif state == State.STARTING:
00142 self._widget.state_name.setText("STARTING")
00143 elif state == State.IDLE:
00144 self._widget.state_name.setText("IDLE")
00145 elif state == State.SUSPENDED:
00146 self._widget.state_name.setText("SUSPENDED")
00147 elif state == State.EXECUTE:
00148 self._widget.state_name.setText("EXECUTE")
00149 elif state == State.STOPPING:
00150 self._widget.state_name.setText("STOPPING")
00151 elif state == State.ABORTING:
00152 self._widget.state_name.setText("ABORTING")
00153 elif state == State.ABORTED:
00154 self._widget.state_name.setText("ABORTED")
00155 elif state == State.HOLDING:
00156 self._widget.state_name.setText("HOLDING")
00157 elif state == State.HELD:
00158 self._widget.state_name.setText("HELD")
00159 elif state == State.RESETTING:
00160 self._widget.state_name.setText("RESETTING")
00161 elif state == State.SUSPENDING:
00162 self._widget.state_name.setText("SUSPENDING")
00163 elif state == State.UNSUSPENDING:
00164 self._widget.state_name.setText("UNSUSPENDING")
00165 elif state == State.CLEARING:
00166 self._widget.state_name.setText("CLEARING")
00167 elif state == State.UNHOLDING:
00168 self._widget.state_name.setText("UNHOLDING")
00169 elif state == State.COMPLETING:
00170 self._widget.state_name.setText("COMPLETING")
00171 elif state == State.COMPLETE:
00172 self._widget.state_name.setText("COMPLETE")
00173 else:
00174 self._widget.state_name.setTest("UNKNOWN")
00175
00176
00177
00178 def update_mode_field(self, mode):
00179 if mode == Mode.UNDEFINED:
00180 self._widget.mode_name.setText("UNDEFINED")
00181 elif mode == Mode.AUTOMATIC:
00182 self._widget.mode_name.setText("AUTOMATIC")
00183 elif mode == Mode.SEMI_AUTOMATIC:
00184 self._widget.mode_name.setText("SEMI-AUTOMATIC")
00185 elif mode == Mode.MANUAL:
00186 self._widget.mode_name.setText("MANUAL")
00187 elif mode == Mode.IDLE:
00188 self._widget.mode_name.setText("IDLE")
00189 elif mode == Mode.SETUP:
00190 self._widget.mode_name.setText("SETUP")
00191 else:
00192 self._widget.mode_name.setText("UNKNOWN")
00193
00194
00195
00196 def wait_for_services(self):
00197 self._widget.setEnabled(False)
00198 transition_service_name = 'packml/transition'
00199 rospy.wait_for_service(transition_service_name, 30)
00200 self.transition_service = rospy.ServiceProxy(transition_service_name, Transition)
00201 self._widget.setEnabled(True)
00202
00203 def shutdown_plugin(self):
00204 self._status_sub.unregister()
00205 pass
00206
00207 def save_settings(self, plugin_settings, instance_settings):
00208
00209
00210 pass
00211
00212 def restore_settings(self, plugin_settings, instance_settings):
00213
00214
00215 pass
00216
00217
00218 def __handle_start_clicked(self, checked):
00219 rospy.loginfo("Start button press")
00220 res = self.transition_service(TransitionRequest.START)
00221 self.set_message_text(res.message)
00222
00223 def __handle_stop_clicked(self, checked):
00224 rospy.loginfo("Stop button press")
00225 res = self.transition_service(TransitionRequest.STOP)
00226 self.set_message_text(res.message)
00227
00228 def __handle_reset_clicked(self, checked):
00229 rospy.loginfo("Reset button press")
00230 res = self.transition_service(TransitionRequest.RESET)
00231 self.set_message_text(res.message)
00232
00233 def __handle_clear_clicked(self, checked):
00234 rospy.loginfo("Clear button press")
00235 res = self.transition_service(TransitionRequest.CLEAR)
00236 self.set_message_text(res.message)
00237
00238 def __handle_hold_clicked(self, checked):
00239 rospy.loginfo("Hold button press")
00240 res = self.transition_service(TransitionRequest.HOLD)
00241 self.set_message_text(res.message)
00242
00243 def __handle_unhold_clicked(self, checked):
00244 rospy.loginfo("Unhold button press")
00245 res = self.transition_service(TransitionRequest.UNHOLD)
00246 self.set_message_text(res.message)
00247
00248 def __handle_suspend_clicked(self, checked):
00249 rospy.loginfo("Suspend button press")
00250 res = self.transition_service(TransitionRequest.SUSPEND)
00251 self.set_message_text(res.message)
00252
00253 def __handle_unsuspend_clicked(self, checked):
00254 rospy.loginfo("Unsuspend button press")
00255 res = self.transition_service(TransitionRequest.UNSUSPEND)
00256 self.set_message_text(res.message)
00257
00258 def __handle_abort_clicked(self, checked):
00259 rospy.loginfo("Abort button press")
00260 res = self.transition_service(TransitionRequest.ABORT)
00261 self.set_message_text(res.message)
00262
00263
00264
00265
00266 @staticmethod
00267 def add_arguments(parser):
00268 rospy.loginfo("Add arguments callback")
00269 group = parser.add_argument_group('Options for PackML plugin')
00270 group.add_argument('--arg1', action='store_true', help='arg1 help')
00271
00272
00273
00274