ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I think you can write the messages into different topics at the same time, in one bag. (Ref 1, Ref 2) import rosbag import rospy from sensor_msgs.msg import LaserScan from math import floor

# Bag Names
old_bag_name = 'old_bag.bag'        
new_bag_names = 'new_bag.bag'

# List of topic names in the bag(have one topic that is of type LaserScan)
topics = ['scan_topic']
new_topics = ['new_scan_topic_1', 'new_scan_topic_2']     

# Open Bags for writing and reading
old_bag = rosbag.Bag(old_bag_name)
new_bag = rosbag.Bag(new_bag_names[0], 'w')

# Array to store Messages in chronological order
scan_array = []

# Read Bag and append messages from topics accordingly
for topic, msg, t in old_bag.read_messages(topics=topics):
    # Based on the topic name, read into an array(has only one topic right now)
    if(topic == "scan_topic"):
        scan_array.append(msg)
bag.close()


# Write message into new bag for each topic, while updating the time stamp
t = 0
half_time = floor(len(scan_array/2))
while t < half_time:

    # Update Time for required messages
    current_time = rospy.Time.now()
    scan_array[t].header.stamp = current_time
    scan_array[time_offest+t].header.stamp = current_time

    # Write messages onto bag for the current_time
    bag.write(new_topics[0], scan_array[t], current_time)
    bag.write(new_topics[0], scan_array[time_offest+t], current_time)
    t = t +1

I think you can write the messages into different topics at the same time, in one bag. (Ref 1, Ref 2) import rosbag import rospy from sensor_msgs.msg import LaserScan from math import floor

# Bag Names
old_bag_name = 'old_bag.bag'        
new_bag_names new_bag_name = 'new_bag.bag'

# List of topic names in the bag(have one topic that is of type LaserScan)
topics = ['scan_topic']
new_topics = ['new_scan_topic_1', 'new_scan_topic_2']     

# Open Bags for writing and reading
old_bag = rosbag.Bag(old_bag_name)
new_bag = rosbag.Bag(new_bag_names[0], 'w')

# Array to store Messages in chronological order
scan_array = []

# Read Bag and append messages from topics accordingly
for topic, msg, t in old_bag.read_messages(topics=topics):
    # Based on the topic name, read into an array(has only one topic right now)
    if(topic == "scan_topic"):
        scan_array.append(msg)
bag.close()


# Write message into new bag for each topic, while updating the time stamp
t = 0
half_time = floor(len(scan_array/2))
while t < half_time:

    # Update Time for required messages
    current_time = rospy.Time.now()
    scan_array[t].header.stamp = current_time
    scan_array[time_offest+t].header.stamp = current_time

    # Write messages onto bag for the current_time
    bag.write(new_topics[0], new_bag.write(new_topics[0], scan_array[t], current_time)
    bag.write(new_topics[0], new_bag.write(new_topics[0], scan_array[time_offest+t], current_time)
    t = t +1

I think you can write the messages into different topics at the same time, in one bag. (Ref 1, Ref 2) import rosbag import rospy from sensor_msgs.msg import LaserScan from math import floor

# Bag Names
old_bag_name = 'old_bag.bag'        
new_bag_name = 'new_bag.bag'

# List of topic names in the bag(have one topic that is of type LaserScan)
topics = ['scan_topic']
new_topics = ['new_scan_topic_1', 'new_scan_topic_2']     

# Open Bags for writing and reading
old_bag = rosbag.Bag(old_bag_name)
new_bag = rosbag.Bag(new_bag_names[0], 'w')

# Array to store Messages in chronological order
scan_array = []

# Read Bag and append messages from topics accordingly
for topic, msg, t in old_bag.read_messages(topics=topics):
    # Based on the topic name, read into an array(has only one topic right now)
    if(topic == "scan_topic"):
        scan_array.append(msg)
bag.close()


# Write message into new bag for each topic, while updating the time stamp
t = 0
half_time = floor(len(scan_array/2))
while t < half_time:

    # Update Time for required messages
    current_time = rospy.Time.now()
    scan_array[t].header.stamp = current_time
    scan_array[time_offest+t].header.stamp = current_time

    # Write messages onto bag for the current_time
    new_bag.write(new_topics[0], scan_array[t], current_time)
    new_bag.write(new_topics[0], scan_array[time_offest+t], current_time)
    t = t +1
+ 1

I think you can write the messages into different topics at the same time, in one bag. (Ref 1, Ref 2) import rosbag import rospy from sensor_msgs.msg import LaserScan from math import floor

# Bag Names
old_bag_name = 'old_bag.bag'        
new_bag_name = 'new_bag.bag'

# List of topic names in the bag(have one topic that is of type LaserScan)
topics = ['scan_topic']

# List of New Topic Names
new_topics = ['new_scan_topic_1', 'new_scan_topic_2']     

# Open Bags for writing and reading
old_bag = rosbag.Bag(old_bag_name)
new_bag = rosbag.Bag(new_bag_names[0], 'w')

# Array to store Messages in chronological order
scan_array = []

# Read Bag and append messages from topics accordingly
for topic, msg, t in old_bag.read_messages(topics=topics):
    # Based on the topic name, read into an array(has only one topic right now)
    if(topic == "scan_topic"):
        scan_array.append(msg)
bag.close()


# Write message into new bag for each topic, while updating the time stamp
t = 0
half_time = floor(len(scan_array/2))
while t < half_time:

    # Update Time for required messages
    current_time = rospy.Time.now()
    scan_array[t].header.stamp = current_time
    scan_array[time_offest+t].header.stamp = current_time

    # Write messages onto bag for the current_time
    new_bag.write(new_topics[0], scan_array[t], current_time)
    new_bag.write(new_topics[0], new_bag.write(new_topics[1], scan_array[time_offest+t], current_time)
    t = t + 1

I think you can write the messages into different topics at the same time, in one bag. (Ref 1, Ref 2) import rosbag import rospy from sensor_msgs.msg import LaserScan from math import floor

# Bag Names
old_bag_name = 'old_bag.bag'        
new_bag_name = 'new_bag.bag'

# List of topic names in the bag(have one topic that is of type LaserScan)
topics = ['scan_topic']

# List of New Topic Names
new_topics = ['new_scan_topic_1', 'new_scan_topic_2']     

# Open Bags for writing and reading
old_bag = rosbag.Bag(old_bag_name)
new_bag = rosbag.Bag(new_bag_names[0], rosbag.Bag(new_bag_name, 'w')

# Array to store Messages in chronological order
scan_array = []

# Read Bag and append messages from topics accordingly
for topic, msg, t in old_bag.read_messages(topics=topics):
    # Based on the topic name, read into an array(has only one topic right now)
    if(topic == "scan_topic"):
        scan_array.append(msg)
bag.close()


# Write message into new bag for each topic, while updating the time stamp
t = 0
half_time = floor(len(scan_array/2))
while t < half_time:

    # Update Time for required messages
    current_time = rospy.Time.now()
    scan_array[t].header.stamp = current_time
    scan_array[time_offest+t].header.stamp = current_time

    # Write messages onto bag for the current_time
    new_bag.write(new_topics[0], scan_array[t], current_time)
    new_bag.write(new_topics[1], scan_array[time_offest+t], current_time)
    t = t + 1

I think you can write the messages into different topics at the same time, in one bag. (Ref 1, Ref 2) )

import rosbag
 import rospy
 from sensor_msgs.msg import LaserScan
 from math import floor

floor

# Bag Names
old_bag_name = 'old_bag.bag'        
new_bag_name = 'new_bag.bag'

# List of topic names in the bag(have one topic that is of type LaserScan)
topics = ['scan_topic']

# List of New Topic Names
new_topics = ['new_scan_topic_1', 'new_scan_topic_2']     

# Open Bags for writing and reading
old_bag = rosbag.Bag(old_bag_name)
new_bag = rosbag.Bag(new_bag_name, 'w')

# Array to store Messages in chronological order
scan_array = []

# Read Bag and append messages from topics accordingly
for topic, msg, t in old_bag.read_messages(topics=topics):
    # Based on the topic name, read into an array(has only one topic right now)
    if(topic == "scan_topic"):
        scan_array.append(msg)
bag.close()


# Write message into new bag for each topic, while updating the time stamp
t = 0
half_time = floor(len(scan_array/2))
while t < half_time:

    # Update Time for required messages
    current_time = rospy.Time.now()
    scan_array[t].header.stamp = current_time
    scan_array[time_offest+t].header.stamp = current_time

    # Write messages onto bag for the current_time
    new_bag.write(new_topics[0], scan_array[t], current_time)
    new_bag.write(new_topics[1], scan_array[time_offest+t], current_time)
    t = t + 1