Run a launch script in subscriber callback
I want to spawn a robot B when robot A pass certain location. Currently, my implementation is having a subscriber subscribe to odom of robot A. When robot A odom msg.pose.pose.position>1, run the launch script ( to launch robot B) in callback. I unsubscribe to the node afterwards since I only need to spawn the robot B once. but I was getting the bad call back error.
(a lot of red lines: [ERROR] [1648267836.094904, 23.063000]: bad callback: <function callback="" at="" 0x7f8b743a9310=""> Traceback (most recent call last): File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 750, in _invoke_callback cb(msg) File "/home/xxx/catkin_ws/src/beginner_tutorials/scripts/py_launch.py", line 28, in callback py_launch() )
is it possible to run a launch script in the subscriber callback? or is there a better way to achieve the above?
#! /usr/bin/env python3
import roslaunch
import rospy
from nav_msgs.msg import Odometry
count=0
def callback(msg):
global count
print(msg.pose.pose.position.z)
if msg.pose.pose.position.z>1:
count=count+1
else:
pass
print(count)
if count==1:
count=count+1
odom_sub.unregister()
py_launch()
def py_launch():
uuid = roslaunch.rlutil.get_or_generate_uuid(None, False)
roslaunch.configure_logging(uuid)
launch = roslaunch.parent.ROSLaunchParent(uuid, ["/home/xxx/catkin_ws/src/beginner_tutorials/launch/test_replace.launch"])
launch.start()
rospy.loginfo("started")
rospy.sleep(3)
launch.shutdown()
def odom_listen():
global odom_sub
odom_sub = rospy.Subscriber('/odom', Odometry, callback)
if __name__ == '__main__':
rospy.init_node('en_Mapping', anonymous=True)
odom_listen()
rospy.spin()
Why wait to launch robot B until robot A satisfies
msg.pose.pose.position > 1
? Why not launch robot B the same time as robot A, and then make robot B do its thing after that condition for robot A evaluates to true?for my specific simulation, I need to only have robot A appears at the beginning, then, when it reaches certain location. second robot B appears from the sky.