35 from pyHand_api 
import *
    37 from std_msgs.msg 
import String
    38 from bhand_controller.msg 
import State, TactileArray, Service, ForceTorque
    39 from bhand_controller.srv 
import Actions, SetControlMode
    40 from sensor_msgs.msg 
import JointState
    42 import time, threading
    49 CONTROL_MODE_POSITION = 
"PID"    50 CONTROL_MODE_VELOCITY = 
"VELOCITY"    51 CONTROL_MODE_IDLE = 
"IDLE"    53 WATCHDOG_VELOCITY = 0.1 
    76                         rospy.loginfo(
'%s::init: Desired freq (%f) is not possible. Setting desired_freq to %f'%(rospy.get_name(), self.
desired_freq, DEFAULT_FREQ))
    83                 joint_ids_arg = args[
'joint_ids']
    84                 joint_names_arg = args[
'joint_names']
    88                         rospy.loginfo(
'%s: init: setting control mode by default to %s'%(rospy.get_name(), CONTROL_MODE_POSITION))
   158                 for i 
in range(len(joint_ids_arg)):
   159                         self.
joint_names[joint_ids_arg[i]] = [joint_names_arg[i], i]
   164                 self.
temperatures = {
'F1': [0.0,  0.0], 
'F2': [0.0,  0.0], 
'F3': [0.0,  0.0], 
'SPREAD': [0.0,  0.0]}
   169                         self.joint_state.name.append(self.
joint_names[i][0])
   173                         self.joint_state.position.append(0.0)
   174                         self.joint_state.velocity.append(0.0)
   175                         self.joint_state.effort.append(0.0)
   194                         @return: True if OK, False otherwise   198                             rospy.loginfo(
'%s:setup: initializing FT sensor'%rospy.get_name() )
   199                             self.hand.initialize_fts()
   200                         rospy.loginfo(
'%s:setup: ok'%rospy.get_name() )
   204                         self.hand.can_uninit()
   205                         rospy.logerr(
'%s:setup: Error initializing the hand'%rospy.get_name() )
   211                         Creates and inits ROS components   239                         @return: 0 if it's performed successfully, -1 if there's any problem or the component is running   243                 rospy.loginfo(
'%s::shutdown'%rospy.get_name())
   246                 self.hand.can_uninit()
   248                 self.t_publish_state.cancel()
   249                 self.t_read_temp.cancel()
   260                         Shutdows all ROS components   261                         @return: 0 if it's performed successfully, -1 if there's any problem or the component is running   267                 self._state_publisher.unregister()
   268                 self._joints_publisher.unregister()
   269                 self._tact_array_publisher.unregister()
   270                 self._joints_subscriber.unregister()
   271                 self._ft_sensor_publisher.unregister()
   272                 self._set_control_mode_service.shutdown()
   273                 self._hand_service.shutdown()
   282                         Creates and inits ROS components   291                         Runs ROS configuration and the main control loop   308                         Main loop of the component   309                         Manages actions by state   312                 while self.
running and not rospy.is_shutdown():
   315                         if self.
state == State.INIT_STATE:
   318                         elif self.
state == State.STANDBY_STATE:
   321                         elif self.
state == State.READY_STATE:
   324                         elif self.
state == State.EMERGENCY_STATE:
   327                         elif self.
state == State.FAILURE_STATE:
   330                         elif self.
state == State.SHUTDOWN_STATE:
   353                 rospy.loginfo(
'%s::controlLoop: exit control loop'%rospy.get_name())
   361                         Publish topics at standard frequency   368                 self.joint_state.header.stamp = t
   372                         self.msg_tact_array.header.stamp = t
   376                     self.msg_ft.header.stamp = t
   377                     self._ft_sensor_publisher.publish(self.
msg_ft)
   385                         Actions performed in init state   402                         Actions performed in standby state   412                         Actions performed in ready state   427                         self.hand.read_packed_position(HAND_GROUP)
   430                         self.hand.read_strain(HAND_GROUP)
   436                                 self.hand.read_temp(HAND_GROUP)
   437                                 self.hand.read_therm(HAND_GROUP)
   443                                 self.hand.read_full_tact(FINGER1)
   444                                 self.hand.read_full_tact(FINGER2)
   445                                 self.hand.read_full_tact(FINGER3)
   446                                 self.hand.read_full_tact(SPREAD)
   451                                 self.hand.read_full_force_torque()
   455                         rospy.logerr(
'%s::readyState: error getting info: %s'%(rospy.get_name(), e))
   462                         self.actions_list.remove(action)
   468                         if action == Service.CLOSE_GRASP:
   471                         if action == Service.CLOSE_HALF_GRASP:
   474                         elif action == Service.OPEN_GRASP:
   477                         elif action == Service.SET_GRASP_1:
   478                                 if self.joint_state.position[f1_index] > 0.1 
or self.joint_state.position[f2_index] > 0.1 
or self.joint_state.position[f3_index] > 0.1:
   488                         elif action == Service.SET_GRASP_2:
   489                                 if self.joint_state.position[f1_index] > 0.1 
or self.joint_state.position[f2_index] > 0.1 
or self.joint_state.position[f3_index] > 0.1:
   499                         elif action == Service.TARE_FTS:
   529                                                         self.hand.move_to(SPREAD, self.hand.rad_to_enc(self.
desired_joints_position[spread1_joint], SPREAD_TYPE), 
False)
   532                                                         self.hand.move_to(SPREAD, self.hand.rad_to_enc(self.
desired_joints_position[spread2_joint], SPREAD_TYPE), 
False)
   535                                                 rospy.logerr(
'%s::readyState: error sending command: %s'%(rospy.get_name(), e))
   555                                                 rospy.logerr(
'%s::readyState: error sending command: %s'%(rospy.get_name(), e))
   568                                                 rospy.logerr(
'%s::readyState: error sending command: %s'%(rospy.get_name(), e))
   576                 if self.hand.process_can_messages() != 0:
   582                 self.joint_state.position[f1_index], self.joint_state.position[f1_tip_index] = self.hand.get_packed_position(FINGER1)
   583                 self.joint_state.position[f2_index], self.joint_state.position[f2_tip_index] = self.hand.get_packed_position(FINGER2)
   584                 self.joint_state.position[f3_index], self.joint_state.position[f3_tip_index] = self.hand.get_packed_position(FINGER3)
   585                 self.joint_state.position[spread1_index], e = self.hand.get_packed_position(SPREAD)
   586                 self.joint_state.position[spread2_index] = self.joint_state.position[spread1_index]
   589                 self.joint_state.effort[f1_index] = self.joint_state.effort[f1_tip_index] = self.
strain_to_nm(self.hand.get_strain(FINGER1))
   590                 self.joint_state.effort[f2_index] = self.joint_state.effort[f2_tip_index] = self.
strain_to_nm(self.hand.get_strain(FINGER2))
   591                 self.joint_state.effort[f3_index] = self.joint_state.effort[f3_tip_index] = self.
strain_to_nm(self.hand.get_strain(FINGER3))
   594                 self.msg_state.temp_f1[1] = self.hand.get_temp(FINGER1)
   595                 self.msg_state.temp_f2[1] = self.hand.get_temp(FINGER2)
   596                 self.msg_state.temp_f3[1] = self.hand.get_temp(FINGER3)
   597                 self.msg_state.temp_spread[1] = self.hand.get_temp(SPREAD)
   598                 self.msg_state.temp_f1[0] = self.hand.get_therm(FINGER1)
   599                 self.msg_state.temp_f2[0] = self.hand.get_therm(FINGER2)
   600                 self.msg_state.temp_f3[0] = self.hand.get_therm(FINGER3)
   601                 self.msg_state.temp_spread[0] = self.hand.get_therm(SPREAD)
   605                     self.msg_tact_array.finger1 = self.hand.get_full_tact(FINGER1)
   606                     self.msg_tact_array.finger2 = self.hand.get_full_tact(FINGER2)
   607                     self.msg_tact_array.finger3 = self.hand.get_full_tact(FINGER3)
   608                     self.msg_tact_array.palm = self.hand.get_full_tact(SPREAD)
   611                     fts = self.hand.get_fts()
   612                     self.msg_ft.force.x = fts[
'force'][0]
   613                     self.msg_ft.force.y = fts[
'force'][1]
   614                     self.msg_ft.force.z = fts[
'force'][2]
   615                     self.msg_ft.torque.x = fts[
'torque'][0]
   616                     self.msg_ft.torque.y = fts[
'torque'][1]
   617                     self.msg_ft.torque.z = fts[
'torque'][2]
   627                         Sets the joint velocity of the desired joint   628                         Takes the velocity value from the attribute desired_joints_velocity   629                         @param joint: finger of the hand (F1, F2, F3, SPREAD)   632                 if self.joint_names.has_key(finger):
   642                                         self.hand.set_velocity(FINGER1, value)
   644                                         self.hand.set_velocity(FINGER2, value)
   646                                         self.hand.set_velocity(FINGER3, value)
   647                                 elif finger == 
'SPREAD_1' or finger == 
'SPREAD_2':
   648                                         self.hand.set_velocity(SPREAD, value)
   653                         Actions performed in shutdown state    655                 rospy.loginfo(
'%s:shutdownState', rospy.get_name())
   663                         Actions performed in emergency state   670                         Actions performed in failure state   672                 if self.failure_recover:
   674                         self.hand.can_uninit()
   675                         self.can_initialized = 
False   676                         self.switchToState(State.INIT_STATE)
   677                         self.failure_recover = 
False   684                         Performs the change of state   686                 if self.
state != new_state:
   687                         self.
state = new_state
   690                         if self.
state == State.READY_STATE:
   694                         elif self.
state == State.FAILURE_STATE:
   706                         Actions performed in all states   715                         @param state: state to convert   716                         @type state: bhand_controller.msg.State   717                         @returns the equivalent string of the state   719                 if state == State.INIT_STATE:
   722                 elif state == State.STANDBY_STATE:
   723                         return 'STANDBY_STATE'   725                 elif state == State.READY_STATE:
   728                 elif state == State.EMERGENCY_STATE:
   729                         return 'EMERGENCY_STATE'   731                 elif state == State.FAILURE_STATE:
   732                         return 'FAILURE_STATE'   734                 elif state == State.SHUTDOWN_STATE:
   735                         return 'SHUTDOWN_STATE'   737                         return 'UNKNOWN_STATE'   742                         @param action: action to convert   743                         @type state: bhand_controller.msg.Service   744                         @returns the equivalent string of the action   746                 if action == Service.INIT_HAND:
   749                 elif action == Service.CLOSE_GRASP:
   752                 elif action == Service.OPEN_GRASP:
   755                 elif action == Service.SET_GRASP_1:
   758                 elif action == Service.SET_GRASP_2:
   761                 elif action == Service.CLOSE_HALF_GRASP:
   762                         return 'CLOSE_HALF_GRASP'   765                         return 'UNKNOWN_ACTION'   770                         Function called when receive a new value   771                         @param data: state to convert   772                         @type data: sensor_msgs.JointState   776                 for i 
in range(len(data.name)):
   777                         if self.desired_joints_position.has_key(data.name[i]):
   791                         Function periodically to enable the read of temperature   804                         Function to start the timer to read the temperature   809                 self.t_read_temp.start()
   814                         Publish the State of the component at the desired frequency   816                 self.msg_state.state = self.
state   819                 self.msg_state.real_freq = self.
real_freq   822                 self._state_publisher.publish(self.
msg_state)
   825                 self.t_publish_state.start()
   830                 Converts from raw encoder unit reading to Newton-meters.   832                 @param x: value to convert   834                 @returns nm: the torque value in Newton-meters.    840                 nm= p1*x**3 + p2*x**2 + p3*x + p4
   847                 Handles the callback to Actions ROS service. Allows a set of predefined actions like init_hand, close_grasp, ...   849                 @param req: action id to perform   850                 @type req: bhand_controller.srv.Actions   852                 @returns: True or false depending on the result   854                 if req.action == Service.INIT_HAND:
   855                         if self.
state != State.INIT_STATE:
   856                                 rospy.logerr(
'%s::handActions: action INIT_HAND not allowed in state %s'%(rospy.get_name() ,self.
stateToString(self.
state)))
   860                                 ret = self.hand.init_hand()
   864                                         rospy.loginfo(
'%s::handActions: INIT HAND service OK'%rospy.get_name() )
   868                                         rospy.logerr(
'%s::handActions: error on INIT_HAND service'%rospy.get_name() )
   873                 if self.
state != State.READY_STATE:
   874                         rospy.logerr(
'%s::handActions: action not allowed in state %s'%(rospy.get_name(), self.
stateToString(self.
state)))
   877                         rospy.loginfo(
'%s::handActions: Received new action %s'%(rospy.get_name(), self.
actionsToString(req.action)))
   878                         self.actions_list.append(req.action)
   885                 Manages the CAN errors ocurred   887                 @param n: number of produced   890                 @returns: True or false depending on the result   898                         rospy.logerr(
'%s::canError: Errors on CAN bus'%rospy.get_name())
   904                         Function to start the timer to recover from FAILURE STATE   912                         Sets the recovery flag TRUE. Function called after timer   919                         Sets the hand control mode   920                         @param req: Requested service   921                         @type req: bhand_controller.srv.SetControlMode   923                 if self.
state != State.READY_STATE:
   924                         rospy.logerr(
'%s:setControlModeServiceCb: bhand is not in a valid state to change the mode'%rospy.get_name() )
   927                 if req.mode != CONTROL_MODE_POSITION 
and req.mode != CONTROL_MODE_VELOCITY:
   928                         rospy.logerr(
'%s:setControlModeServiceCb: invalid control mode %s'%(rospy.get_name(), req.mode))
   933                                 rospy.loginfo(
'%s:setControlModeServiceCb: control mode %s set successfully'%(rospy.get_name(), req.mode))
   942                         Configures the hand to work under the desired control mode   943                         @param mode: new mode   949                 if mode == CONTROL_MODE_POSITION:
   950                         self.hand.set_mode(HAND_GROUP, 
'PID')
   951                 elif mode == CONTROL_MODE_VELOCITY:
   952                         self.hand.set_mode(HAND_GROUP, 
'VEL')
   953                 elif mode == CONTROL_MODE_IDLE:
   954                         self.hand.set_mode(HAND_GROUP, 
'IDLE')
   961                         Opens all the fingers   973                         Closes all the fingers   986         rospy.init_node(
"bhand_node")
   989         _name = rospy.get_name()
   992           'port':  
'/dev/pcan32',
   993           'topic_state': 
'state',
   994           'desired_freq': DEFAULT_FREQ,
   995           'tactile_sensors': 
True,
   997           'control_mode': 
'POSITION',
   998           'joint_ids': [ 
'F1', 
'F1_TIP', 
'F2', 
'F2_TIP', 
'F3', 
'F3_TIP', 
'SPREAD_1', 
'SPREAD_2'],
   999           'joint_names': [
'bh_j12_joint', 
'bh_j13_joint', 
'bh_j22_joint', 
'bh_j23_joint', 
'bh_j32_joint', 
'bh_j31_joint', 
'bh_j11_joint', 
'bh_j21_joint'],
  1000           'ft_sensor_frame_id': 
'bhand_ft_sensor_link'  1005         for name 
in arg_defaults:
  1007                         if rospy.search_param(name): 
  1008                                 args[name] = rospy.get_param(
'%s/%s'%(_name, name)) 
  1010                                 args[name] = arg_defaults[name]
  1012                 except rospy.ROSException, e:
  1013                         rospy.logerror(
'bhand_node: %s'%e)
  1016         bhand_node = 
BHand(args)
  1018         rospy.loginfo(
'bhand_node: starting')
  1023 if __name__ == 
"__main__":
 
def switchToState(self, new_state)
 
def handActions(self, req)
 
def strain_to_nm(self, x)
 
def closeFingers(self, value)
 
def stateToString(self, state)
 
def initFailureRecoverTimer(self, timer=5)
 
def setControlMode(self, mode, force=False)
 
def setControlModeServiceCb(self, req)
 
def __init__(self, id, name)
 
def commandCallback(self, data)
 
def initReadTempTimer(self)
 
def publishROSstate(self)
 
def actionsToString(self, action)
 
def setJointVelocity(self, finger)
 
_set_control_mode_service