hil_plugin.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import os
00003 import rospy
00004 import rospkg
00005 
00006 from mavros_msgs.msg import State
00007 from mavros_msgs.srv import CommandBool
00008 from mavros_msgs.srv import CommandLong
00009 from mavros_msgs.srv import SetMode
00010 
00011 from qt_gui.plugin import Plugin
00012 from python_qt_binding import loadUi
00013 from python_qt_binding import QtCore
00014 from python_qt_binding.QtCore import QTimer, Slot
00015 from python_qt_binding.QtGui import QWidget, QFormLayout
00016 
00017 import time
00018 
00019 class HilPlugin(Plugin):
00020     # MAV mode flags
00021     MAV_MODE_FLAG_SAFETY_ARMED = 128
00022     MAV_MODE_FLAG_MANUAL_INPUT_ENABLED = 64
00023     MAV_MODE_FLAG_HIL_ENABLED = 32
00024     MAV_MODE_FLAG_STABILIZE_ENABLED = 16
00025     MAV_MODE_FLAG_GUIDED_ENABLED = 8
00026     MAV_MODE_FLAG_AUTO_ENABLED = 4
00027     MAV_MODE_FLAG_TEST_ENABLED = 2
00028     MAV_MODE_FLAG_CUSTOM_MODE_ENABLED = 1
00029 
00030     # MAV state dictionary
00031     mav_state = {0: 'Uninitialized',
00032     1: 'Booting up',
00033     2: 'Calibrating',
00034     3: 'Standby',
00035     4: 'Active',
00036     5: 'Critical',
00037     6: 'Emergency',
00038     7: 'Poweroff'}
00039 
00040     # Constants
00041     STR_ON = 'ON'
00042     STR_OFF = 'OFF'
00043     STR_UNKNOWN = 'N/A'
00044 
00045     STR_MAVROS_ARM_SERVICE_NAME = '/mavros/cmd/arming'
00046     STR_MAVROS_COMMAND_LONG_SERVICE_NAME = '/mavros/cmd/command'
00047     STR_MAVROS_SET_MODE_SERVICE_NAME = '/mavros/set_mode'
00048 
00049     STR_SYS_STATUS_SUB_TOPIC = '/mavros/state'
00050 
00051     TIMEOUT_HIL_HEARTBEAT = 2.0
00052 
00053     def __init__(self, context):
00054         super(HilPlugin, self).__init__(context)
00055         self.setObjectName('HilPlugin')
00056 
00057         self._widget = QWidget()
00058         rp = rospkg.RosPack()
00059         ui_file = os.path.join(rospkg.RosPack().get_path('rqt_rotors'), 'resource', 'HilPlugin.ui')
00060         loadUi(ui_file, self._widget)
00061         self._widget.setObjectName('HilPluginUi')
00062 
00063         if context.serial_number() > 1:
00064             self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % context.serial_number()))
00065 
00066         context.add_widget(self._widget)
00067 
00068         # Set the initial parameters of UI elements
00069         self._widget.button_set_hil_mode.setEnabled(False)
00070         self._widget.button_arm.setEnabled(False)
00071         self._widget.button_reboot_autopilot.setEnabled(False)
00072         self._widget.text_state.setText(self.STR_UNKNOWN)
00073         self.clear_mav_mode()
00074 
00075         # Initialize class variables
00076         self.last_heartbeat_time = time.time()
00077         self.mav_mode = 65
00078         self.mav_status = 255
00079         self.armed = False
00080         self.connected = False
00081         self.guided = False
00082         self.hil_enabled = False
00083         
00084         # Set the functions that are called when signals are emitted
00085         self._widget.button_set_hil_mode.pressed.connect(self.on_set_hil_mode_button_pressed)
00086         self._widget.button_arm.pressed.connect(self.on_arm_button_pressed)
00087         self._widget.button_reboot_autopilot.pressed.connect(self.on_reboot_autopilot_button_pressed)
00088 
00089         # Create ROS service proxies
00090         self.arm = rospy.ServiceProxy(self.STR_MAVROS_ARM_SERVICE_NAME, CommandBool)
00091         self.send_command_long = rospy.ServiceProxy(self.STR_MAVROS_COMMAND_LONG_SERVICE_NAME, CommandLong)
00092         self.set_mode = rospy.ServiceProxy(self.STR_MAVROS_SET_MODE_SERVICE_NAME, SetMode)
00093         
00094         # Initialize ROS subscribers and publishers
00095         self.sys_status_sub = rospy.Subscriber(self.STR_SYS_STATUS_SUB_TOPIC, State, self.sys_status_callback, queue_size=1)
00096 
00097     def on_set_hil_mode_button_pressed(self):
00098         new_mode = self.mav_mode | self.MAV_MODE_FLAG_HIL_ENABLED
00099         self.hil_enabled = True
00100         self.mav_mode = new_mode
00101         self.set_mode(new_mode, '')
00102         self._widget.text_mode_hil.setText(self.mav_mode_text(self.hil_enabled))
00103 
00104     def on_arm_button_pressed(self):
00105         self.arm(True)
00106 
00107     def on_reboot_autopilot_button_pressed(self):
00108         self.send_command_long(False, 246, 1, 1, 0, 0, 0, 0, 0, 0)
00109     
00110     def sys_status_callback(self, msg):
00111         if (not self.connected and msg.connected):
00112             self._widget.button_set_hil_mode.setEnabled(True)
00113             self._widget.button_arm.setEnabled(True)
00114             self._widget.button_reboot_autopilot.setEnabled(True)
00115             self.connected = True
00116             self.last_heartbeat_time = time.time()
00117             self._widget.text_mode_safety_armed.setText(self.mav_mode_text(msg.armed))
00118             self._widget.text_mode_guided.setText(self.mav_mode_text(msg.guided))
00119             return
00120 
00121         if (((time.time() - self.last_heartbeat_time) >= self.TIMEOUT_HIL_HEARTBEAT) and self.hil_enabled):
00122             new_mode = self.mav_mode | self.MAV_MODE_FLAG_HIL_ENABLED
00123             self.set_mode(new_mode, '')
00124 
00125         if (self.armed != msg.armed):
00126             self.armed = msg.armed
00127             self._widget.text_mode_safety_armed.setText(self.mav_mode_text(self.armed))
00128             self._widget.button_arm.setEnabled(not(self.armed))
00129             self.mav_mode = self.mav_mode | self.MAV_MODE_FLAG_SAFETY_ARMED
00130 
00131         if (self.guided != msg.guided):
00132             self.guided = msg.guided
00133             self._widget.text_mode_guided.setText(self.mav_mode_text(self.guided))
00134 
00135         self.last_heartbeat_time = time.time()
00136 
00137     def clear_mav_mode(self):
00138         count = self._widget.mav_mode_layout.rowCount()
00139         for i in range(count):
00140             self._widget.mav_mode_layout.itemAt(i, QFormLayout.FieldRole).widget().setText(self.STR_UNKNOWN)
00141 
00142     def mav_mode_text(self, mode_enabled):
00143         return self.STR_ON if mode_enabled else self.STR_OFF
00144 
00145     def shutdown_plugin(self):
00146         if self.sys_status_sub is not None:
00147             self.sys_status_sub.unregister()


rqt_rotors
Author(s): Pavel Vechersky
autogenerated on Thu Apr 18 2019 02:43:54