새소식

Dev record/ROS

[ROS] rosbag 최대 bandwidth로 로깅 하기

  • -
반응형

문제 상황

rosbag record를 이용해서 topic들을 로깅하는데 위와같이 버퍼 문제로 로깅이 제대로 되지 않는 경우가 종종 발생하였다.

특히 외부 장비에서 로깅을 진행할 경우 ethernet 통신을 하다 보니 버퍼 사이즈가 자동적으로 줄어들어 토픽이 제대로 레코딩 되지 않고 일정한 간격으로 로깅되지 않고 특정 영역에서 뭉치는 현상이 발생하였다.

해결 방안

1. 최대 bandwidth 사용

완변한 방법은 아니지만 bandwidth를 최대로 늘려 로깅을 할 경우 어느정도 개선이 있었다.

기존 rosbag record <topic> 명령어에 -b <bandwidth_size> 옵션을 통해 버퍼사이즈를 늘린다.

rosbag record -b <bandwidht_size> 

# 최대 bandwidht_size 사용하기
rosbag record -b 0

2. 동일 머신에서 레코딩하기

 

roswiki에서 위와같이 나와있듯 해당 장비가 연결된 머신에서 레코딩을 하는 것을 추천하였다.

그 후 각각의 rosbag 파일들을 하나로 합치는 방법이 있다.

2-2 merge rosbags

 

pip3 install rosbag-merge

 

개별 로깅된 rosbag 파일들을 하나로 합치는 작업이다.

이를 위해서는 개별 머신의 시간 동기화가 필수다. 이 작업이 선행되었다면 개별 머신에서 로깅된 데이터를 

위와 같은 방식으로 하나로 합쳐주면 된다.

2-3 rosbag mergy python code

2-2 방법 외 다음의 코드를 사용하는 방법이 있다.

#!/usr/bin/env python

import sys
import argparse
from fnmatch import fnmatchcase

from rosbag import Bag

def main():

    parser = argparse.ArgumentParser(description='Merge one or more bag files with the possibilities of filtering topics.')
    parser.add_argument('outputbag',
                        help='output bag file with topics merged')
    parser.add_argument('inputbag', nargs='+',
                        help='input bag files')
    parser.add_argument('-v', '--verbose', action="store_true", default=False,
                        help='verbose output')
    parser.add_argument('-t', '--topics', default="*",
                        help='string interpreted as a list of topics (wildcards \'*\' and \'?\' allowed) to include in the merged bag file')

    args = parser.parse_args()

    topics = args.topics.split(' ')

    total_included_count = 0
    total_skipped_count = 0

    if (args.verbose):
        print("Writing bag file: " + args.outputbag)
        print("Matching topics against patters: '%s'" % ' '.join(topics))

    with Bag(args.outputbag, 'w') as o: 
        for ifile in args.inputbag:
            matchedtopics = []
            included_count = 0
            skipped_count = 0
            if (args.verbose):
                print("> Reading bag file: " + ifile)
            with Bag(ifile, 'r') as ib:
                for topic, msg, t in ib:
                    if any(fnmatchcase(topic, pattern) for pattern in topics):
                        if not topic in matchedtopics:
                            matchedtopics.append(topic)
                            if (args.verbose):
                                print("Including matched topic '%s'" % topic)
                        o.write(topic, msg, t)
                        included_count += 1
                    else:
                        skipped_count += 1
            total_included_count += included_count
            total_skipped_count += skipped_count
            if (args.verbose):
                print("< Included %d messages and skipped %d" % (included_count, skipped_count))

    if (args.verbose):
        print("Total: Included %d messages and skipped %d" % (total_included_count, total_skipped_count))

if __name__ == "__main__":
    main()

 

reference

http://wiki.ros.org/rosbag/Commandline

https://pypi.org/project/rosbag-merge/

https://www.clearpathrobotics.com/assets/downloads/support/merge_bag.py

반응형
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.