camera_coords_change_trigger.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 from __future__ import absolute_import
00004 from __future__ import division
00005 from __future__ import print_function
00006 
00007 import numpy as np
00008 from termcolor import colored
00009 
00010 from geometry_msgs.msg import PoseStamped
00011 import rospy
00012 from std_srvs.srv import Trigger
00013 import tf
00014 
00015 
00016 def compute_pose_delta(pose1, pose2):
00017     delta_x = pose1.position.x - pose2.position.x
00018     delta_y = pose1.position.y - pose2.position.y
00019     delta_z = pose1.position.z - pose2.position.z
00020     delta_pos = np.linalg.norm([delta_x, delta_y, delta_z])
00021     delta_x = pose1.orientation.x - pose2.orientation.x
00022     delta_y = pose1.orientation.y - pose2.orientation.y
00023     delta_z = pose1.orientation.z - pose2.orientation.z
00024     delta_w = pose1.orientation.w - pose2.orientation.w
00025     delta_ori = np.linalg.norm([delta_x, delta_y, delta_z, delta_w])
00026     return delta_pos, delta_ori
00027 
00028 
00029 class CameraCoordsChangeTrigger(object):
00030 
00031     def __init__(self):
00032         self.delta_pos = rospy.get_param('~delta_position', 0.03)
00033         self.delta_ori = rospy.get_param('~delta_orientation', 0.03)
00034         self.velocify_pos = rospy.get_param('~velocify_position', 0.01)
00035         self.velocify_ori = rospy.get_param('~velocify_orientation', 0.01)
00036 
00037         self.listener = tf.TransformListener()
00038         rospy.loginfo("Waiting for service '{}'"
00039                       .format(rospy.resolve_name('~trigger')))
00040         rospy.wait_for_service('~trigger')
00041         self.trigger = rospy.ServiceProxy('~trigger', Trigger)
00042         self.timer = rospy.Timer(rospy.Duration(1. / 100),
00043                                  self.timer_callback)
00044         self.last_pose_stamped = None
00045         self.last_saved_pose_stamped = None
00046 
00047     def timer_callback(self, event):
00048         # Get current camera coords
00049         stamp = rospy.Time.now()
00050         src_frame = '/world'
00051         dst_frame = '/head_mount_kinect_rgb_optical_frame'
00052         try:
00053             self.listener.waitForTransform(src_frame, dst_frame, stamp,
00054                                            timeout=rospy.Duration(1))
00055         except Exception as e:
00056             rospy.logerr(e)
00057             return
00058         dst_pose = self.listener.lookupTransform(src_frame, dst_frame, stamp)
00059         pose_stamped = PoseStamped()
00060         pose_stamped.header.frame_id = src_frame
00061         pose_stamped.header.stamp = stamp
00062         pose_stamped.pose.position.x = dst_pose[0][0]
00063         pose_stamped.pose.position.y = dst_pose[0][1]
00064         pose_stamped.pose.position.z = dst_pose[0][2]
00065         pose_stamped.pose.orientation.x = dst_pose[1][0]
00066         pose_stamped.pose.orientation.y = dst_pose[1][1]
00067         pose_stamped.pose.orientation.z = dst_pose[1][2]
00068         pose_stamped.pose.orientation.w = dst_pose[1][3]
00069 
00070         # Check the change
00071         delta_saving_pos, delta_saving_ori = np.inf, np.inf
00072         vel_pos, vel_ori = 0, 0
00073         if self.last_pose_stamped is not None:
00074             delta_pos, delta_ori = compute_pose_delta(
00075                 pose_stamped.pose, self.last_pose_stamped.pose)
00076             delta_time = stamp - self.last_pose_stamped.header.stamp
00077             vel_pos = delta_pos / delta_time.to_sec()
00078             vel_ori = delta_ori / delta_time.to_sec()
00079         if self.last_saved_pose_stamped is not None:
00080             delta_saving_pos, delta_saving_ori = compute_pose_delta(
00081                 pose_stamped.pose, self.last_saved_pose_stamped.pose)
00082 
00083         self.last_pose_stamped = pose_stamped
00084 
00085         logging_msg = ('delta_saving_pos: {}, delta_saving_ori: {}, '
00086                        'vel_pos: {}, vel_ori: {}'
00087                        .format(delta_saving_pos,
00088                                delta_saving_ori, vel_pos, vel_ori))
00089         # Save according to the change
00090         if (delta_saving_pos > self.delta_pos and
00091                 delta_saving_ori > self.delta_ori and
00092                 vel_pos < self.velocify_pos and
00093                 vel_ori < self.velocify_ori):
00094             rospy.loginfo(logging_msg)
00095             rospy.loginfo(colored('Sending saving result', 'blue'))
00096             res = self.trigger()
00097             rospy.loginfo(colored('Saving request result success: {}'
00098                                   .format(res.success),
00099                                   'green' if res.success else 'red'))
00100             if res.success:
00101                 self.last_saved_pose_stamped = pose_stamped
00102         else:
00103             rospy.loginfo_throttle(1, logging_msg)
00104 
00105 
00106 def main():
00107     rospy.init_node('camera_coords_change_trigger')
00108     CameraCoordsChangeTrigger()
00109     rospy.spin()
00110 
00111 
00112 if __name__ == '__main__':
00113     main()


jsk_data
Author(s):
autogenerated on Fri Sep 8 2017 03:39:16