Skip to content

Commit e275b9b

Browse files
committed
VIC-14350 Sample code fixes (#182)
1 parent 2492514 commit e275b9b

File tree

10 files changed

+70
-84
lines changed

10 files changed

+70
-84
lines changed

anki_vector/annotate.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,33 @@ class ImageText: # pylint: disable=too-few-public-methods
9292
9393
.. testcode::
9494
95+
import time
96+
97+
try:
98+
from PIL import ImageDraw
99+
except ImportError:
100+
sys.exit("run `pip3 install --user Pillow numpy` to run this example")
101+
95102
import anki_vector
96103
from anki_vector import annotate
97-
with anki_vector.Robot(show_viewer=True) as robot:
98-
text = annotate.ImageText("Text Annotations",
104+
105+
106+
# Define an annotator using the annotator decorator
107+
@annotate.annotator
108+
def clock(image, scale, annotator=None, world=None, **kw):
109+
d = ImageDraw.Draw(image)
110+
bounds = (0, 0, image.width, image.height)
111+
text = annotate.ImageText(time.strftime("%H:%m:%S"),
99112
position=annotate.AnnotationPosition.TOP_LEFT,
100113
outline_color="black")
101-
robot.camera.image_annotator.add_static_text("custom-text", text)
114+
text.render(d, bounds)
115+
116+
with anki_vector.Robot(show_viewer=True, enable_face_detection=True, enable_custom_object_detection=True) as robot:
117+
robot.camera.image_annotator.add_static_text("text", "Vec-Cam", position=annotate.AnnotationPosition.TOP_RIGHT)
118+
robot.camera.image_annotator.add_annotator("clock", clock)
119+
120+
time.sleep(3)
121+
102122
103123
:param text: The text to display; may contain newlines
104124
:param position: Where on the screen to render the text
@@ -401,6 +421,7 @@ class ImageAnnotator:
401421
402422
import anki_vector
403423
from anki_vector import annotate
424+
import time
404425
405426
@annotate.annotator
406427
def clock(image, scale, annotator=None, world=None, **kw):
@@ -416,7 +437,7 @@ def clock(image, scale, annotator=None, world=None, **kw):
416437
robot.camera.image_annotator.add_annotator("custom-annotator", clock)
417438
time.sleep(5)
418439
# Disable the custom annotator
419-
robot.camera.image_annotator.disable_annotator("custom-annotator", clock)
440+
robot.camera.image_annotator.disable_annotator("custom-annotator")
420441
time.sleep(5)
421442
"""
422443

anki_vector/audio.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ class AudioComponent(util.Component):
6161
The :class:`anki_vector.robot.Robot` or :class:`anki_vector.robot.AsyncRobot` instance
6262
owns this audio component.
6363
64-
.. code-block:: python
64+
.. testcode::
65+
66+
import anki_vector
6567
6668
with anki_vector.Robot() as robot:
67-
robot.audio.stream_wav_file('sounds/vector_alert.wav')
69+
robot.audio.stream_wav_file('../examples/tutorials/sounds/vector_alert.wav')
6870
"""
6971

7072
# TODO restore audio feed code when ready
@@ -82,7 +84,7 @@ async def set_master_volume(self, volume: RobotVolumeLevel) -> protocol.MasterVo
8284
8385
Note that muting the robot is not supported from the SDK.
8486
85-
.. code-block:: python
87+
.. testcode::
8688
8789
import anki_vector
8890
from anki_vector import audio
@@ -160,10 +162,12 @@ async def _request_handler(self, reader, params, volume):
160162
async def stream_wav_file(self, filename, volume=50):
161163
""" Plays audio using Vector's speakers.
162164
163-
.. code-block:: python
165+
.. testcode::
166+
167+
import anki_vector
164168
165169
with anki_vector.Robot() as robot:
166-
robot.audio.stream_wav_file('sounds/vector_alert.wav')
170+
robot.audio.stream_wav_file('../examples/tutorials/sounds/vector_alert.wav')
167171
168172
:param filename: the filename/path to the .wav audio file
169173
:param volume: the audio playback level (0-100)

anki_vector/behavior.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -950,22 +950,17 @@ class ReserveBehaviorControl():
950950
import anki_vector
951951
from anki_vector import behavior
952952
953-
args = anki_vector.util.parse_command_args()
954-
with behavior.ReserveBehaviorControl(args.serial):
953+
with behavior.ReserveBehaviorControl():
955954
956955
# At this point, Vector will remain still, even without
957956
# a Robot instance being in scope.
958957
959-
...
960-
961958
# take control of the robot as usual
962959
with anki_vector.Robot() as robot:
963960
964961
robot.anim.play_animation("anim_turn_left_01")
965962
966-
# Robot will not perform idle behaviors until the script completes
967-
968-
...
963+
# Robot will not perform idle behaviors until the script completes
969964
970965
:param serial: Vector's serial number. The robot's serial number (ex. 00e20100) is located on
971966
the underside of Vector, or accessible from Vector's debug screen. Used to

anki_vector/events.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,6 @@ def on_robot_observed_face(robot, event_type, event, evt):
304304
robot.behavior.say_text("I see a face!")
305305
evt.set()
306306
307-
args = anki_vector.util.parse_command_args()
308307
with anki_vector.Robot(enable_face_detection=True) as robot:
309308
310309
# If necessary, move Vector's Head and Lift to make it easy to see his face
@@ -389,7 +388,6 @@ def on_robot_observed_face(robot, event_type, event, evt):
389388
robot.behavior.say_text("I see a face!")
390389
evt.set()
391390
392-
args = anki_vector.util.parse_command_args()
393391
with anki_vector.Robot(enable_face_detection=True) as robot:
394392
395393
# If necessary, move Vector's Head and Lift to make it easy to see his face

anki_vector/faces.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,7 @@ def handle_face_observed(robot, event_type, event):
7575
robot.behavior.set_lift_height(0.0)
7676
robot.behavior.set_head_angle(degrees(45.0))
7777
78-
try:
79-
while True:
80-
time.sleep(0.5)
81-
except KeyboardInterrupt:
82-
pass
78+
time.sleep(5.0)
8379
8480
:param face: The Face instance that was observed
8581
:param image_rect: An :class:`anki_vector.util.ImageRect`: defining where the face is within Vector's camera view
@@ -135,11 +131,7 @@ def handle_face_disappeared(robot, event_type, event):
135131
robot.behavior.set_lift_height(0.0)
136132
robot.behavior.set_head_angle(degrees(45.0))
137133
138-
try:
139-
while True:
140-
time.sleep(0.5)
141-
except KeyboardInterrupt:
142-
pass
134+
time.sleep(5.0)
143135
144136
:param face:'The Face instance that appeared
145137
:param image_rect: An :class:`anki_vector.util.ImageRect`: defining where the face is within Vector's camera view
@@ -186,11 +178,7 @@ def handle_face_disappeared(robot, event_type, event):
186178
robot.behavior.set_lift_height(0.0)
187179
robot.behavior.set_head_angle(degrees(45.0))
188180
189-
try:
190-
while True:
191-
time.sleep(0.5)
192-
except KeyboardInterrupt:
193-
pass
181+
time.sleep(5.0)
194182
195183
:param face: The Face instance that is no longer being observed
196184
"""

anki_vector/objects.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,7 @@ def handle_object_observed(robot, event_type, event):
9494
robot.behavior.set_lift_height(0.0)
9595
robot.behavior.set_head_angle(degrees(0.0))
9696
97-
try:
98-
while True:
99-
time.sleep(0.5)
100-
except KeyboardInterrupt:
101-
pass
97+
time.sleep(3.0)
10298
10399
:param obj: The object that was observed
104100
:param image_rect: An :class:`anki_vector.util.ImageRect`: defining where the object is within Vector's camera view
@@ -148,11 +144,7 @@ def handle_object_appeared(robot, event_type, event):
148144
robot.behavior.set_lift_height(0.0)
149145
robot.behavior.set_head_angle(degrees(0.0))
150146
151-
try:
152-
while True:
153-
time.sleep(0.5)
154-
except KeyboardInterrupt:
155-
pass
147+
time.sleep(3.0)
156148
157149
:param obj: The object that is starting to be observed
158150
:param image_rect: An :class:`anki_vector.util.ImageRect`: defining where the object is within Vector's camera view
@@ -181,8 +173,7 @@ def handle_object_disappeared(robot, event_type, event):
181173
# whenever an Object goes out of view.
182174
print(f"--------- Vector stopped seeing an object --------- \\n{event.obj}")
183175
184-
with anki_vector.Robot(args.serial,
185-
default_logging=False,
176+
with anki_vector.Robot(default_logging=False,
186177
show_viewer=True,
187178
show_3d_viewer=True,
188179
enable_nav_map_feed=True) as robot:
@@ -194,11 +185,7 @@ def handle_object_disappeared(robot, event_type, event):
194185
robot.behavior.set_lift_height(0.0)
195186
robot.behavior.set_head_angle(degrees(0.0))
196187
197-
try:
198-
while True:
199-
time.sleep(0.5)
200-
except KeyboardInterrupt:
201-
pass
188+
time.sleep(3.0)
202189
203190
:param obj: The object that is no longer being observed
204191
"""
@@ -1163,8 +1150,8 @@ def descriptive_name(self) -> str:
11631150
import anki_vector
11641151
11651152
with anki_vector.Robot() as robot:
1166-
if robot.world.charger.is_visible:
1167-
print(f"{obot.world.charger.descriptive_name}")
1153+
if robot.world.charger:
1154+
print(f"{robot.world.charger.descriptive_name}")
11681155
"""
11691156
return f"{self.__class__.__name__} id={self._object_id}"
11701157

anki_vector/proximity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class ProximityComponent(util.Component):
141141
with anki_vector.Robot() as robot:
142142
proximity_data = robot.proximity.last_sensor_reading
143143
if proximity_data is not None:
144-
print('Proximity distance: {0}, engine considers useful: {1}'.format(proximity_data.distance, proximity_data.is_valid))
144+
print('Proximity distance: {0}'.format(proximity_data.distance))
145145
"""
146146

147147
def __init__(self, robot):

anki_vector/user_intent.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,25 @@ class UserIntent:
140140
from anki_vector.events import Events
141141
from anki_vector.user_intent import UserIntent, UserIntentEvent
142142
143-
def main():
144-
def on_user_intent(robot, event_type, event, done):
145-
user_intent = UserIntent(event)
146-
if user_intent.intent_event is UserIntentEvent.weather_response:
147-
data = json.loads(user_intent.intent_data)
148-
print(f"Weather report for {data['speakableLocationString']}: "
149-
f"{data['condition']}, temperature {data['temperature']} degrees")
150-
done.set()
151-
152-
args = anki_vector.util.parse_command_args()
153-
with anki_vector.Robot(args.serial) as robot:
154-
done = threading.Event()
155-
robot.events.subscribe(on_user_intent, Events.user_intent, done)
156-
157-
print('------ Vector is waiting to be asked "Hey Vector! What is the weather report?" Press ctrl+c to exit early ------')
158-
159-
try:
160-
if not done.wait(timeout=10):
161-
print('------ Vector never heard a request for the weather report ------')
162-
except KeyboardInterrupt:
163-
pass
143+
def on_user_intent(robot, event_type, event, done):
144+
user_intent = UserIntent(event)
145+
if user_intent.intent_event is UserIntentEvent.weather_response:
146+
data = json.loads(user_intent.intent_data)
147+
print(f"Weather report for {data['speakableLocationString']}: "
148+
f"{data['condition']}, temperature {data['temperature']} degrees")
149+
done.set()
150+
151+
with anki_vector.Robot() as robot:
152+
done = threading.Event()
153+
robot.events.subscribe(on_user_intent, Events.user_intent, done)
154+
155+
print('------ Vector is waiting to be asked "Hey Vector! What is the weather report?" Press ctrl+c to exit early ------')
156+
157+
try:
158+
if not done.wait(timeout=10):
159+
print('------ Vector never heard a request for the weather report ------')
160+
except KeyboardInterrupt:
161+
pass
164162
165163
:param event: an event containing UserIntent data
166164
"""

anki_vector/vision.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,9 @@ def on_robot_observed_motion(robot, event_type, event):
166166
167167
robot.vision.enable_motion_detection(detect_motion=True)
168168
169-
print("\n\nVector is waiting to see motion. Make some movement within Vector's camera view.\n\n")
169+
print("Vector is waiting to see motion. Make some movement within Vector's camera view")
170170
171-
try:
172-
while True:
173-
time.sleep(0.5)
174-
except KeyboardInterrupt:
175-
pass
171+
time.sleep(3.0)
176172
177173
robot.events.unsubscribe(on_robot_observed_motion, Events.robot_observed_motion)
178174
"""

anki_vector/world.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def charger(self) -> objects.Charger:
268268
# First, place Vector directly in front of his charger so he can observe it.
269269
270270
with anki_vector.Robot() as robot:
271-
print('most recently observed charger: {0}.'.format(robot.world.charger))
271+
print('Most recently observed charger: {0}'.format(robot.world.charger))
272272
"""
273273
if self._charger is not None:
274274
return self._charger
@@ -323,8 +323,7 @@ def get_face(self, face_id: int) -> faces.Face:
323323
324324
def test_subscriber(robot, event_type, event):
325325
for face in robot.world.visible_faces:
326-
my_face = robot.world.get_face(face.face_id)
327-
print(f"Name: {my_face.name}")
326+
print(f"Face id: {face.face_id}")
328327
329328
with anki_vector.Robot(enable_face_detection=True) as robot:
330329
# If necessary, move Vector's Head and Lift to make it easy to see his face
@@ -336,7 +335,7 @@ def test_subscriber(robot, event_type, event):
336335
337336
print("------ show vector your face, press ctrl+c to exit early ------")
338337
try:
339-
time.sleep(10)
338+
time.sleep(5)
340339
except KeyboardInterrupt:
341340
robot.disconnect()
342341
"""

0 commit comments

Comments
 (0)