biotac_json_logger.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Copyright (c) 2012, University of Pennsylvania
00003 # All rights reserved.
00004 #
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are met:
00007 #
00008 #     * Redistributions of source code must retain the above copyright
00009 #       notice, this list of conditions and the following disclaimer.
00010 #     * Redistributions in binary form must reproduce the above copyright
00011 #       notice, this list of conditions and the following disclaimer in the
00012 #       documentation and/or other materials provided with the distribution.
00013 #     * Neither the name of the University of Pennsylvania nor the names of its
00014 #       contributors may be used to endorse or promote products derived from
00015 #       this software without specific prior written permission.
00016 #
00017 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027 # POSSIBILITY OF SUCH DAMAGE.
00028 
00029 # Authors: Vivian Chu (chuv@grasp.upenn.edu) 
00030 #          Ian McMahon (imcmahon@grasp.upenn.edu)
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     #Check to see if logging time was set
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     # FILE WRITING SETUP 
00059     # Find Node Parameter Name
00060     self.file_param = rospy.get_name() + '/filename'
00061     # Grab directory
00062     self.package_dir = roslib.packages.get_pkg_dir('biotac_logger')
00063     # Check for a 'data' directory
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     # Set output filename
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     # Create initial file - delete existing file with same name 
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     #Begin starting to log for specified time
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   # Called each time there is a new message
00084   def biotacCallback(self,data):
00085     #rospy.loginfo(rospy.get_name()+' FRAME ID: %d',self.frame_count)
00086 
00087     # Stores the frame count into the message
00088     data.header.frame_id = self.frame_count
00089 
00090     # Uses rosjson to convert message to JSON 
00091     toWrite = rosjson_time.ros_message_to_json(data) + '\n'
00092     self.fout.write(toWrite);
00093 
00094     #Check to see if Elapsed time has run out
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     # Move to next frame 
00104     self.frame_count += 1
00105 
00106   #Check if directory exits & create it
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   # Setup the subscriber Node
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()


biotac_logger
Author(s): Vivian Chu
autogenerated on Sun Oct 5 2014 22:30:28