switch.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 from std_msgs.msg import Bool
00010 from micros_mars_task_alloc.msg import Heartbeat
00011 from actionlib_msgs.msg import GoalID
00012 from move_base_msgs.msg import MoveBaseActionGoal
00013 
00014 class Switch(FunctionUnit):
00015     def __init__(self, node_name, topic_1 = None, msg_type_1 = None, send_topic_1 = None, topic_2 = None, msg_type_2 = None, send_topic_2 = None):
00016         
00017         self._motive_topic_1=node_name+'/activate'
00018         if not(topic_1 == None) and not(msg_type_1 == None):
00019             self._topic_1 = topic_1
00020             self._msg_type_1 = msg_type_1
00021             self._send_topic_1=send_topic_1
00022             self.send_1 = FunctionUnit.create_send(self, self._send_topic_1, self._msg_type_1)
00023         else:
00024             self._topic_1 = None
00025             self._msg_type_1 = None            
00026 
00027         if not(topic_2 == None) and not(msg_type_2 == None):
00028             self._topic_2 = topic_2
00029             self._msg_type_2 = msg_type_2
00030             self._send_topic_2=send_topic_2
00031             self.send_2 = FunctionUnit.create_send(self, self._send_topic_2, self._msg_type_2)
00032         else:
00033             self._topic_2 = None
00034         
00035         #print 'init'
00036         FunctionUnit.__init__(self, node_name)
00037         self.motivational_shared_var = False #'True' means the behavior set is activated
00038         self._action_mode = False
00039         self._last_goal = GoalID()
00040 
00041     def run(self):
00042         self.start_switch()
00043         pass
00044 
00045     def start_switch(self):
00046         FunctionUnit.init_node(self)
00047         #print 'run'
00048         if not(self._topic_1 == None):
00049             receive_1 = FunctionUnit.create_receive(self, self._topic_1, self._msg_type_1, self.receive_cb_1)
00050         if not(self._topic_2 == None):
00051             receive_2 = FunctionUnit.create_receive(self, self._topic_2, self._msg_type_2, self.receive_cb_2)
00052         receive_3 = FunctionUnit.create_receive(self, self._motive_topic_1, Bool, self.motivational_shared_var_update)
00053         FunctionUnit.spin(self)
00054 
00055     def motivational_shared_var_update(self, msg):
00056         self.motivational_shared_var = msg.data
00057         if msg.data==False:
00058            print "switch off"
00059            if self._action_mode:
00060               self._send_cancel.send(self._last_goal)
00061               print "cancel_goal"
00062         else:
00063            print "switch on"
00064 
00065     def receive_cb_1(self, msg):
00066         if self.motivational_shared_var == True:
00067             self.send_1.send(msg)
00068 
00069     def receive_cb_2(self, msg):#这里有可能会报错,因为receive_cb_2有可能是空的,根本不知道传到哪里,不过应该也没关系。
00070         if self.motivational_shared_var == True:    
00071             self.send_2.send(msg)
00072 
00073     def add_action(self,sub_action_name,pub_action_name):
00074         #self._action=action_client
00075         self._action_mode=True
00076         self._recieve_action_cancel = FunctionUnit.create_receive(self, sub_action_name + '/goal', MoveBaseActionGoal, self.receive_cb_goal)
00077         self._send_cancel= FunctionUnit.create_send(self, pub_action_name+'/cancel', GoalID)
00078 
00079     def receive_cb_goal(self,msg):
00080         print 'goal rcv'
00081         #if self.motivational_shared_var == True:    
00082         self._last_goal.stamp.secs = msg.goal_id.stamp.secs
00083         self._last_goal.stamp.nsecs = msg.goal_id.stamp.nsecs
00084         self._last_goal.id = msg.goal_id.id
00085 


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