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 
00032 import roslib; roslib.load_manifest('force_torque')
00033 import rospy
00034 import hrl_lib.rutils as ru
00035 import hrl_lib.util as ut
00036 from hrl_msgs.msg import FloatArray
00037 from geometry_msgs.msg import WrenchStamped
00038 import numpy as np
00039 
00040 
00041 
00042 class FTClient(ru.GenericListener):
00043     def __init__(self, topic_name, netft=False):
00044         if netft:
00045             def msg_converter(msg):
00046                 fx, fy, fz = msg.wrench.force.x, msg.wrench.force.y, msg.wrench.force.z
00047                 tx, ty, tz = msg.wrench.torque.x, msg.wrench.torque.y, msg.wrench.torque.z
00048                 msg_time = rospy.get_time()
00049                 return -np.matrix([fx, fy, fz, tx, ty, tz]).T, msg_time
00050             msg_type=WrenchStamped
00051         else:
00052             def msg_converter(msg):
00053                 m = np.matrix(msg.data, 'f').T
00054                 msg_time = msg.header.stamp.to_time()
00055                 return m, msg_time
00056             msg_type = FloatArray
00057 
00058         ru.GenericListener.__init__(self, 'FTClient', msg_type,
00059                                     topic_name, 50.0,
00060                                     message_extractor = msg_converter,
00061                                     queue_size = None)
00062         self.bias_val = np.matrix([0, 0, 0, 0, 0, 0.]).T
00063 
00064     
00065     
00066     
00067     
00068     
00069     
00070     
00071     def read(self, avg=1, without_bias=False, fresh=False, with_time_stamp=False):
00072         assert(avg > 0)
00073         if avg > 1:
00074             fresh = True
00075             if with_time_stamp:
00076                 raise RuntimeError('Can\'t request averaging and timestamping at the same time')
00077 
00078         rs = []
00079         for i in range(avg):
00080             if fresh:
00081                 r, msg_time = ru.GenericListener.read(self, allow_duplication=False, willing_to_wait=True) 
00082             else:
00083                 r, msg_time = ru.GenericListener.read(self, allow_duplication=True, willing_to_wait=False) 
00084             rs.append(r)
00085         readings = ut.list_mat_to_mat(rs, axis=1)
00086 
00087         if not without_bias:
00088             
00089             
00090 
00091             ret = readings.mean(1) - self.bias_val
00092         else:
00093             ret = readings.mean(1)
00094 
00095         if with_time_stamp:
00096             return ret, msg_time
00097         else:
00098             return ret
00099 
00100     def bias(self):
00101         print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
00102         print 'BIASING FT'
00103         print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
00104 
00105         b_list = []
00106         for i in range(20):
00107             r, msg_time = ru.GenericListener.read(self, allow_duplication=False, willing_to_wait=True) 
00108             b_list.append(r)
00109 
00110         if b_list[0] != None:
00111             r = np.mean(np.column_stack(b_list), 1)
00112             self.bias_val = r
00113 
00114         print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
00115         print 'DONE biasing ft'
00116         print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
00117 
00118 if __name__ == '__main__':
00119     import optparse
00120     import time
00121     p = optparse.OptionParser()
00122     p.add_option('-t', action='store', default='force_torque_ft1', type='string', 
00123                  dest='topic', help='which topic to listen to (default force_torque_ft1)')
00124     p.add_option('--netft', action='store_true', dest='netft',
00125                  help='is this a NetFT sensor')
00126     opt, args = p.parse_args()
00127 
00128     client = FTClient(opt.topic, opt.netft)
00129     client.bias()
00130     while not rospy.is_shutdown():
00131         el = client.read()
00132         if el != None:
00133             
00134             f = el.A1
00135             print ' %.2f %.2f %.2f'%(f[0], f[1], f[2])
00136         time.sleep(1/100.0)
00137 
00138 
00139 
00140 
00141 
00142 
00143 
00144 
00145 
00146 
00147 
00148