slam_widget.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # License: BSD
00004 #   https://raw.github.com/robotics-in-concert/rocon_qt_gui/license/LICENSE
00005 #
00006 #
00007 # Imports
00008 #
00009 
00010 from __future__ import division
00011 import os
00012 import math
00013 
00014 from python_qt_binding import loadUi
00015 from python_qt_binding.QtCore import Qt, SIGNAL, pyqtSlot, QPointF
00016 from python_qt_binding.QtGui import QWidget, QPen, QBrush, QColor, QPolygonF, QMatrix, QMessageBox
00017 
00018 from qt_gui.plugin import Plugin
00019 import rospkg
00020 import rospy
00021 import tf
00022 from tf.transformations import quaternion_from_euler
00023 
00024 from rocon_qt_library.interfaces import SlamWidgetInterface
00025 from rocon_qt_library.views import QMapView
00026 
00027 #
00028 # SlamWidget
00029 #
00030 
00031 
00032 class QSlamWidget(QWidget):
00033 
00034     def __init__(self, parent=None):
00035         super(QSlamWidget, self).__init__()
00036         self._parent = parent
00037         self._slam_widget_interface = None
00038 
00039         self._load_ui()
00040         self._init_events()
00041         self.destroyed.connect(self.close)
00042 
00043         self._scene = self.map_view._scene
00044         self._robot_pose = None
00045         self._robot_pose_item = None
00046         self._robot_polygon = QPolygonF([QPointF(-4, 4), QPointF(-4, -4), QPointF(12, 0)])
00047 
00048         self._scan = None
00049         self._scan_items = []
00050 
00051         self._callback={}
00052         self._callback['save_map'] = None
00053 
00054     def _load_ui(self):
00055         rospack = rospkg.RosPack()
00056         ui_file = os.path.join(rospack.get_path('rocon_qt_library'), 'ui', 'slam_widget.ui')
00057         loadUi(ui_file, self, {'QMapView': QMapView})
00058 
00059     def _init_events(self):
00060         self.on_map_received = self.map_view.map_cb
00061         self.save_map_btn.pressed.connect(self.save_map)
00062         self.connect(self, SIGNAL("map_saved"), self.show_saved_map_message)
00063 
00064     def init_slam_widget_interface(self, map_topic, scan_received_slot, scan_topic, robot_pose_received_slot, robot_pose_topic, wc_namespace, map_saved_callbacks):
00065         self._slam_widget_interface = SlamWidgetInterface(map_received_slot=self.on_map_received,
00066                                                           map_topic=map_topic,
00067                                                           scan_received_slot=scan_received_slot,
00068                                                           scan_topic=scan_topic,
00069                                                           robot_pose_received_slot=robot_pose_received_slot,
00070                                                           robot_pose_topic=robot_pose_topic,
00071                                                           wc_namespace=wc_namespace,
00072                                                           map_saved_callbacks=map_saved_callbacks)
00073         self._callback['save_map'] = self._slam_widget_interface.save_map
00074 
00075     def unset_slam_interface(self):
00076         self._slam_widget_interface = None
00077     
00078     def map_saved_callback(self, msg):
00079         try:
00080             self.emit(SIGNAL("map_saved"), msg)
00081         except: 
00082             pass
00083 
00084     def show_saved_map_message(self, rtn):
00085         (success, message) = rtn
00086         if success:
00087             QMessageBox.warning(self, 'SUCCESS', "SAVE!!!!", QMessageBox.Ok | QMessageBox.Ok)           
00088         else:
00089             QMessageBox.warning(self, 'FAIL', "FAIURE CAPTURE[%s]"%str(message), QMessageBox.Ok | QMessageBox.Ok)
00090         self.setDisabled(False)
00091     
00092     def save_map(self):
00093         if self._slam_widget_interface:
00094             self.setDisabled(True)
00095             map_name = str(self.map_name_txt.toPlainText()).lower().replace(' ', '_')
00096             world_name = str(self.world_name_txt.toPlainText()).lower().replace(' ', '_')
00097             self.map_name_txt.setPlainText(map_name)
00098             self.world_name_txt.setPlainText(world_name)
00099             self._callback['save_map'](world=world_name, map_name=map_name)
00100         else:
00101             QMessageBox.warning(self, 'FAIL', "No map has created",QMessageBox.Ok | QMessageBox.Ok)
00102 
00103     @pyqtSlot(list)
00104     def draw_scan(self, data):
00105         old_items = []
00106         if len(self._scan_items):
00107             for item in self._scan_items:
00108                 old_items.append(item)
00109 
00110         for pt in data:
00111             scan_item = self._scene.addRect(pt[0], pt[1], 1, 1, pen=QPen(QColor(0, 255, 0)), brush=QBrush(QColor(0, 255, 0)))
00112             # Everything must be mirrored
00113             self._mirror(scan_item)
00114             self._scan_items.append(scan_item)
00115 
00116         if len(old_items):
00117             for item in old_items:
00118                 self._scene.removeItem(item)
00119                 self._scan_items.remove(item)
00120 
00121     @pyqtSlot(dict)
00122     def draw_robot_pose(self, data):
00123         old_item = None
00124         if self._robot_pose_item:
00125             old_item = self._robot_pose_item
00126 
00127         robot_pose = QMatrix().rotate(data['yaw']).map(self._robot_polygon).translated(data['x'], data['y'])
00128         self._robot_pose_item = self._scene.addPolygon(robot_pose, pen=QPen(QColor(255, 0, 0)), brush=QBrush(QColor(255, 0, 0)))
00129         # Everything must be mirrored
00130         self._mirror(self._robot_pose_item)
00131         if old_item:
00132             self._scene.removeItem(old_item)
00133 
00134     def _mirror(self, item):
00135         item.scale(-1, 1)
00136 
00137     def save_settings(self, plugin_settings, instance_settings):
00138         # TODO add any settings to be saved
00139         pass
00140 
00141     def restore_settings(self, plugin_settings, instance_settings):
00142         # TODO add any settings to be restored
00143         pass


rocon_qt_library
Author(s): Donguk Lee
autogenerated on Fri Feb 12 2016 02:50:13