~shadowrobot/sr-visualization/new_gui

« back to all changes in this revision

Viewing changes to sr_gui_hand_calibration/lib/SrHandCalibration.py

  • Committer: Ugo Cupcic
  • Date: 2012-03-22 08:50:56 UTC
  • mfrom: (0.82.8 new_gui)
  • Revision ID: ugo@shadowrobot.com-20120322085056-k4h51wzxv0evmcwr
Added the new_gui plugins to sr_visualization.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# Copyright 2011 Shadow Robot Company Ltd.
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify it
 
6
# under the terms of the GNU General Public License as published by the Free
 
7
# Software Foundation, either version 2 of the License, or (at your option)
 
8
# any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful, but WITHOUT
 
11
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
12
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
13
# more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License along
 
16
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
#
 
18
 
 
19
from __future__ import division
 
20
import os
 
21
 
 
22
from rosgui.QtBindingHelper import loadUi
 
23
from QtCore import QEvent, QObject, Qt, QTimer, Slot
 
24
from QtGui import QDockWidget, QShortcut, QColor, QTreeWidgetItem, QFileDialog, QMessageBox
 
25
from QtCore import QVariant
 
26
 
 
27
import roslib
 
28
roslib.load_manifest('sr_gui_hand_calibration')
 
29
import rospy
 
30
 
 
31
from sr_hand_calibration_model import HandCalibration
 
32
 
 
33
class SrHandCalibration(QObject):
 
34
 
 
35
    def __init__(self, parent, plugin_context):
 
36
        super(SrHandCalibration, self).__init__(parent)
 
37
        self.setObjectName('SrHandCalibration')
 
38
 
 
39
        self._publisher = None
 
40
        main_window = plugin_context.main_window()
 
41
        self._widget = QDockWidget(main_window)
 
42
 
 
43
        ui_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../uis/SrHandCalibration.ui')
 
44
        loadUi(ui_file, self._widget)
 
45
        self._widget.setObjectName('SrHandCalibrationUi')
 
46
        if plugin_context.serial_number() > 1:
 
47
            self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % plugin_context.serial_number()))
 
48
        main_window.addDockWidget(Qt.RightDockWidgetArea, self._widget)
 
49
 
 
50
        # trigger deleteLater for plugin when _widget is closed
 
51
        self._widget.installEventFilter(self)
 
52
 
 
53
        self._widget.tree_calibration.setColumnCount(4)
 
54
        self._widget.tree_calibration.setHeaderLabels(["Finger", "Joint", "Raw Value", "Calibrated Value"])
 
55
 
 
56
        self.hand_model = None
 
57
 
 
58
        self._widget.btn_save.clicked.connect(self.btn_save_clicked_)
 
59
        self._widget.btn_load.clicked.connect(self.btn_load_clicked_)
 
60
        self._widget.btn_joint_0s.clicked.connect(self.btn_joint_0s_clicked_)
 
61
 
 
62
        self.populate_tree()
 
63
 
 
64
    @Slot(str)
 
65
 
 
66
    def populate_tree(self):
 
67
        self._widget.tree_calibration.clear()
 
68
 
 
69
        self.hand_model = HandCalibration( tree_widget = self._widget.tree_calibration, progress_bar = self._widget.progress )
 
70
        if not self.hand_model.is_active:
 
71
            self.close_plugin()
 
72
            return
 
73
 
 
74
        self._widget.tree_calibration.expandAll()
 
75
 
 
76
        for col in range(0, self._widget.tree_calibration.columnCount()):
 
77
            self._widget.tree_calibration.resizeColumnToContents(col)
 
78
 
 
79
    def btn_save_clicked_(self):
 
80
        path_to_config = "~"
 
81
        try:
 
82
            path_to_config = roslib.packages.get_pkg_dir("sr_robot_lib") + "/config"
 
83
        except:
 
84
            rospy.logwarn("couldnt find the sr_edc_controller_configuration package")
 
85
 
 
86
        filter_files = "*.yaml"
 
87
        filename, _ = QFileDialog.getOpenFileName(self._widget.tree_calibration, self._widget.tr('Save Controller Settings'),
 
88
                                                  self._widget.tr(path_to_config),
 
89
                                                  self._widget.tr(filter_files))
 
90
 
 
91
        if filename == "":
 
92
            return
 
93
 
 
94
        if not self.hand_model.is_calibration_complete():
 
95
            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)",
 
96
                                              buttons = QMessageBox.Ok |  QMessageBox.Cancel)
 
97
 
 
98
            if btn_pressed == QMessageBox.Cancel:
 
99
                return
 
100
        self.hand_model.save( filename )
 
101
 
 
102
    def btn_load_clicked_(self):
 
103
        path_to_config = "~"
 
104
        try:
 
105
            path_to_config = roslib.packages.get_pkg_dir("sr_robot_lib") + "/config"
 
106
        except:
 
107
            rospy.logwarn("couldnt find the sr_edc_controller_configuration package")
 
108
 
 
109
        filter_files = "*.yaml"
 
110
        filename, _ = QFileDialog.getOpenFileName(self._widget.tree_calibration, self._widget.tr('Save Controller Settings'),
 
111
                                                  self._widget.tr(path_to_config),
 
112
                                                  self._widget.tr(filter_files))
 
113
 
 
114
        if filename == "":
 
115
            return
 
116
 
 
117
        self.hand_model.load( filename )
 
118
 
 
119
    def btn_joint_0s_clicked_(self):
 
120
        self.hand_model.calibrate_joint0s( self._widget.btn_joint_0s )
 
121
 
 
122
    def _unregisterPublisher(self):
 
123
        if self._publisher is not None:
 
124
            self._publisher.unregister()
 
125
            self._publisher = None
 
126
 
 
127
        self.hand_model.unregister()
 
128
 
 
129
    def eventFilter(self, obj, event):
 
130
        if obj is self._widget and event.type() == QEvent.Close:
 
131
            # TODO: ignore() should not be necessary when returning True
 
132
            event.ignore()
 
133
            self.deleteLater()
 
134
            return True
 
135
        return QObject.eventFilter(self, obj, event)
 
136
 
 
137
    def close_plugin(self):
 
138
        self._unregisterPublisher()
 
139
        self._widget.close()
 
140
        self._widget.deleteLater()
 
141
 
 
142
    def save_settings(self, global_settings, perspective_settings):
 
143
        print "saving settings"
 
144
        #topic = self._widget.topic_line_edit.text()
 
145
        #perspective_settings.set_value('topic', topic)
 
146
 
 
147
    def restore_settings(self, global_settings, perspective_settings):
 
148
        print "restoring settings"
 
149
        #topic = perspective_settings.value('topic', '/cmd_vel')
 
150