3 from hrpsys_ros_bridge.srv
import OpenHRP_StabilizerService_getParameter
as getParameter
5 from tf2_py
import ExtrapolationException
7 from geometry_msgs.msg
import PointStamped
8 from sensor_msgs.msg
import Image
10 from cv_bridge
import CvBridge
14 from threading
import Lock
15 from math
import pi, isnan
17 from hrpsys_ros_bridge.msg
import ContactStatesStamped, ContactStateStamped, ContactState
18 from jsk_footstep_controller.cfg
import FootstepVisualizerConfig
19 from dynamic_reconfigure.server
import Server
23 return concatenate_matrices(translation_matrix(trans),
27 original_vertices_3d_local = [[v[0], v[1], 0.0]
for v
in original_vertices]
28 vertices_3d_pose = [concatenate_matrices(origin_pose, translation_matrix(v))
for v
in original_vertices_3d_local]
29 vertices_3d = [translation_from_matrix(v)
for v
in vertices_3d_pose]
31 return [(int(v[0] / (scale / 2.0) * resolution_size / 2.0 + resolution_size / 2.0),
32 int(v[1] / (scale / 2.0) * resolution_size / 2.0 + resolution_size / 2.0))
36 for v
in original_vertices:
37 if isnan(v[0])
or isnan(v[1])
or isnan(v[2]):
39 original_vertices_3d_local = original_vertices
40 vertices_3d_pose = [concatenate_matrices(origin_pose, translation_matrix(v))
for v
in original_vertices_3d_local]
41 vertices_3d = [translation_from_matrix(v)
for v
in vertices_3d_pose]
44 return [(int(v[0] / (scale / 2.0) * resolution_size / 2.0 + resolution_size / 2.0),
45 int(v[1] / (scale / 2.0) * resolution_size / 2.0 + resolution_size / 2.0))
50 transform.translation.y,
51 transform.translation.z],
52 [transform.rotation.x,
55 transform.rotation.w])
57 ACT_CP_COLOR=(192, 202, 164)
58 REF_CP_COLOR=(167, 60, 151)
61 cv.circle(image, point, 7, color + (255,), -1)
62 cv.putText(image, text, (point[0] + 5, point[1] + 5),
63 cv.FONT_HERSHEY_PLAIN, 1.0, color + (255,))
68 global tf_buffer, lleg_cop_msg, rleg_cop_msg, zmp_msg, act_cp_msg, ref_cp_msg, act_contact_states_msg
72 _lleg_pose = tf_buffer.lookup_transform(root_link, lleg_end_coords, rospy.Time())
73 _rleg_pose = tf_buffer.lookup_transform(root_link, rleg_end_coords, rospy.Time())
75 _lleg_cop_origin = tf_buffer.lookup_transform(root_link, lleg_cop_msg.header.frame_id, rospy.Time())
77 lleg_cop_point = np.array([lleg_cop_msg.point.x,
79 lleg_cop_msg.point.z])
81 _rleg_cop_origin = tf_buffer.lookup_transform(root_link, rleg_cop_msg.header.frame_id, rospy.Time())
83 rleg_cop_point = np.array([rleg_cop_msg.point.x,
85 rleg_cop_msg.point.z])
87 _zmp_origin = tf_buffer.lookup_transform(root_link, zmp_msg.header.frame_id, rospy.Time())
89 zmp_point = np.array([zmp_msg.point.x,
93 _act_cp_origin = tf_buffer.lookup_transform(root_link, act_cp_msg.header.frame_id, rospy.Time())
95 act_cp_point = np.array([act_cp_msg.point.x,
99 _ref_cp_origin = tf_buffer.lookup_transform(root_link, ref_cp_msg.header.frame_id, rospy.Time())
101 ref_cp_point = np.array([ref_cp_msg.point.x,
104 lleg_pos = np.array([_lleg_pose.transform.translation.x,
105 _lleg_pose.transform.translation.y,
106 _lleg_pose.transform.translation.z])
107 lleg_rot = np.array([_lleg_pose.transform.rotation.x,
108 _lleg_pose.transform.rotation.y,
109 _lleg_pose.transform.rotation.z,
110 _lleg_pose.transform.rotation.w])
111 rleg_pos = np.array([_rleg_pose.transform.translation.x,
112 _rleg_pose.transform.translation.y,
113 _rleg_pose.transform.translation.z])
114 rleg_rot = np.array([_rleg_pose.transform.rotation.x,
115 _rleg_pose.transform.rotation.y,
116 _rleg_pose.transform.rotation.z,
117 _rleg_pose.transform.rotation.w])
122 quaternion_slerp(lleg_rot, rleg_rot, 0.5)),
123 euler_matrix(pi, 0, 0))
124 lleg_from_mid = concatenate_matrices(inverse_matrix(mid_coords),
126 rleg_from_mid = concatenate_matrices(inverse_matrix(mid_coords),
128 lleg_points =
verticesPoints(lleg_vertices, lleg_from_mid, scale, image_size)
129 rleg_points =
verticesPoints(rleg_vertices, rleg_from_mid, scale, image_size)
130 lleg_margined_points =
verticesPoints(lleg_margined_vertices, lleg_from_mid, scale, image_size)
131 rleg_margined_points =
verticesPoints(rleg_margined_vertices, rleg_from_mid, scale, image_size)
132 image = np.zeros((image_size, image_size, 4), dtype=np.uint8)
135 lleg_color = (0, 255, 0, 255)
136 rleg_color = (0, 0, 255, 255)
139 if act_contact_states_msg:
140 for state
in act_contact_states_msg.states:
141 if state.header.frame_id ==
"lfsensor":
142 if state.state.state == ContactState.ON:
143 lleg_color = (0, 255, 0, 255)
147 lleg_color = (150, 255, 150, 255)
149 if state.header.frame_id ==
"rfsensor":
150 if state.state.state == ContactState.ON:
151 rleg_color = (0, 0, 255, 255)
155 rleg_color = (150, 150, 255, 255)
158 cv.line(image, lleg_points[0], lleg_points[1], lleg_color, lleg_width)
159 cv.line(image, lleg_points[1], lleg_points[2], lleg_color, lleg_width)
160 cv.line(image, lleg_points[2], lleg_points[3], lleg_color, lleg_width)
161 cv.line(image, lleg_points[3], lleg_points[0], lleg_color, lleg_width)
162 cv.line(image, rleg_points[0], rleg_points[1], rleg_color, rleg_width)
163 cv.line(image, rleg_points[1], rleg_points[2], rleg_color, rleg_width)
164 cv.line(image, rleg_points[2], rleg_points[3], rleg_color, rleg_width)
165 cv.line(image, rleg_points[3], rleg_points[0], rleg_color, rleg_width)
166 if lleg_contact
and rleg_contact:
167 hull = cv.convexHull(np.array(lleg_points + rleg_points))
168 cv.drawContours(image, [hull], -1, (155, 155, 155, 255), 2)
169 margined_hull = cv.convexHull(np.array(lleg_margined_points + rleg_margined_points))
171 margined_hull = cv.convexHull(np.array(lleg_margined_points))
173 margined_hull = cv.convexHull(np.array(rleg_margined_points))
174 if (lleg_contact
or rleg_contact)
and g_config.cp_safe_area:
175 cv.drawContours(image, [margined_hull], -1, (255, 255, 255, 255), 2)
176 if lleg_cop_msg
and g_config.lcop:
178 concatenate_matrices(inverse_matrix(mid_coords),
182 if lleg_cop_point_2d:
183 drawPoint(image, lleg_cop_point_2d, 5, (0, 255, 0),
"LCoP")
185 if rleg_cop_msg
and g_config.rcop:
187 concatenate_matrices(inverse_matrix(mid_coords),
191 if rleg_cop_point_2d:
192 drawPoint(image, rleg_cop_point_2d, 5, (0, 0, 255),
"RCoP")
194 if zmp_msg
and g_config.zmp:
196 concatenate_matrices(inverse_matrix(mid_coords),
201 drawPoint(image, zmp_point_2d, 5, (0, 255, 255),
"ZMP")
203 if ref_cp_msg
and g_config.ref_cp:
205 concatenate_matrices(inverse_matrix(mid_coords),
210 drawPoint(image, ref_cp_point_2d, 7, REF_CP_COLOR,
"RCP")
212 if act_cp_msg
and g_config.act_cp:
214 concatenate_matrices(inverse_matrix(mid_coords),
219 drawPoint(image, act_cp_point_2d, 7, ACT_CP_COLOR,
"ACP")
222 pub.publish(bridge.cv2_to_imgmsg(image,
"bgra8"))
223 except ExtrapolationException:
224 rospy.logwarn(
"tf error")
235 act_contact_states_msg =
None 258 global act_contact_states_msg
260 act_contact_states_msg = msg
272 if __name__ ==
"__main__":
273 rospy.init_node(
"footstep_visualizer")
274 pub = rospy.Publisher(
"~output", Image, queue_size=1)
275 srv = Server(FootstepVisualizerConfig, config_callback)
278 rospy.loginfo(
"wait for st server")
279 rospy.wait_for_service(
"/StabilizerServiceROSBridge/getParameter")
280 rospy.loginfo(
"st server found")
281 g_get_parameter_srv = rospy.ServiceProxy(
"/StabilizerServiceROSBridge/getParameter", getParameter)
282 lleg_end_coords = rospy.get_param(
"~lleg_end_coords",
"lleg_end_coords")
283 rleg_end_coords = rospy.get_param(
"~rleg_end_coords",
"rleg_end_coords")
284 image_size = rospy.get_param(
"~image_size", 400)
285 scale = rospy.get_param(
"~scale", 0.6)
287 leg_front_margin = i_param.eefm_leg_front_margin
288 leg_rear_margin = i_param.eefm_leg_rear_margin
289 leg_inside_margin = i_param.eefm_leg_inside_margin
290 leg_outside_margin = i_param.eefm_leg_outside_margin
291 cp_check_margin = i_param.cp_check_margin
292 lleg_vertices = [[leg_front_margin, leg_outside_margin],
293 [leg_front_margin, -1*leg_inside_margin],
294 [-1*leg_rear_margin, -1*leg_inside_margin],
295 [-1*leg_rear_margin, leg_outside_margin]]
296 rleg_vertices = [[leg_front_margin, leg_inside_margin],
297 [leg_front_margin, -1*leg_outside_margin],
298 [-1*leg_rear_margin, -1*leg_outside_margin],
299 [-1*leg_rear_margin, leg_inside_margin]]
300 lleg_margined_vertices = [[leg_front_margin-cp_check_margin[0], leg_outside_margin-cp_check_margin[3]],
301 [leg_front_margin-cp_check_margin[0], -1*(leg_inside_margin-cp_check_margin[2])],
302 [-1*(leg_rear_margin-cp_check_margin[1]), -1*(leg_inside_margin-cp_check_margin[2])],
303 [-1*(leg_rear_margin-cp_check_margin[1]), leg_outside_margin-cp_check_margin[3]]]
304 rleg_margined_vertices = [[leg_front_margin-cp_check_margin[0], leg_inside_margin-cp_check_margin[2]],
305 [leg_front_margin-cp_check_margin[0], -1*(leg_outside_margin-cp_check_margin[3])],
306 [-1*(leg_rear_margin-cp_check_margin[1]), -1*(leg_outside_margin-cp_check_margin[3])],
307 [-1*(leg_rear_margin-cp_check_margin[1]), leg_inside_margin-cp_check_margin[2]]]
308 root_link = rospy.get_param(
"~root_link",
"BODY")
309 lleg_sub = rospy.Subscriber(
"/lfsensor_cop", PointStamped, llegCopCallback)
310 rleg_sub = rospy.Subscriber(
"/rfsensor_cop", PointStamped, rlegCopCallback)
311 zmp_sub = rospy.Subscriber(
"/zmp", PointStamped, zmpCallback)
312 act_cp_point_sub = rospy.Subscriber(
"/act_capture_point", PointStamped, actCPCallback)
313 ref_cp_point_sub = rospy.Subscriber(
"/ref_capture_point", PointStamped, refCPCallback)
314 contact_states_sub = rospy.Subscriber(
"/act_contact_states", ContactStatesStamped, contactStatesCallback)
319 rospy.Timer(rospy.Duration(0.1), periodicCallback)