Skip to content

Commit c50cf9d

Browse files
author
Michelle Sintov
committed
VIC-13150 Turn on motion detection event #160
1 parent 2fa38ea commit c50cf9d

2 files changed

Lines changed: 52 additions & 30 deletions

File tree

anki_vector/events.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ class Events(Enum):
4848
robot_observed_object = "robot_observed_object" #: Robot event triggered when an object is observed by the robot.
4949
cube_connection_lost = "cube_connection_lost" #: Robot event triggered when an object's subscribed connection has been lost.
5050

51-
robot_observed_face = "robot_observed_face" #: Robot event for when a face is observed by the robot.
52-
robot_changed_observed_face_id = "robot_changed_observed_face_id" # : Robot event for when a known face changes its id.
51+
robot_observed_motion = "robot_observed_motion" #: Robot event dispatched when Vector observes motion.
52+
robot_observed_face = "robot_observed_face" #: Robot event for when a face is observed by the robot.
53+
robot_changed_observed_face_id = "robot_changed_observed_face_id" #: Robot event for when a known face changes its id.
5354

54-
wake_word = "wake_word" #: Robot event triggered when Vector hears "Hey Vector"
55-
user_intent = "user_intent" #: Robot event triggered after Vector processes voice commands
55+
wake_word = "wake_word" #: Robot event triggered when Vector hears "Hey Vector".
56+
user_intent = "user_intent" #: Robot event triggered after Vector processes voice commands.
5657

5758
# Audio
5859
audio_send_mode_changed = "audio_send_mode_changed" #: Robot event containing changes to the robot's audio stream source data processing mode.

anki_vector/vision.py

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ def __init__(self, robot):
4343

4444
self._detect_faces = False
4545
self._detect_custom_objects = False
46-
# TODO implement
47-
# self._detect_motion = False
46+
self._detect_motion = False
4847
self._display_camera_feed_on_face = False
4948

5049
robot.events.subscribe(self._handle_mirror_mode_disabled_event, events.Events.mirror_mode_disabled)
@@ -62,7 +61,7 @@ def _handle_mirror_mode_disabled_event(self, _robot, _event_type, _msg):
6261
def _handle_vision_modes_auto_disabled_event(self, _robot, _event_type, _msg):
6362
self._detect_faces = False
6463
self._detect_custom_objects = False
65-
# self._detect_motion = False
64+
self._detect_motion = False
6665
self._display_camera_feed_on_face = False
6766

6867
@property
@@ -73,10 +72,9 @@ def detect_faces(self):
7372
def detect_custom_objects(self):
7473
return self._detect_custom_objects
7574

76-
# TODO implement
77-
# @property
78-
# def detect_motion(self):
79-
# return self._detect_motion
75+
@property
76+
def detect_motion(self):
77+
return self._detect_motion
8078

8179
@property
8280
def display_camera_feed_on_face(self):
@@ -88,8 +86,8 @@ async def disable_all_vision_modes(self):
8886
await self.enable_face_detection(False, False)
8987
if self.detect_custom_objects:
9088
await self.enable_custom_object_detection(False)
91-
# if self.detect_motion:
92-
# await self.enable_motion_detection(False)
89+
if self.detect_motion:
90+
await self.enable_motion_detection(False)
9391
if self.display_camera_feed_on_face:
9492
await self.enable_display_camera_feed_on_face(False)
9593

@@ -142,23 +140,46 @@ async def enable_face_detection(
142140
enable_gaze_detection=False)
143141
return await self.grpc_interface.EnableFaceDetection(enable_face_detection_request)
144142

145-
# TODO implement
146-
# @connection.on_connection_thread()
147-
# async def enable_motion_detection(self, detect_motion: bool = True):
148-
# """Enable motion detection on the robot's camera
149-
#
150-
# :param detect_motion: Specify whether we want the robot to detect motion.
151-
#
152-
# .. testcode::
153-
#
154-
# import anki_vector
155-
# with anki_vector.Robot() as robot:
156-
# robot.vision.enable_motion_detection(detect_motion=True)
157-
# """
158-
# self._detect_motion = detect_motion
159-
160-
# enable_motion_detection_request = protocol.EnableMotionDetectionRequest(enable=detect_motion)
161-
# return await self.grpc_interface.EnableMotionDetection(enable_motion_detection_request)
143+
@connection.on_connection_thread()
144+
async def enable_motion_detection(self, detect_motion: bool = True):
145+
"""Enable motion detection on the robot's camera
146+
147+
:param detect_motion: Specify whether we want the robot to detect motion.
148+
149+
.. testcode::
150+
151+
import time
152+
153+
import anki_vector
154+
from anki_vector.events import Events
155+
from anki_vector.util import degrees
156+
157+
def on_robot_observed_motion(robot, event_type, event):
158+
print(f"--------- Vector observed motion --------- \n{event}")
159+
160+
with anki_vector.Robot(show_viewer=True) as robot:
161+
robot.events.subscribe(on_robot_observed_motion, Events.robot_observed_motion)
162+
163+
# If necessary, move Vector's Head and Lift to make it easy to see his face
164+
robot.behavior.set_head_angle(degrees(45.0))
165+
robot.behavior.set_lift_height(0.0)
166+
167+
robot.vision.enable_motion_detection(detect_motion=True)
168+
169+
print("\n\nVector is waiting to see motion. Make some movement within Vector's camera view.\n\n")
170+
171+
try:
172+
while True:
173+
time.sleep(0.5)
174+
except KeyboardInterrupt:
175+
pass
176+
177+
robot.events.unsubscribe(on_robot_observed_motion, Events.robot_observed_motion)
178+
"""
179+
self._detect_motion = detect_motion
180+
181+
enable_motion_detection_request = protocol.EnableMotionDetectionRequest(enable=detect_motion)
182+
return await self.grpc_interface.EnableMotionDetection(enable_motion_detection_request)
162183

163184
@connection.on_connection_thread()
164185
async def enable_display_camera_feed_on_face(self, display_camera_feed_on_face: bool = True):

0 commit comments

Comments
 (0)