36 from time
import sleep
39 from python_qt_binding
import loadUi
41 from QtWidgets
import QMessageBox, QWidget
42 from QtGui
import QIcon
43 from controller_manager_msgs.srv
import ListControllers
44 from controller_manager_msgs.srv
import SwitchController, LoadController
45 from sr_robot_msgs.srv
import ChangeControlType
46 from sr_robot_msgs.msg
import ControlType
47 from sr_utilities.hand_finder
import HandFinder
53 A rosgui plugin for loading the different controllers 55 ICON_DIR = os.path.join(
56 rospkg.RosPack().get_path(
'sr_visualization_icons'),
'icons')
57 CONTROLLER_ON_ICON = QIcon(os.path.join(ICON_DIR,
'green.png'))
58 CONTROLLER_OFF_ICON = QIcon(os.path.join(ICON_DIR,
'red.png'))
63 hand_joint_prefixes = []
65 if rospy.has_param(
"/hand/mapping"):
66 hand_mapping = rospy.get_param(
"/hand/mapping")
67 for _, value
in hand_mapping.items():
71 self.hand_ids.append(value)
73 self.hand_ids.append(
"")
75 if rospy.has_param(
"/hand/joint_prefix"):
76 hand_joint_prefix_mapping = rospy.get_param(
"/hand/joint_prefix")
77 for _, value
in hand_joint_prefix_mapping.items():
81 hand_joint_prefixes.append(value)
82 if len(hand_joint_prefixes) == 0:
83 QMessageBox.warning(self.
_widget,
"Warning",
84 "No hand found with prefix :" + self.
_prefix)
85 hand_joint_prefixes.append(
"")
87 rospy.loginfo(
"no joint prefix found, not appending prefix")
88 hand_joint_prefixes.append(
"")
90 joints = [
"ffj0",
"ffj3",
"ffj4",
91 "mfj0",
"mfj3",
"mfj4",
92 "rfj0",
"rfj3",
"rfj4",
93 "lfj0",
"lfj3",
"lfj4",
"lfj5",
94 "thj1",
"thj2",
"thj3",
"thj4",
"thj5",
98 "sh_{0}{1}_effort_controller".format(hand_joint_prefix, joint)
99 for joint
in joints
for hand_joint_prefix
in 100 hand_joint_prefixes],
102 "sh_{0}{1}_position_controller".format(hand_joint_prefix,
104 for joint
in joints
for hand_joint_prefix
in 105 hand_joint_prefixes],
107 "sh_{0}{1}_mixed_position_velocity_controller".format(
108 hand_joint_prefix, joint)
109 for joint
in joints
for hand_joint_prefix
in 110 hand_joint_prefixes],
112 "sh_{0}{1}_velocity_controller".format(
113 hand_joint_prefix, joint)
114 for joint
in joints
for hand_joint_prefix
in 115 hand_joint_prefixes],
"stop": []}
118 cont
for type_conts
in self.controllers.itervalues()
119 for cont
in type_conts]
122 super(SrGuiAdvancedControls, self).
__init__(context)
123 self.setObjectName(
'SrGuiAdvancedControls')
129 ui_file = os.path.join(
130 rospkg.RosPack().get_path(
'sr_gui_advanced_controls'),
'uis',
131 'SrAdvancedControls.ui')
133 self._widget.setObjectName(
'SrAdvancedControlsUI')
134 context.add_widget(self.
_widget)
138 hand_parameters = self._hand_finder.get_hand_parameters()
141 for hand
in hand_parameters.mapping:
142 self._widget.select_prefix.addItem(hand_parameters.mapping[hand])
144 if not hand_parameters.mapping:
145 rospy.logerr(
"No hand detected")
147 self.
_widget,
"warning",
"No hand is detected")
149 self._widget.select_prefix.setCurrentIndex(0)
150 self.
_prefix = hand_parameters.mapping.values()[0]
152 self._widget.select_prefix.currentIndexChanged[
'QString'].connect(
159 self._widget.btn_mixed.setChecked(
False)
161 self._widget.btn_effort.setChecked(
False)
163 self._widget.btn_position.setChecked(
False)
165 self._widget.btn_velocity.setChecked(
False)
170 self._widget.btn_position.pressed.connect(
173 self._widget.btn_velocity.pressed.connect(
177 if os.environ.get(
'PWM_CONTROL')
in [
None,
'0']:
178 self._widget.radioButtonTorque.setChecked(
True)
179 self._widget.radioButtonPWM.setChecked(
False)
181 self._widget.radioButtonTorque.setChecked(
False)
182 self._widget.radioButtonPWM.setChecked(
True)
184 self._widget.radioButtonTorque.toggled.connect(
186 self._widget.radioButtonPWM.toggled.connect(
191 Switch between FORCE, PWM modes 192 We only react to the currently ON radio button event 195 change_type_msg = ChangeControlType()
196 if self._widget.radioButtonTorque.isChecked():
197 change_type_msg.control_type = ControlType.FORCE
198 rospy.loginfo(
"Change Control mode to FORCE")
200 change_type_msg.control_type = ControlType.PWM
201 rospy.loginfo(
"Change Control mode to PWM")
208 self._widget.btn_stop.setEnabled(
False)
210 self._widget.btn_mixed.setChecked(
False)
212 self._widget.btn_effort.setChecked(
False)
214 self._widget.btn_position.setChecked(
False)
216 self._widget.btn_velocity.setChecked(
False)
218 self._widget.btn_stop.setEnabled(
True)
222 Effort controller selected 224 self._widget.btn_effort.setEnabled(
False)
225 if not self._widget.btn_effort.isChecked():
227 self._widget.btn_effort.setChecked(
True)
229 self._widget.btn_position.setChecked(
False)
231 self._widget.btn_mixed.setChecked(
False)
233 self._widget.btn_velocity.setChecked(
False)
234 rospy.loginfo(
"Effort checked: " + str(
235 self._widget.btn_effort.isChecked()))
239 self._widget.btn_effort.setChecked(
False)
240 rospy.loginfo(
"Effort checked: " + str(
241 self._widget.btn_effort.isChecked()))
243 self._widget.btn_effort.setEnabled(
True)
247 Position controller selected 249 self._widget.btn_position.setEnabled(
False)
250 if not self._widget.btn_position.isChecked():
252 self._widget.btn_position.setChecked(
True)
254 self._widget.btn_effort.setChecked(
False)
256 self._widget.btn_mixed.setChecked(
False)
258 self._widget.btn_velocity.setChecked(
False)
259 rospy.loginfo(
"Position checked: " +
260 str(self._widget.btn_position.isChecked()))
264 self._widget.btn_position.setChecked(
False)
265 rospy.loginfo(
"Position checked: " +
266 str(self._widget.btn_position.isChecked()))
268 self._widget.btn_position.setEnabled(
True)
272 Mixed controller selected 274 self._widget.btn_mixed.setEnabled(
False)
275 if not self._widget.btn_mixed.isChecked():
277 self._widget.btn_mixed.setChecked(
True)
279 self._widget.btn_effort.setChecked(
False)
281 self._widget.btn_position.setChecked(
False)
283 self._widget.btn_velocity.setChecked(
False)
284 rospy.loginfo(
"Mixed checked: " +
285 str(self._widget.btn_mixed.isChecked()))
289 self._widget.btn_mixed.setChecked(
False)
290 rospy.loginfo(
"Mixed checked: " +
291 str(self._widget.btn_mixed.isChecked()))
293 self._widget.btn_mixed.setEnabled(
True)
297 Velocity controller selected 299 self._widget.btn_velocity.setEnabled(
False)
300 if not self._widget.btn_velocity.isChecked():
302 self._widget.btn_velocity.setChecked(
True)
304 self._widget.btn_effort.setChecked(
False)
306 self._widget.btn_position.setChecked(
False)
308 self._widget.btn_mixed.setChecked(
False)
309 rospy.loginfo(
"Velocity checked: " +
310 str(self._widget.btn_velocity.isChecked()))
314 self._widget.btn_velocity.setChecked(
False)
315 rospy.loginfo(
"Velocity checked: " +
316 str(self._widget.btn_velocity.isChecked()))
318 self._widget.btn_velocity.setEnabled(
True)
322 Switch the current controller 325 list_controllers = rospy.ServiceProxy(
326 'controller_manager/list_controllers', ListControllers)
328 resp1 = list_controllers()
329 except rospy.ServiceException:
333 controllers_to_stop = [
334 c.name
for c
in resp1.controller
336 all_loaded_controllers = [c.name
for c
in resp1.controller]
338 controllers_to_start = self.
controllers[controller]
340 load_controllers =
None 341 for load_control
in controllers_to_start:
342 if load_control
not in all_loaded_controllers:
344 load_controllers = rospy.ServiceProxy(
345 'controller_manager/load_controller',
347 resp1 = load_controllers(load_control)
348 except rospy.ServiceException:
353 switch_controllers = rospy.ServiceProxy(
354 'controller_manager/switch_controller', SwitchController)
356 resp1 = switch_controllers(
357 controllers_to_start, controllers_to_stop,
358 SwitchController._request_class.BEST_EFFORT,
False, 0)
359 except rospy.ServiceException:
367 "Failed to change some of the controllers. " 368 "This is normal if this is not a 5 finger hand.")
372 Calls the service (sr_hand_robot/change_control_type) that allows to 373 tell the driver (sr_robot_lib) 374 which type of force control has to be sent to the motor: 375 - torque demand (sr_robot_msgs::ControlType::FORCE) 376 - PWM (sr_robot_msgs::ControlType::PWM) 377 it will deactivate the Effort, Position, Mixed and Velocity buttons 378 for 3 secs to allow hardware controllers to be updated 382 srv_path =
'sr_hand_robot/' + hand_id +
'/change_control_type' 384 srv_path.replace(
"//",
"/")
385 change_control_type = rospy.ServiceProxy(srv_path,
388 resp1 = change_control_type(chng_type_msg)
389 if resp1.result.control_type != chng_type_msg.control_type:
391 except rospy.ServiceException:
395 self._widget.btn_effort.setEnabled(
False)
396 self._widget.btn_position.setEnabled(
False)
397 self._widget.btn_mixed.setEnabled(
False)
398 self._widget.btn_velocity.setEnabled(
False)
400 self._widget.btn_effort.setEnabled(
True)
401 self._widget.btn_position.setEnabled(
True)
402 self._widget.btn_mixed.setEnabled(
True)
403 self._widget.btn_velocity.setEnabled(
True)
406 QMessageBox.warning(self.
_widget,
"Warning",
407 "Failed to change the control type.")
415 self._publisher.unregister()
def on_stop_ctrl_clicked_(self)
def on_effort_ctrl_clicked_(self)
def _unregisterPublisher(self)
def on_position_ctrl_clicked_(self)
def on_velocity_ctrl_clicked_(self)
def on_mixed_ctrl_clicked_(self)
def __init__(self, context)
def change_force_ctrl_type(self, chng_type_msg)
def shutdown_plugin(self)
def save_settings(self, global_settings, perspective_settings)
def prefix_selected(self, prefix)
def restore_settings(self, global_settings, perspective_settings)
def on_control_mode_radio_button_toggled_(self, checked)
def populate_controllers(self)
def change_ctrl(self, controller)