robot_follow.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # coding=utf-8
00003 
00004 __author__ = 'Minglong Li'
00005 
00006 import rospy
00007 import math
00008 
00009 import geometry_msgs.msg
00010 from nav_msgs.msg import Odometry
00011 
00012 class Robot_follow(object):
00013 
00014 
00015     def __init__(self, name):
00016         #self.follower = rospy.get_param("~follower")
00017         #self.intruder = rospy.get_param("~intruder")
00018         self.follower_pose_x = 0.0
00019         self.follower_pose_y = 0.0
00020         self.intruder_pose_x = 0.0
00021         self.intruder_pose_y = 0.0 
00022         self.follow_distance = 0.5 # The follower will be away from the intruder according to this distance      
00023         self.linear_velocity = 0.3
00024         self.angular_velocity = 0.5
00025         self.follower = 'robot_0'
00026         self.intruder = 'robot_3'
00027         self.sub1 = rospy.Subscriber('/%s/base_pose_ground_truth' % self.follower,
00028                                     Odometry,
00029                                     self.robot_follow_cb)
00030         self.sub2 = rospy.Subscriber('/%s/base_pose_ground_truth' % self.intruder,
00031                                     Odometry,
00032                                     self.intruder_pose_update_cb)        
00033 
00034         self.robot_vel_pub = rospy.Publisher('robot_0/cmd_vel', geometry_msgs.msg.Twist, queue_size=1)
00035 
00036 
00037     def robot_follow_cb(self, msg):
00038 
00039         x = msg.pose.pose.orientation.x
00040         y = msg.pose.pose.orientation.y
00041         z = msg.pose.pose.orientation.z
00042         w = msg.pose.pose.orientation.w 
00043         print "self.intruder_pose_y, self.follower_pose_y, self.intruder_pose_x, self.follower_pose_x: %f %f %f %f" %(self.intruder_pose_y, self.follower_pose_y, self.intruder_pose_x, self.follower_pose_x)
00044         print "2*(w*z+x*y), 1-2*(y**2+z**2): %f %f" %( 2*(w*z+x*y), 1-2*(y**2+z**2) )
00045         print "self.intruder_pose_y - self.follower_pose_y, self.intruder_pose_x - self.follower_pose_x: %f %f" %(self.intruder_pose_y - self.follower_pose_y, self.intruder_pose_x - self.follower_pose_x)
00046         leader_direction = math.atan2(self.intruder_pose_y - self.follower_pose_y, self.intruder_pose_x - self.follower_pose_x)
00047         if (leader_direction > -1*math.pi or leader_direction == -1*math.pi) and leader_direction < 0:
00048             leader_direction = 2*math.pi + leader_direction
00049 
00050         follower_orientation = math.atan2(2*(w*z+x*y), 1-2*(y**2+z**2))
00051         if (follower_orientation > -1*math.pi or follower_orientation  == -1*math.pi) and follower_orientation < 0:
00052             follower_orientation = 2*math.pi + follower_orientation
00053         print 'follower_orientation: %f' %(follower_orientation)
00054         print 'leader_direction: %f' %(leader_direction)
00055 
00056 
00057         self.follower_pose_x = msg.pose.pose.position.x
00058         self.follower_pose_y = msg.pose.pose.position.y
00059         if ((self.follower_pose_x - self.intruder_pose_x)**2 + (self.follower_pose_y - self.intruder_pose_y)**2) > self.follow_distance ** 2:
00060             if follower_orientation < leader_direction:  
00061                 cmd = geometry_msgs.msg.Twist()
00062                 cmd.linear.x = self.linear_velocity
00063                 cmd.angular.z = self.angular_velocity
00064                 self.robot_vel_pub.publish(cmd)
00065             else:
00066                 cmd = geometry_msgs.msg.Twist()
00067                 cmd.linear.x = self.linear_velocity
00068                 cmd.angular.z = -1 * self.angular_velocity
00069                 self.robot_vel_pub.publish(cmd)
00070         else:
00071             cmd = geometry_msgs.msg.Twist()
00072             cmd.linear.x = 0.0
00073             cmd.angular.z = 0.0
00074             self.robot_vel_pub.publish(cmd)
00075                         
00076     def intruder_pose_update_cb(self, msg):
00077         self.intruder_pose_x = msg.pose.pose.position.x
00078         self.intruder_pose_y = msg.pose.pose.position.y
00079      
00080 if __name__ == '__main__':
00081     rospy.init_node('robot_follow')
00082     robot_follow_obj = Robot_follow(rospy.get_name())
00083     rospy.spin()
00084 
00085 
00086 


micros_mars_task_alloc
Author(s): Minglong Li , Xiaodong Yi , Yanzhen Wang , Zhongxuan Cai
autogenerated on Mon Jul 1 2019 19:55:03