ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org
Ask Your Question
0

I can't clean inside my bag file with python

asked 2022-04-01 04:22:25 -0500

Onurcan gravatar image

In my code i am trying to clean my bag file in every 10 seconds and then trying to write again, but it doesn't work. I couldn't find any rosbag function to clean inside my bag file. I tried with python functions but it didn't work too.

Here is my code.

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import sys
import rospy 
import rosbag
from geometry_msgs.msg import Twist
import time

current_time = time.time()
last_time=time.time()
filename='test2.bag'
print(current_time)
bag = rosbag.Bag(filename, 'w')
def move(msg):
    global last_time
    current_time = time.time()
    if current_time - last_time >10: 
        last_time=time.time()
        with open(filename, 'r+') as f:
            #f.read()
            f.truncate()
            print(f.encoding)
            #f.close()
        print("outside")
    else:

        bag.write('/cmd_vel', msg)  
        print("inside")



def main(args):
    rospy.init_node('bag', anonymous=True)
    rospy.Subscriber("/cmd_vel",Twist,move)
    rospy.spin()
    bag.close()
    print("Closing...")

if __name__ == '__main__':
  main(sys.argv)
edit retag flag offensive close merge delete

Comments

What kind of errors/issues have you encountered? Can you add their description and terminal output etc.?

ljaniec gravatar image ljaniec  ( 2022-04-01 05:03:00 -0500 )edit
1

I encountered with this error. "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcc in position 40: invalid continuation byte"

After i post this, i solved problem. I've removed truncate function and added "os.remove()". I guess deleting file and create it again is better solution. Here is new code:

if current_time - last_time >10: 
    last_time=time.time()
    os.remove(filename)
    bag = rosbag.Bag(filename, 'w')
    print("outside")
Onurcan gravatar image Onurcan  ( 2022-04-01 06:02:23 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-04-01 05:57:02 -0500

Mike Scheutzow gravatar image

You can not just modify the underlying file being used by the bag object. If you want to start a new one, close the existing bag object and create a new bag object.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2022-04-01 04:22:25 -0500

Seen: 63 times

Last updated: Apr 01