kinematics_estimator_least_squares.py
Go to the documentation of this file.
00001 #
00002 #
00003 # Copyright (c) 2010, 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 
00031 
00032 import roslib; roslib.load_manifest('2010_icra_epc_pull')
00033 import rospy
00034 
00035 from 2010_icra_epc_pull.msg import MechanismKinematicsRot
00036 from geometry_msgs.msg import Point32
00037 
00038 import arm_trajectories as at
00039 
00040 from threading import RLock
00041 import numpy as np
00042 import time
00043 
00044 ##
00045 # fit circle to the trajectory, publish the computed kinematics.
00046 # @param cartesian_pts_list - list of 3-tuples. trajectory of the mechanism
00047 # @param pbshr - publisher for the MechanismKinematics message
00048 # @param lock - to make list operations thread safe. (there is a callback too.)
00049 def circle_estimator(cartesian_pts_list, pbshr, lock):
00050     lock.acquire()
00051     n_pts = len(cartesian_pts_list)
00052     pts_2d = (np.matrix(cartesian_pts_list).T)[0:2,:]
00053     lock.release()
00054 
00055     if n_pts<2:
00056         time.sleep(0.1)
00057         #pbshr.publish(mk) # don't publish anything.
00058         return
00059 
00060     st = pts_2d[:,0]
00061     now = pts_2d[:,-1]
00062 
00063     mk = MechanismKinematicsRot()
00064     mk.cx = 0.5
00065     mk.cy = -3.5
00066     mk.cz = cartesian_pts_list[0][2]
00067     mk.rad = 10.
00068 
00069     dist_moved = np.linalg.norm(st-now)
00070 
00071     if dist_moved<=0.1:
00072         reject_pts_num = n_pts
00073     else:
00074         reject_pts_num = 1
00075 
00076     if dist_moved<=0.15:
00077         time.sleep(0.1)
00078         pbshr.publish(mk)
00079         return
00080 
00081 #    pts_2d = (np.matrix(cartesian_pts_list).T)[0:2,:]
00082     pts_2d = pts_2d[:,reject_pts_num:]
00083 
00084     rad = 1.0
00085     #start_pos = np.matrix(cartesian_pts_list[0]).T
00086     start_pos = st
00087     rad,cx,cy = at.fit_circle(rad, start_pos[0,0], start_pos[1,0]-rad,
00088                               pts_2d, method='fmin_bfgs', verbose=False)
00089     mk.cx = cx
00090     mk.cy = cy
00091     mk.rad = rad
00092     pbshr.publish(mk)
00093 
00094 # append the point to the trajectory
00095 def trajectory_cb(pt32, tup):
00096     cp_list, lock = tup
00097     lock.acquire()
00098     cp_list.append([pt32.x, pt32.y, pt32.z])
00099     lock.release()
00100 
00101 
00102 if __name__ == '__main__':
00103 
00104     cartesian_points_list = []
00105     lock = RLock()
00106     rospy.init_node('kinematics_estimator_least_sq')
00107     mech_kin_pub = rospy.Publisher('mechanism_kinematics_rot',
00108                                     MechanismKinematicsRot)
00109     rospy.Subscriber('mechanism_trajectory', Point32, trajectory_cb,
00110                      (cartesian_points_list, lock))
00111 
00112     print 'Begin'
00113     while not rospy.is_shutdown():
00114         circle_estimator(cartesian_points_list, mech_kin_pub, lock)
00115         time.sleep(0.01)
00116     print 'End'
00117 
00118 


2010_icra_epc_pull
Author(s): Advait Jain, Charles C. Kemp (Healthcare Robotics Lab, Georgia Tech)
autogenerated on Wed Nov 27 2013 12:14:43