Raising Exceptions in Subscriber Callbacks
ROS distribution: noetic, rospy
I want to raise specific exceptions depending on the data received from the rostopic. However, raising exceptions in the subscriber callback (user_callback) returns a bad callback error due to rospy callback exception handling: https://github.com/ros/ros_comm/blob/...
Is there a way to raise custom ROS exceptions in one node (user_command node) and handle it in another (control node)? Or is there another way to filter data from a subscribed topic and raise an exception accordingly?
For example: I am subscribed to /user_commands. If a "t" is published I want to immediately exit the "scan" state while loop and switch to another state (track)
class command_t(Exception):
pass
class command_h(Exception):
pass
class command_hv(Exception):
pass
class actions:
def __init__(self):
#initialize all my variables
#CALLBACKS
def user_callback(self,msg):
if (msg.data == 't'):
rospy.loginfo(msg.data)
raise command_t #BAD CALLBACK ERROR
if (msg.data == 'h'):
raise command_h
#STATES
def scan(self):
try:
rospy.loginfo("scan mode")
local_delta = 0
while (x0 + local_delta < x1):
self.waypoint(x0 + local_delta, y0, z)
self.waypoint(x0 + local_delta, y1, z)
local_delta = local_delta + self.delta
self.waypoint(x0 + local_delta, y1, z)
self.waypoint(x0 + local_delta, y0 , z)
local_delta = local_delta + self.delta
local_delta = 0
except command_t:
self.track()
except command_h:
self.home()
if __name__ == '__main__':
rospy.init_node("control")
action = actions()
user_sub = rospy.Subscriber("/user_commands", String, action.user_callback)
rate = rospy.Rate(100)
action.scan()
Regardless of the technical implementation, and probably pedantic, but using exceptions for regular control flow seems like an abuse of the concept to me.
If you're happy, we're happy of course, but you might be trying to solve an xy-problem, or have other options which would follow best-practices more and don't rely on (potentially) incomplete implementations.