diff --git a/assistant/brain.py b/assistant/brain.py index 148ac12..2b7fced 100644 --- a/assistant/brain.py +++ b/assistant/brain.py @@ -46,7 +46,10 @@ Rules: speaks: casual 반말 ("뭐 해?", "이리 와"), polite -요 and formal -습니다 all mean the same request to you. - Answer exactly what was asked, nothing else: short and exact, usually 1 or 2 sentences, no lists, no markdown, no emojis. Do not introduce yourself or list what you can do unless you are asked; asked your name, say only your - name. Never end with an offer like "How can I help you?" / "무엇을 도와드릴까요?". Only when the visitor asks + name. Never end with an offer like "How can I help you?" / "무엇을 도와드릴까요?". Say "nice to meet you" or + call the visitor by a name only when they clearly tell you their name ("I'm James", "my name is …", "제 이름은 …"); + "I am standing or sitting", "it looks like a phone", a lone odd word ("Kenny Mow") are no names: answer what + they say, or choose "unclear". Only when the visitor asks for more ("tell me more", "why", "details", "자세히") give more about that same topic. - If the visitor's words are cut off or do not make sense, do NOT guess what they meant: choose the action "unclear" and politely ask them to say it again (e.g. "Sorry, I didn't catch that. Could you say it again?"). @@ -338,6 +341,7 @@ Short Korean commands are commands: "컵 가져다 줘" -> fetch cup, "컵 옮 - sing: the visitor asks for a song or for music ("play / make / do some music", "음악 틀어 줘", "한 곡 해 줘"), or says yes / "any" / "you choose" / "아무거나" to your offer of a song. You make music by singing one of your songs: never say you can't play music. A real song with music then plays: only write a short "Okay!" / "네!". + You sing your songs yourself: say "I'll sing ..." / "불러 드릴게요", never "play" / "틀어 드릴게요". You know only your four songs: any other song or singer asked for ("sing Yesterday", "BTS 노래 불러 줘") is none: say kindly you can't sing that one and offer yours; never sing another song in its place. A joke is not a song: "tell me a joke", "prove it", "try again" after a joke is none, with a short joke. @@ -1983,6 +1987,8 @@ DO_IT = re.compile(r"^\W*(?:(?:okay|ok|sure|yes|yeah|then|so|well|go on|come on) SING_ASK = re.compile(r"\bsing|\bsong|\bmusic\b|노래|한 ?곡|불러", re.I) PROMISE = { # Lumi's own sentence saying it does this now ("Okay! I'll sing a song for you.", "러브 어택 불러 드릴게요.") "sing": re.compile(r"\b(?:i'll|i will|let me|i'm going to) sing\b|\bhere(?:'s| is| comes| goes) (?:a |my |one |the )?song\b|" + r"\b(?:i'll|i will|let me|i'm going to) play (?:it|that(?: one)?|this one|(?:a |the |some |my |one of my )?(?:songs?|music))\b|" + r"틀어\s*(?:드릴게요|드리겠습니다|줄게)|" r"(?:불러|들려)\s*(?:드릴게요|드리겠습니다|줄게|볼게요)|부를게요", re.I), "wave": re.compile(r"\b(?:i'll|i will|let me) wave\b|흔들어\s*(?:드릴게요|볼게요|줄게)|흔들게요", re.I), # any other motion: never done on the reply alone (the action line said none), lumi.answer() asks instead diff --git a/assistant/lumi.py b/assistant/lumi.py index 22d6533..907dc82 100644 --- a/assistant/lumi.py +++ b/assistant/lumi.py @@ -8,15 +8,17 @@ Behaviour - Every person the camera sees gets a track (an ID followed from frame to frame). -- A new person who stays in the greeting zone is greeted once: Lumi turns its head to them and says hello - (a greeting never moves the arm). A second person who arrives is greeted the same way, then the talk goes on. -- While a greeted person stays but says nothing, Lumi says nothing more (MAX_PROMPTS 0): it never speaks by itself. +- Lumi speaks only when called: a wake word ("Lumi", "Hi Lumi", "Hi", "How are you", "루미야", "안녕하세요", "하이") + or a wave (a hand up, facing it) from someone in the greeting zone. Someone who only stands there or walks past is + not greeted, and nobody is greeted during a task or a drive. Greeted: the head turns to them, "Hi, I'm Lumi..." + (a greeting never moves the arm). The talk then goes on without the wake word until a goodbye or TALK_IDLE_S of + silence (ended without a word); then a call is needed again. +- An answer is never cut for someone new: a call while Lumi talks is answered after it, turned to the caller. - When the person disappears from the camera (LOST_S), listening and speaking stop at once. - Someone who comes back later is a new visit and is greeted again. - Voice commands (English / Korean): go to , go home, look left/right/up/down/center, wave, stop. The arm waves only when asked, with an empty hand, from `look`, along a checked route (manipulation/wave.py). -- Patrol (--patrol): drive marker to marker in a loop. Only a person in the robot's path (PATH_HALF_WIDTH to - each side) closer than PATROL_NEAR_MAX makes Lumi stop and greet; people sitting beside the path are ignored. +- Patrol (--patrol): drive marker to marker in a loop. People are never greeted while driving (the base's own + sensors stop it for obstacles); a wake word stops it and Lumi talks. If the person says nothing for PATROL_WAIT_S, or the talk is over, or the person left, the patrol goes on (the base drives around a person who still stands in the way). Below LOW_BATTERY % Lumi drives home to charge and continues at RESUME_BATTERY %. @@ -106,6 +108,8 @@ FACE_WAIT_S = 1.0 # s after HOLD_S Lumi waits for those faces a # 2026-10-21: 6 of 8 returns had 3 good faces within 1.7 s, the other two after 5 s LOST_S = 2.0 # s not seen -> the person is gone (detections can miss a few frames) PROMPT_S = 12.0 # s of silence before "How can I help you?" +TALK_IDLE_S = 20.0 # s of silence with the visitor in view that end a conversation: the next talk needs + # the wake word or a wave again (owner, 2026-10-23) MAX_PROMPTS = 0 # Lumi never speaks into a silence by itself: the greeting asked already (owner, # 2026-10-22: "How can I help you?" twice to someone standing silent) TOO_CLOSE = 0.45 # m, a running wave is stopped @@ -118,7 +122,7 @@ CLOSE_BOX = 0.85 # a person is really closer than TOO_CLOSE on HEAD_SIGN = 1.0 # +image angle -> +head yaw (checked: + = robot's left) LOOK = {"left": (40, None), "right": (-40, None), "center": (0, 0), "up": (None, 20), "down": (None, -5)} # ^ yaw/pitch in degrees for "look" commands: +yaw = left, +pitch = up (checked on the dashboard 2026-10-12) -WAKE = re.compile(r"\b(hi|hey|hello|okay|ok|hai|high)?[\s,.!]*(lumi|lumia|loomi|lumie|lumy|lummy|looney|lu-mi|rumi|roomie)\b" +WAKE = re.compile(r"\b(hi|hey|hello|okay|ok|hai|high)?[\s,.!]*(lumi|lumia|loomi|loomy|lumie|lumee|lumy|lummy|lummi|looney|lu-mi|rumi|rumy|roomie)\b" r"|루미|누미", re.I) WAKE_REPLY = {"en": "Yes? How can I help?", "ko": "네? 무엇을 도와드릴까요?"} NAME_ONLY = re.compile(r"^\s*(hi |hey |hello )?(lumi|loomi|rumi|루미)[\s,.!?]*$", re.I) @@ -287,7 +291,11 @@ STATUS_Q = re.compile( r"(?:컵|병|사과|바나나|오렌지)(?:을|를)? ?들고 ?있(?:어|니|나요|어요)?\?|손이 ?비어|뭐 ?가져가는|그거 ?(?:누구 ?거|나 ?주는)", re.I) NOT_STATUS_NOW = re.compile(r"\b(tonight|after work|weekend|tomorrow|later today|for fun|in your free time)\b|주말|쉬는 날|" r"저 사람|우리|내일|퇴근|나중에", re.I) # not about what Lumi is doing now -HELLO_ONLY = re.compile(r"^\s*(hi|hey|hello|hiya)( there)?[\s,.!]*$|^\s*(안녕|안녕하세요)[\s.!]*$", re.I) +HELLO_ONLY = re.compile(r"^(?=\W*\w)\s*(?:(?:hi|hey|hello|hiya|hai|hallo|helo|high)(?: there)?[\s,.!]*)?" # a plain "Hi!", + r"(?:how are you(?: doing)?(?: today)?[\s,.!?]*)?$|" # "how are you?" + r"^\s*(?:안녕|안녕하세요|안녕하십니까|하이)[\s.!~?]*$", re.I) # "안녕하세요", "하이" +CALL_REST = re.compile(r"^(?:[\W_]|\b(?:hi|hey|hello|hiya|hai|ok|okay|there|how are you(?: doing)?(?: today)?)\b|" + r"안녕(?:하세요|하십니까)?|하이|야|아)*$", re.I) # what is left of a call without the name CAMERA_HALF_FOV = 42.0 # deg; half the camera's view (+ the head's YAW_LIMIT for the head camera) WAKE_MAX_S = 6.0 # s; longer utterances are not checked for the wake word BARGE_IN_LOGPROB = -0.6 # an interruption must be heard more clearly than a normal answer (-1.0) @@ -327,7 +335,8 @@ STUCK_S = 90.0 # s without a main-loop pass -> stacks of all GREETING = "Hi, I'm Lumi, the assistant robot. How can I help you?" # same voice as every other answer PROMPT = {"en": "How can I help you?", "ko": "무엇을 도와드릴까요?"} WELCOME_BACK = {"en": "Welcome back! How can I help you?", "ko": "다시 오셨네요, 무엇을 도와드릴까요?"} -RECENT_S = 150.0 # s; someone known back within this while the talk was open: Lumi just listens on +WAVE_CHECK_S = 0.4 # s between two looks for a raised hand (pose net, ~20 ms); two looks in a row +WAVE_MATCH_DEG = 15.0 # deg; a raised hand belongs to the tracked person within this angle IN_VIEW_S = 0.5 # s; a track seen this recently is someone else in view (not the one coming back) PATH_HALF_WIDTH = 0.4 # m to each side of the robot's centre line; on patrol only people in this # corridor are greeted (not people sitting at desks beside the aisle) @@ -1109,7 +1118,8 @@ class Lumi: """Answered without the LLM: what Lumi can do (assistant/abilities.py; the rest on "tell me more" / "yes"), which songs it knows, the battery now and whether it is charging (assistant/battery.py), and right after a song "another one" / "again" / "그거 말고" (audio/sing.py). True if handled.""" - if last.startswith("ACTION: sing") and sing.AFTER_SONG.search(text) and sing.asked(text, last): + if last.startswith("ACTION: sing") and sing.AFTER_SONG.search(text) and sing.asked(text, last) or \ + sing.picked(text, last): # after the list: "the first one", "Fantastic Baby" self.sing_song(text) return True if sing.list_asked(text): @@ -1150,7 +1160,8 @@ class Lumi: self.fetch_say("Sorry, I'm on a task right now. I can sing when I've finished.", "죄송해요, 지금 다른 일을 하고 있어요. 끝나면 불러 드릴게요.") return - song = sing.choose(text, self.lang, self.last_song) + song = sing.picked(text, self.brain.history[-1][1] if self.brain.history else "") or \ + sing.choose(text, self.lang, self.last_song) # after the list: "the first one", a title, "아무거나" if song is None: # a song we don't have: say which ones we know say = sing.unknown_reply(self.lang) self.brain.history.append((text, f"ACTION: none songs\n{say}")) # a title next is sung @@ -1168,8 +1179,8 @@ class Lumi: def song_abort(self): """While singing (clip mic only, like barge_in): "stop" / "그만" ends the song; another clear request ends it and is answered next (pending_text). The song's own words and unclear sounds are ignored.""" - if self.interrupted(): - return True + # not self.interrupted(): the head dances away from the listener, who then "left" (songs cut after 2-4 s, + # 2026-10-23); only words end a song u = self.mic.next(0.0) if u is None or not getattr(u, "during_speech", False) or u.seconds < 0.25 or mouth.own_voice(u): return False @@ -1974,6 +1985,10 @@ class Lumi: if not ears.understood(BARGE_IN_LOGPROB): # unclear while Lumi talks = mostly Lumi's own voice log(f" ignored while talking (unclear): {text!r}") return False + if call_only(text): # "Hi Lumi" / "루미야" while Lumi answers (another visitor): + log(f" called while talking: {text!r} - answer finished first, then turn to them") # never cut for it + self.pending_text, self.pending_call = text, (text, u) + return False if seeing.recheck(text): # "Really?", "진짜?", "Now?" right after a seeing answer log(f" interrupted by a check of what I see: {text!r}") self.pending_text = text @@ -2005,24 +2020,17 @@ class Lumi: # ---------- interrupts ---------- def interrupted(self): - """Stop listening/speaking: the person left, or a new person is waiting to be greeted.""" + """Stop listening/speaking: the person left. Never for a newcomer: the answer is finished first, then + someone who waves or calls is turned to (owner, 2026-10-23).""" if not self.vision: return False - if self.current is None and time.time() < self.voice_until: - return False - return (self.current is not None and not self.vision.present(self.current)) or \ - self.vision.to_greet() is not None + return self.current is not None and not self.vision.present(self.current) # ---------- behaviour ---------- def greet(self, track): - """A full greeting for someone new; someone known back: listen on, or a short welcome back.""" + """Someone waved at Lumi: a greeting for someone new, a short welcome back for someone known.""" track.greeted = True who, gone = self.vision.know(track) - if gone is not None and gone < RECENT_S and who.talking: - log(f"person #{track.id} is back after {gone:.0f} s - listening on") - self.current = track - self.turn_head(HEAD_SIGN * track.angle) - return if self.current is None: self.brain.reset() self.current = track @@ -2035,6 +2043,24 @@ class Lumi: self.turn_head(HEAD_SIGN * track.angle) # speech only: a greeting never moves the arm mouth.say(GREETING) # cached at start: plays at once + def waver(self): + """The person in the greeting zone not greeted yet who waves at Lumi (a hand up, facing it) in two looks + WAVE_CHECK_S apart, or None. The pose net runs only while someone waits in the zone.""" + t = self.vision.to_greet() + now = time.time() + if t is None or now - getattr(self, "wave_checked", 0.0) < WAVE_CHECK_S: + return None + self.wave_checked = now + from vision import scene + frames = safe(scene.snapshot, (self.vision.camera,), default=[]) or [] + up = [w for w in (safe(hands_up, frames, default=[]) or []) if self.vision.near_angle(w, WAVE_MATCH_DEG) is t] + seen, self.wave_seen = getattr(self, "wave_seen", None), (t.id, now) if up else None + if up and seen and seen[0] == t.id and now - seen[1] <= 2.5 * WAVE_CHECK_S: + log(f"WAVE from person #{t.id} at {t.distance} m, {t.angle:+.0f} deg") + self.wave_seen = None + return t + return None + def warm_llm(self): """brain.warm in the background (markers and the task state are read there, listening starts at once).""" threading.Thread(target=lambda: safe(lambda: self.brain.warm(self.markers, self.now_doing()[0])), daemon=True).start() @@ -2043,6 +2069,9 @@ class Lumi: """Listen once; answer if something was said. Returns True if the visitor spoke.""" if self.pending_text: # asked while Lumi was talking text, self.pending_text = self.pending_text, None + call, self.pending_call = getattr(self, "pending_call", None), None + if call and call[0] == text: # a call while Lumi talked: turn to the caller first + self.who_spoke(call[1]) self.answer(text) return True self.warm_llm() # the LLM's prompt start cached while they speak @@ -2052,6 +2081,7 @@ class Lumi: log(f" ignored own voice (sound match {mouth.last_echo:.2f}): {text!r}") text = None if text and self.own_echo(text) and not stop_order(text, self.markers) and \ + not sing.picked(text, self.brain.history[-1][1] if self.brain.history else "") and \ (u is None or (u.during_speech if u.source == "clip" else len(text.split()) <= 2)): # Lumi heard itself: the clip mic while Lumi talked, or the room's echo of the last word on the head array # (muted while Lumi talks); a longer head-array sentence is the visitor ("okay go to P9" after a list with @@ -2671,7 +2701,7 @@ class Lumi: if text: log(f" heard {text!r} (no wake word)") return False - rest = text[m.end():].strip(" ,.!?") + rest = re.sub(r"^(?:야|아)(?![가-힣])", "", text[m.end():]).strip(" ,.!?") # "루미야, 뭐 보여?" lang = ears.heard_lang("ko" if re.search(r"[가-힣]", text) else "en") self.lang = lang cal = doa.load_calib() @@ -2687,7 +2717,7 @@ class Lumi: self.quiet_until = 0.0 self.brain.reset() self.closing_pending = None - command = rest if len(rest) > 2 else "" + command = rest if len(rest) > 2 else text if re.search(r"how are you", text, re.I) else "" # answered target = self.find_caller(robot_angle, trusted) if target is not None: target.greeted = True @@ -2929,16 +2959,13 @@ class Lumi: self.write_calib("finished") from tasks import follow following = follow.running() # no greetings (the head would turn away), keep listening - greet_now = (self.modes.patrol if moving else self.modes.greeter) and time.time() > self.quiet_until \ - and not following - self.note_talk() - candidate = self.vision.to_greet() if greet_now else None - focus = self.current or candidate + greet_now = self.modes.greeter and not moving and time.time() > self.quiet_until and not following \ + and not self.doing() # never during a task: standing, idle or talking only + candidate = self.waver() if greet_now else None # Lumi never speaks to someone who only stands there or + focus = self.current or candidate # walks past: a wave (or the wake word) calls it self.vision.det.focus_angle = focus.angle if focus else None # drawn red in the dashboard new = candidate - if new: # a new visitor (first or second person) - if self.patrol: - self.patrol.pause() + if new: # someone waved at Lumi (first or second person) self.greet(new) self.last_activity, self.prompts = time.time(), 0 return @@ -2975,9 +3002,8 @@ class Lumi: self.looked_at = 0.0 self.turn_head(0, 0) if self.voice_until: - talked = bool(self.brain.history) and self.vision is not None and self.vision.anyone_near() - log(f"voice conversation over (silence){'' if talked else ', nobody here or nothing said: no goodbye'}") - self.end_conversation(None, quiet=not talked) + log("voice conversation over (silence): the wake word or a wave again") + self.end_conversation(None, quiet=True) # Lumi never speaks unasked: no goodbye into a silence if self.check_wake(): # "Hi Lumi" works in every mode return if self.patrol: @@ -2993,23 +3019,15 @@ class Lumi: if self.talk_turn(): self.last_activity, self.prompts = time.time(), 0 + elif not following and not self.doing() and time.time() - self.last_activity > TALK_IDLE_S: + log(f"conversation over (nothing said for {TALK_IDLE_S:.0f} s): the wake word or a wave again") + self.current.greeted = True # still here: only a new call starts the next talk + self.end_conversation(None, quiet=True) # Lumi never speaks unasked, no goodbye into a silence elif (not following and not self.interrupted() and time.time() - self.last_activity > PROMPT_S and self.prompts < MAX_PROMPTS): log(f"silent for {PROMPT_S:.0f} s - prompt") mouth.say(PROMPT[self.lang], abort=self.interrupted) self.last_activity, self.prompts = time.time(), self.prompts + 1 - def note_talk(self): - """Remember whom Lumi talks with and how the talk ended: with them in view (a goodbye: closed) or after they - left (open: back within RECENT_S, Lumi listens on without a word).""" - if self.current is not None: - if self.current.person is None: - self.vision.know(self.current) - self.talked_with = self.current.person - self.talked_with.talking = True - elif getattr(self, "talked_with", None) and time.time() >= self.voice_until: - self.talked_with.talking = time.time() - self.talked_with.last_seen > LOST_S - self.talked_with = None - def watchdog(self): """If the main loop is stuck for STUCK_S, write every thread's stack to the log (to find the cause).""" reported = 0.0 @@ -3135,6 +3153,8 @@ def route(text, markers, b, holding=False, seen=None, last="", chain_ok=True): from assistant import chain if stop_order(text, markers): return "stop", None + if sing.picked(text, last): # after the list of songs: "the first one", "Fantastic Baby" + return "sing", None hold = brain_mod.not_an_order(text) or not brain_mod.direct_order(text) # only a direct order to Lumi acts stuck = detour.recent() # right after a blocked drive: "try again", "go around it", "go back"; how = stuck and detour.read(text, markers, stuck["goal"]) # only a direct order (detour.read), else the LLM @@ -3300,6 +3320,25 @@ def empty_hand_request(text): return bool(EMPTY_HAND.search(text)) and not any(w in text.lower() for w in brain_mod.OBJECTS) +def call_only(text): + """"Hi Lumi", "루미야", "안녕하세요", "How are you?": a call, nothing asked yet.""" + t = text or "" + return bool(WAKE.search(t) or HELLO_ONLY.match(t)) and bool(CALL_REST.match(WAKE.sub(" ", t))) + + +def hands_up(frames): + """Waist-frame bearings (deg) of the people in these camera frames (scene.snapshot) holding a hand up while facing + Lumi: a wave or a hi at the camera.""" + from vision import pose, scene + out = [] + for f in frames: + for p in pose.detect(f["color"]): + raised, _ = scene._gesture(p["kp"]) + if raised and scene._facing(p["kp"]) is not False: + out.append(scene._bearing(f, (p["box"][0] + p["box"][2]) / 2)) + return out + + def claimed_name(text): """"I'm James" / "James here" / "제가 제임스예요" -> "james" / "제임스", or None.""" for m in I_AM.finditer(text or ""): diff --git a/audio/mouth.py b/audio/mouth.py index b7a2cfd..a9123b5 100644 --- a/audio/mouth.py +++ b/audio/mouth.py @@ -150,9 +150,13 @@ def lang_of(text): return "ko" if re.search(r"[가-힣]", text) else "en" +FOREIGN = re.compile(r"[Ѐ-ӿ぀-ヿ㐀-䶿一-鿿豈-﫿]+") # Cyrillic, kana, +# Chinese: Lumi speaks only English and Korean, such text from the LLM is dropped, never read out + + def sentences(text): - parts = re.split(r"(?<=[.!?。!?])\s+", text.strip()) - return [p for p in parts if p] + parts = re.split(r"(?<=[.!?。!?])\s+", FOREIGN.sub("", text).strip()) + return [p for p in parts if re.search(r"\w", p)] def first_parts(sentence): diff --git a/tests/test_reid.py b/tests/test_reid.py index c4fba24..cbc7de8 100644 --- a/tests/test_reid.py +++ b/tests/test_reid.py @@ -1,4 +1,4 @@ -"""Self-check of knowing people again (vision/reid.py, lumi.Vision.know, Lumi.greet / note_talk), no robot, no camera: +"""Self-check of knowing people again (vision/reid.py, lumi.Vision.know, Lumi.greet after a wave), no robot, no camera: python tests/test_reid.py""" import types @@ -72,7 +72,6 @@ def greet_next(L): t = L.vision.to_greet() assert t is not None L.greet(t) - L.note_talk() return t @@ -81,15 +80,12 @@ def leave(L, seconds): see(L.vision, [], 3.0) if L.current is not None: L.current, L.voice_until = None, clock["t"] + lumi.VOICE_SESSION_S - L.note_talk() clock["t"] += seconds - L.note_talk() def goodbye(L): """The person says goodbye in view (answer() / end_conversation clear the talk).""" L.current, L.voice_until = None, 0.0 - L.note_talk() # who it is is decided once a few faces are seen, or after FACE_WAIT_S without them @@ -102,29 +98,21 @@ L = make() see(L.vision, [("green", 40, "ben")], 1.0) assert L.vision.to_greet() is not None # faces seen: no wait -# a new person: the full greeting +# a new person who waves: the full greeting L = make() see(L.vision, [("red", 0)]) a = greet_next(L) -assert lumi.GREETING in said and L.current is a and a.person.talking, said +assert lumi.GREETING in said and L.current is a, said -# gone briefly while talking, back at another angle (a new track): no word, Lumi listens on -leave(L, 60) -see(L.vision, [("red", 30)]) -b = greet_next(L) -assert b is not a and b.person is a.person and L.current is b, said -assert said == [], said # no greeting, the history kept - -# a goodbye in view, back within RECENT_S: the short welcome back +# gone, back at another angle (a new track) and waves again: known, the short welcome back (a wave always answered) goodbye(L) -assert not a.person.talking leave(L, 60) see(L.vision, [("red", -10)]) greet_next(L) assert said == ["", lumi.WELCOME_BACK["en"]], said -# talk open but away longer than RECENT_S: welcome back too -leave(L, lumi.RECENT_S + 10) +# away a few minutes: welcome back too +leave(L, 160) see(L.vision, [("red", 0)]) greet_next(L) assert lumi.WELCOME_BACK["en"] in said, said diff --git a/tests/test_wake.py b/tests/test_wake.py new file mode 100644 index 0000000..50b57da --- /dev/null +++ b/tests/test_wake.py @@ -0,0 +1,240 @@ +"""Self-check of when Lumi speaks (owner's rules and live test 2026-10-23 16:30-17:10, logs/lumi.log): only when +called (wake word or a wave), never to someone who only stands there or walks past, never during a task; a talk +goes on without the wake word until it ends, then a call is needed again; an answer is never cut to greet someone, +the call is answered after it; "I am standing or sitting" is no name. Fake mic, camera, speech; nothing reaches the +robot: python tests/test_wake.py""" +import threading +import time +import types + +from assistant import brain as brain_mod +from assistant import lumi +from tasks import fetch, follow + +done = [] +lumi.agv.cancel = lambda: done.append("agv cancel") +lumi.agv.status = lambda: {"move_status": "idle"} +lumi.agv.turn_by = lambda a: done.append("TURN") +lumi.skills.cancel = lambda: done.append("skills cancel") +lumi.motion.stop = lambda: done.append("arm stop") +follow.running = lambda: False +fetch.state = lambda: {"status": "idle"} +lumi.mouth.say = lambda text, **k: done.append(("say", text)) +lumi.mouth.own_voice = lambda u: False +lumi.mouth.recent_speech = lambda s: "" +lumi.mouth.last_echo = 0.0 +lumi.doa.load_calib = lambda: {"fitted": False} +lumi.doa.estimate = lambda mics, cal: (0.0, 0.0, 0.0) +lumi.ears.heard_lang = lambda lang: lang +lumi.ears.transcribe = lambda mono: mono +lumi.ears.understood = lambda lp: True +lumi.time.sleep = lambda s: None +M = ["home", "dock2"] + + +class U: # one utterance from audio/mic.py + def __init__(self, text, seconds=0.8, during_speech=False): + self.mono, self.mics, self.seconds, self.during_speech, self.source = text, None, seconds, during_speech, "clip" + + +class Mic: + def __init__(self, *utts): + self.q = list(utts) + + def next(self, timeout): + return self.q.pop(0) if self.q else None + + +class Track: + def __init__(self, id, angle=0.0, distance=1.0): + self.id, self.angle, self.distance, self.greeted, self.person = id, angle, distance, False, None + + +class Vision: # people in the greeting zone, nobody waving unless `up` says so + def __init__(self, *tracks): + self.tracks, self.camera, self.pinned = list(tracks), "head", True + self.det = types.SimpleNamespace(focus_angle=None) + + def to_greet(self): + return next((t for t in self.tracks if not t.greeted), None) + + def near_angle(self, a, d=lumi.SPEAKER_MATCH_DEG): + return min((t for t in self.tracks if abs(t.angle - a) <= d), key=lambda t: abs(t.angle - a), default=None) + + def present(self, t): + return t in self.tracks + + def know(self, t): + return types.SimpleNamespace(talking=False), None + + def only_person(self): + return self.tracks[0] if len(self.tracks) == 1 else None + + def anyone_near(self): + return bool(self.tracks) + + def nearest_greeted(self): + return next((t for t in self.tracks if t.greeted), None) + + +up = [] # bearings of raised hands the fake pose net sees +lumi.hands_up = lambda frames: list(up) +import vision.scene as scene # noqa: E402 +scene.snapshot = lambda cams: [] + + +class Brain: + history = [] + + def reset(self): + done.append("") + + +def fresh(*tracks, utts=()): + L = lumi.Lumi.__new__(lumi.Lumi) + L.markers, L.lang, L.current, L.pending_text, L.patrol = M, "en", None, None, None + L.vision, L.mic, L.brain, L.chain_stop = Vision(*tracks), Mic(*utts), Brain(), threading.Event() + L.modes = types.SimpleNamespace(greeter=True, patrol=False, drive=True, refresh=lambda: None) + L.voice_until = L.quiet_until = L.calib_until = 0.0 + L.last_activity, L.closing_pending, L._yaw, L._yaw_time = time.time(), None, 0.0, time.time() + L.skill_thread, L.prompts = None, 0 + L.poll_commands = L.mission_wait = L.say_stuck = lambda: None + L.publish_busy = lambda what: None + L.turn_head = lambda *a, **k: done.append("head") + L.answer = lambda text: done.append(("answer", text)) + L.fetch_say = lambda en, ko: done.append(("say", en)) + L.mission_running = lambda: False + L.mission_hold = None + L.warm_llm = lambda: None + L.who_spoke = lambda u: done.append("who spoke") + lumi.ears.listen = lambda **k: (None, None) + done.clear() + up.clear() + return L + + +said = lambda: [d[1] for d in done if isinstance(d, tuple) and d[0] == "say"] # noqa: E731 + +# ---- the calls that wake Lumi (EN + KO + whisper slips) and what does not ---- +for call in ("Lumi.", "Hi Lumi.", "Hi.", "Hello Lumi!", "Hello.", "Hey Lumi", "How are you?", "Hi, how are you?", + "루미야.", "루미.", "안녕.", "안녕하세요.", "하이.", "하이 루미!", "Hi, Loomi.", "Hello, Rumi.", "Hai.", + "Lummy?", "누미야", "안녕하십니까."): + L = fresh() + L.mic = Mic(U(call)) + assert L.check_wake(), call + assert lumi.call_only(call), call +for heard in ("Let's make music.", "What is that?", "We make it.", "Let's.", "It looks fun.", "Okay.", ""): + L = fresh() + L.mic = Mic(U(heard)) + assert not L.check_wake(), heard # 16:57:00, 17:05:03 live: "(no wake word)" +assert not lumi.call_only("Lumi, what time is it?") and not lumi.call_only("루미야 노래 불러줘") +L = fresh() # "How are you?" is answered, not "Yes? How can I help?" +L.mic = Mic(U("How are you?")) +L.check_wake() +assert ("answer", "How are you?") in done and lumi.WAKE_REPLY["en"] not in said(), done +L = fresh() # "Hi Lumi, go home": done at once +L.mic = Mic(U("Hi Lumi, go home.")) +L.check_wake() +assert ("answer", "go home") in done, done + +# ---- someone who only stands in front (16:45:07 "see #11 ... greeted", 17:05:08 "GREET person #23"): no word ---- +a = Track(23, -28, 0.87) +L = fresh(a) +for _ in range(10): + L.step() +assert said() == [] and L.current is None and not a.greeted, done + +# ---- a wave (a hand up, facing Lumi, two looks in a row): greeted ---- +L = fresh(a := Track(23, -28, 0.87)) +up.append(-30.0) +for _ in range(3): + L.step() + L.wave_checked = 0.0 # the next look at once (no real time passes here) +assert lumi.GREETING in said() and L.current is a, done +L = fresh(a := Track(5, 0)) # one frame with a hand up: not yet +up.append(0.0) +assert L.waver() is None +up.clear() +L.wave_checked = 0.0 +assert L.waver() is None +L = fresh(a := Track(5, 40), Track(6, -40)) # the other person's hand: not this one +up.append(-40.0) +L.vision.tracks[1].greeted = True +for _ in range(3): + L.step() + L.wave_checked = 0.0 +assert said() == [], done + +# ---- never during a task or a drive, even for a wave ---- +for busy in ("skill", "drive"): + L = fresh(a := Track(7, 0)) + up.append(0.0) + L.doing = lambda b=busy: b + if busy == "drive": + L.traveling = lambda: True + L.patrol = types.SimpleNamespace(mode="patrol", moving=True, tick=lambda: None, pause=lambda: None) + for _ in range(3): + L.step() + L.wave_checked = 0.0 + assert lumi.GREETING not in said(), (busy, done) + +# ---- talking with #23, a newcomer stands in the zone: the answer is not cut (interrupted() stays False) ---- +a, b = Track(23, -28), Track(26, 55) +L = fresh(a, b) +a.greeted, L.current = True, a +assert not L.interrupted() and not L.speak_abort.__self__.interrupted() + +# ---- 17:05:24 "루미야." while Lumi answered "Give me.": the answer went on, the call answered after it ---- +for call in ("루미야.", "Hi Lumi.", "안녕하세요.", "Hello, Lumi!"): + L = fresh(a := Track(23, 0)) + a.greeted, L.current = True, a + L.mic = Mic(U(call, during_speech=True)) + assert L.barge_in() is False, call # the sentence is not cut + assert L.pending_text == call + assert L.talk_turn() and ("answer", call) in done and "who spoke" in done, (call, done) # turned to them +L = fresh(a := Track(23, 0)) # a real question from the visitor still interrupts +a.greeted, L.current = True, a +L.mic = Mic(U("Lumi, what is in my hand?", during_speech=True)) +assert L.barge_in() is True +L = fresh(a := Track(23, 0)) # and a stop word from anyone stops at once +a.greeted, L.current = True, a +L.act = lambda action, target: done.append(("act", action)) +L.mic = Mic(U("Stop.", during_speech=True)) +assert L.barge_in() is True and ("act", "stop") in done + +# ---- the talk goes on without the wake word; silence ends it quietly; then a call is needed again ---- +L = fresh(a := Track(14, 60)) +a.greeted, L.current = True, a +lumi.ears.listen = lambda **k: ("What is behind me?", None) +L.follow_filter = lambda text, u: False +L.step() +assert ("answer", "What is behind me?") in done # no wake word needed in an open talk +lumi.ears.listen = lambda **k: (None, None) +L.last_activity = time.time() - lumi.TALK_IDLE_S - 1 +done.clear() +L.step() +assert L.current is None and said() == [], done # over, no goodbye into the silence +L.mic = Mic(U("What is that?")) +L.step() +assert not any(d[0] == "answer" for d in done if isinstance(d, tuple)), done # needs "Lumi" again +assert L.vision.to_greet() is None # still in view, not greeted again by standing there +L.mic = Mic(U("루미야, 뭐 보여?")) +L.step() +assert ("answer", "뭐 보여") in done, done + +L = fresh() # a talk started by voice ends the same way +L.voice_until = time.time() - 1 +L.brain.history = [("hi", "hello")] +L.end_conversation = lambda text=None, quiet=False: done.append(("end", quiet)) +L.check_wake = lambda: False +L.step() +assert ("end", True) in done, done + +# ---- 16:46:41 "I am standing or sitting." -> "It is nice to meet you.": no name in it ---- +for text in ("I am standing or sitting.", "Am I sitting or standing?", "It looks like a phone.", "It looks iPhone.", + "I'm sitting.", "I am standing here."): + assert lumi.claimed_name(text) is None, text +assert lumi.claimed_name("I'm James.") == "james" +assert "only when they clearly tell you their name" in brain_mod.SYSTEM + +print("ok: wake words, waves, no greeting to passers-by or during tasks, answers never cut, names")