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, SwitchControllerRequest, 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():
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(
158 'controller_manager/list_controllers', ListControllers)
160 'controller_manager/switch_controller', SwitchController)
164 self.
_widget.btn_mixed.setChecked(
False)
166 self.
_widget.btn_effort.setChecked(
False)
168 self.
_widget.btn_position.setChecked(
False)
170 self.
_widget.btn_velocity.setChecked(
False)
175 self.
_widget.btn_position.pressed.connect(
178 self.
_widget.btn_velocity.pressed.connect(
182 if os.environ.get(
'PWM_CONTROL')
in [
None,
'0']:
183 self.
_widget.radioButtonTorque.setChecked(
True)
184 self.
_widget.radioButtonPWM.setChecked(
False)
186 self.
_widget.radioButtonTorque.setChecked(
False)
187 self.
_widget.radioButtonPWM.setChecked(
True)
189 self.
_widget.radioButtonTorque.toggled.connect(
191 self.
_widget.radioButtonPWM.toggled.connect(
196 Switch between FORCE, PWM modes 197 We only react to the currently ON radio button event 200 change_type_msg = ChangeControlType()
201 if self.
_widget.radioButtonTorque.isChecked():
202 change_type_msg.control_type = ControlType.FORCE
203 rospy.loginfo(
"Change Control mode to FORCE")
205 change_type_msg.control_type = ControlType.PWM
206 rospy.loginfo(
"Change Control mode to PWM")
213 self.
_widget.btn_stop.setEnabled(
False)
215 self.
_widget.btn_mixed.setChecked(
False)
217 self.
_widget.btn_effort.setChecked(
False)
219 self.
_widget.btn_position.setChecked(
False)
221 self.
_widget.btn_velocity.setChecked(
False)
223 self.
_widget.btn_stop.setEnabled(
True)
227 Effort controller selected 229 self.
_widget.btn_effort.setEnabled(
False)
230 if not self.
_widget.btn_effort.isChecked():
232 self.
_widget.btn_effort.setChecked(
True)
234 self.
_widget.btn_position.setChecked(
False)
236 self.
_widget.btn_mixed.setChecked(
False)
238 self.
_widget.btn_velocity.setChecked(
False)
239 rospy.loginfo(
"Effort checked: " + str(
240 self.
_widget.btn_effort.isChecked()))
244 self.
_widget.btn_effort.setChecked(
False)
245 rospy.loginfo(
"Effort checked: " + str(
246 self.
_widget.btn_effort.isChecked()))
248 self.
_widget.btn_effort.setEnabled(
True)
252 Position controller selected 254 self.
_widget.btn_position.setEnabled(
False)
255 if not self.
_widget.btn_position.isChecked():
257 self.
_widget.btn_position.setChecked(
True)
259 self.
_widget.btn_effort.setChecked(
False)
261 self.
_widget.btn_mixed.setChecked(
False)
263 self.
_widget.btn_velocity.setChecked(
False)
264 rospy.loginfo(
"Position checked: " +
265 str(self.
_widget.btn_position.isChecked()))
269 self.
_widget.btn_position.setChecked(
False)
270 rospy.loginfo(
"Position checked: " +
271 str(self.
_widget.btn_position.isChecked()))
273 self.
_widget.btn_position.setEnabled(
True)
277 Mixed controller selected 279 self.
_widget.btn_mixed.setEnabled(
False)
280 if not self.
_widget.btn_mixed.isChecked():
282 self.
_widget.btn_mixed.setChecked(
True)
284 self.
_widget.btn_effort.setChecked(
False)
286 self.
_widget.btn_position.setChecked(
False)
288 self.
_widget.btn_velocity.setChecked(
False)
289 rospy.loginfo(
"Mixed checked: " +
290 str(self.
_widget.btn_mixed.isChecked()))
294 self.
_widget.btn_mixed.setChecked(
False)
295 rospy.loginfo(
"Mixed checked: " +
296 str(self.
_widget.btn_mixed.isChecked()))
298 self.
_widget.btn_mixed.setEnabled(
True)
302 Velocity controller selected 304 self.
_widget.btn_velocity.setEnabled(
False)
305 if not self.
_widget.btn_velocity.isChecked():
307 self.
_widget.btn_velocity.setChecked(
True)
309 self.
_widget.btn_effort.setChecked(
False)
311 self.
_widget.btn_position.setChecked(
False)
313 self.
_widget.btn_mixed.setChecked(
False)
314 rospy.loginfo(
"Velocity checked: " +
315 str(self.
_widget.btn_velocity.isChecked()))
319 self.
_widget.btn_velocity.setChecked(
False)
320 rospy.loginfo(
"Velocity checked: " +
321 str(self.
_widget.btn_velocity.isChecked()))
323 self.
_widget.btn_velocity.setEnabled(
True)
327 Switch the current controller 333 except rospy.ServiceException:
337 controllers_to_stop = [
338 c.name
for c
in resp1.controller
340 all_loaded_controllers = [c.name
for c
in resp1.controller]
342 controllers_to_start = self.
controllers[controller]
344 load_controllers =
None 345 for load_control
in controllers_to_start:
346 if load_control
not in all_loaded_controllers:
348 load_controllers = rospy.ServiceProxy(
349 'controller_manager/load_controller',
351 resp1 = load_controllers(load_control)
354 except rospy.ServiceException:
357 req = SwitchControllerRequest()
358 req.start_controllers = controllers_to_start
359 req.stop_controllers = controllers_to_stop
360 req.strictness = SwitchControllerRequest.BEST_EFFORT
366 except rospy.ServiceException:
374 "Failed to change some of the controllers. " 375 "This is normal if this is not a 5 finger hand.")
379 Calls the service (sr_hand_robot/change_control_type) that allows to 380 tell the driver (sr_robot_lib) 381 which type of force control has to be sent to the motor: 382 - torque demand (sr_robot_msgs::ControlType::FORCE) 383 - PWM (sr_robot_msgs::ControlType::PWM) 384 it will deactivate the Effort, Position, Mixed and Velocity buttons 385 for 3 secs to allow hardware controllers to be updated 389 srv_path =
'sr_hand_robot/' + hand_id +
'/change_control_type' 391 srv_path.replace(
"//",
"/")
392 change_control_type = rospy.ServiceProxy(srv_path,
395 resp1 = change_control_type(chng_type_msg)
396 if resp1.result.control_type != chng_type_msg.control_type:
398 except rospy.ServiceException:
402 self.
_widget.btn_effort.setEnabled(
False)
403 self.
_widget.btn_position.setEnabled(
False)
404 self.
_widget.btn_mixed.setEnabled(
False)
405 self.
_widget.btn_velocity.setEnabled(
False)
407 self.
_widget.btn_effort.setEnabled(
True)
408 self.
_widget.btn_position.setEnabled(
True)
409 self.
_widget.btn_mixed.setEnabled(
True)
410 self.
_widget.btn_velocity.setEnabled(
True)
413 QMessageBox.warning(self.
_widget,
"Warning",
414 "Failed to change the control type.")
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)