hand_calibration.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2011 Shadow Robot Company Ltd.
00004 #
00005 # This program is free software: you can redistribute it and/or modify it
00006 # under the terms of the GNU General Public License as published by the Free
00007 # Software Foundation, either version 2 of the License, or (at your option)
00008 # any later version.
00009 #
00010 # This program is distributed in the hope that it will be useful, but WITHOUT
00011 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00013 # more details.
00014 #
00015 # You should have received a copy of the GNU General Public License along
00016 # with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 #
00018 
00019 import os, rospy, rospkg
00020 
00021 from qt_gui.plugin import Plugin
00022 from python_qt_binding import loadUi
00023 
00024 from QtCore import QEvent, QObject, Qt, QTimer, Slot
00025 from QtGui import QWidget, QShortcut, QColor, QTreeWidgetItem, QFileDialog, QMessageBox
00026 from QtCore import QVariant
00027 from sr_gui_hand_calibration.sr_hand_calibration_model import HandCalibration
00028 
00029 class SrHandCalibration(Plugin):
00030     """
00031     A rosgui plugin for calibrating the Shadow EtherCAT Hand
00032     """
00033 
00034     def __init__(self, context):
00035         super(SrHandCalibration, self).__init__(context)
00036         self.setObjectName('SrHandCalibration')
00037 
00038         self._publisher = None
00039         self._widget = QWidget()
00040 
00041         ui_file = os.path.join(rospkg.RosPack().get_path('sr_gui_hand_calibration'), 'uis', 'SrHandCalibration.ui')
00042         loadUi(ui_file, self._widget)
00043         self._widget.setObjectName('SrHandCalibrationUi')
00044         context.add_widget(self._widget)
00045 
00046         self._widget.tree_calibration.setColumnCount(4)
00047         self._widget.tree_calibration.setHeaderLabels(["Finger", "Joint", "Raw Value", "Calibrated Value"])
00048 
00049         self.hand_model = None
00050 
00051         self._widget.btn_save.clicked.connect(self.btn_save_clicked_)
00052         self._widget.btn_load.clicked.connect(self.btn_load_clicked_)
00053         self._widget.btn_joint_0s.clicked.connect(self.btn_joint_0s_clicked_)
00054 
00055         self.populate_tree()
00056 
00057     def populate_tree(self):
00058         """
00059         Create tree with calibrations
00060         """
00061         self._widget.tree_calibration.clear()
00062 
00063         self.hand_model = HandCalibration( tree_widget = self._widget.tree_calibration, progress_bar = self._widget.progress )
00064         if not self.hand_model.is_active:
00065             self.close_plugin()
00066             return
00067 
00068         self._widget.tree_calibration.expandAll()
00069 
00070         for col in range(0, self._widget.tree_calibration.columnCount()):
00071             self._widget.tree_calibration.resizeColumnToContents(col)
00072 
00073     def btn_save_clicked_(self):
00074         """
00075         Save calibration to a yaml file.
00076         sr_ethercat_hand_config package must be installed
00077         """
00078         path_to_config = "~"
00079         #Reading the param that contains the config_dir suffix that we should use for this hand (e.g. '' normally for a right hand  or 'lh' if this is for a left hand)
00080         config_dir = rospy.get_param('config_dir', '')
00081         try:
00082             path_to_config = os.path.join(rospkg.RosPack().get_path('sr_ethercat_hand_config'), 'calibrations', config_dir)
00083         except:
00084             rospy.logwarn("couldnt find the sr_ethercat_hand_config package")
00085 
00086         filter_files = "*.yaml"
00087         filename, _ = QFileDialog.getOpenFileName(self._widget.tree_calibration, self._widget.tr('Save Calibration'),
00088                                                   self._widget.tr(path_to_config),
00089                                                   self._widget.tr(filter_files))
00090 
00091         if filename == "":
00092             return
00093 
00094         if not self.hand_model.is_calibration_complete():
00095             btn_pressed = QMessageBox.warning(self._widget.tree_calibration, "Warning", "Are you sure you want to save this incomplete calibration? The uncalibrated values will be saved as a flat map (the calibrated value will always be 0)",
00096                                               buttons = QMessageBox.Ok |  QMessageBox.Cancel)
00097 
00098             if btn_pressed == QMessageBox.Cancel:
00099                 return
00100         self.hand_model.save( filename )
00101 
00102     def btn_load_clicked_(self):
00103         """
00104         Load calibration from a yaml file.
00105         sr_ethercat_hand_config package must be installed
00106         """
00107         path_to_config = "~"
00108         #Reading the param that contains the config_dir suffix that we should use for this hand (e.g. '' normally for a right hand  or 'lh' if this is for a left hand)
00109         config_dir = rospy.get_param('config_dir', '')
00110         try:
00111             path_to_config = os.path.join(rospkg.RosPack().get_path('sr_ethercat_hand_config'), 'calibrations', config_dir)
00112         except:
00113             rospy.logwarn("couldn't find the sr_ethercat_hand_config package")
00114 
00115         filter_files = "*.yaml"
00116         filename, _ = QFileDialog.getOpenFileName(self._widget.tree_calibration, self._widget.tr('Load Calibration'),
00117                                                   self._widget.tr(path_to_config),
00118                                                   self._widget.tr(filter_files))
00119 
00120         if filename == "":
00121             return
00122 
00123         self.hand_model.load( filename )
00124 
00125     def btn_joint_0s_clicked_(self):
00126         """
00127         calibrate the first joint of each finger
00128         """
00129         self.hand_model.calibrate_joint0s( self._widget.btn_joint_0s )
00130 
00131     def _unregisterPublisher(self):
00132         if self._publisher is not None:
00133             self._publisher.unregister()
00134             self._publisher = None
00135 
00136         self.hand_model.unregister()
00137 
00138     def shutdown_plugin(self):
00139         self._unregisterPublisher()
00140 
00141     def save_settings(self, global_settings, perspective_settings):
00142         pass
00143 
00144     def restore_settings(self, global_settings, perspective_settings):
00145         pass


sr_gui_hand_calibration
Author(s): Ugo Cupcic
autogenerated on Fri Aug 28 2015 13:17:17