Ubuntu 18.04.4 LTS通过
record_bag.py
import sys
import commands
def sub_exec_1cmd(cmd):
(status, output) = commands.getstatusoutput(cmd)
if 0 != status:
print "execute ", cmd, ", failed. exit."
sys.exit(-1)
def exec_1xdo_key(kes, wait_sec=0, olog=""):
if "" != olog:
print olog
sub_exec_1cmd("xdotool key " + kes)
if wait_sec != 0:
sub_exec_1cmd("xdotool sleep " + str(wait_sec))
def exec_1xdo_type(cmds, wait_sec=0.2, olog=""):
if "" != olog:
print olog
sub_exec_1cmd("xdotool type '" + cmds + "'")
exec_1xdo_key("Return", wait_sec)
def new_console_window(wait_sec=0, olog=""):
if "" != olog:
print olog
exec_1xdo_key("ctrl+shift+n", wait_sec)
def close_console_window(after_close_wait_sec=0.3, need_confirm="need_confirm", after_confirm_wait_sec=1.2, olog=""):
if "" != olog:
print olog
exec_1xdo_key("alt+F4", after_close_wait_sec)
if "need_confirm" == need_confirm: #on confirm window, press default button(close-confirmed)
exec_1xdo_key("Return", after_confirm_wait_sec)
def new_tab(wait_sec=0.2, olog=""):
if "" != olog:
print olog
exec_1xdo_key("ctrl+shift+t", wait_sec)
def record_1bag(topic_name, out_bag_file_name):
print "-------------------->", out_bag_file_name
new_console_window(1, "open new console window")
exec_1xdo_type("roscore", 0.5, "run roscore")
new_tab()
exec_1xdo_type("rosrun turtlesim turtlesim_node", 1, "run turtlesim_node node")
exec_1xdo_key("alt+Tab", 0.3)
new_tab()
exec_1xdo_type("rosrun turtlesim turtle_teleop_key", 0.5, "run turtle_teleop_key node")
new_tab()
exec_1xdo_type("rosnode list", 0.5, "list all nodes")
#rewrite if same bag existed
rec_cmd = "rosbag record " + topic_name + " -O " + out_bag_file_name
exec_1xdo_type(rec_cmd, 1, "record a bag file@" + out_bag_file_name)
exec_1xdo_key("ctrl+Page_Up", 1.5, "select turtle_teleop_key console") #notePc test ok
exec_1xdo_key("Up", 1.2, "do some action on teleop_key\r\n forward")
exec_1xdo_key("Left", 1.5, " left turn")
exec_1xdo_key("Up", 1.2, " forward")
exec_1xdo_key("ctrl+Page_Down", 0.2, "select turtle_teleop_key console")
exec_1xdo_key("ctrl+c", 1.5, "stop record bag file")
close_console_window()
print "work done!"
record_1bag("/turtle1/cmd_vel", "./test1.bag")
record_1bag("/turtle1/cmd_vel", "./test2.bag")
#when play, using this cmd
#rosservice call /reset && rosbag play test1.bag --wait-for-subscribers