ロボットが立ち上がった際、自動的にコマンドの待ち受け状態に入り、launchファイルのロード、開始、停止、再開、アウトなどのコマンドを受け付けて、指定されたlaunchファイルを実行することが可能な仕組みを作る必要があった。
#!/usr/bin/env python import roslaunch import os import rospy class Ros_Launch(object): def __init__(self, ): self.launchfile = "" self.reinitialize() def __del__(self,): self.exit() def load_file(self, launch_file): if self.is_alive(): raise RuntimeError("you need to stop before you load another launch file") return False self.launchfile = os.path.abspath(os.path.expanduser(launch_file)) self.launch.parent.config = roslaunch.config.load_config_default([self.launchfile], None) return True def start(self, ): local_machine = roslaunch.core.local_machine() for n in self.launch.parent.config.nodes: n.machine = local_machine self.launch.start() def is_alive(self, ): if self.launch.parent.pm is not None and self.launch.parent.pm.is_alive() and len(self.launch.parent.pm.get_active_names())>0: return True return False def current_file(self, ): return self.launchfile def reinitialize(self, ): self.launch = roslaunch.scriptapi.ROSLaunch() def resume(self, launch_file): self.launch.stop() self.reinitialize() self.load_file(launch_file) def stop(self, ): self.launch.stop() self.reinitialize() #self.load_file(self.launchfile) def exit(self, ): self.launch.stop()
以上