footstep_visualizer.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import rospy
00004 import tf2_ros
00005 from geometry_msgs.msg import PointStamped
00006 from sensor_msgs.msg import Image
00007 
00008 from cv_bridge import CvBridge
00009 from tf.transformations import *
00010 import cv2 as cv
00011 import numpy as np
00012 from threading import Lock
00013 from math import pi
00014 msg_lock = Lock()
00015 def matrixFromTranslationQuaternion(trans, q):
00016     return  concatenate_matrices(translation_matrix(trans),
00017                                  quaternion_matrix(q))
00018 
00019 def verticesPoints(original_vertices, origin_pose, scale, resolution_size):
00020     original_vertices_3d_local = [[v[0], v[1], 0.0] for v in original_vertices]
00021     vertices_3d_pose = [concatenate_matrices(origin_pose, translation_matrix(v)) for v in original_vertices_3d_local]
00022     vertices_3d = [translation_from_matrix(v) for v in vertices_3d_pose]
00023     
00024     return [(int(v[0] / (scale / 2.0) * resolution_size / 2.0 + resolution_size / 2.0),
00025              int(v[1] / (scale / 2.0) * resolution_size / 2.0 + resolution_size / 2.0))
00026             for v in vertices_3d]
00027 
00028 def transformToMatrix(transform):
00029     return matrixFromTranslationQuaternion([transform.translation.x,
00030                                             transform.translation.y,
00031                                             transform.translation.z],
00032                                            [transform.rotation.x,
00033                                             transform.rotation.y,
00034                                             transform.rotation.z,
00035                                             transform.rotation.w])
00036 
00037 #def cop_callback(lleg_cop, rleg_cop):
00038 def periodicCallback(event):
00039     global tf_buffer, lleg_cop_msg, rleg_cop_msg, zmp_msg
00040     try:
00041         msg_lock.acquire()
00042         _lleg_pose = tf_buffer.lookup_transform(root_link, lleg_end_coords, rospy.Time())
00043         _rleg_pose = tf_buffer.lookup_transform(root_link, rleg_end_coords, rospy.Time())
00044         if lleg_cop_msg:
00045             _lleg_cop_origin = tf_buffer.lookup_transform(root_link, lleg_cop_msg.header.frame_id, rospy.Time())
00046             lleg_cop_origin = transformToMatrix(_lleg_cop_origin.transform)
00047             lleg_cop_point = np.array([lleg_cop_msg.point.x,
00048                                        lleg_cop_msg.point.y,
00049                                        lleg_cop_msg.point.z])
00050         if rleg_cop_msg:
00051             _rleg_cop_origin = tf_buffer.lookup_transform(root_link, rleg_cop_msg.header.frame_id, rospy.Time())
00052             rleg_cop_origin = transformToMatrix(_rleg_cop_origin.transform)
00053             rleg_cop_point = np.array([rleg_cop_msg.point.x,
00054                                        rleg_cop_msg.point.y,
00055                                        rleg_cop_msg.point.z])
00056         if zmp_msg:
00057             _zmp_origin = tf_buffer.lookup_transform(root_link, zmp_msg.header.frame_id, rospy.Time())
00058             zmp_origin = transformToMatrix(_zmp_origin.transform)
00059             zmp_point = np.array([zmp_msg.point.x,
00060                                        zmp_msg.point.y,
00061                                        zmp_msg.point.z])
00062         lleg_pos = np.array([_lleg_pose.transform.translation.x,
00063                              _lleg_pose.transform.translation.y,
00064                              _lleg_pose.transform.translation.z])
00065         lleg_rot = np.array([_lleg_pose.transform.rotation.x,
00066                              _lleg_pose.transform.rotation.y,
00067                              _lleg_pose.transform.rotation.z,
00068                              _lleg_pose.transform.rotation.w])
00069         rleg_pos = np.array([_rleg_pose.transform.translation.x,
00070                              _rleg_pose.transform.translation.y,
00071                              _rleg_pose.transform.translation.z])
00072         rleg_rot = np.array([_rleg_pose.transform.rotation.x,
00073                              _rleg_pose.transform.rotation.y,
00074                              _rleg_pose.transform.rotation.z,
00075                              _rleg_pose.transform.rotation.w])
00076         
00077         lleg_pose = matrixFromTranslationQuaternion(lleg_pos, lleg_rot)
00078         rleg_pose = matrixFromTranslationQuaternion(rleg_pos, rleg_rot)
00079         mid_coords = concatenate_matrices(matrixFromTranslationQuaternion((lleg_pos + rleg_pos) / 2.0,
00080                                                                           quaternion_slerp(lleg_rot, rleg_rot, 0.5)),
00081                                           euler_matrix(pi, 0, 0))
00082         lleg_from_mid = concatenate_matrices(inverse_matrix(mid_coords),
00083                                              lleg_pose)
00084         rleg_from_mid = concatenate_matrices(inverse_matrix(mid_coords),
00085                                              rleg_pose)
00086         lleg_points = verticesPoints(lleg_vertices, lleg_from_mid, scale, image_size)
00087         rleg_points = verticesPoints(rleg_vertices, rleg_from_mid, scale, image_size)
00088         image = np.zeros((image_size, image_size, 4), dtype=np.uint8)
00089         cv.line(image, lleg_points[0], lleg_points[1], (0, 255, 0, 255), 2)
00090         cv.line(image, lleg_points[1], lleg_points[2], (0, 255, 0, 255), 2)
00091         cv.line(image, lleg_points[2], lleg_points[3], (0, 255, 0, 255), 2)
00092         cv.line(image, lleg_points[3], lleg_points[0], (0, 255, 0, 255), 2)
00093         cv.line(image, rleg_points[0], rleg_points[1], (0, 0, 255, 255), 2)
00094         cv.line(image, rleg_points[1], rleg_points[2], (0, 0, 255, 255), 2)
00095         cv.line(image, rleg_points[2], rleg_points[3], (0, 0, 255, 255), 2)
00096         cv.line(image, rleg_points[3], rleg_points[0], (0, 0, 255, 255), 2)
00097         if lleg_cop_msg:
00098             lleg_cop_point_2d = verticesPoints([lleg_cop_point],
00099                                                concatenate_matrices(inverse_matrix(mid_coords),
00100                                                                     lleg_cop_origin),
00101                                                scale,
00102                                                image_size)[0]
00103             cv.circle(image, lleg_cop_point_2d, 5, (0, 255, 0, 255), -1)
00104             lleg_cop_msg = None
00105         if rleg_cop_msg:
00106             rleg_cop_point_2d = verticesPoints([rleg_cop_point],
00107                                                concatenate_matrices(inverse_matrix(mid_coords),
00108                                                                     rleg_cop_origin),
00109                                                scale,
00110                                                image_size)[0]
00111             cv.circle(image, rleg_cop_point_2d, 5, (0, 0, 255, 255), -1)
00112             rleg_cop_msg = None
00113         if zmp_msg:
00114             zmp_point_2d = verticesPoints([zmp_point],
00115                                                concatenate_matrices(inverse_matrix(mid_coords),
00116                                                                     zmp_origin),
00117                                                scale,
00118                                                image_size)[0]
00119             cv.circle(image, zmp_point_2d, 5, (0, 255, 255, 255), -1)
00120             zmp_msg = None
00121         bridge = CvBridge()
00122         pub.publish(bridge.cv2_to_imgmsg(image, "bgra8"))
00123     except (tf2_ros.LookupException, tf2_ros.ConnectivityException, tf2_ros.ExtrapolationException):
00124         # rospy.logerr("Failed to lookup transform")
00125         pass
00126     finally:
00127         msg_lock.release()
00128 
00129 lleg_cop_msg = None
00130 rleg_cop_msg = None
00131 zmp_msg = None
00132 def llegCopCallback(msg):
00133     global lleg_cop_msg
00134     with msg_lock:
00135         lleg_cop_msg = msg
00136 
00137 
00138 def rlegCopCallback(msg):
00139     global rleg_cop_msg
00140     with msg_lock:
00141         rleg_cop_msg = msg
00142 
00143 
00144 def zmpCallback(msg):
00145     global zmp_msg
00146     with msg_lock:
00147         zmp_msg = msg
00148 
00149         
00150 if __name__ == "__main__":
00151     rospy.init_node("footstep_visualizer")
00152     pub = rospy.Publisher("~output", Image)
00153     tf_buffer = tf2_ros.Buffer()
00154     tf_listener = tf2_ros.TransformListener(tf_buffer)
00155     lleg_end_coords = rospy.get_param("~lleg_end_coords", "lleg_end_coords")
00156     rleg_end_coords = rospy.get_param("~rleg_end_coords", "rleg_end_coords")
00157     image_size = rospy.get_param("~image_size", 400)
00158     scale = rospy.get_param("~scale", 0.6)
00159     lleg_vertices = rospy.get_param("~lleg_vertices", [[0.137525, -0.070104],
00160                                                        [-0.106925, -0.070104],
00161                                                        [-0.106925,  0.070104],
00162                                                        [ 0.137525,  0.070104]])
00163     rleg_vertices = rospy.get_param("~rleg_vertices", [[0.137525, -0.070104],
00164                                                        [-0.106925, -0.070104],
00165                                                        [-0.106925, 0.070104],
00166                                                        [0.137525, 0.070104]])
00167                                                        
00168     root_link = rospy.get_param("~root_link", "BODY")
00169     lleg_sub = rospy.Subscriber("/lfsensor_cop", PointStamped, llegCopCallback)
00170     rleg_sub = rospy.Subscriber("/rfsensor_cop", PointStamped, rlegCopCallback)
00171     zmp_sub = rospy.Subscriber("/zmp", PointStamped, zmpCallback)
00172     # lleg_cop_sub = Subscriber("/lfsensor_cop", PointStamped)
00173     # rleg_cop_sub = Subscriber("/rfsensor_cop", PointStamped)
00174     # ts = TimeSynchronizer([lleg_cop_sub, rleg_cop_sub], 10)
00175     # ts.registerCallback(cop_callback)
00176     rospy.Timer(rospy.Duration(0.1), periodicCallback)
00177     rospy.spin()
00178     


jsk_footstep_controller
Author(s):
autogenerated on Wed Sep 16 2015 04:38:12