Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Commit d026d70

Browse files
committed
Version 0.9.16: added world._events to track bulb and food state
1 parent 3c6e036 commit d026d70

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

aitk/robots/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
#
99
# *************************************
1010

11-
version_info = (0, 9, 15)
11+
version_info = (0, 9, 16)
1212
__version__ = ".".join(map(str, version_info))

aitk/robots/devices/bulbs.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class Bulb(BaseDevice):
1616
"""
1717
Class representing lights in the world.
1818
"""
19-
def __init__(self, color, x=0, y=0, z=0, brightness=50, name=None, **kwargs):
19+
def __init__(self, color, x=0, y=0, z=0, brightness=50, name=None,
20+
world=None, **kwargs):
2021
"""
2122
Create a lightbulb.
2223
@@ -27,6 +28,7 @@ def __init__(self, color, x=0, y=0, z=0, brightness=50, name=None, **kwargs):
2728
z (float): the height of bulb from ground (range 0 to 1)
2829
brightness (float): the brightness of the bulb (default 50)
2930
name (str): the name of the bulb
31+
world (World): if bulb is in world
3032
"""
3133
config = {
3234
"color": color,
@@ -42,6 +44,7 @@ def __init__(self, color, x=0, y=0, z=0, brightness=50, name=None, **kwargs):
4244
self.draw_rings = 7 # rings around bulb light in views
4345
self._watcher = None
4446
self.robot = None
47+
self.world = world
4548
self.from_json(config)
4649

4750
def initialize(self):
@@ -104,15 +107,19 @@ def on(self):
104107
"""
105108
self.state = "on"
106109
if self.robot is not None and self.robot.world is not None:
107-
self.robot.world.update()
110+
self.robot.world._event("bulb-on", bulb=self)
111+
elif self.world is not None:
112+
self.world._event("bulb-on", bulb=self)
108113

109114
def off(self):
110115
"""
111116
Turn the bulb "off".
112117
"""
113118
self.state = "off"
114119
if self.robot is not None and self.robot.world is not None:
115-
self.robot.world.update()
120+
self.robot.world._event("bulb-off", bulb=self)
121+
elif self.world is not None:
122+
self.world._event("bulb-off", bulb=self)
116123

117124
@property
118125
def x(self):

aitk/robots/robot.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,13 @@ def set_pose(self, x=None, y=None, a=None, clear_trace=True):
476476
if a is not None:
477477
a = degrees_to_world(a)
478478
self._set_pose(x, y, a, clear_trace)
479+
# Set all velocities to zero
480+
self.vx = 0.0 # velocity in x direction, CM per second
481+
self.vy = 0.0 # velocity in y direction, degrees per second
482+
self.va = 0.0 # turn velocity
483+
self.tvx = 0.0
484+
self.tvy = 0.0
485+
self.tva = 0.0
479486
# Save the robot's pose to the config
480487
self.world.update()
481488
self.world.save()
@@ -497,6 +504,13 @@ def set_random_pose(self, clear_trace=True):
497504
# Direction is in radians, in world coordinates:
498505
x, y, a = self.world._find_random_pose(self)
499506
self._set_pose(x, y, a, clear_trace)
507+
# Set all velocities to zero
508+
self.vx = 0.0 # velocity in x direction, CM per second
509+
self.vy = 0.0 # velocity in y direction, degrees per second
510+
self.va = 0.0 # turn velocity
511+
self.tvx = 0.0
512+
self.tvy = 0.0
513+
self.tva = 0.0
500514
# Save the robot's pose to the config
501515
self.world.update()
502516
self.world.save()
@@ -729,11 +743,8 @@ def eat(self):
729743
if self.world is not None:
730744
for food in self.world._food[:]: # copy
731745
if distance(self.x, self.y, food[0], food[1]) <= self.eat_food_distance:
732-
self.food_eaten += 1
733746
success = True
734-
self.world._food.remove(food)
735-
self.world._grid.need_update = True
736-
self.world.update() # request draw
747+
self.world._event("eat-food", self, food)
737748
return success
738749

739750
def speak(self, text=None):

aitk/robots/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ def get(self, x, y):
745745
Get the reading at the grid point.
746746
"""
747747
x = max(min(self.width - 1, int(x)), 0)
748-
y = max(min(self.height - 1, int(x)), 0)
748+
y = max(min(self.height - 1, int(y)), 0)
749749
return self.grid[x][y]
750750

751751
def get_image(self):

aitk/robots/world.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ def __init__(
196196
"quiet": quiet,
197197
"smell_cell_size": smell_cell_size,
198198
}
199-
self._messages = []
200199
if filename is not None:
201200
config["filename"] = filename
202201
if ground_image_filename is not None:
@@ -209,6 +208,7 @@ def __init__(
209208
raise AttributeError(
210209
"unknown arguments for World: %s" % list(kwargs.keys())
211210
)
211+
self._events = []
212212
self._show_throttle_percentage = 0.40
213213
self._time_decimal_places = 1
214214
self._throttle_period = 0.1
@@ -229,6 +229,22 @@ def __init__(
229229
def __repr__(self):
230230
return "<World width=%r, height=%r>" % (self.width, self.height)
231231

232+
def _event(self, etype, **kwargs):
233+
if etype == "eat-food":
234+
robot = kwargs["robot"]
235+
food = kwargs["food"]
236+
robot.food_eaten += 1
237+
self._food.remove(food)
238+
self._grid.need_update = True
239+
self._events.append((self.time, "eat-food", robot, food))
240+
elif etype == "bulb-on":
241+
bulb = kwargs["bulb"]
242+
self._events.append((self.time, "bulb-on", bulb))
243+
elif etype == "bulb-off":
244+
bulb = kwargs["bulb"]
245+
self._events.append((self.time, "bulb-off", bulb))
246+
self.update() # request draw
247+
232248
def get_image(self, index=None, size=100):
233249
"""
234250
Get a PIL image of the world, or of a robot.
@@ -365,6 +381,7 @@ def reset(self):
365381
self._initialize()
366382
self._reset_watchers()
367383
self._food = []
384+
self._events = []
368385
self._grid = None
369386
self.from_json(self.config)
370387
self.time = 0.0
@@ -808,7 +825,7 @@ def add_bulb(self, color, x, y, z, brightness, name=None):
808825

809826
def _add_bulb(self, color, x, y, z, brightness, name=None):
810827
name = name if name is not None else "bulb-%s" % (len(self._bulbs) + 1)
811-
bulb = Bulb(color, x, y, z, brightness, name)
828+
bulb = Bulb(color, x, y, z, brightness, name, self)
812829
self._bulbs.append(bulb)
813830

814831
def add_wall(self, color, x1, y1, x2, y2, box=True, wtype="wall"):

0 commit comments

Comments
 (0)