in_hand_recognition_manager.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # program for recognition in hand
4 # depends on the node-structure of icp_registration
5 # suppose that robot does not move throughout recognizing
6 
7 import rospy
8 
9 PKG='jsk_pcl_ros'
10 
11 import imp
12 try:
13  imp.find_module(PKG)
14 except:
15  import roslib;roslib.load_manifest(PKG)
16 
17 from geometry_msgs.msg import PoseStamped, Pose
18 from sensor_msgs.msg import PointCloud2
19 from jsk_recognition_msgs.msg import PointsArray
20 from tf.transformations import *
21 import tf
22 from std_srvs import srv
23 
24 teacher_pose_stamped = None
25 renew_flag = False
26 def pose_teacher_cb(pose_stamped):
27  global teacher_pose_stamped
28  teacher_pose_stamped = pose_stamped
29  InputPosePub.publish(teacher_pose_stamped)
30  # pub pose and save pose
32  return concatenate_matrices(
33  translation_matrix([pose.position.x, pose.position.y, pose.position.z]),
34  quaternion_matrix([pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w])
35  )
37  translation = translation_from_matrix(mat)
38  rotation = quaternion_from_matrix(mat)
39  pose = Pose()
40  pose.position.x = translation[0]
41  pose.position.y = translation[1]
42  pose.position.z = translation[2]
43  pose.orientation.x = rotation[0]
44  pose.orientation.y = rotation[1]
45  pose.orientation.z = rotation[2]
46  pose.orientation.w = rotation[3]
47  return pose
48 
49 def pose_diff_cb(pose_stamped):
50  global teacher_pose_stamped, renew_flag
51  # add diff and pub
52  if (not teacher_pose_stamped):
53  rospy.loginfo("teacher is empty")
54  return
55  try:
56  teacher_pose_stamped.header.stamp = rospy.Time(0)
57  teacher_pose_stamped_recog_frame = listener.transformPose(pose_stamped.header.frame_id, teacher_pose_stamped)
59  print("tf error: %s" % e)
60  return
61  teacher_pose_mat = (get_mat_from_pose(teacher_pose_stamped_recog_frame.pose))
62  diff_pose_mat = (get_mat_from_pose(pose_stamped.pose))
63  new_pose_mat = concatenate_matrices(diff_pose_mat, teacher_pose_mat)
64  new_pose_stamped = PoseStamped()
65  new_pose_stamped.pose = get_pose_from_mat(new_pose_mat)
66  new_pose_stamped.header = pose_stamped.header
67  OutputPosePub.publish(new_pose_stamped)
68  # teachear_pose_stamped = None
69  if renew_flag:
70  try:
71  new_pose_stamped.header.stamp = rospy.Time(0)
72  new_pose_stamped_for_renew = listener.transformPose(teacher_pose_stamped.header.frame_id, new_pose_stamped)
74  print("tf error: %s" % e)
75  return
76  pose_teacher_cb(new_pose_stamped_for_renew)
77  renew_flag = False
78 def renew_cb(req):
79  global renew_flag
80  renew_flag = True
81  return srv.EmptyResponse()
82 if __name__ == "__main__":
83  rospy.init_node("in_hand_recognition_manager")
84  InputPosePub = rospy.Publisher(
85  "~output/recognition", PoseStamped, queue_size=1)
86  OutputPosePub = rospy.Publisher("~output", PoseStamped, queue_size=1)
87  listener = tf.TransformListener()
88  rospy.Subscriber("~input", PoseStamped, pose_teacher_cb)
89  rospy.Subscriber("~input/result", PoseStamped, pose_diff_cb)
90  rospy.Service("~renew", srv.Empty, renew_cb)
91  rospy.spin()
92 


jsk_pcl_ros
Author(s): Yohei Kakiuchi
autogenerated on Mon May 3 2021 03:03:46