利用python脚本监听json文件,当map_builder_exit的值为1时,结束与cartographer建图有关的node,释放节点。python脚本较为简单,仅涉及python对json的处理,和利用python执行shell命令。
脚本map_save.py如下:
# coding=utf-8 //设置文本格式
import os,time,sys
from multiprocessing import Process
import json
def sub_exit_signal():
filename = '/home/cabin/Desktop/remote_communication/task_state.json'
while 1 :
time.sleep(1)
with open(filename, 'r') as fr:
json_data = json.load(fr)
exit_signal = json_data['map_builder']['map_builder_exit']
fr.close()
if 1 == exit_signal :
print "get exit signal"
json_data['map_builder']['map_builder_exit'] = 0
m_json_data = json_data;
os.system("rosnode kill cartographer_occupancy_grid_node map_to_laser_odom robot_state_publisher rviz cartographer_node")
break
print "end map_build ..."
sys.exit(0)
if __name__ == '__main__':
sub_exit_signal()
print "building the map ..."
注意对json文件的监听时,不能读取频率太高,应设一个时延,否则会报错。