0
$\begingroup$

Rosanswers logo

Below is my code snippet to record rostopics using ROSbag API. My messages are empty when I do a rostopic echo. I am not sure what is happening.

My bag is being recorded and my rosbag info 17Apr17.bag gives my this output:

path:        17Apr17.bag
version:     2.0
duration:    8.9s
start:       Apr 17 2017 18:57:55.21 (1492469875.21)
end:         Apr 17 2017 18:58:04.08 (1492469884.08)
size:        1022.8 KB
messages:    10656
compression: none [2/2 chunks]
types:       baxter_core_msgs/EndEffectorState [ade777f069d738595bc19e246b8ec7a0]
             sensor_msgs/Image                 [060021388200f6f0f447d0fcd9c64743]
             sensor_msgs/JointState            [3066dcd76a6cfaef579bd0f34173e9fd]
             tf2_msgs/TFMessage                [94810edda583a504dfda3829e70d7eec]
topics:      /camera/depth/image_raw                   888 msgs    : sensor_msgs/Image                
             /camera/rgb/image_color                   888 msgs    : sensor_msgs/Image                
             /cameras/left_hand_camera/image           888 msgs    : sensor_msgs/Image                
             /cameras/right_hand_camera/image          888 msgs    : sensor_msgs/Image                
             /robot/end_effector/left_gripper/state    888 msgs    : baxter_core_msgs/EndEffectorState
             /robot/end_effector/right_gripper/state   888 msgs    : baxter_core_msgs/EndEffectorState
             /robot/joint_states                       888 msgs    : sensor_msgs/JointState           
             /softkinetic_left/depth/image_raw         888 msgs    : sensor_msgs/Image                
             /softkinetic_left/rgb/image_color         888 msgs    : sensor_msgs/Image                
             /softkinetic_right/depth/image_raw        888 msgs    : sensor_msgs/Image                
             /softkinetic_right/rgb/image_color        888 msgs    : sensor_msgs/Image                
             tf                                        888 msgs    : tf2_msgs/TFMessage

CODE:

#!/usr/bin/env python

import rosbag
import rospy
from random import randint
from tf2_msgs.msg import TFMessage
from baxter_core_msgs.msg import DigitalIOState
from std_msgs.msg import String
from sensor_msgs.msg import Image
from sensor_msgs.msg import JointState
from baxter_core_msgs.msg import EndEffectorState



from create_folder import FileRead


flag = 1
print flag
tfMsg = TFMessage()

num = FileRead()
print num[0]
bag = rosbag.Bag("/home/dhiraj/ros_ws/src/baxter_examples/scripts/"+str(num[0])+".bag",'w')

# Flag = 0 will not record(callbackStop) the file and Flag=1 will recrd the file
# I need to record rosbag by the click of the buttons and hence the the setup
def callbackStop(data):
 if data.state:
      global flag
      if flag == 1:
        print "closing bag"
        flag = 0
        print "closing bag with flag", flag
        bag.close()


def callbackRestart(data):
 if data.state == 0:
   print "ENTERING CALLBVBACK"
   global flag
   if flag == 1:

    print "RECORDING NEW BAG"
    bag.write("tf",tfMsg)
    bag.write("/cameras/right_hand_camera/image",Image())
    bag.write("/cameras/left_hand_camera/image",Image())
    bag.write("/robot/joint_states",JointState())
    bag.write("/robot/end_effector/right_gripper/state",EndEffectorState())
    bag.write("/robot/end_effector/left_gripper/state",EndEffectorState())
    bag.write("/softkinetic_left/rgb/image_color",Image())
    bag.write("/softkinetic_right/rgb/image_color",Image())
    bag.write("/softkinetic_left/depth/image_raw",Image())
    bag.write("/softkinetic_right/depth/image_raw",Image())
    bag.write("/camera/rgb/image_color",Image())
    bag.write("/camera/depth/image_raw",Image())

def tf_info():
    rospy.init_node("get_tf")
    rightStart = rospy.Subscriber("/robot/digital_io/right_shoulder_button/state",DigitalIOState, callbackRestart)
    bagClosRight = rospy.Subscriber("/robot/digital_io/right_itb_button2/state",DigitalIOState,callbackStop)
    bagCloseLeft = rospy.Subscriber("/robot/digital_io/left_itb_button2/state",DigitalIOState,callbackStop)
    leftRestart  = rospy.Subscriber("/robot/digital_io/left_itb_button1/state",DigitalIOState,callbackSetFlag)
    rightRestart  = rospy.Subscriber("/robot/digital_io/right_itb_button1/state",DigitalIOState,callbackSetFlag)
    rospy.spin()




if __name__=='__main__':
    tf_info()

Originally posted by Joy16 on ROS Answers with karma: 112 on 2017-04-18

Post score: 0


Original comments

Comment by Thomas D on 2017-04-18:
What is your question?

Comment by Joy16 on 2017-04-18:
My rosbag messages contain no data. All fields in the ros messages are empty

Comment by Thomas D on 2017-04-18:
How are you playing back your bag files? How did you determine that your messages are empty? Are you setting the parameter use_sim_time to true?

Comment by Joy16 on 2017-04-18:
Ya. I set rosparam set use_sim_time true. Iam lpaying my rosbag file as rosbag play --clock 17Apr18.bag -l my messages are empty as a rostopic echo on any topic gives me empty fields.

header: 
  seq:0
  stamp: 
    secs:0
    nsecs:   
  frame_id: '"
  data =[]
$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

You are writing empty message objects into your bag file:

print "RECORDING NEW BAG"
bag.write("tf",tfMsg)
bag.write("/cameras/right_hand_camera/image",Image())
bag.write("/cameras/left_hand_camera/image",Image())
bag.write("/robot/joint_states",JointState())
bag.write("/robot/end_effector/right_gripper/state",EndEffectorState())
bag.write("/robot/end_effector/left_gripper/state",EndEffectorState())
bag.write("/softkinetic_left/rgb/image_color",Image())
bag.write("/softkinetic_right/rgb/image_color",Image())
bag.write("/softkinetic_left/depth/image_raw",Image())
bag.write("/softkinetic_right/depth/image_raw",Image())
bag.write("/camera/rgb/image_color",Image())
bag.write("/camera/depth/image_raw",Image())

bag.write() writes the message object that you give it to the bag file with the specific topic name. Since you're supplying default-constructed messages (which are empty), that's what it is writing to the bag file.

If you want to subscribe to a topic and record the messages on that topic to a bag, you need to either call the robag command-line tool from your program and let it subscribe, or you need to create subscribers on each of the topics that you care about, and then actually write the received messages to the bag file.


Originally posted by ahendrix with karma: 47576 on 2017-04-18

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by Joy16 on 2017-04-18:
Hi! Thanks a lot! I am sorry, I had not posted the latest code. I am subscribing to these topics, but I am sure how to pass them to bag.write()

Comment by Joy16 on 2017-04-18:
#This is giving me stop_writing_chunk error

def callbackt(data):       
               bag.write("/cam",data)
    
def callbackRestart(data):
 if data.state == 0:
   global flag_bag
   if flag_bag == 1:
    rospy.Subscriber("/cam",Image,callback)

Comment by ahendrix on 2017-04-18:
You need to shut down the subscribers that write to the bag before you close the bag file.

Comment by ahendrix on 2017-04-18:
You should probably set up your subscribers once (instead of every time you get callbackRestart); note that because subscriber callbacks happen in a separate thread, the if flag_bag == 1: won't prevent callbacks.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.