Skip to content

Commit

Permalink
Added default main generator helper, updated all python nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Purg committed Dec 7, 2023
1 parent 79ce797 commit b02c549
Show file tree
Hide file tree
Showing 28 changed files with 210 additions and 382 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,19 @@
"""
import json
from heapq import heappush, heappop
import logging
from pathlib import Path
from threading import Condition, Event, Lock, Thread
from typing import Callable
from typing import Dict
from typing import List
from typing import Optional
from typing import Tuple

import kwcoco
from builtin_interfaces.msg import Time
import numpy as np
import numpy.typing as npt
import rclpy
from rclpy.callback_groups import MutuallyExclusiveCallbackGroup
import rclpy.logging
from rclpy.node import Node
from rclpy.executors import MultiThreadedExecutor
import torch

from angel_system.activity_classification.tcn_hpl.predict import (
Expand All @@ -39,7 +34,7 @@
ObjectDetection2dSet,
ActivityDetection,
)
from angel_utils import declare_and_get_parameters, RateTracker
from angel_utils import declare_and_get_parameters, make_default_main, RateTracker
from angel_utils.activity_classification import InputWindow, InputBuffer
from angel_utils.conversion import time_to_int
from angel_utils.object_detection import max_labels_and_confs
Expand Down Expand Up @@ -683,40 +678,16 @@ def _save_results(self):

def destroy_node(self):
log = self.get_logger()
log.info("Stopping node runtime")
self.rt_stop()
with SimpleTimer("Shutting down runtime thread...", log.info):
self._rt_active.clear() # make RT active flag "False"
self._rt_thread.join()
self._save_results()
super().destroy_node()


def main():
logging.basicConfig(
format="[%(levelname)s] [%(asctime)s] [%(name)s.%(funcName)s]: %(message)s"
)
logging.getLogger().setLevel(logging.INFO)

rclpy.init()
log = rclpy.logging.get_logger("main")

activity_classifier = ActivityClassifierTCN()

executor = MultiThreadedExecutor(num_threads=4)
executor.add_node(activity_classifier)
try:
executor.spin()
except KeyboardInterrupt:
log.info("Keyboard interrupt, shutting down.\n")
finally:
log.info("Stopping node runtime")
activity_classifier.rt_stop()

# Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
activity_classifier.destroy_node()

rclpy.shutdown()
main = make_default_main(ActivityClassifierTCN, multithreaded_executor=4)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from threading import Lock

from pynput import keyboard
import rclpy
from rclpy.node import Node

from angel_msgs.msg import AnnotationEvent
from angel_utils import make_default_main


class AnnotationEventMonitor(Node):
Expand Down Expand Up @@ -90,23 +90,16 @@ def on_press(self, key):
self._publisher.publish(msg)


def main():
rclpy.init()

event_monitor = AnnotationEventMonitor()

keyboard_t = threading.Thread(target=event_monitor.monitor_keypress)
def init_kb_thread(node):
"""
Initialize the
"""
keyboard_t = threading.Thread(target=node.monitor_keypress)
keyboard_t.daemon = True
keyboard_t.start()

rclpy.spin(event_monitor)

# Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
event_monitor.destroy_node()

rclpy.shutdown()
main = make_default_main(AnnotationEventMonitor, pre_spin_callback=init_kb_thread)


if __name__ == "__main__":
Expand Down
8 changes: 2 additions & 6 deletions ros/angel_system_nodes/angel_system_nodes/audio/asr.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import simpleaudio as sa

from angel_msgs.msg import HeadsetAudioData, Utterance
from angel_utils import make_default_main


AUDIO_TOPIC = "audio_topic"
Expand Down Expand Up @@ -215,12 +216,7 @@ def asr_server_request_thread(self, audio_data, num_channels, sample_rate):
self._publisher.publish(utterance_msg)


def main():
rclpy.init()
asr = ASR()
rclpy.spin(asr)
asr.destroy_node()
rclpy.shutdown()
main = make_default_main(ASR)


if __name__ == "__main__":
Expand Down
17 changes: 3 additions & 14 deletions ros/angel_system_nodes/angel_system_nodes/audio/audio_player.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import threading
import time

import simpleaudio as sa

import rclpy
from rclpy.node import Node

from angel_msgs.msg import HeadsetAudioData
from angel_utils import make_default_main


class AudioPlayer(Node):
Expand Down Expand Up @@ -101,18 +101,7 @@ def audio_playback_thread(self, audio_data, num_channels, sample_rate):
audio_player_object.wait_done()


def main():
rclpy.init()

audio_player = AudioPlayer()

rclpy.spin(audio_player)

# Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
audio_player.destroy_node()
rclpy.shutdown()
main = make_default_main(AudioPlayer)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import queue
import rclpy
from rclpy.node import Node
from termcolor import colored
import threading
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

from angel_msgs.msg import InterpretedAudioUserEmotion, InterpretedAudioUserIntent
from angel_utils import declare_and_get_parameters
from angel_utils import make_default_main


IN_EXPECT_USER_INTENT_TOPIC = "expect_user_intent_topic"
IN_INTERP_USER_INTENT_TOPIC = "interp_user_intent_topic"
Expand Down Expand Up @@ -156,12 +157,7 @@ def _apply_filter(self, msg):
return msg


def main():
rclpy.init()
emotion_detector = BaseEmotionDetector()
rclpy.spin(emotion_detector)
emotion_detector.destroy_node()
rclpy.shutdown()
main = make_default_main(BaseEmotionDetector)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from langchain.chat_models import ChatOpenAI
import openai
import os
import rclpy

from ros.angel_system_nodes.angel_system_nodes.audio.emotion.base_emotion_detector import (
from angel_system_nodes.audio.emotion.base_emotion_detector import (
BaseEmotionDetector,
LABEL_MAPPINGS,
)
from angel_utils import make_default_main

openai.organization = os.getenv("OPENAI_ORG_ID")
openai.api_key = os.getenv("OPENAI_API_KEY")
Expand Down Expand Up @@ -90,12 +90,7 @@ def get_inference(self, msg):
return (self.chain.run(utterance=msg.utterance_text), 0.5)


def main():
rclpy.init()
emotion_detector = GptEmotionDetector()
rclpy.spin(emotion_detector)
emotion_detector.destroy_node()
rclpy.shutdown()
main = make_default_main(GptEmotionDetector)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import queue
import rclpy
from rclpy.node import Node
from termcolor import colored
import threading

from angel_msgs.msg import InterpretedAudioUserIntent, Utterance
from angel_utils import declare_and_get_parameters
from angel_utils import make_default_main

NEXT_STEP_KEYPHRASES = ["skip", "next", "next step"]
PREV_STEP_KEYPHRASES = ["previous", "previous step", "last step", "go back"]
Expand Down Expand Up @@ -152,12 +152,7 @@ def _contains_phrase(self, utterance, phrases):
return False


def main():
rclpy.init()
intent_detector = BaseIntentDetector()
rclpy.spin(intent_detector)
intent_detector.destroy_node()
rclpy.shutdown()
main = make_default_main(BaseIntentDetector)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from langchain import PromptTemplate, FewShotPromptTemplate
from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.llms import OpenAI
import openai
import os
import rclpy

from ros.angel_system_nodes.angel_system_nodes.audio.intent.base_intent_detector import (
from angel_system_nodes.audio.intent.base_intent_detector import (
BaseIntentDetector,
INTENT_LABELS,
)
from angel_utils import make_default_main


openai.organization = os.getenv("OPENAI_ORG_ID")
openai.api_key = os.getenv("OPENAI_API_KEY")
Expand Down Expand Up @@ -91,12 +92,7 @@ def detect_intents(self, msg):
return self.chain.run(utterance=msg), 0.5


def main():
rclpy.init()
intent_detector = GptIntentDetector()
rclpy.spin(intent_detector)
intent_detector.destroy_node()
rclpy.shutdown()
main = make_default_main(GptIntentDetector)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from rclpy.node import Node

from angel_msgs.msg import InterpretedAudioUserIntent, Utterance
from angel_utils import make_default_main


# Please refer to labels defined in
# https://docs.google.com/document/d/1uuvSL5de3LVM9c0tKpRKYazDxckffRHf7IAcabSw9UA .
Expand Down Expand Up @@ -118,15 +120,7 @@ def contains_phrase(self, utterance, phrases):
return False


def main():
rclpy.init()

intentDetector = IntentDetector()

rclpy.spin(intentDetector)

intentDetector.destroy_node()
rclpy.shutdown()
main = make_default_main(IntentDetector)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
InterpretedAudioUserIntent,
SystemCommands,
)
from angel_utils import make_default_main


# Parameter name constants
Expand Down Expand Up @@ -113,18 +114,7 @@ def intent_callback(self, intent: InterpretedAudioUserIntent) -> None:
self._sys_cmd_publisher.publish(sys_cmd_msg)


def main():
rclpy.init()

node = IntentToCommand()
rclpy.spin(node)

# Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
node.destroy_node()

rclpy.shutdown()
main = make_default_main(IntentToCommand)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
import openai
import os
import queue
import rclpy
from rclpy.node import Node
import requests
from termcolor import colored
import threading

from angel_msgs.msg import InterpretedAudioUserEmotion, SystemTextResponse
from angel_utils import declare_and_get_parameters
from angel_utils import make_default_main


openai.organization = os.getenv("OPENAI_ORG_ID")
openai.api_key = os.getenv("OPENAI_API_KEY")
Expand Down Expand Up @@ -153,12 +154,7 @@ def _apply_filter(self, msg):
return msg


def main():
rclpy.init()
question_answerer = QuestionAnswerer()
rclpy.spin(question_answerer)
question_answerer.destroy_node()
rclpy.shutdown()
main = make_default_main(QuestionAnswerer)


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit b02c549

Please sign in to comment.