天天看點

代碼中啟動ros節點roslaunch和rosrun,代碼啟動節點與關閉節點

                                                          roslaunch和rosrun,代碼啟動節點與關閉節點

py_slam_startros_在代碼中啟動ros節點roslaunch和rosrun

import subprocess
import rospy
import rosnode
 
class launch_demo:
    def __init__(self, cmd=None):
        self.cmd = cmd
 
    def launch(self):
        self.child = subprocess.Popen(self.cmd)
        return True
 
    def shutdown(self):
        self.child.terminate()
        self.child.wait()
        return True
 
if __name__ == "__main__":
    rospy.init_node('launch_demo',anonymous=True)
 
    launch_nav = launch_demo(["roslaunch", "pibot_simulator", "nav.launch"])
 
    launch_nav.launch()
 
    r = rospy.Rate(0.2)
    r.sleep()
 
    rospy.loginfo("switch map...")
    r = rospy.Rate(1)
    r.sleep()
 
    rosnode.kill_nodes(['map_server'])
 
    map_name = "/home/pibot/ros_ws/src/pibot_simulator/maps/blank_map_with_obstacle.yaml"
 
    map_node = subprocess.Popen(["rosrun", "map_server", "map_server", map_name, "__name:=map_server"])
 
    while not rospy.is_shutdown():
        r.sleep()
           

上面使用

python

代碼啟動了一個

PIBOT

模拟器的導航,然後

5s

後切換了一個地圖

  • 使用

    subprocess.Popen

    可以啟動一個程序(

    roslaunch

    或者

    rosrun

    )
  • 使用

    rosnode.kill_nodes

    可以殺死一個

    rosnode

繼續閱讀