motivational_behavior.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 micros_mars_task_alloc.msg import Heartbeat
00010 from std_msgs.msg import Bool
00011 import time
00012 import thread
00013 import rospy
00014 import random
00015 
00016 class MotivationalBehavior(FunctionUnit):
00017     active_behavior = [0 for row in range(10)]
00018     def __init__(self, node_name, robot_id, behavior_id, switch_topic, heartbeat_topic = None):
00019         if heartbeat_topic == None:
00020             self._heartbeat_topic = 'heart_beat'
00021          #param
00022         self._fast = 20#fast rate, constant for now
00023         self._slow = 3#slow rate,constant for now
00024         self._comm_influence_time=10
00025         self._comm_keep_time=20
00026         self._reset_influence_time=10
00027         self._acquienscence_time_0=20#if other robot is runing the same behavior
00028         self._acquienscence_time_1=60
00029         self._acquienscence_influence_time=5
00030         self._robot_id=robot_id#i
00031         self._behavior_id=behavior_id#j
00032         self._switch_topic=switch_topic
00033         self._broadcast_rate=5
00034         self._random_inc = False
00035 
00036         self._node_name=node_name
00037         FunctionUnit.__init__(self, node_name)
00038         self._threshold_of_activation = 100#The initial value of threshold_of_activation is 100.
00039         self._sensory_feedback = 0#If the sensory information is valid, the sensory feedback is true, the value is 1 or 0.
00040         self._comm_recieved = [[0 for col in range(1)] for row in range(10)]#_comm_received,row=k,col=t
00041         self._activity_suppression = 1        
00042         self._impatience = self._fast
00043         self._impatience_reset = 1#if this robot is running an other behavior,reset=0
00044         self._acquiescence = 1#if this behavior has run too long, acquiescence=0
00045         self._m = 0#final score
00046         self._time = 0
00047         self._active=0#bool, active=0 else=1
00048         self._active_time = 0#when this behavior is active
00049        
00050         #self.
00051         #self.sensory_feedback = 'topic...'
00052 
00053     def run(self):
00054         FunctionUnit.init_node(self)
00055         thread.start_new_thread(self.timer,())
00056         heartbeat_receive = FunctionUnit.create_receive(self, self._heartbeat_topic, Heartbeat, self.heartbeat_on_received)
00057         self._heartbeat_send = FunctionUnit.create_send(self, 'heart_beat', Heartbeat)
00058         self._switch_send = FunctionUnit.create_send(self, self._switch_topic, Bool)
00059         #sensory_receive = FunctionUnit.create_receive(self, self._node_name+"/sensory_feedback", Bool, self.sensory_on_received)
00060         FunctionUnit.spin(self)
00061 
00062    # def start_motive(self):
00063         #FunctionUnit.init_node(self)
00064         #heartbeat_receive = FunctionUnit.create_receive(self, self._heartbeat_topic, Heartbeat, self.heartbeat_on_received)
00065         #FunctionUnit.spin(self)
00066 
00067     def heartbeat_on_received(self,msg):
00068         if msg.behavior_set_ID == self._behavior_id and msg.heartbeat == True:
00069               self._comm_recieved[msg.robot_ID][self._time] = 1
00070               print "hb msg rcv"
00071         if msg.robot_ID == self._robot_id:
00072               if msg.heartbeat == True:
00073                    MotivationalBehavior.active_behavior[msg.behavior_set_ID] = 1
00074               else:
00075                    MotivationalBehavior.active_behavior[msg.behavior_set_ID] = 0
00076 
00077     def sensory_on_received(self,msg):
00078         #print "sensory msg rcv"
00079         if msg.data:
00080              self._sensory_feedback = 1
00081         else:
00082              self._sensory_feedback = 0
00083 
00084     def timer(self):
00085         while not rospy.is_shutdown():
00086             time.sleep(1)
00087             for i in range(0,10):
00088                 self._comm_recieved[i].append(0)
00089             self._time = self._time + 1
00090             #self._sensory_feedback = 1#constant for now
00091             self.calculate_activity_suppression()
00092             #self._fast = 10#constant for now
00093             #self._slow = 3#constant for now
00094             if self._random_inc:
00095                 self._fast = random.randint(10,40)
00096             self.calculate_impatience()
00097             self.calculate_impatience_reset()
00098             self.calculate_acquiescence()
00099             self._m = (self._m + self._impatience) * self._sensory_feedback * self._activity_suppression * self._impatience_reset * self._acquiescence
00100             if self._m > self._threshold_of_activation:
00101                 if self._active==0:
00102                    print "activate switch,send hb msg"
00103                    self.send_heartbeat(True)
00104                    self._switch_send.send(True)
00105                    self._active_time=self._time
00106                 self._active=1
00107             else:
00108                 if self._active==1:
00109                    self._active=0
00110                    self.send_heartbeat(False)
00111                    self._switch_send.send(False)
00112                    print "turn off switch,send hb msg "
00113             if self._time % self._broadcast_rate == 1:
00114                 if self._active == 1:
00115                    print "active, send hb msg"
00116                    self.send_heartbeat(True)
00117             print self._time
00118             print "set%d time%d: %d sup%d impare%d acq%d inc%d sensor%d actime%d"%(self._behavior_id, self._time, self._m, self._activity_suppression, self._impatience_reset, self._acquiescence, self._impatience, self._sensory_feedback, self._active_time)
00119    
00120     def calculate_activity_suppression(self):
00121          mark=0
00122          for i in range(0,10):
00123              if i != self._behavior_id and MotivationalBehavior.active_behavior[i] == 1:
00124                   mark=1
00125          if mark == 0:
00126              self._activity_suppression = 1
00127          else:
00128              self._activity_suppression = 0
00129      
00130     def calculate_impatience(self):
00131           mark=0
00132           start_time=self._time - self._comm_influence_time
00133           end_time=self._time - self._comm_keep_time
00134           if start_time < 0:
00135               start_time = 0
00136           if end_time < 0:
00137               end_time = 0
00138           for i in range(0,10):
00139               if i != self._robot_id:
00140                    mark1=0
00141                    for j in range(start_time, self._time+1):
00142                         if self._comm_recieved[i][j] == 1:
00143                              mark1=1
00144                    #mark2=0
00145                    #for k in range(0,end_time):
00146                    #     if self._comm_recieved[i][k] ==1:
00147                    #          mark2=1
00148                    if mark1 == 1: #and mark2 == 0:
00149                         mark=1#assume _slow is constant for diferrent robots
00150           if mark == 1:
00151               self._impatience = self._slow
00152           else:
00153               self._impatience = self._fast
00154          
00155     def calculate_impatience_reset(self):
00156           mark=0
00157           start_time=self._time - self._reset_influence_time
00158           if start_time < 0:
00159               start_time = 0
00160           for i in range(0,10):
00161               if i != self._robot_id:
00162                    mark1=0
00163                    for j in range(start_time, self._time+1):
00164                         if self._comm_recieved[i][j] == 1:
00165                              mark1=1
00166                    mark2=0
00167                    for k in range(0,start_time):
00168                         if self._comm_recieved[i][k] ==1:
00169                              mark2=1
00170                    if mark1 == 1 and mark2 == 0:
00171                         mark=1
00172           if mark ==1:
00173               self._impatience_reset = 0
00174           else:
00175               self._impatience_reset = 1
00176 
00177     def calculate_acquiescence(self):
00178           if self._active == 0:
00179               self._acquiescence = 1#not active
00180           else:
00181               if self._time - self._active_time > self._acquienscence_time_1:#time>time1
00182                   self._acquiescence = 0
00183               else:
00184                   if self._time - self._active_time > self._acquienscence_time_0:#time>time0,check
00185                       mark=0
00186                       start_time=self._time - self._acquienscence_influence_time
00187                       if start_time < 0:
00188                           start_time = 0
00189                       for i in range(0,10):
00190                           if i != self._robot_id:
00191                               for x in range(start_time, self._time+1):
00192                                   if self._comm_recieved[i][x] ==1:
00193                                       mark = 1
00194                       if mark == 1:
00195                          self._acquiescence = 0
00196                       else:
00197                          self._acquiescence = 1
00198                   else:
00199                       self._acquiescence = 1
00200     def send_heartbeat(self,hb):
00201           msg=Heartbeat()
00202           msg.robot_ID=self._robot_id
00203           msg.behavior_set_ID=self._behavior_id
00204           msg.heartbeat=hb
00205           self._heartbeat_send.send(msg)
00206 
00207     def set_fast(self,fast):
00208           self._fast=fast
00209 
00210     def set_sensory_feedback(self,value):
00211           self._sensory_feedback = value
00212 
00213     def set_sensor(self,sensor_topic):
00214           sensory_receive = FunctionUnit.create_receive(self, sensor_topic, Bool, self.sensory_on_received)
00215 
00216     def enable_random_inc(self):
00217           self._random_inc=True


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