robot_detect.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 sys
00007 sys.path.append("..")
00008 from middle_abstraction.function_unit import FunctionUnit
00009 import rospy
00010 import cv2
00011 import numpy as np
00012 from cv_bridge import CvBridge
00013 from sensor_msgs.msg import Image
00014 from std_msgs.msg import Bool
00015 
00016 '''
00017 class Robot_detect(object):
00018     br = CvBridge()
00019     count = 0
00020     def __init__(self, name):
00021         sub = rospy.Subscriber("robot_0/image", Image, self.image_cb)
00022     def image_cb(self, msg):
00023         im = self.br.imgmsg_to_cv2(msg, desired_encoding='passthrough')
00024         print 'message received'
00025         for i in range(0, im.shape[0]):
00026             for j in range(0, im.shape[1]):
00027                 if im[i, j, 0] == 255 and im[i, j, 1] == 0 and im[i, j, 2] == 0:
00028                     print 'Detect an intruder!' 
00029 
00030 if __name__ == '__main__':
00031     rospy.init_node('robot_detect')
00032     robot_detect = Robot_detect(rospy.get_name())
00033     rospy.spin()
00034 '''
00035 
00036 class RobotDetect(FunctionUnit):
00037     def __init__(self, node_name, receive_topic, send_topic, virtual_off= None):
00038         FunctionUnit.__init__(self, node_name)    
00039         self._receive_topic = receive_topic
00040         self._send_topic = send_topic
00041         self._virtual = virtual_off
00042         self.br = CvBridge()
00043         self._virtual_send = FunctionUnit.create_send(self, virtual_off, Bool)
00044         
00045     def run(self):
00046         pass#这里也可以改成子类直接调用父类的run方法来做。
00047 
00048     def start_detect(self):
00049         FunctionUnit.init_node(self)
00050         #print 'hello 1'
00051         #print self._receive_topic
00052         receive_1 = FunctionUnit.create_receive(self, self._receive_topic, Image, self.receive_1_cb)
00053         #print 'hello 2'
00054         FunctionUnit.spin(self)
00055     
00056     def receive_1_cb(self, msg):
00057         #print 'message received'
00058         #print msg
00059         im = self.br.imgmsg_to_cv2(msg, desired_encoding='passthrough')
00060         for i in range(0, im.shape[0]):
00061             for j in range(0, im.shape[1]):
00062                 #if not (im[i, j, 0] == 255 and im[i, j, 1] == 0 and im[i, j, 2] == 0):
00063                 if im[i, j, 0] == 255 and im[i, j, 1] == 0 and im[i, j, 2] == 0:
00064                     #print 'Detect an intruder!'      
00065                     msg_sended = Bool()
00066                     msg_sended.data = True             
00067                     send = FunctionUnit.create_send(self, self._send_topic, Bool)
00068                     send.send(msg_sended)
00069                     virtual_msg = Bool()
00070                     virtual_msg.data = False
00071                     self._virtual_send.send(virtual_msg)
00072 
00073 class RobotDetect0(RobotDetect):
00074     def __init__(self):
00075         RobotDetect.__init__(self, node_name = 'robot_0_detect', receive_topic = 'robot_0/image', send_topic = 'robot_0/follow_sensory_feedback', virtual_off='robot_0/patrol_sensory_feedback')
00076         
00077     def run(self):
00078         RobotDetect.start_detect(self)
00079         
00080 class RobotDetect1(RobotDetect):
00081     def __init__(self):
00082         RobotDetect.__init__(self, node_name = 'robot_1_detect', receive_topic = 'robot_1/image', send_topic = 'robot_1/follow_sensory_feedback', virtual_off='robot_1/patrol_sensory_feedback')
00083         
00084     def run(self):
00085         RobotDetect.start_detect(self)
00086 
00087 class RobotDetect2(RobotDetect):
00088     def __init__(self):
00089         RobotDetect.__init__(self, node_name = 'robot_2_detect', receive_topic = 'robot_2/image', send_topic = 'robot_2/follow_sensory_feedback', virtual_off='robot_2/patrol_sensory_feedback')
00090         
00091     def run(self):
00092         RobotDetect.start_detect(self)
00093 if __name__ == '__main__':
00094     robot_detect_0_ob = RobotDetect0()
00095     robot_detect_0_ob.start()
00096 
00097         
00098 
00099 


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