Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 import roslib; roslib.load_manifest('biotac_logger')
00034 import rospy
00035 import os, sys
00036 from rosjson_time import rosjson_time
00037 from types import *
00038
00039 from std_msgs.msg import String
00040 from biotac_sensors.msg import BioTacHand
00041
00042 class BioTacListener:
00043
00044 def __init__(self):
00045 self.frame_count = 1;
00046 rospy.init_node('biotac_json_logger')
00047 self.time_to_log = rospy.myargv(argv=sys.argv)
00048
00049 try:
00050 input_arg = float(self.time_to_log[1])
00051 if type(input_arg) is FloatType:
00052 self.node_log_time = input_arg
00053 else:
00054 self.node_log_time = 'inf'
00055 except:
00056 self.node_log_time = 'inf'
00057
00058
00059
00060 self.file_param = rospy.get_name() + '/filename'
00061
00062 self.package_dir = roslib.packages.get_pkg_dir('biotac_logger')
00063
00064 dir_status = self.check_dir(os.path.expanduser('~/.ros/biotac_data' ))
00065 if dir_status:
00066 rospy.loginfo('The ''~/.ros/biotac_data'' directory was successfully created.')
00067
00068 self.fileName = os.path.expanduser('~/.ros/biotac_data/') + rospy.get_param(self.file_param,'default.json')
00069 if not self.fileName.endswith('.json'):
00070 self.fileName = self.fileName + '.json'
00071
00072 self.fout = open(self.fileName,'w')
00073 self.fout.write("[\n")
00074
00075 rospy.loginfo(rospy.get_name()+' Starting to Log to file %s:',self.fileName);
00076
00077
00078 if self.node_log_time is not 'inf':
00079 while not rospy.get_time(): pass
00080 self.start_time = rospy.get_time()
00081 rospy.loginfo("Logging start time: %f" % self.start_time)
00082
00083
00084 def biotacCallback(self,data):
00085
00086
00087
00088 data.header.frame_id = self.frame_count
00089
00090
00091 toWrite = rosjson_time.ros_message_to_json(data) + '\n'
00092 self.fout.write(toWrite);
00093
00094
00095 if self.node_log_time is not 'inf':
00096 elapsed_time = rospy.get_time() - self.start_time
00097 if elapsed_time >= self.node_log_time:
00098 finish_msg = 'Logging for %f second(s) Complete!' % self.node_log_time
00099 rospy.loginfo(finish_msg)
00100 self.__del__()
00101 rospy.signal_shutdown(finish_msg)
00102
00103
00104 self.frame_count += 1
00105
00106
00107 def check_dir(self, f):
00108 if not os.path.exists(f):
00109 os.makedirs(f)
00110 return True
00111 return False
00112
00113
00114 def listener(self):
00115 rospy.Subscriber('biotac_pub', BioTacHand, self.biotacCallback,queue_size=1000)
00116 rospy.spin()
00117
00118 def __del__(self):
00119 self.fout.write("]")
00120 self.fout.close()
00121
00122
00123 if __name__ == '__main__':
00124
00125 bt_listener = BioTacListener()
00126 bt_listener.listener()