Hi all!
I am trying to record a rosbag at click of a button on Baxter. At each click, a new bag starts recording with a different file name and the previous one closes. Below is the code trying to implement my idea, but I am getting an error
#!/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 # reads a number from a text file
flag = 1 # initialized as global variable
print flag
tfMsg = TFMessage()
num = FileRead() #reads a number
print num[0]
bag = rosbag.Bag("/home/dhiraj/ros_ws/src/baxter_examples/scripts/"+str(num[0])+".bag",'w')
def callbackStop(data):
if data.state: # If the button is pressed
global flag
if flag == 1: # flag = 1 means bag is still recording
print "closing bag"
bag.close()
flag = 0 # stop recording
print "CLOSING flag value is", flag
def callbackRestart(data):
global flag
if flag == 1: #record bag only if flag is 1
print "RECORDING NEW BAG,with flag value",flag
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():
print "inside tf info"
rospy.init_node("get_tf")
restart = rospy.Subscriber("/robot/digital_io/right_shoulder_button/state",DigitalIOState, callbackRestart)
bagClose=rospy.Subscriber("/robot/digital_io/right_itb_button2/state",DigitalIOState,callbackStop)
rospy.spin()
if __name__=='__main__':
tf_info()
OUTPUT:
RECORDING NEW BAG,with flag value 1
RECORDING NEW BAG,with flag value 1
RECORDING NEW BAG,with flag value 1
closing bag
RECORDING NEW BAG,with flag value 1
[ERROR] [WallTime: 1492468923.696646] bad callback:
Traceback (most recent call last):
File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/topics.py", line 720, in _invoke_callback
cb(msg)
File "/home/dhiraj/ros_ws/src/baxter_examples/scripts/rosbag_recorder.py", line 32, in callbackStop
bag.close()
File "/opt/ros/indigo/lib/python2.7/dist-packages/rosbag/bag.py", line 417, in close
self._close_file()
File "/opt/ros/indigo/lib/python2.7/dist-packages/rosbag/bag.py", line 1201, in _close_file
self._file.close()
IOError: close() called during concurrent operation on the same file object.
**EDIT**
**Problem got solved when I changed**
def callbackStop(data):
if data.state:
global flag
if flag == 1:
flag = 0
print "closing bag, with value of flag", flag
bag.close()
Which means my callback was exiting right after the "bag.close()" according to the function written at the top, which was why it was not setting my flag =0. Making the edit mentioned just right above solved the problem.
I don't understand why my previous callbackStop was exiting after bag.close() without completing the entire function. Can you please explain?
↧