FTRelay.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 import roslib
00003 roslib.load_manifest('force_torque')
00004 import rospy
00005 from geometry_msgs.msg import Vector3Stamped
00006 
00007 
00008 from threading import RLock
00009 
00010 
00011 
00012 ## 1D kalman filter update.
00013 def kalman_update(xhat, P, Q, R, z):
00014     #time update
00015     xhatminus = xhat
00016     Pminus = P + Q
00017     #measurement update
00018     K = Pminus / (Pminus + R)
00019     xhat = xhatminus + K * (z-xhatminus)
00020     P = (1-K) * Pminus
00021     return xhat, P
00022 
00023 
00024 
00025 class FTRelay:
00026     def __init__(self):
00027         self.lock = RLock()
00028         self.fresh = False
00029 
00030     def set_ft(self, value, time_acquired):
00031         self.lock.acquire()
00032         self.data = value, time_acquired
00033         self.fresh = True
00034         self.lock.release()
00035         #print 'got', value, time_acquired
00036 
00037     def get_msg(self):
00038         r = None
00039         self.lock.acquire()
00040         if self.fresh:
00041             self.fresh = False
00042             r = self.data
00043         self.lock.release()
00044         return r
00045 
00046 def FTread_to_Force( ftval, frame_id ):
00047     retval = Vector3Stamped()
00048     retval.header.stamp = rospy.rostime.get_rostime()
00049     retval.header.frame_id = frame_id
00050     retval.vector.x = ftval[0]
00051     retval.vector.y = ftval[1]
00052     retval.vector.z = ftval[2]
00053 
00054     return retval
00055 
00056 if __name__ == '__main__':
00057     import roslib; roslib.load_manifest('force_torque')
00058     import rospy
00059     from force_torque.srv import *
00060     from hrl_msgs.msg import FloatArray as FloatArray
00061     import hrl_lib.rutils as ru
00062     import time
00063     import force_torque.FTSensor as ftc
00064     import numpy as np
00065 
00066     import optparse
00067     p = optparse.OptionParser()
00068     p.add_option('--name', action='store', default='ft1', type='string', 
00069                  dest='name', help='name given to FTSensor')
00070     opt, args = p.parse_args()
00071 
00072     node_name       = 'FTRelay_'     + opt.name
00073     ft_channel_name = 'force_torque_' + opt.name
00074     service_name    = node_name + '_set_ft'
00075     print node_name + ': serving service', service_name
00076     ftserver = FTRelay()
00077 
00078     rospy.init_node(node_name)
00079     rospy.Service(service_name, StringService, 
00080             ru.wrap(ftserver.set_ft, ['value', 'time'], 
00081                     response=StringServiceResponse))
00082 
00083     channel = rospy.Publisher(ft_channel_name, FloatArray, tcp_nodelay=True)
00084     channel2 = rospy.Publisher(ft_channel_name + '_raw', FloatArray, tcp_nodelay=True)
00085     chan_vec3 = rospy.Publisher(ft_channel_name + '_Vec3', Vector3Stamped, tcp_nodelay=True)    
00086     print node_name + ': publishing on channel', ft_channel_name
00087     P_force = [1., 1., 1.]
00088     xhat_force = [0., 0., 0., 0., 0., 0.]
00089     while not rospy.is_shutdown():
00090         msg = ftserver.get_msg()
00091         if msg is not None:
00092             data, tme = msg
00093             ftvalue = ftc.binary_to_ft(data)
00094             ftvalue = np.array(ftvalue)
00095             for i in range(3):
00096                 xhat, p = kalman_update(xhat_force[i], P_force[i],
00097                         1e-3, 0.04, ftvalue[i])
00098                 P_force[i] = p
00099                 xhat_force[i] = xhat
00100                 #ftvalue[i] = xhat
00101             xhat_force[3] = ftvalue[3]
00102             xhat_force[4] = ftvalue[4]
00103             xhat_force[5] = ftvalue[5]
00104             ftvalue = ftvalue.tolist()
00105 
00106             channel.publish(FloatArray(rospy.Header(stamp=rospy.Time.from_seconds(tme)),
00107                 xhat_force))
00108             channel2.publish(FloatArray(rospy.Header(stamp=rospy.Time.from_seconds(tme)), ftvalue))
00109             chan_vec3.publish( FTread_to_Force( ftvalue, opt.name ))
00110             #times.append(time.time())
00111         #else:
00112         #    time.sleep(1/5000.0)
00113         time.sleep(1/5000.0)
00114 
00115     #import pylab as pl
00116     #import numpy as np
00117     #a = np.array(times)
00118     #pl.plot(a[1:] - a[:-1], '.')
00119     #pl.show()
00120 
00121 
00122 
00123 
00124 
00125 
00126 


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