Go to the documentation of this file.00001
00002
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
00022 self._fast = 20
00023 self._slow = 3
00024 self._comm_influence_time=10
00025 self._comm_keep_time=20
00026 self._reset_influence_time=10
00027 self._acquienscence_time_0=20
00028 self._acquienscence_time_1=60
00029 self._acquienscence_influence_time=5
00030 self._robot_id=robot_id
00031 self._behavior_id=behavior_id
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
00039 self._sensory_feedback = 0
00040 self._comm_recieved = [[0 for col in range(1)] for row in range(10)]
00041 self._activity_suppression = 1
00042 self._impatience = self._fast
00043 self._impatience_reset = 1
00044 self._acquiescence = 1
00045 self._m = 0
00046 self._time = 0
00047 self._active=0
00048 self._active_time = 0
00049
00050
00051
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
00060 FunctionUnit.spin(self)
00061
00062
00063
00064
00065
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
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
00091 self.calculate_activity_suppression()
00092
00093
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
00145
00146
00147
00148 if mark1 == 1:
00149 mark=1
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
00180 else:
00181 if self._time - self._active_time > self._acquienscence_time_1:
00182 self._acquiescence = 0
00183 else:
00184 if self._time - self._active_time > self._acquienscence_time_0:
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