00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 from __future__ import division
00032 import os
00033 import rospkg
00034 import threading
00035 
00036 import rospy
00037 from python_qt_binding import loadUi
00038 from python_qt_binding.QtCore import Qt, QTimer, Slot, QBasicTimer, SIGNAL
00039 from python_qt_binding.QtGui import QKeySequence, QShortcut, QWidget, QPixmap, QMessageBox, QStandardItemModel,QStandardItem
00040 from rqt_gui_py.plugin import Plugin
00041 
00042 import time
00043 import math
00044 
00045 from bhand_controller.msg import State, TactileArray, Service
00046 from bhand_controller.srv import Actions, SetControlMode
00047 from sensor_msgs.msg import JointState
00048 from rospy.exceptions import ROSException
00049 
00050 
00051 max_base_spread = 3.14
00052 max_finger_spread = 2.44
00053 
00054 MAX_VELOCITY = 0.1 
00055 BHAND_VELOCITY_COMMANDS_FREQ = 50 
00056 
00057 class BHandGUI(Plugin):
00058         
00059         def __init__(self, context):
00060                 super(BHandGUI, self).__init__(context)
00061                 self.setObjectName('BHandGUI')
00062                 
00063                 self.read_ros_params()
00064                 self._publisher = None
00065 
00066                 self._widget = QWidget()
00067                 
00068                 
00069                 self._bhand_data = State()
00070                 self._joint_data = JointState()
00071                 self._tact_data = None
00072                         
00073                 rp = rospkg.RosPack()
00074                 
00075                 
00076                 self.enable_commands = False
00077                 
00078                 
00079                 
00080                 
00081                 self.base_spread = 0.0
00082                 self.finger1_spread = 0.0
00083                 self.finger2_spread = 0.0
00084                 self.finger3_spread = 0.0
00085                 
00086                 self.base_spread_vel = 0.0
00087                 self.finger1_spread_vel = 0.0
00088                 self.finger2_spread_vel = 0.0
00089                 self.finger3_spread_vel = 0.0
00090                 self.max_vel = MAX_VELOCITY
00091                 self.vel_factor = self.max_vel/100.0 
00092                 
00093                 self.red_string = "background-color: rgb(255,0,0)"
00094                 self.orange_string = "background-color: rgb(255,128,0)"
00095                 self.yellow_string = "background-color: rgb(255,255,0)"
00096                 self.green_string = "background-color: rgb(128,255,0)"
00097                 self.black_string = "background-color: rgb(0,0,0)"
00098                 
00099                 self.palm_factor = max_base_spread/99
00100                 self.finger_factor = max_finger_spread/99
00101                 
00102                 self.state_string = " "
00103                 
00104                 
00105                 
00106                 ui_file = os.path.join(rp.get_path('rqt_bhand'), 'resource', 'BHandGUI.ui')
00107                 loadUi(ui_file, self._widget)
00108                 self._widget.setObjectName('BHandGUI')
00109                 
00110                 pixmap_red_file = os.path.join(rp.get_path('rqt_bhand'), 'resource', 'red.png')
00111                 pixmap_green_file = os.path.join(rp.get_path('rqt_bhand'), 'resource', 'green.png')
00112                 self._pixmap_red = QPixmap(pixmap_red_file)
00113                 self._pixmap_green = QPixmap(pixmap_green_file)
00114                 self._widget.qlabel_state_connection.setPixmap(self._pixmap_red) 
00115                 self._widget.qlabel_jointstate_connection.setPixmap(self._pixmap_red) 
00116                 
00117                                 
00118                 if context.serial_number() > 1:
00119                         self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % context.serial_number()))           
00120                 
00121                 
00122                 context.add_widget(self._widget)
00123 
00124                 
00125                 self._topic = '/%s/state'%self.bhand_node_name 
00126                 self._joint_states_topic = '/joint_states'
00127                 self._tact_topic = '/%s/tact_array'%self.bhand_node_name 
00128                 self._command_topic = '/%s/command'%self.bhand_node_name 
00129                 self._set_mode_service_name = '/%s/set_control_mode'%self.bhand_node_name
00130                 self._actions_service_name = '/%s/actions'%self.bhand_node_name
00131                  
00132                 
00133                 self.desired_ref = JointState()
00134                 
00135                 
00136                 
00137                 
00138                 
00139                 
00140                 
00141                 self.joint_state_pointer = {} 
00142                 for i in range(len(self.joint_ids)):
00143                         self.joint_state_pointer[self.joint_ids[i]] = {'joint': self.joint_names[i], 'values': [0, 0, 0]}
00144                         
00145                         self._joint_data.name.append(self.joint_names[i])
00146                         self._joint_data.position.append(0.0)
00147                         self._joint_data.velocity.append(0.0)
00148                         self._joint_data.effort.append(0.0)
00149                 
00150                 
00151                 try:
00152                         self._subscriber = rospy.Subscriber(self._topic, State, self._receive_state_data)
00153                 except ValueError, e:
00154                         rospy.logerr('BHandGUI: Error connecting topic (%s)'%e)
00155                         
00156                 try:
00157                         self._joint_subscriber = rospy.Subscriber(self._joint_states_topic, JointState, self._receive_joints_data)
00158                 except ValueError, e:
00159                         rospy.logerr('BHandGUI: Error connecting topic (%s)'%e)
00160                         
00161                 try:
00162                         self._tact_subscriber = rospy.Subscriber(self._tact_topic, TactileArray, self._receive_tact_data)
00163                 except ValueError, e:
00164                         rospy.logerr('BHandGUI: Error connecting topic (%s)'%e)
00165                 
00166                 
00167                 try:
00168                         self._publisher_command = rospy.Publisher(self._command_topic, JointState, queue_size=10)
00169                 except ROSException, e:
00170                         rospy.logerr('BHandGUI: Error creating publisher for topic %s (%s)'%(self._command_topic, e))
00171                 
00172                 
00173                 try:
00174                         self._service_bhand_actions = rospy.ServiceProxy(self._actions_service_name, Actions)
00175                 except ValueError, e:
00176                         rospy.logerr('BHandGUI: Error connecting service (%s)'%e)
00177                         
00178                 try:
00179                         self._service_set_mode = rospy.ServiceProxy(self._set_mode_service_name, SetControlMode)
00180                 except ValueError, e:
00181                         rospy.logerr('BHandGUI: Error connecting service (%s)'%e)
00182                 
00183                 
00184                         
00185                 self.fixed_fingers = 0
00186                 
00187                 
00188                 
00189                 self._widget.pushButtonStart.clicked.connect(self.start_button_pressed)
00190                 self._widget.pushButton_stop.clicked.connect(self.stop)
00191                 self._widget.pushButton_close.clicked.connect(self.close_button_pressed)
00192                 self._widget.pushButton_open.clicked.connect(self.open_button_pressed)
00193                 self._widget.pushButton_mode1.clicked.connect(self.mode1_button_pressed)
00194                 self._widget.pushButton_mode2.clicked.connect(self.mode2_button_pressed)
00195                 self._widget.horizontalSlider.valueChanged.connect(self.slider_1_changed)
00196                 self._widget.horizontalSlider_2.valueChanged.connect(self.slider_2_changed)
00197                 self._widget.horizontalSlider_3.valueChanged.connect(self.slider_3_changed)
00198                 self._widget.horizontalSlider_4.valueChanged.connect(self.slider_4_changed)
00199                 self._widget.horizontalSlider_v_spread.valueChanged.connect(self.slider_v_spread_changed)
00200                 self._widget.horizontalSlider_v_f1.valueChanged.connect(self.slider_v_f1_changed)
00201                 self._widget.horizontalSlider_v_f2.valueChanged.connect(self.slider_v_f2_changed)
00202                 self._widget.horizontalSlider_v_f3.valueChanged.connect(self.slider_v_f3_changed)
00203                 
00204                 self.connect(self._widget.checkBox_enable_control_vel, SIGNAL("stateChanged(int)"),  self.enable_command_vel_changed)
00205                 self.connect(self._widget.checkBox_enable_control_pos, SIGNAL("stateChanged(int)"),  self.enable_command_pos_changed)
00206                 self._widget.checkBox_enable_control_vel.setChecked(False)
00207                 
00208                 
00209                 
00210                 self.connect(self._widget.radioButtonPosition, SIGNAL("clicked(bool)"),  self.radio_button_position_clicked)
00211                 self.connect(self._widget.radioButtonVelocity, SIGNAL("clicked(bool)"),  self.radio_button_velocity_clicked)
00212                 
00213                 
00214                 self._widget.checkBox.stateChanged.connect(self.fingers_check_box_pressed)
00215                 
00216                 
00217                 self._init_timers()
00218         
00219         
00220         def read_ros_params(self):
00221                 '''
00222                         Read ROS params from server
00223                 '''
00224                 _name = rospy.get_name()
00225                 
00226                 self.bhand_node_name = rospy.get_param('%s/bhand_node_name'%_name, 'bhand_node')
00227                 
00228                 self.joint_ids  = rospy.get_param('%s/joint_ids'%(self.bhand_node_name), ['F1', 'F1_TIP', 'F2', 'F2_TIP', 'F3', 'F3_TIP', 'SPREAD_1', 'SPREAD_2'])
00229                 self.joint_names  = rospy.get_param('%s/joint_names'%(self.bhand_node_name), ['bh_j12_joint', 'bh_j13_joint', 'bh_j22_joint', 'bh_j23_joint', 'bh_j32_joint', 'bh_j31_joint', 'bh_j11_joint', 'bh_j21_joint'])
00230                 
00231                 rospy.loginfo('%s::read_ros_params: bhand_node_name = %s'%(_name, self.bhand_node_name))
00232                 rospy.loginfo('%s::read_ros_params: joint_ids = %s'%(_name, self.joint_ids))
00233                 rospy.loginfo('%s::read_ros_params: joint_names = %s'%(_name, self.joint_names))
00234                 
00235                 
00236                 
00237         
00238         def _init_timers(self):
00239                 self._topic_connected = False
00240                 self._topic_joint_states_connected = False
00241                 
00242                 self._topic_timer = time.time()
00243                 self._topic_joint_states_timer = time.time()
00244                 
00245                 
00246                 
00247                 
00248                 self._topic_timeout_connection = 5 
00249                 
00250                 self._timer = QBasicTimer()
00251                 self._timer.start(1, self)
00252                 
00253                 self._timer_commands = QTimer()
00254                 self.connect(self._timer_commands, SIGNAL("timeout()"),  self.timeout_command_timer)
00255                 
00256         def start_button_pressed(self):
00257                 '''
00258                         Handles the press button event to call initialization service
00259                 '''
00260                 self.send_bhand_action(Service.INIT_HAND)
00261                 '''
00262                         rospy.wait_for_service(self._actions_service_name)
00263                         try:
00264                                 action = rospy.ServiceProxy(self._actions_service_name, Actions)
00265                                 resp1 = action(Service.INIT_HAND)
00266                                 return resp1
00267                         except rospy.ServiceException, e:
00268                                 rospy.logerr("Service call failed: %s"%e)
00269                 '''
00270         
00271         def close_button_pressed(self):
00272                 '''
00273                         Handles the press button event to call close hand service
00274                 '''
00275                 
00276                 self._widget.radioButtonPosition.setChecked(True)
00277                 self.send_bhand_action(Service.CLOSE_GRASP)
00278         
00279         
00280         def open_button_pressed(self):
00281                 '''
00282                         Handles the press button event to call open hand service
00283                 '''
00284                 self.send_bhand_action(Service.OPEN_GRASP)
00285         
00286         
00287         def mode1_button_pressed(self):
00288                 '''
00289                         Handles the press button event to call set mode 1  service
00290                 '''
00291                 self.send_bhand_action(Service.SET_GRASP_1)
00292         
00293         
00294         def mode2_button_pressed(self):
00295                 '''
00296                         Handles the press button event to call set mode 2 service
00297                 '''
00298                 self.send_bhand_action(Service.SET_GRASP_2)
00299                 
00300                 
00301         
00302         def slider_1_changed(self):
00303                 self.base_spread = self._widget.horizontalSlider.value() * self.palm_factor
00304                 if self.enable_commands and self._bhand_data.control_mode == 'POSITION':
00305                         self.send_position_command()
00306                 
00307                 
00308                 
00309         def slider_2_changed(self):
00310                 self.finger3_spread = self._widget.horizontalSlider_2.value() * self.finger_factor
00311                 if self.fixed_fingers == 1:
00312                         self.finger1_spread = self.finger2_spread = self.finger3_spread
00313                         self._widget.horizontalSlider_3.setSliderPosition(self._widget.horizontalSlider_2.value())
00314                         self._widget.horizontalSlider_4.setSliderPosition(self._widget.horizontalSlider_2.value())
00315                 if self.enable_commands and self._bhand_data.control_mode == 'POSITION':
00316                         self.send_position_command()
00317                 
00318         def slider_3_changed(self):
00319                 self.finger1_spread = self._widget.horizontalSlider_3.value() * self.finger_factor
00320                 if self.fixed_fingers == 1:
00321                         self.finger3_spread = self.finger2_spread = self.finger1_spread
00322                         self._widget.horizontalSlider_2.setSliderPosition(self._widget.horizontalSlider_3.value())
00323                         self._widget.horizontalSlider_4.setSliderPosition(self._widget.horizontalSlider_3.value())
00324                 if self.enable_commands and self._bhand_data.control_mode == 'POSITION':
00325                         self.send_position_command()
00326                 
00327         def slider_4_changed(self):
00328                 self.finger2_spread = self._widget.horizontalSlider_4.value() * self.finger_factor
00329                 if self.fixed_fingers == 1:
00330                         self.finger1_spread = self.finger2_spread = self.finger2_spread
00331                         self._widget.horizontalSlider_2.setSliderPosition(self._widget.horizontalSlider_4.value())
00332                         self._widget.horizontalSlider_3.setSliderPosition(self._widget.horizontalSlider_4.value())
00333                 if self.enable_commands and self._bhand_data.control_mode == 'POSITION':
00334                         self.send_position_command()
00335         
00336         def slider_v_spread_changed(self):
00337                 '''
00338                         Handler for slider v_spread
00339                 '''
00340                 self.base_spread_vel = self._widget.horizontalSlider_v_spread.value() * self.vel_factor
00341                 
00342         
00343         def slider_v_f1_changed(self):
00344                 '''
00345                         Handler for slider v_f1
00346                 '''
00347                 self.finger1_spread_vel = self._widget.horizontalSlider_v_f1.value() * self.vel_factor
00348                 if self._widget.checkBox_moveall_velocity.isChecked():
00349                         self._widget.horizontalSlider_v_f2.setSliderPosition(self._widget.horizontalSlider_v_f1.value())
00350                         self._widget.horizontalSlider_v_f3.setSliderPosition(self._widget.horizontalSlider_v_f1.value())
00351         
00352         
00353         def slider_v_f2_changed(self):
00354                 '''
00355                         Handler for slider v_f2
00356                 '''
00357                 self.finger2_spread_vel = self._widget.horizontalSlider_v_f2.value() * self.vel_factor
00358                 if self._widget.checkBox_moveall_velocity.isChecked():
00359                         self._widget.horizontalSlider_v_f1.setSliderPosition(self._widget.horizontalSlider_v_f2.value())
00360                         self._widget.horizontalSlider_v_f3.setSliderPosition(self._widget.horizontalSlider_v_f2.value())
00361                 
00362         
00363         def slider_v_f3_changed(self):
00364                 '''
00365                         Handler for slider v_f3
00366                 '''
00367                 self.finger3_spread_vel = self._widget.horizontalSlider_v_f3.value() * self.vel_factor
00368                 if self._widget.checkBox_moveall_velocity.isChecked():
00369                         self._widget.horizontalSlider_v_f2.setSliderPosition(self._widget.horizontalSlider_v_f3.value())
00370                         self._widget.horizontalSlider_v_f1.setSliderPosition(self._widget.horizontalSlider_v_f3.value())
00371         
00372 
00373                 
00374         def send_position_command(self):
00375                 '''
00376                         Sends a command in position mode
00377                 '''
00378                 
00379                 self.desired_ref.name = [self.joint_state_pointer['SPREAD_1']['joint'], self.joint_state_pointer['F3']['joint'], self.joint_state_pointer['F1']['joint'], self.joint_state_pointer['F2']['joint']]
00380                 self.desired_ref.position = [self.base_spread, self.finger3_spread, self.finger1_spread, self.finger2_spread]
00381                 self.desired_ref.velocity = [0.1, 0.1, 0.1, 0.1]
00382                 self.desired_ref.effort = [0.0, 0.0, 0.0, 0.0]
00383                 self._publisher_command.publish(self.desired_ref)
00384                 
00385                 
00386         def     fingers_check_box_pressed(self, state):
00387                 '''
00388                         Handler call when clicking checkbox to control all fingers
00389                 '''
00390                 if state == 0:
00391                         self.fixed_fingers = 0
00392                 elif state == 2:
00393                         self.fixed_fingers = 1
00394                 
00395                         
00396                 
00397         @Slot(str)
00398         
00399         def _on_topic_changed(self):
00400                 
00401                 
00402                 self._subscriber.unregister()
00403                 try:
00404                         self._subscriber = rospy.Subscriber(self._topic, State, self._receive_state_data)
00405                 except ValueError, e:
00406                         rospy.logerr('BHandGUI: Error connecting topic (%s)'%e)
00407                 
00408         
00409         def _receive_state_data(self, msg):
00410                 self._bhand_data = msg
00411                 self._topic_timer = time.time()
00412                 
00413                 if not self._topic_connected:
00414                         rospy.loginfo('BHandGUI: connection stablished with %s'%self._topic)
00415                         self._topic_connected = True
00416         
00417         
00418         def _receive_joints_data(self, msg):
00419                 '''
00420                         Handler for Joint States
00421                 '''
00422                 self._joints_data = msg
00423                 
00424                 self._topic_joint_states_timer = time.time()
00425                 
00426                 for i in range(len(msg.name)):
00427                         for j in self.joint_state_pointer:
00428                                 if self.joint_state_pointer[j]['joint'] == msg.name[i]:
00429                                         self.joint_state_pointer[j]['values'] = [msg.position[i], msg.velocity[i], msg.effort[i]]
00430                         
00431                 
00432                 if not self._topic_joint_states_connected:
00433                         rospy.loginfo('Bhand: connection stablished with %s'%self._joint_states_topic)
00434                         self._topic_joint_states_connected = True
00435                 
00436                 
00437                                 
00438                 
00439         def _receive_tact_data(self, msg):
00440                 
00441                 if self._tact_data is None:
00442                         self._tact_data = TactileArray()
00443                         
00444                 self._tact_data = msg
00445         
00446                         
00447                         
00448                                 
00449         
00450         def _receive_dspic_data(self, msg):
00451                 self._dspic_data = msg
00452                 self._topic_dspic_timer = time.time()
00453                 
00454                 if not self._topic_dspic_connected:
00455                         rospy.loginfo('BHandGUI: connection stablished with %s'%self._topic_dspic)
00456                         self._topic_dspic_connected = True
00457         
00458                         
00459         
00460         def press_reset_dspic_odom(self):       
00461                 
00462                 ret = QMessageBox.question(self._widget, "Dspic Reset Odometry", 'Are you sure of resetting the odometry?', QMessageBox.Ok, QMessageBox.Cancel)
00463                 
00464                 if ret == QMessageBox.Ok:
00465                         
00466                         
00467                         try:
00468                                 ret = self._service_dspic_reset_odom(0.0, 0.0, 0.0, 0.0)                                
00469                         except ValueError, e:
00470                                 rospy.logerr('BHandGUI::press_reset_dspic_odom: (%s)'%e)
00471                         except rospy.ServiceException, e:
00472                                 rospy.logerr('BHandGUI::press_reset_dspic_odom: (%s)'%e)
00473                                 QMessageBox.warning(self._widget, "Warning", "Servicio no disponible")
00474         
00475         
00476         def enable_command_vel_changed(self, value):
00477                 '''
00478                         Handles command enabled event from checkbox
00479                 '''
00480                  
00481                 if value != 0:
00482                         self._widget.checkBox_enable_control_pos.setChecked(True)
00483                         self.enable_commands = True
00484                         
00485                         
00486                                 
00487                                  
00488                 else:
00489                         self._widget.checkBox_enable_control_pos.setChecked(False)
00490                         self.enable_commands = False
00491                         
00492                         
00493                         
00494                         
00495         
00496         def enable_command_pos_changed(self, value):
00497                 '''
00498                         Handles command enabled event from checkbox
00499                 '''
00500                 
00501                 if value != 0:
00502                         self._widget.checkBox_enable_control_vel.setChecked(True)
00503                         self.enable_commands = True
00504                 else:
00505                         self._widget.checkBox_enable_control_vel.setChecked(False)
00506                         self.enable_commands = False
00507         
00508         
00509         def radio_button_position_clicked(self, value):
00510                 '''
00511                         Handles the click of this radio button
00512                 '''
00513                 self.set_control_mode('POSITION')
00514                 self._timer_commands.stop()
00515                 self.stop()
00516                 
00517         
00518         def radio_button_velocity_clicked(self, value):
00519                 '''
00520                         Handles the click of this radio button
00521                 '''
00522                 self.set_control_mode('VELOCITY')
00523                 self._timer_commands.start(BHAND_VELOCITY_COMMANDS_FREQ)
00524         
00525 
00526         def set_control_mode(self, mode):       
00527                 '''
00528                         Calls the service to set the control mode of the hand
00529                         @param mode: Desired Bhand mode of operation ('POSITION', 'VELOCITY')
00530                         @type mode: string
00531                 '''                     
00532                 try:
00533                         ret = self._service_set_mode(mode)                              
00534                 except ValueError, e:
00535                         rospy.logerr('BHandGUI::set_control_mode: (%s)'%e)
00536                 except rospy.ServiceException, e:
00537                         rospy.logerr('BHandGUI::set_control_mode: (%s)'%e)
00538                         QMessageBox.warning(self._widget, "Warning", "Servicio no disponible")
00539         
00540         
00541         def send_bhand_action(self, action):    
00542                 '''
00543                         Calls the service to set the control mode of the hand
00544                         @param action: Action number (defined in msgs/Service.msg)
00545                         @type action: int
00546                 '''                     
00547                 try:
00548                         ret = self._service_bhand_actions(action)                               
00549                 except ValueError, e:
00550                         rospy.logerr('BHandGUI::send_bhand_action: (%s)'%e)
00551                 except rospy.ServiceException, e:
00552                         rospy.logerr('BHandGUI::send_bhand_action: (%s)'%e)
00553                         QMessageBox.warning(self._widget, "Warning", "Servicio no disponible")
00554                                                 
00555         
00556         def send_velocity_command(self):
00557                 '''
00558                         Sends a command to the bhand
00559                 '''
00560                 self.desired_ref.name = [self.joint_state_pointer['SPREAD_1']['joint'], self.joint_state_pointer['F3']['joint'], self.joint_state_pointer['F1']['joint'], self.joint_state_pointer['F2']['joint']]
00561                 self.desired_ref.position = [0, 0, 0, 0]
00562                 self.desired_ref.velocity = [self.base_spread_vel, self.finger3_spread_vel, self.finger1_spread_vel, self.finger2_spread_vel]
00563                 self.desired_ref.effort = [0.0, 0.0, 0.0, 0.0]
00564                 self._publisher_command.publish(self.desired_ref)
00565                 
00566 
00567         def timeout_command_timer(self):
00568                 '''
00569                         Handles every timeout triggered by the Qtimer for sending commands      
00570                 '''
00571                 if self.enable_commands and self._bhand_data.control_mode == 'VELOCITY':
00572                         self.send_velocity_command()
00573         
00574         
00575         def stop(self):
00576                 '''
00577                         Stops the movement of the joints
00578                         It sets the velocity to 0
00579                 '''
00580                 self._widget.horizontalSlider_v_spread.setValue(0)
00581                 self._widget.horizontalSlider_v_f1.setValue(0)
00582                 self._widget.horizontalSlider_v_f2.setValue(0)
00583                 self._widget.horizontalSlider_v_f3.setValue(0)
00584         
00585         
00586         def shutdown_plugin(self):
00587                 '''
00588                         Shutdowns connections
00589                 '''
00590                 self._timer_commands.stop()
00591                 self._timer.stop()
00592                 self._subscriber.unregister()
00593                 self._joint_subscriber.unregister()
00594                 self._tact_subscriber.unregister()
00595                 self._publisher_command.unregister()
00596                 self._service_bhand_actions.close()
00597                 self._service_set_mode.close()
00598                 
00599         
00600         
00601         
00602         def timerEvent(self, e):
00603                 
00604                 
00605                 
00606                 
00607                 if self._bhand_data.hand_initialized:
00608                         self._widget.checkBox_hand_initialized.setChecked(1)
00609                 else:
00610                         self._widget.checkBox_hand_initialized.setChecked(0)
00611                 
00612                 
00613                 if self._bhand_data.state == 100:
00614                         self.state_string = "INIT_STATE"
00615                 elif self._bhand_data.state == 200:
00616                         self.state_string = "STANDBY_STATE"
00617                 elif self._bhand_data.state == 300:
00618                         self.state_string = "READY_STATE"
00619                 elif self._bhand_data.state == 400:
00620                         self.state_string = "EMERGENCY_STATE"
00621                 elif self._bhand_data.state == 500:
00622                         self.state_string = "FAILURE_STATE"
00623                 elif self._bhand_data.state == 600:
00624                         self.state_string = "SHUTDOWN_STATE"
00625                         
00626                 if self.state_string != " ":
00627                         self._widget.lineEdit_state.setText(self.state_string)
00628                 
00629                 self._widget.lineEdit_mode.setText(self._bhand_data.control_mode)
00630                         
00631                 
00632                 self._widget.lineEdit_motor_pos_hz.setText(str(self._bhand_data.desired_freq))
00633                 self._widget.lineEdit_motor_pos_hz_2.setText(str(round(self._bhand_data.real_freq,1)))
00634                 
00635                 
00636                 self._widget.lineEdit.setText(str(self._bhand_data.temp_spread[0]))
00637                 self._widget.lineEdit_3.setText(str(self._bhand_data.temp_spread[1]))
00638                 self._widget.lineEdit_2.setText(str(self._bhand_data.temp_f1[0]))
00639                 self._widget.lineEdit_4.setText(str(self._bhand_data.temp_f1[1]))
00640                 self._widget.lineEdit_5.setText(str(self._bhand_data.temp_f2[0]))
00641                 self._widget.lineEdit_6.setText(str(self._bhand_data.temp_f2[1]))
00642                 self._widget.lineEdit_7.setText(str(self._bhand_data.temp_f3[0]))
00643                 self._widget.lineEdit_8.setText(str(self._bhand_data.temp_f3[1]))
00644                 
00645                 
00646                 self._widget.lineEdit_spread_des_pos.setText(str(round(self.base_spread,3)))
00647                 self._widget.lineEdit_f1_des_pos.setText(str(round(self.finger1_spread,3)))
00648                 self._widget.lineEdit_f2_des_pos.setText(str(round(self.finger2_spread,3)))
00649                 self._widget.lineEdit_f3_des_pos.setText(str(round(self.finger3_spread,3)))
00650                 
00651                 self._widget.lineEdit_spread_read_pos.setText(str(round(self.joint_state_pointer['SPREAD_1']['values'][0],3)))
00652                 self._widget.lineEdit_f1_read_pos.setText(str(round(self.joint_state_pointer['F1']['values'][0],3)))
00653                 self._widget.lineEdit_f2_read_pos.setText(str(round(self.joint_state_pointer['F2']['values'][0],3)))
00654                 self._widget.lineEdit_f3_read_pos.setText(str(round(self.joint_state_pointer['F3']['values'][0],3)))
00655                 self._widget.lineEdit_spread_read_pos_2.setText(str(round(self.joint_state_pointer['SPREAD_1']['values'][0],3)))
00656                 self._widget.lineEdit_f1_read_pos_2.setText(str(round(self.joint_state_pointer['F1']['values'][0],3)))
00657                 self._widget.lineEdit_f2_read_pos_2.setText(str(round(self.joint_state_pointer['F2']['values'][0],3)))
00658                 self._widget.lineEdit_f3_read_pos_2.setText(str(round(self.joint_state_pointer['F3']['values'][0],3)))
00659                 
00660                 self._widget.lineEdit_spread_des_vel.setText(str(round(self.base_spread_vel,3)))
00661                 self._widget.lineEdit_f1_des_vel.setText(str(round(self.finger1_spread_vel,3)))
00662                 self._widget.lineEdit_f2_des_vel.setText(str(round(self.finger2_spread_vel,3)))
00663                 self._widget.lineEdit_f3_des_vel.setText(str(round(self.finger3_spread_vel,3)))
00664                 
00665                 
00666                 if self._bhand_data.state == 300:
00667                         self._widget.lineEdit_fingertip1.setText(str(round(self.joint_state_pointer['F1_TIP']['values'][2],4)))
00668                         self._widget.lineEdit_fingertip2.setText(str(round(self.joint_state_pointer['F2_TIP']['values'][2],4)))
00669                         self._widget.lineEdit_fingertip3.setText(str(round(self.joint_state_pointer['F3_TIP']['values'][2],4)))
00670                 
00671                 
00672                 
00673                 
00674                 
00675                 
00676                 
00677                 
00678                 if self._tact_data is not None and self._bhand_data.state == 300:
00679                         
00680                         
00681                         for i in range(0,8):
00682                                 for j in range(0,3):
00683                                         lcd_string = "lcdNumber" + str(i*3 + j)
00684                                         value = round(self._tact_data.finger1[(7-i)*3 + j ], 1)
00685                                         getattr(self._widget,lcd_string).display(value)
00686                                         if 0.0 <= value and value < 4.0:
00687                                                 color_string = self.green_string
00688                                         elif 4.0 <= value and value < 8.0:
00689                                                 color_string = self.yellow_string
00690                                         elif 8.0 <= value and value < 12.0:
00691                                                 color_string = self.orange_string
00692                                         elif 12.0 <= value and value <= 16.0:
00693                                                 color_string = self.red_string
00694                                         else:
00695                                                 color_string = self.black_string
00696                                         getattr(self._widget,lcd_string).setStyleSheet(color_string)
00697                 
00698                         
00699                         for i in range(0,8):
00700                                 for j in range(0,3):
00701                                         lcd_string = "lcdNumber" + str(i*3 + j) + "_3"
00702                                         value = round(self._tact_data.finger2[(7-i)*3 + j ], 1)
00703                                         getattr(self._widget,lcd_string).display(value)
00704                                         if 0.0 <= value and value < 4.0:
00705                                                 color_string = self.green_string
00706                                         elif 4.0 <= value and value < 8.0:
00707                                                 color_string = self.yellow_string
00708                                         elif 8.0 <= value and value < 12.0:
00709                                                 color_string = self.orange_string
00710                                         elif 12.0 <= value and value <= 16.0:
00711                                                 color_string = self.red_string
00712                                         else:
00713                                                 color_string = self.black_string
00714                                         getattr(self._widget,lcd_string).setStyleSheet(color_string)
00715                                 
00716                         
00717                         for i in range(0,8):
00718                                 for j in range(0,3):
00719                                         lcd_string = "lcdNumber" + str(i*3 + j) + "_4"
00720                                         value = round(self._tact_data.finger3[(7-i)*3 + j], 1)
00721                                         getattr(self._widget,lcd_string).display(value)
00722                                         if 0.0 <= value and value < 4.0:
00723                                                 color_string = self.green_string
00724                                         elif 4.0 <= value and value < 8.0:
00725                                                 color_string = self.yellow_string
00726                                         elif 8.0 <= value and value < 12.0:
00727                                                 color_string = self.orange_string
00728                                         elif 12.0 <= value and value <= 16.0:
00729                                                 color_string = self.red_string
00730                                         else:
00731                                                 color_string = self.black_string
00732                                         getattr(self._widget,lcd_string).setStyleSheet(color_string)
00733                                 
00734                         
00735                         for i in range(0,24):
00736                                 lcd_string = "lcdNumber" + str(i) + "_6"
00737                                 value = round(self._tact_data.palm[i], 1)
00738                                 getattr(self._widget,lcd_string).display(value)
00739                                 if 0.0 <= value and value < 2.5:
00740                                         color_string = self.green_string
00741                                 elif 2.5 <= value and value < 5.0:
00742                                         color_string = self.yellow_string
00743                                 elif 5.0 <= value and value < 7.5:
00744                                         color_string = self.orange_string
00745                                 elif 7.5 <= value and value <= 16.0:
00746                                         color_string = self.red_string
00747                                 else:
00748                                         color_string = self.black_string
00749                                 getattr(self._widget,lcd_string).setStyleSheet(color_string)
00750                 
00751                 
00752                 
00753                 t = time.time()
00754                 if self._topic_connected and (t - self._topic_timer >= self._topic_timeout_connection):
00755                         self._topic_connected = False
00756                         rospy.logerr('BHandGUI: error in communication with %s'%self._topic)
00757                         self._widget.qlabel_state_connection.setPixmap(self._pixmap_red)
00758                         
00759                 if self._topic_joint_states_connected and (t - self._topic_joint_states_timer >= self._topic_timeout_connection):
00760                         self._topic_joint_states_connected = False
00761                         rospy.logerr('BHandGUI: error in communication with %s'%self._joint_states_topic)
00762                         self._widget.qlabel_jointstate_connection.setPixmap(self._pixmap_red)
00763                 
00764                 if self._topic_connected:
00765                         self._widget.qlabel_state_connection.setPixmap(self._pixmap_green)
00766                         
00767                 if self._topic_joint_states_connected:
00768                         self._widget.qlabel_jointstate_connection.setPixmap(self._pixmap_green)