change_controllers.py
Go to the documentation of this file.
1 # Copyright (c) 2011, Dirk Thomas, TU Darmstadt
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
6 # are met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following
12 # disclaimer in the documentation and/or other materials provided
13 # with the distribution.
14 # * Neither the name of the TU Darmstadt nor the names of its
15 # contributors may be used to endorse or promote products derived
16 # from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
30 
31 
32 import os
33 import rospy
34 import rospkg
35 
36 from qt_gui.plugin import Plugin
37 from python_qt_binding import loadUi
38 
39 from QtCore import QEvent, QObject, Qt, QTimer, Slot
40 from QtGui import QIcon
41 from QtWidgets import QShortcut, QMessageBox, QWidget
42 from controller_manager_msgs.srv import ListControllers, SwitchController, LoadController
43 from sr_robot_msgs.srv import ChangeControlType
44 from sr_robot_msgs.msg import ControlType
45 
46 
48 
49  """
50  A rosgui plugin for loading the different controllers
51  """
52  ICON_DIR = os.path.join(
53  rospkg.RosPack().get_path('sr_visualization_icons'), 'icons')
54  CONTROLLER_ON_ICON = QIcon(os.path.join(ICON_DIR, 'green.png'))
55  CONTROLLER_OFF_ICON = QIcon(os.path.join(ICON_DIR, 'red.png'))
56 
57  controllers = {
58  "valve": ["sh_ffj0_muscle_valve_controller",
59  "sh_ffj3_muscle_valve_controller",
60  "sh_ffj4_muscle_valve_controller",
61  "sh_mfj0_muscle_valve_controller",
62  "sh_mfj3_muscle_valve_controller",
63  "sh_mfj4_muscle_valve_controller",
64  "sh_rfj0_muscle_valve_controller",
65  "sh_rfj3_muscle_valve_controller",
66  "sh_rfj4_muscle_valve_controller",
67  "sh_lfj0_muscle_valve_controller",
68  "sh_lfj3_muscle_valve_controller",
69  "sh_lfj4_muscle_valve_controller",
70  "sh_lfj5_muscle_valve_controller",
71  "sh_thj1_muscle_valve_controller",
72  "sh_thj2_muscle_valve_controller",
73  "sh_thj3_muscle_valve_controller",
74  "sh_thj4_muscle_valve_controller",
75  "sh_thj5_muscle_valve_controller",
76  "sh_wrj1_muscle_valve_controller",
77  "sh_wrj2_muscle_valve_controller"],
78  "position": ["sh_ffj0_muscle_position_controller",
79  "sh_ffj3_muscle_position_controller",
80  "sh_ffj4_muscle_position_controller",
81  "sh_mfj0_muscle_position_controller",
82  "sh_mfj3_muscle_position_controller",
83  "sh_mfj4_muscle_position_controller",
84  "sh_rfj0_muscle_position_controller",
85  "sh_rfj3_muscle_position_controller",
86  "sh_rfj4_muscle_position_controller",
87  "sh_lfj0_muscle_position_controller",
88  "sh_lfj3_muscle_position_controller",
89  "sh_lfj4_muscle_position_controller",
90  "sh_lfj5_muscle_position_controller",
91  "sh_thj1_muscle_position_controller",
92  "sh_thj2_muscle_position_controller",
93  "sh_thj3_muscle_position_controller",
94  "sh_thj4_muscle_position_controller",
95  "sh_thj5_muscle_position_controller",
96  "sh_wrj1_muscle_position_controller",
97  "sh_wrj2_muscle_position_controller"],
98  "stop": []}
99 
100  def __init__(self, context):
101  super(SrGuiChangeControllers, self).__init__(context)
102  self.setObjectName('SrGuiChangeControllers')
103 
104  self._publisher = None
105  self._widget = QWidget()
106 
107  ui_file = os.path.join(rospkg.RosPack().get_path(
108  'sr_gui_change_muscle_controllers'), 'uis', 'SrChangeControllers.ui')
109  loadUi(ui_file, self._widget)
110  self._widget.setObjectName('SrChangeControllersUi')
111  context.add_widget(self._widget)
112 
113  # Setting the initial state of the controller buttons
114  self._widget.btn_valve.setIcon(self.CONTROLLER_OFF_ICON)
115  self._widget.btn_valve.setChecked(False)
116  self._widget.btn_position.setIcon(self.CONTROLLER_OFF_ICON)
117  self._widget.btn_position.setChecked(False)
118 
119  # attaching the button press event to their actions
120  self._widget.btn_stop.pressed.connect(self.on_stop_ctrl_clicked_)
121  self._widget.btn_valve.pressed.connect(self.on_valve_ctrl_clicked_)
122  self._widget.btn_position.pressed.connect(
124 
126  """
127  Stop the controller
128  """
129  self._widget.btn_stop.setEnabled(False)
130  self._widget.btn_valve.setIcon(self.CONTROLLER_OFF_ICON)
131  self._widget.btn_valve.setChecked(False)
132  self._widget.btn_position.setIcon(self.CONTROLLER_OFF_ICON)
133  self._widget.btn_position.setChecked(False)
134  self.change_ctrl("stop")
135  self._widget.btn_stop.setEnabled(True)
136 
138  """
139  Switch to valve control
140  """
141  self._widget.btn_valve.setEnabled(False)
142  if not self._widget.btn_valve.isChecked():
143  self._widget.btn_valve.setIcon(self.CONTROLLER_ON_ICON)
144  self._widget.btn_valve.setChecked(True)
145  self._widget.btn_position.setIcon(self.CONTROLLER_OFF_ICON)
146  self._widget.btn_position.setChecked(False)
147  rospy.loginfo("Valve checked: " + str(
148  self._widget.btn_valve.isChecked()))
149  self.change_ctrl("valve")
150  else:
151  self._widget.btn_valve.setIcon(self.CONTROLLER_OFF_ICON)
152  self._widget.btn_valve.setChecked(False)
153  rospy.loginfo("Valve checked: " + str(
154  self._widget.btn_valve.isChecked()))
155  self.change_ctrl("stop")
156  self._widget.btn_valve.setEnabled(True)
157 
159  """
160  Switch to position control
161  """
162  self._widget.btn_position.setEnabled(False)
163  if not self._widget.btn_position.isChecked():
164  self._widget.btn_position.setIcon(self.CONTROLLER_ON_ICON)
165  self._widget.btn_position.setChecked(True)
166  self._widget.btn_valve.setIcon(self.CONTROLLER_OFF_ICON)
167  self._widget.btn_valve.setChecked(False)
168  rospy.loginfo("Position checked: " + str(
169  self._widget.btn_position.isChecked()))
170  self.change_ctrl("position")
171  else:
172  self._widget.btn_position.setIcon(self.CONTROLLER_OFF_ICON)
173  self._widget.btn_position.setChecked(False)
174  rospy.loginfo("Position checked: " + str(
175  self._widget.btn_position.isChecked()))
176  self.change_ctrl("stop")
177  self._widget.btn_position.setEnabled(True)
178 
179  def change_ctrl(self, controller):
180  """
181  Switch controller type
182  """
183  success = True
184  list_controllers = rospy.ServiceProxy(
185  'controller_manager/list_controllers', ListControllers)
186  try:
187  resp1 = list_controllers()
188  except rospy.ServiceException:
189  success = False
190 
191  if success:
192  current_controllers = [
193  c.name for c in resp1.controller if c.state == "running"]
194  all_loaded_controllers = [c.name for c in resp1.controller]
195 
196  controllers_to_start = self.controllers[controller]
197  controllers_to_start.append('joint_state_controller')
198 
199  load_controllers = rospy.ServiceProxy(
200  'controller_manager/load_controller', LoadController)
201  for load_control in controllers_to_start:
202  if load_control not in all_loaded_controllers:
203  try:
204  resp1 = load_controllers(load_control)
205  except rospy.ServiceException:
206  success = False
207  if not resp1.ok:
208  success = False
209 
210  switch_controllers = rospy.ServiceProxy(
211  'controller_manager/switch_controller', SwitchController)
212  try:
213  resp1 = switch_controllers(
214  controllers_to_start, current_controllers, SwitchController._request_class.BEST_EFFORT, False, 0)
215  except rospy.ServiceException:
216  success = False
217 
218  if not resp1.ok:
219  success = False
220 
221  if not success:
222  rospy.logwarn(
223  "Failed to change some of the controllers. This is normal if this is not a 5 finger hand.")
224 
226  if self._publisher is not None:
227  self._publisher.unregister()
228  self._publisher = None
229 
230  def shutdown_plugin(self):
231  self._unregisterPublisher()
232 
233  def save_settings(self, global_settings, perspective_settings):
234  pass
235 
236  def restore_settings(self, global_settings, perspective_settings):
237  pass


sr_gui_change_muscle_controllers
Author(s): Ugo Cupcic
autogenerated on Wed Oct 14 2020 03:22:48