-
Notifications
You must be signed in to change notification settings - Fork 1
/
behavior.py
185 lines (151 loc) · 6.39 KB
/
behavior.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
from camera import Camera
from helper import Sensob
from ultrasonic import Ultrasonic
from reflectance_sensors import ReflectanceSensors
import logging, sys
import time
from random import randrange
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
class Behavior():
def __init__(self, BBCON):
self.motor_recommendations = [0,0] #A list of numbers, one for each motob, in range [-1,1]
self.active_flag = False #Used to check in the consider methods what it should check for
self.halt_request = False #If set to true, the program (thus robot) should terminate its run
self.match_degree = 0 #Number in range [0,1], the degree of how close current conditions are to desired behavior
self.bbcon = BBCON #Reference to the BBCON, mainly for communications such as activate/deactivate
def get_weight(self):
return self.weight
def get_halt_request(self):
return self.halt_request
def get_motor_recommendations(self):
return self.motor_recommendations
#Checks if the behavior needs to be activated or deactivated according to a predefined test
#Should be expanded to also deactivate senseobs, but requires bookkeeping to see if it's in other behaviors as well.
#Maybe have a activeSenseob/senseOb in BBCON, similarily to behaviors?
def considerState(self):
if self.active_flag: #If the behavior is active...
if not self.test(): #... and if the test fails
self.active_flag = False #Deactivate the behavior
self.bbcon.deactivate_behavior(self)
else:
if self.test():
self.active_flag = True
self.bbcon.activate_behavior(self)
def test(self):
return False #The superclass shouldn't constitute a valid behavior
# def update(self):
# self.considerState()
# self.sense_and_act()
# self.weight = self.priority * self.match_degree
#
# def sense_and_act(self):
# pass #Highly specialized method
# __update = update
# __sense = sense_and_act
class PictureWhenClose(Behavior):
def __init__(self, BBCON, priority):
super().__init__(BBCON)
self.sensobs = [Sensob(Camera(img_height=1920,img_width=1080)), Sensob(Ultrasonic())]
self.priority = priority #Preset value that is set by the user
self.picTaken = False
for sensob in self.sensobs:
sensob.update()
def printName(self):
return "Behavior PictureWhenClose"
#Update is executed in three steps (one if deactivated):
# 1) Check to see if it should be active, 2) sense and act, 3) calculate weight
def update(self):
print("ULTRASONIC READING: ", self.sensobs[1].get_value())
super().considerState()
if self.active_flag:
self.sense_and_act()
self.weight = self.priority * self.match_degree
def test(self):
self.sensobs[1].update()
v = self.sensobs[1].get_value()
print("Value ultrasonic",v)
if (v > 70):
logging.debug("Should deactivate or stay deactivated")
return False
else:
logging.debug("Should activate, therefore terminate...")
return True
def sense_and_act(self):
cameraData = self.sensobs[0].get_value()
cameraData.save("result.png")
self.picTaken = True
self.halt_request = True
self.match_degree = 1
# faceCascade = cv2.CascadeClasifier('haarcascade_frontalface_default.xml')
# cvimage = cv2.imread('image.png')
# gray = cv2.cvtColor(cvimage, cv2.COLOR_BGR2GRAY)
# faces = faceCascade.detectMultiScale(
# gray,
# scaleFactor=1.1,
# minNeighbors=5,
# minSize=(30, 30),
# flags = cv2.cv.CV_HAAR_SCALE_IMAGE
# )
# if faces is not 0:
# cv2.imwrite('FACE.jpg')
#
# else:
# pass
class RandomWalk(Behavior):
def __init__(self, BBCON, priority):
super().__init__(BBCON)
self.sensobs = []
self.priority = priority #Preset value that is set by the user
self.randomCount = 0
for sensob in self.sensobs:
sensob.update()
def printName(self):
return "Behavior random walk"
#Update is executed in three steps (one if deactivated):
# 1) Check to see if it should be active, 2) sense and act, 3) calculate weight
def update(self):
super().considerState()
if self.active_flag:
self.sense_and_act()
self.weight = self.priority * self.match_degree
def test(self):
return True
def sense_and_act(self):
if self.randomCount%2 == 0:
self.motor_recommendations = [0.1,0.1]
else:
randomNumber1 = randrange(-10,10,1)/10
randomNumber2 = randrange(-10,10,1)/10
self.motor_recommendations = [randomNumber1,randomNumber2]
self.motor_recommendations = [x*0.4 for x in self.motor_recommendations]
self.randomCount += 1
self.match_degree = 1
class AvoidEdge(Behavior):
def __init__(self, BBCON, priority):
super().__init__(BBCON)
print("We are going in to calibration mode in 2 sec")
time.sleep(2)
self.sensob = ReflectanceSensors(auto_calibrate=True)
self.priority = priority #Preset value that is set by the user
self.sensob.update()
self.old = self.sensob.get_value()
def printName(self):
return "Behavior AvoidEdge "
def update(self):
super().considerState()
if self.active_flag:
self.sense_and_act()
self.weight = self.priority * self.match_degree
def sense_and_act(self):
self.sensob.update()
self.new = self.sensob.get_value()
print(sum(self.old)/len(self.old) - sum(self.new)/len(self.new) > 0.1)
if (sum(self.old)/len(self.old) - sum(self.new)/len(self.new) > 0.1 ):
self.motor_recommendations = [-1,-1]
self.match_degree = 1
else:
self.match_degree = 0
self.motor_recommendations = [1,1]
self.old = self.new
def test(self):
return True