test_goto_crossing.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 # Test the behavior of the goto_crossing with a random walk.
00003 
00004 import copy
00005 import random
00006 
00007 import rospy
00008 import tf
00009 from geometry_msgs.msg import PoseStamped
00010 from nav_msgs.msg import Odometry
00011 from std_msgs.msg import Bool
00012 from visualization_msgs.msg import Marker
00013 
00014 from lama_msgs.msg import Crossing
00015 from lama_msgs.msg import Frontier
00016 from goto_crossing.srv import ResetIntegrals
00017 from goto_crossing.srv import ResetIntegralsRequest
00018 
00019 _max_length = 2
00020 
00021 _goal_reached = False
00022 _odom = Odometry()
00023 _odom.header.frame_id = 'odom'
00024 _transform_listener = None
00025 _reset_integrals_client = None
00026 
00027 
00028 def callback_goal_reached(msg):
00029     global _goal_reached
00030     _goal_reached = msg.data
00031 
00032 
00033 def callback_odom(msg):
00034     global _odom
00035     _odom = msg
00036 
00037 
00038 def main():
00039     global _goal_reached
00040     crossing = Crossing()
00041     # We should have at least 3 frontiers for goto_crossing to go
00042     # to the crossing center.
00043     crossing.frontiers.append(Frontier())
00044     crossing.frontiers.append(Frontier())
00045     crossing.frontiers.append(Frontier())
00046     crossing_publisher = rospy.Publisher('~crossing', Crossing, queue_size=1)
00047     marker = Marker()
00048     marker.header.frame_id = 'odom'
00049     marker.type = marker.SPHERE
00050     marker.scale.x = 0.5
00051     marker.scale.y = 0.5
00052     marker.scale.z = 0.5
00053     marker.pose.orientation.w = 1.0
00054     marker.color.r = 1.0
00055     marker.color.g = 1.0
00056     marker.color.a = 0.5
00057     marker_publisher = rospy.Publisher('~crossing_marker', Marker,
00058                                        queue_size=1, latch=True)
00059     while not rospy.is_shutdown():
00060         _goal_reached = False
00061         srv = ResetIntegralsRequest()
00062         _reset_integrals_client.call(srv)
00063         x_center = _max_length * 2 * (random.random() - 0.5)
00064         y_center = _max_length * 2 * (random.random() - 0.5)
00065         target_pose = copy.deepcopy(_odom.pose.pose)
00066         target_pose.position.x += x_center
00067         target_pose.position.y += y_center
00068         target_pose_stamped = PoseStamped(_odom.header, target_pose)
00069         rospy.loginfo('New relative target: ({}, {})'.format(
00070             x_center, y_center))
00071         marker.pose.position.x = target_pose.position.x
00072         marker.pose.position.y = target_pose.position.y
00073         marker_publisher.publish(marker)
00074         r = rospy.Rate(10)
00075         while not _goal_reached and not rospy.is_shutdown():
00076             target_pose_stamped.header = _odom.header
00077             _transform_listener.waitForTransform('base_link',
00078                                                  _odom.header.frame_id,
00079                                                  _odom.header.stamp,
00080                                                  rospy.Duration(0.1))
00081             rel_target_pose_stamped = _transform_listener.transformPose(
00082                 'base_link', target_pose_stamped)
00083             crossing.center.x = rel_target_pose_stamped.pose.position.x
00084             crossing.center.y = rel_target_pose_stamped.pose.position.y
00085             rospy.logdebug('Crossing: ({}, {})'.format(
00086                 crossing.center.x, crossing.center.y))
00087             crossing_publisher.publish(crossing)
00088             r.sleep()
00089         rospy.sleep(1)
00090 
00091 if __name__ == '__main__':
00092     rospy.init_node('test_nj_oa', log_level=rospy.INFO)
00093     _transform_listener = tf.TransformListener()
00094     _transform_listener.waitForTransform('odom', 'base_link', rospy.Time(),
00095                                          rospy.Duration(5.0))
00096 
00097     _reset_integrals_client = rospy.ServiceProxy(
00098         'goto_crossing/reset_integrals', ResetIntegrals)
00099     rospy.Subscriber('~goal_reached', Bool, callback_goal_reached)
00100     rospy.Subscriber('~odom', Odometry, callback_odom)
00101     main()
00102     rospy.spin()


lama_test
Author(s): Gaƫl Ecorchard
autogenerated on Thu Jun 6 2019 19:06:31