How to shut down a ros node and start another ros node in python

asked 2022-05-21 05:15:16 -0500

zlq gravatar image

updated 2022-05-21 05:20:00 -0500

How to shut down a ros node and start another ros node in python? Below is my code, I want to know how should I write the code to achieve my function?

#!/usr/bin/python3
import rospy
from geometry_msgs.msg import Twist

def move():
    rospy.init_node('stop_robot', anonymous=True, disable_signals=True)
    velocity_publisher = rospy.Publisher(
        '/mobile_base/commands/velocity', Twist, queue_size=10)
    vel_msg = Twist()
    vel_msg.linear.x = 0
    vel_msg.linear.y = 0
    vel_msg.linear.z = 0
    vel_msg.angular.x = 0
    vel_msg.angular.y = 0
    vel_msg.angular.z = 0
    flag = True
    while not rospy.is_shutdown():
        velocity_publisher.publish(vel_msg)
        if flag == True:
            """
            **how can I start another node here?
            like: rosrun node_pkg node?**
            """
            rospy.loginfo("exit()!")
            rospy.signal_shutdown("exit()!")


if __name__ == '__main__':
    try:
        move()
    except rospy.ROSInterruptException:
        pass
edit retag flag offensive close merge delete