hand_calibration.py
Go to the documentation of this file.
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 version 2 of the License.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 # more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 
18 import os
19 import rospy
20 import rospkg
21 
22 from qt_gui.plugin import Plugin
23 from PyQt5.uic import loadUi
24 
25 from PyQt5.QtCore import QEvent, QObject, Qt, QTimer, Slot
26 from PyQt5.QtGui import QColor
27 from PyQt5.QtWidgets import QWidget, QShortcut, QTreeWidgetItem, QFileDialog, QMessageBox
28 from PyQt5.QtCore import QVariant
29 from sr_gui_hand_calibration.sr_hand_calibration_model import HandCalibration
30 
31 
33 
34  """
35  A rosgui plugin for calibrating the Shadow EtherCAT Hand
36  """
37 
38  def __init__(self, context):
39  super(SrHandCalibration, self).__init__(context)
40  self.setObjectName('SrHandCalibration')
41 
42  self._publisher = None
43  self._widget = QWidget()
44 
45  ui_file = os.path.join(rospkg.RosPack().get_path(
46  'sr_gui_hand_calibration'), 'uis', 'SrHandCalibration.ui')
47  loadUi(ui_file, self._widget)
48  self._widget.setObjectName('SrHandCalibrationUi')
49  context.add_widget(self._widget)
50 
51  self._widget.tree_calibration.setColumnCount(4)
52  self._widget.tree_calibration.setHeaderLabels(
53  ["Finger", "Joint", "Raw Value", "Calibrated Value"])
54 
55  self.hand_model = None
56 
57  self._widget.btn_save.clicked.connect(self.btn_save_clicked_)
58  self._widget.btn_load.clicked.connect(self.btn_load_clicked_)
59  self._widget.cb_old_version.stateChanged.connect(self.cb_state_changed_)
60  self._widget.btn_joint_0s.clicked.connect(self.btn_joint_0s_clicked_)
61 
62  self.populate_tree()
63 
64  def populate_tree(self, old_version=False):
65  """
66  Create tree with calibrations
67  """
68  self._widget.tree_calibration.clear()
69 
71  tree_widget=self._widget.tree_calibration, progress_bar=self._widget.progress, old_version=old_version)
72  if not self.hand_model.is_active:
73  self.close_plugin()
74  return
75 
76  self._widget.tree_calibration.expandAll()
77 
78  for col in range(0, self._widget.tree_calibration.columnCount()):
79  self._widget.tree_calibration.resizeColumnToContents(col)
80 
81  def btn_save_clicked_(self):
82  """
83  Save calibration to a yaml file.
84  sr_ethercat_hand_config package must be installed
85  """
86  path_to_config = "~"
87  # Reading the param that contains the config_dir suffix that we should
88  # use for this hand (e.g. '' normally for a right hand or 'lh' if this
89  # is for a left hand)
90  config_dir = rospy.get_param('config_dir', '')
91  try:
92  path_to_config = os.path.join(rospkg.RosPack().get_path(
93  'sr_ethercat_hand_config'), 'calibrations', config_dir)
94  except:
95  rospy.logwarn("couldnt find the sr_ethercat_hand_config package")
96 
97  filter_files = "*.yaml"
98  filename, _ = QFileDialog.getOpenFileName(
99  self._widget.tree_calibration, self._widget.tr('Save Calibration'),
100  self._widget.tr(
101  path_to_config),
102  self._widget.tr(filter_files))
103 
104  if filename == "":
105  return
106 
107  if not self.hand_model.is_calibration_complete():
108  btn_pressed = QMessageBox.warning(
109  self._widget.tree_calibration, "Warning", "Are you sure you want to save this incomplete calibration?"
110  " The uncalibrated values will be saved as a flat map (the calibrated value will always be 0)",
111  buttons=QMessageBox.Ok | QMessageBox.Cancel)
112 
113  if btn_pressed == QMessageBox.Cancel:
114  return
115  self.hand_model.save(filename)
116 
117  def btn_load_clicked_(self):
118  """
119  Load calibration from a yaml file.
120  sr_ethercat_hand_config package must be installed
121  """
122  path_to_config = "~"
123  # Reading the param that contains the config_dir suffix that we should
124  # use for this hand (e.g. '' normally for a right hand or 'lh' if this
125  # is for a left hand)
126  config_dir = rospy.get_param('config_dir', '')
127  try:
128  path_to_config = os.path.join(rospkg.RosPack().get_path('sr_ethercat_hand_config'),
129  'calibrations', config_dir)
130  except:
131  rospy.logwarn("couldn't find the sr_ethercat_hand_config package")
132 
133  filter_files = "*.yaml"
134  filename, _ = QFileDialog.getOpenFileName(self._widget.tree_calibration,
135  self._widget.tr('Load Calibration'),
136  self._widget.tr(path_to_config),
137  self._widget.tr(filter_files))
138 
139  if filename == "":
140  return
141 
142  self.hand_model.load(filename)
143 
145  """
146  calibrate the first joint of each finger
147  """
148  self.hand_model.calibrate_joint0s(self._widget.btn_joint_0s)
149 
150  def cb_state_changed_(self):
151  if self._widget.cb_old_version.isChecked():
152  self.populate_tree(old_version=True)
153  else:
154  self.populate_tree(old_version=False)
155 
157  if self._publisher is not None:
158  self._publisher.unregister()
159  self._publisher = None
160 
161  self.hand_model.unregister()
162 
163  def shutdown_plugin(self):
164  self._unregisterPublisher()
165 
166  def save_settings(self, global_settings, perspective_settings):
167  pass
168 
169  def restore_settings(self, global_settings, perspective_settings):
170  pass
def save_settings(self, global_settings, perspective_settings)
def restore_settings(self, global_settings, perspective_settings)


sr_gui_hand_calibration
Author(s): Ugo Cupcic
autogenerated on Wed Oct 14 2020 03:22:44