FTClient.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 #
00003 # Copyright (c) 2009, Georgia Tech Research Corporation
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions are met:
00008 #     * Redistributions of source code must retain the above copyright
00009 #       notice, this list of conditions and the following disclaimer.
00010 #     * Redistributions in binary form must reproduce the above copyright
00011 #       notice, this list of conditions and the following disclaimer in the
00012 #       documentation and/or other materials provided with the distribution.
00013 #     * Neither the name of the Georgia Tech Research Corporation nor the
00014 #       names of its contributors may be used to endorse or promote products
00015 #       derived from this software without specific prior written permission.
00016 #
00017 # THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS'' AND
00018 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 # DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH BE LIABLE FOR ANY DIRECT, INDIRECT,
00021 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00025 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00026 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 #
00028 
00029 #  \author Advait Jain (Healthcare Robotics Lab, Georgia Tech.)
00030 #  \author Cressel Anderson (Healthcare Robotics Lab, Georgia Tech.)
00031 #  \author Hai Nguyen (Healthcare Robotics Lab, Georgia Tech.)
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 # Corresponding client class
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     # Read a force torque value
00066     # @param avg how many force torque value to average
00067     # @param without_bias
00068     # @param fresh
00069     # @param with_time_stamp 
00070     # @return an averaged force torque value (6x1 matrix)
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             #print 'readiings.mean(1)', readings.mean(1)
00089             #print 'self.bias_val', self.bias_val
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             #print np.linalg.norm(el.T)
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 


force_torque
Author(s): Advait Jain, Cressel Anderson, Hai Nguyen, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:58:12