00001
00002
00003
00004
00005
00006
00007 '''
00008 import sys
00009 sys.path.append("..")
00010 from middle_abstraction.function_unit import FunctionUnit
00011 import rospy
00012 import cv2
00013 import numpy as np
00014 from cv_bridge import CvBridge
00015 from sensor_msgs.msg import Image
00016 from std_msgs.msg import Bool
00017
00018
00019 class RobotDetect(FunctionUnit):
00020 def __init__(self, node_name, receive_topic, send_topic):
00021 FunctionUnit.__init__(self, node_name)
00022 self._receive_topic = receive_topic
00023 self._send_topic = send_topic
00024 self.br = CvBridge()
00025
00026 def run(self):
00027 pass#这里也可以改成子类直接调用父类的run方法来做。
00028
00029 def start_detect(self):
00030 FunctionUnit.init_node(self)
00031 #print 'hello 1'
00032 #print self._receive_topic
00033 receive_1 = FunctionUnit.create_receive(self, self._receive_topic, Image, self.receive_1_cb)
00034 #print 'hello 2'
00035 FunctionUnit.spin(self)
00036
00037 def receive_1_cb(self, msg):
00038 #print 'message received'
00039 #print msg
00040 im = self.br.imgmsg_to_cv2(msg, desired_encoding='passthrough')
00041 for i in range(0, im.shape[0]):
00042 for j in range(0, im.shape[1]):
00043 #if not (im[i, j, 0] == 255 and im[i, j, 1] == 0 and im[i, j, 2] == 0):
00044 if im[i, j, 0] == 255 and im[i, j, 1] == 0 and im[i, j, 2] == 0:
00045 print 'Detect an intruder!'
00046 msg_sended = Bool()
00047 msg_sended.data = True
00048 send = FunctionUnit.create_send(self, self._send_topic, Bool)
00049 send.send(msg_sended)
00050
00051 class RobotDetect0(RobotDetect):
00052 def __init__(self):
00053 RobotDetect.__init__(self, node_name = 'robot_1_detect', receive_topic = 'robot_0/image', send_topic = 'robot_0/detected')
00054
00055 def run(self):
00056 RobotDetect.start_detect(self)
00057
00058
00059 if __name__ == '__main__':
00060 robot_detect_0_ob = RobotDetect0()
00061 robot_detect_0_ob.start()
00062 '''
00063
00064 __author__ = 'Minglong Li'
00065
00066 import sys
00067 sys.path.append("..")
00068
00069 from middle_abstraction.function_unit import FunctionUnit
00070 import math
00071 from geometry_msgs.msg import Twist
00072 from nav_msgs.msg import Odometry
00073
00074 class RobotFollow(FunctionUnit):
00075 def __init__(self, node_name, receive_topic_follower, receive_topic_intruder, send_topic):
00076 FunctionUnit.__init__(self, node_name)
00077 self._receive_topic_follower = receive_topic_follower
00078 self._receive_topic_intruder = receive_topic_intruder
00079 self.follower_pose_x = 0.0
00080 self.follower_pose_y = 0.0
00081 self.intruder_pose_x = 0.0
00082 self.intruder_pose_y = 0.0
00083 self.follow_distance = 0.5
00084 self.linear_velocity = 0.3
00085 self.angular_velocity = 0.5
00086 self.send = FunctionUnit.create_send(self, send_topic, Twist)
00087
00088
00089
00090 def run(self):
00091 pass
00092
00093 def start_follow(self):
00094 FunctionUnit.init_node(self)
00095
00096
00097 receive_1 = FunctionUnit.create_receive(self, self._receive_topic_follower, Odometry, self.receive_1_cb)
00098 receive_2 = FunctionUnit.create_receive(self, self._receive_topic_intruder, Odometry, self.receive_2_cb)
00099
00100 FunctionUnit.spin(self)
00101
00102 def receive_1_cb(self, msg):
00103 x = msg.pose.pose.orientation.x
00104 y = msg.pose.pose.orientation.y
00105 z = msg.pose.pose.orientation.z
00106 w = msg.pose.pose.orientation.w
00107
00108
00109
00110 leader_direction = math.atan2(self.intruder_pose_y - self.follower_pose_y, self.intruder_pose_x - self.follower_pose_x)
00111 if (leader_direction > -1*math.pi or leader_direction == -1*math.pi) and leader_direction < 0:
00112 leader_direction = 2*math.pi + leader_direction
00113
00114 follower_orientation = math.atan2(2*(w*z+x*y), 1-2*(y**2+z**2))
00115 if (follower_orientation > -1*math.pi or follower_orientation == -1*math.pi) and follower_orientation < 0:
00116 follower_orientation = 2*math.pi + follower_orientation
00117
00118
00119
00120 self.follower_pose_x = msg.pose.pose.position.x
00121 self.follower_pose_y = msg.pose.pose.position.y
00122 if ((self.follower_pose_x - self.intruder_pose_x)**2 + (self.follower_pose_y - self.intruder_pose_y)**2) > self.follow_distance ** 2:
00123 if follower_orientation < leader_direction:
00124 cmd = Twist()
00125 cmd.linear.x = self.linear_velocity
00126 cmd.angular.z = self.angular_velocity
00127 self.send.send(cmd)
00128 else:
00129 cmd = Twist()
00130 cmd.linear.x = self.linear_velocity
00131 cmd.angular.z = -1 * self.angular_velocity
00132 self.send.send(cmd)
00133 else:
00134 cmd = Twist()
00135 cmd.linear.x = 0.0
00136 cmd.angular.z = 0.0
00137 self.send.send(cmd)
00138
00139 def receive_2_cb(self, msg):
00140 self.intruder_pose_x = msg.pose.pose.position.x
00141 self.intruder_pose_y = msg.pose.pose.position.y
00142
00143 class RobotFollow0(RobotFollow):
00144 def __init__(self):
00145 RobotFollow.__init__(self, node_name='robot_follow_0', receive_topic_follower='robot_0/base_pose_ground_truth',
00146 receive_topic_intruder='robot_3/base_pose_ground_truth', send_topic='robot_0/cmd_vel/follow')
00147
00148 def run(self):
00149 RobotFollow.start_follow(self)
00150
00151 class RobotFollow1(RobotFollow):
00152 def __init__(self):
00153 RobotFollow.__init__(self, node_name='robot_follow_1', receive_topic_follower='robot_1/base_pose_ground_truth',
00154 receive_topic_intruder='robot_3/base_pose_ground_truth', send_topic='robot_1/cmd_vel/follow')
00155
00156 def run(self):
00157 RobotFollow.start_follow(self)
00158
00159 class RobotFollow2(RobotFollow):
00160 def __init__(self):
00161 RobotFollow.__init__(self, node_name='robot_follow_2', receive_topic_follower='robot_2/base_pose_ground_truth',
00162 receive_topic_intruder='robot_3/base_pose_ground_truth', send_topic='robot_2/cmd_vel/follow')
00163
00164 def run(self):
00165 RobotFollow.start_follow(self)
00166
00167 if __name__ == '__main__':
00168 robot_follow_0_ob = RobotFollow0()
00169 robot_follow_0_ob.start()
00170