00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 import roslib
00024 roslib.load_manifest("hrl_haptic_mpc")
00025 import rospy
00026 import tf
00027
00028 import hrl_lib.transforms as tr
00029 import hrl_haptic_manipulation_in_clutter_msgs.msg as haptic_msgs
00030 import geometry_msgs.msg
00031 import std_msgs.msg
00032
00033 import numpy as np
00034 import threading, copy
00035 import sys
00036
00037
00038 class TaxelArrayClient():
00039
00040
00041
00042
00043 def __init__(self, skin_topic_list, torso_frame="/torso_lift_link", tf_listener=None):
00044
00045 self.data_lock = threading.RLock()
00046
00047 self.topic_lock = threading.RLock()
00048
00049
00050 self.torso_frame = torso_frame
00051
00052 self.trim_threshold = 0.0
00053
00054
00055 self.skin_subs = {}
00056
00057 self.skin_data = {}
00058
00059 self.trimmed_skin_data = {}
00060
00061 try:
00062 if tf_listener == None:
00063
00064 self.tf_lstnr = tf.TransformListener()
00065 else:
00066
00067 self.tf_lstnr = tf_listener
00068 except rospy.ServiceException, e:
00069 rospy.loginfo("ServiceException caught while instantiating a TF listener. Seems to be normal")
00070 pass
00071
00072
00073 self.skin_topic_list = skin_topic_list
00074
00075 for skin_topic in self.skin_topic_list:
00076 self.addSkinTopic(skin_topic)
00077
00078
00079 rospy.Subscriber("/haptic_mpc/add_taxel_array", std_msgs.msg.String, self.addSkinTopicCallback)
00080
00081 rospy.Subscriber("/haptic_mpc/remove_taxel_array", std_msgs.msg.String, self.removeSkinTopicCallback)
00082
00083
00084 self.current_topics_pub = rospy.Publisher("/haptic_mpc/skin_topics", haptic_msgs.StringArray, latch=True)
00085 self.current_topics_pub.publish(self.skin_data.keys())
00086
00087
00088
00089 def setTrimThreshold(self, threshold):
00090 self.trim_threshold = threshold
00091
00092
00093
00094 def addSkinTopicCallback(self, msg):
00095 rospy.loginfo("Adding skin TaxelArray topic: %s" % str(msg.data))
00096 self.addSkinTopic(msg.data)
00097 rospy.loginfo("Current skin topics: \n%s", str(self.skin_subs.keys()))
00098 self.current_topics_pub.publish(self.skin_subs.keys())
00099
00100
00101
00102 def removeSkinTopicCallback(self, msg):
00103 rospy.loginfo("Removing skin TaxelArray topic: %s" % str(msg.data))
00104 self.removeSkinTopic(msg.data)
00105 rospy.loginfo("Current skin topics: \n%s", str(self.skin_subs.keys()))
00106 self.current_topics_pub.publish(self.skin_subs.keys())
00107
00108
00109
00110 def addSkinTopic(self, skin_topic):
00111 if skin_topic in self.skin_subs.keys():
00112 return
00113 with self.topic_lock:
00114 self.skin_topic_list.append(skin_topic)
00115 self.skin_data[skin_topic] = haptic_msgs.TaxelArray()
00116 self.skin_subs[skin_topic] = rospy.Subscriber(skin_topic, haptic_msgs.TaxelArray, self.skinCallback, skin_topic)
00117
00118
00119
00120 def removeSkinTopic(self, skin_topic):
00121 if skin_topic not in self.skin_subs.keys():
00122 rospy.loginfo("Skin topic not found")
00123 return
00124 with self.topic_lock:
00125 self.skin_topic_list.remove(skin_topic)
00126 self.skin_data.pop(skin_topic)
00127 self.skin_subs[skin_topic].unregister()
00128 self.skin_subs.pop(skin_topic)
00129
00130
00131
00132
00133
00134 def skinCallback(self, msg, skin_topic):
00135 with self.data_lock:
00136 self.skin_data[skin_topic] = msg
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 trimmed_msg = self.trimTaxelArray(msg, self.trim_threshold)
00164 transformed_msg = self.transformTaxelArray(trimmed_msg, self.torso_frame)
00165 self.trimmed_skin_data[skin_topic] = transformed_msg
00166
00167
00168
00169
00170
00171
00172 def transformTaxelArray(self, ta_msg, new_frame):
00173
00174
00175 if ta_msg.header.frame_id == "":
00176 return ta_msg
00177 self.tf_lstnr.waitForTransform(new_frame, ta_msg.header.frame_id, rospy.Time(0), rospy.Duration(4.0))
00178 t1, q1 = self.tf_lstnr.lookupTransform(new_frame, ta_msg.header.frame_id, rospy.Time(0))
00179
00180 t1 = np.matrix(t1).reshape(3,1)
00181 r1 = tr.quaternion_to_matrix(q1)
00182
00183
00184 new_ta_msg = copy.copy(ta_msg)
00185 new_ta_msg.header.frame_id = new_frame
00186
00187
00188
00189 pts = np.column_stack((ta_msg.centers_x, ta_msg.centers_y, ta_msg.centers_z))
00190 nrmls = np.column_stack((ta_msg.normals_x, ta_msg.normals_y, ta_msg.normals_z))
00191 values = np.column_stack((ta_msg.values_x, ta_msg.values_y, ta_msg.values_z))
00192
00193 pts = r1 * np.matrix(pts).T + t1
00194 nrmls = r1 * np.matrix(nrmls).T
00195 values = r1 * np.matrix(values).T
00196
00197
00198 pts_array = np.asarray(pts)
00199 nrmls_array = np.asarray(nrmls)
00200 values_array = np.asarray(values)
00201
00202 new_ta_msg.centers_x = pts_array[0, :].tolist()
00203 new_ta_msg.centers_y = pts_array[1, :].tolist()
00204 new_ta_msg.centers_z = pts_array[2, :].tolist()
00205
00206 new_ta_msg.normals_x = nrmls_array[0, :].tolist()
00207 new_ta_msg.normals_y = nrmls_array[1, :].tolist()
00208 new_ta_msg.normals_z = nrmls_array[2, :].tolist()
00209
00210 new_ta_msg.values_x = values_array[0, :].tolist()
00211 new_ta_msg.values_y = values_array[1, :].tolist()
00212 new_ta_msg.values_z = values_array[2, :].tolist()
00213
00214 return new_ta_msg
00215
00216
00217
00218 def trimSkinContacts(self, threshold):
00219 with self.data_lock:
00220 skin_data = copy.copy(self.skin_data)
00221
00222 for ta_topic in skin_data.keys():
00223 skin_data[ta_topic] = self.trimTaxelArray(skin_data[ta_topic], threshold)
00224
00225 with self.data_lock:
00226 self.trimmed_skin_data = skin_data
00227
00228 return skin_data
00229
00230
00231
00232 def trimTaxelArray(self, ta_msg, threshold):
00233 if threshold < 0.0:
00234 rospy.logerr("SkinClient Error: Threshold passed to trimContacts must be >= 0.0")
00235 return ta_msg
00236
00237
00238 new_ta_msg = haptic_msgs.TaxelArray()
00239 new_ta_msg.header = copy.copy(ta_msg.header)
00240 new_ta_msg.sensor_type = copy.copy(ta_msg.sensor_type)
00241
00242
00243 for i in range(0, len(ta_msg.centers_x)):
00244 magnitude = np.sqrt(ta_msg.values_x[i]**2 + ta_msg.values_y[i]**2 + ta_msg.values_z[i]**2)
00245
00246 threshold_valid = False
00247 if ta_msg.sensor_type == "force" and (magnitude >= threshold or magnitude <= -threshold):
00248 threshold_valid = True
00249 elif ta_msg.sensor_type == "distance" and (magnitude <= abs(threshold)):
00250 threshold_valid = True
00251 elif ta_msg.sensor_type == '' and (magnitude >= threshold or magnitude <= -threshold):
00252 threshold_valid = True
00253
00254 if threshold_valid:
00255
00256 new_ta_msg.values_x.append(ta_msg.values_x[i])
00257 new_ta_msg.values_y.append(ta_msg.values_y[i])
00258 new_ta_msg.values_z.append(ta_msg.values_z[i])
00259
00260 new_ta_msg.centers_x.append(ta_msg.centers_x[i])
00261 new_ta_msg.centers_y.append(ta_msg.centers_y[i])
00262 new_ta_msg.centers_z.append(ta_msg.centers_z[i])
00263
00264 new_ta_msg.normals_x.append(ta_msg.normals_x[i])
00265 new_ta_msg.normals_y.append(ta_msg.normals_y[i])
00266 new_ta_msg.normals_z.append(ta_msg.normals_z[i])
00267
00268
00269
00270 if i < len(ta_msg.link_names):
00271 new_ta_msg.link_names.append(ta_msg.link_names[i])
00272
00273 return new_ta_msg
00274
00275
00276
00277 def getSkinData(self):
00278 with self.data_lock:
00279 return copy.copy(self.skin_data)
00280
00281
00282
00283 def getTrimmedSkinData(self):
00284 with self.data_lock:
00285 return copy.copy(self.trimmed_skin_data)
00286
00287
00288
00289
00290
00291 def getContactLocationsFromTaxelArray(self, ta_msg):
00292 points_list = []
00293 for i in range(0, len(ta_msg.centers_x)):
00294 point_vector = np.matrix([ta_msg.centers_x[i], ta_msg.centers_y[i], ta_msg.centers_z[i]]).T
00295 points_list.append(point_vector)
00296 return points_list
00297
00298
00299
00300
00301 def getTaxelLocationAndJointList(self):
00302 raise RuntimeError('Unimplemented function.')