Skip to content

Commit

Permalink
feat: toggle callback activation (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
HiroIshida authored Feb 12, 2025
1 parent 5c21d38 commit 4b3b709
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion node_script/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


class DeticRosNode:
is_active: bool
detic_wrapper: DeticWrapper
sub: Subscriber
# some debug image publisher
Expand All @@ -37,9 +38,12 @@ def __init__(self, node_config: Optional[NodeConfig] = None):

rospy.loginfo("node_config: {}".format(node_config))

self.is_active = True
self.detic_wrapper = DeticWrapper(node_config)
self.srv_handler = rospy.Service('~segment_image', DeticSeg, self.callback_srv)
self.vocab_srv_handler = rospy.Service('~custom_vocabulary', CustomVocabulary, self.custom_vocab_srv)
self.inactivate_srv_handler = rospy.Service('~inactivate', Empty, self.inactivate_srv)
self.activate_srv_handler = rospy.Service('~activate', Empty, self.activate_srv)
self.default_vocab_srv_handler = rospy.Service('~default_vocabulary', Empty, self.default_vocab_srv)

if node_config.enable_pubsub:
Expand Down Expand Up @@ -70,7 +74,10 @@ def __init__(self, node_config: Optional[NodeConfig] = None):
rospy.loginfo('initialized node')

def callback_image(self, msg: Image):
# Inference
if not self.is_active:
rospy.sleep(0.1) # to avoid potential busy loop
return

raw_result = self.detic_wrapper.infer(msg)

# Publish main topics
Expand Down Expand Up @@ -105,6 +112,16 @@ def callback_image(self, msg: Image):
time_elapsed_total = (rospy.Time.now() - msg.header.stamp).to_sec()
rospy.loginfo('total elapsed time in callback {}'.format(time_elapsed_total))

def inactivate_srv(self, req: EmptyRequest) -> EmptyResponse:
rospy.loginfo("Inactivate subscriber callback")
self.is_active = False
return EmptyResponse()

def activate_srv(self, req: EmptyRequest) -> EmptyResponse:
rospy.loginfo("Activate subscriber callback")
self.is_active = True
return EmptyResponse()

def callback_srv(self, req: DeticSegRequest) -> DeticSegResponse:
msg = req.image
raw_result = self.detic_wrapper.infer(msg)
Expand Down

0 comments on commit 4b3b709

Please sign in to comment.