-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitoring.py
74 lines (62 loc) · 3.34 KB
/
monitoring.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
import copy
import time
class Monitoring:
"""
Raises events, if monitored sensor data exceed specific thresholds. From actual world, to world model.
"""
@staticmethod
def fire_events(world_model, percept):
"""
Updates the current world model with events: B = fireEvents(B, ρ)
:param world_model: World model, instance of the WorldModel class.
:param percept: Dictionary. Input sensory data.
:return: The updated world model, instance of the WorldModel class with updated list of raised events
"""
if percept is not "":
world_model.world_model_history.append(copy.deepcopy(world_model.current_world_model)) # Store as history
for key in percept.keys(): # TODO: thresholds
if key == "distance": # TODO: if distance sensor <= 4.5 cm (min distance from plain) -> grabbed
distance_to_gripper = percept["distance"]["distance_to_gripper"]
if distance_to_gripper <= world_model.current_world_model.threshold["grabbing_distance"]:
world_model.current_world_model.grabbed["target_object"] = True
world_model.current_world_model.location["target_object"] = "arm"
elif key == "location": # TODO: if xyz of object within limits -> on table else -> not on table
for key2 in percept["location"]:
if key2 == "target_object":
world_model.current_world_model.location["target_object"] = percept["location"][
"target_object"]
elif key == "initialized": # TODO: if servos at 1500-ish -> initialized
world_model.current_world_model.initialized = percept["initialized"]
return world_model
if __name__ == '__main__':
# Sequence for testing
from world_model import WorldModel
beliefs = WorldModel()
monitoring = Monitoring()
time.sleep(0.1)
current_percept = {"distance": {'distance_to_gripper': 8.2}}
beliefs.update_tick()
beliefs = monitoring.fire_events(beliefs, current_percept)
time.sleep(0.1)
current_percept = {"distance": {'distance_to_gripper': 5.2}}
beliefs.update_tick()
beliefs = monitoring.fire_events(beliefs, current_percept)
time.sleep(0.1)
current_percept = {"distance": {'distance_to_gripper': 2.2}}
beliefs.update_tick()
beliefs = monitoring.fire_events(beliefs, current_percept)
print()
print("Final World model:")
print("-- Ticks: {}".format(beliefs.current_world_model.tick))
print("-- initialized: {}".format(beliefs.current_world_model.initialized))
print("-- location: {}".format(beliefs.current_world_model.location))
print("-- grabbed: {}".format(beliefs.current_world_model.grabbed))
print("-- timestamp: {}".format(beliefs.current_world_model.timestamp))
print()
print("World model History:")
for tick in range(len(beliefs.world_model_history)):
print("Tick {}:".format(tick))
print("-- initialized: {}".format(beliefs.world_model_history[tick].initialized))
print("-- location: {}".format(beliefs.world_model_history[tick].location))
print("-- grabbed: {}".format(beliefs.world_model_history[tick].grabbed))
print("-- timestamp: {}".format(beliefs.world_model_history[tick].timestamp))