Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kayukawa/description request #160

Merged
merged 15 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cabot_ui/cabot_ui/description.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def request_description_with_images1(self, global_position, length_index=0):
return None

# for EventMapper2()
def request_description_with_images2(self, global_position, mode, length_index=0):
def request_description_with_images2(self, global_position, mode, length_index=0, timeout=10):
if not self.enabled:
return None
if not self.surround_enabled and not self.stop_reason_enabled and not self.stop_reason_data_collect_enabled:
Expand Down Expand Up @@ -323,14 +323,17 @@ def request_description_with_images2(self, global_position, mode, length_index=0
response = requests.post(
F"{API_URL}?{lat=}&{lng=}&{rotation=}&{max_distance=}&{length_index=}&{distance_to_travel=}",
headers=headers,
data=json_data
data=json_data,
timeout=timeout
)
if response.status_code == 200:
response_json = response.json()
self._logger.info('Successfully sent images to server.')
return response_json
else:
self._logger.error(f'Failed to send images to server. Status code: {response.status_code}')
except requests.exceptions.Timeout:
self._logger.error("Request timed out. Skipping description processing.")
except Exception as e:
self._logger.error(f'Error sending HTTP request: {e}')
return None
7 changes: 4 additions & 3 deletions cabot_ui/cabot_ui/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class SpeechPriority:
REQUIRED = 90
HIGH = 60
NORMAL = 30
MODERATE = 25
LOW = 10


Expand Down Expand Up @@ -400,12 +401,12 @@ def please_return_position(self):

def requesting_describe_surround(self):
self._activity_log("cabot/interface", "requesting_describe_surround", "")
self.speak(i18n.localized_string("REQUESTING_DESCRIBE_SURROUND"), priority=SpeechPriority.HIGH)
self.speak(i18n.localized_string("REQUESTING_DESCRIBE_SURROUND"), priority=SpeechPriority.MODERATE)

def requesting_describe_surround_stop_reason(self):
self._activity_log("cabot/interface", "requesting_describe_surround_stop_reason", "")
self.speak(i18n.localized_string("REQUESTING_DESCRIBE_FORWARD"), priority=SpeechPriority.HIGH)
self.speak(i18n.localized_string("REQUESTING_DESCRIBE_FORWARD"), priority=SpeechPriority.MODERATE)

def describe_surround(self, description):
self._activity_log("cabot/interface", "describe_surround", description)
self.speak(description, priority=SpeechPriority.HIGH)
self.speak(description, priority=SpeechPriority.MODERATE)
91 changes: 72 additions & 19 deletions cabot_ui/scripts/cabot_ui_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import threading
import traceback
import yaml
import requests
import time

from rcl_interfaces.msg import ParameterDescriptor, ParameterType
import rclpy
Expand Down Expand Up @@ -89,6 +91,8 @@ def __init__(self, node, nav_node, tf_node, srv_node, act_node, soc_node, desc_n
self._navigation = Navigation(nav_node, tf_node, srv_node, act_node, soc_node)
self._navigation.delegate = self
self._description = Description(desc_node)
self._last_description_event_time = 0
self._processing_lock = threading.Lock()
# self._exploration = Exploration()
# self._exploration.delegate = self

Expand Down Expand Up @@ -462,31 +466,64 @@ def _process_navigation_event(self, event):
self._interface.describe_surround(result['description'])

if event.subtype == "description_stop_reason" and self._description.enabled:
current_time = time.time()
if current_time - self._last_description_event_time < 10:
return
self._last_description_event_time = current_time

if not self._processing_lock.acquire(blocking=False):
return

self._logger.info("Request Stop Reason Description")
if self._interface.last_pose:
gp = self._interface.last_pose['global_position']
self._interface.requesting_describe_surround_stop_reason()
result = self._description.request_description_with_images2(gp, "stop_reason", length_index=0)
if result:
self._interface.describe_surround(result['description'])
try:
if self._interface.last_pose:
gp = self._interface.last_pose['global_position']
self._interface.requesting_describe_surround_stop_reason()
try:
result = self._description.request_description_with_images2(gp, "stop_reason", length_index=0, timeout=10)
if result:
self._interface.describe_surround(result['description'])
except requests.exceptions.Timeout:
self._logger.error("Request timed out. Skipping description processing.")
except Exception as e:
self._logger.error(f"Error in request: {e}")
finally:
self._processing_lock.release()

if event.subtype == "description_surround" and self._description.enabled:
# TODO: needs to reset last_plan_distance when arrived/paused
current_time = time.time()
if current_time - self._last_description_event_time < 10:
return
self._last_description_event_time = current_time

if not self._processing_lock.acquire(blocking=False):
return

self._logger.info(F"Request Surround Description (Duration: {event.param})")
if self._interface.last_pose:
length_index = event.param
gp = self._interface.last_pose['global_position']
self._interface.requesting_describe_surround()
result = self._description.request_description_with_images2(gp, "surround", length_index=length_index)
if result:
self._interface.describe_surround(result['description'])
try:
if self._interface.last_pose:
length_index = event.param
gp = self._interface.last_pose['global_position']
self._interface.requesting_describe_surround()
try:
result = self._description.request_description_with_images2(gp, "surround", length_index=length_index, timeout=10)
if result:
self._interface.describe_surround(result['description'])
except requests.exceptions.Timeout:
self._logger.error("Request timed out. Skipping description processing.")
except Exception as e:
self._logger.error(f"Error in request: {e}")
finally:
self._processing_lock.release()

if event.subtype == "toggle_speak_state":
self._logger.info("Request Toggle TTS State")
e = NavigationEvent("togglespeakstate", None)
msg = std_msgs.msg.String()
msg.data = str(e)
self._eventPub.publish(msg)
self._last_description_event_time = 0

if event.subtype == "toggle_conversation":
self._logger.info("Request Start/Stop Conversation Interface")
Expand Down Expand Up @@ -616,13 +653,29 @@ def done_callback():
self._interface.pausing_navigation()
else:
if event.subtype == "resume_or_stop_reason" and self._description.enabled:
current_time = time.time()
if current_time - self._last_description_event_time < 10:
return
self._last_description_event_time = current_time

if not self._processing_lock.acquire(blocking=False):
return

self._logger.info("Request Stop Reason Description")
if self._interface.last_pose:
gp = self._interface.last_pose['global_position']
self._interface.requesting_describe_surround_stop_reason()
result = self._description.request_description_with_images2(gp, "stop_reason", length_index=0)
if result:
self._interface.describe_surround(result['description'])
try:
if self._interface.last_pose:
gp = self._interface.last_pose['global_position']
self._interface.requesting_describe_surround_stop_reason()
try:
result = self._description.request_description_with_images2(gp, "stop_reason", length_index=0, timeout=10)
if result:
self._interface.describe_surround(result['description'])
except requests.exceptions.Timeout:
self._logger.error("Request timed out. Skipping description processing.")
except Exception as e:
self._logger.error(f"Error in request: {e}")
finally:
self._processing_lock.release()
else:
self._logger.info("NavigationState: state is not in pause state")
else:
Expand Down
Loading