ros自动脚本-bag file作成例

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值