-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbr.py
257 lines (185 loc) · 6.42 KB
/
bbr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import threading
import time
import queue
class Arbiter():
arb_count = 0
arbiters = []
def __init__(self, robot):
self.__id = Arbiter.arb_count
self.__robot = robot
self.current_behavior = None
self.__timer = Timer()
self.__is_locked = False
self.next_queue = queue.Queue()
Arbiter.arbiters.append(self)
Arbiter.arb_count += 1
def receive_request(self, behavior):
if self.__is_locked:
return
"""
Whenever a behavior make a request, they're competing for the resources
(i.e., the actuators whose action is controlled by te arbiter) in order
to help the robot do its work.
"""
if self.current_behavior is None:
self.current_behavior = behavior
try:
# print 'STARTING THREAD:', self.current_behavior.get_name()
self.current_behavior.start()
except KeyboardInterrupt:
pass
return
# Override the current behavior with the requesting behavior if the
# requesting behavior has higher priority.
if self.current_behavior.get_priority() < behavior.get_priority():
# print '--> JOIN THREAD', self.current_behavior.get_name()
self.current_behavior.stop()
self.current_behavior.reset()
self.current_behavior = None
self.current_behavior = behavior
try:
# print 'STARTING THREAD:', self.current_behavior.get_name()
self.current_behavior.start()
except KeyboardInterrupt:
pass
else:
if not self.current_behavior.is_alive():
# print 'BEHAVIOR NOT ALIVE:', self.current_behavior.get_name()
self.current_behavior.reset()
self.current_behavior = None
def get_robot(self):
return self.__robot
def get_current_behavior_name(self):
if self.current_behavior is not None:
return self.current_behavior.get_name()
else:
return None
def lock_others(self):
self.current_behavior.stop()
for arbiter in Arbiter.arbiters:
if arbiter.__id == self.__id:
continue
arbiter.__is_locked = True
def unlock_others(self):
for arbiter in Arbiter.arbiters:
arbiter.__is_locked = False
class Behavior():
def __init__(self, name, arbiter, priority, func):
"""
@func func: The function with parameters func(behavior, robot).
"""
self.__name = name
self.__func = None
self.__init_func = None
self.__arbiter = arbiter
self.__priority = priority
self.__interrupt = False
self.__is_started = False
self.__thread = None
self.__input_param = None
self.timer = Timer()
self.set_function(func)
def set_function(self, func, init_func=None):
"""
Initialize the thread with the function that runs the behavior.
"""
self.__func = func
self.__init_func = init_func
self.__thread = threading.Thread(
target=self.__func, args=(self, self.__arbiter.get_robot()))
def send_request(self):
"""
Request the arbitrater to run this behavior any number of times.
@param iteration:
The number of iterations that the arbiter run this behavior should
the arbiter accept this behavior's request. If iteration is 0, it
will run indefinitely until it is manually stopped by arbiter.stop()
function. By default, only one iteration of the behavior is
performed.
"""
self.__arbiter.receive_request(self)
def continue_to(self, behavior):
"""
Once the current behavior ends, immediately start the next behavior set
here.
"""
self.__arbiter.next_queue.put(behavior)
def get_name(self):
"""
Get the name (identifier) of the behavior.
"""
return self.__name
def get_priority(self):
"""
Get the priority of the behavior. Higher priority means more
important.
"""
return self.__priority
def get_function(self):
"""
Get the function that runs the behavior.
"""
return self.__func
def get_init_function(self):
return self.__init_func
def get_arbiter(self):
"""
Get the arbiter of this behavior.
"""
return self.__arbiter
def get_param(self, param):
"""
Get the argument parameters that goes into this behavior.
"""
return self.__input_param[param]
def interrupt(self):
"""
Interrupt this behavior, stopping it. This is used to break an
infinite loop within a function that runs the behavior (checked against
the is_interrupted function).
"""
self.__interrupt = True
self.__is_stopped = True
def is_interrupted(self):
"""
A flag that checks if this behavior is to be interrupted. Used in a loop
inside the function that runs the behavior.
"""
if self.__interrupt:
self.__interrupt = False # reset
return True
return False
def is_started(self):
if self.__is_started:
self.__is_started = False
return True
return False
def time_begin(self):
return self.__time_begin
def init(self):
self.__init_func(self, self.__arbiter.get_robot())
def start(self):
self.__thread.start()
def is_alive(self):
return self.__thread.is_alive()
def stop(self):
self.interrupt()
if self.is_alive():
self.__thread.join()
def reset(self):
self.__thread = threading.Thread(
target=self.__func, args=(self, self.__arbiter.get_robot()))
def input_param(self, param):
"""
Set the input parameters to this behavior, which may change the conduct
of the running behavior.
"""
self.__input_param = param
class Timer:
def __init__(self):
self.__begin = time.time()
self.__duration = 0
def timeup(self, duration):
return time.time() - self.__begin > duration
def reset(self):
self.__begin = time.time()