rmse.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 """ Provides tools to compute root mean squared error for each joint in JointTrajectoryControllerState messages, either on-line by listening to a speciefied topic - or offline by reading a bag file."""
00004 
00005 import numpy as np
00006 import sys
00007 
00008 import roslib
00009 roslib.load_manifest( 'gpr_controller' )
00010 import rospy
00011 import rosbag
00012 from pr2_controllers_msgs.msg import JointTrajectoryControllerState
00013 
00014 __author__ = "Daniel Hennes"
00015 
00016 class RMSEListener():
00017     error_sum = None
00018     num_msg = 0
00019 
00020     def __init__( self, topic ):
00021         np.set_printoptions( precision=3 ) # , suppress=True )
00022 
00023         rospy.init_node( 'rsme_joint_states' )
00024         rospy.Subscriber( topic, JointTrajectoryControllerState, 
00025                           self.cb_controller_state )
00026         rospy.spin()
00027 
00028     def cb_controller_state( self, msg ):
00029         if self.error_sum is None:
00030             self.error_sum = np.asarray( msg.error.positions ) ** 2
00031         else:
00032             self.error_sum += np.asarray ( msg.error.positions ) ** 2 
00033         self.num_msg += 1
00034         RMSE = np.sqrt( self.error_sum / self.num_msg )
00035         print RMSE
00036 
00037 class RMSEBagReader():
00038     def __init__( self, fname = 'test.bag', topic = 'r_arm_controller/state' ):
00039         msgs = self.read_file( fname, topic )
00040         print self.rmse ( msgs )
00041 
00042     def read_file( self, fname, topic ):
00043         bag = rosbag.Bag( fname )
00044         msgs = []
00045         for topic, msg, t in bag.read_messages( topics = [topic] ):
00046             msgs.append( msg )
00047         bag.close()
00048         return msgs
00049 
00050     def rmse( self, msgs ):
00051         error_sum = None
00052         num_msg = 0
00053         for msg in msgs:
00054             if error_sum is None:
00055                 error_sum = np.asarray( msg.error.positions ) ** 2
00056             else:
00057                 error_sum += np.asarray ( msg.error.positions ) ** 2 
00058             num_msg += 1
00059         if error_sum is None:
00060             return None
00061         else:
00062             return np.sqrt( error_sum / num_msg )
00063     
00064 if __name__ == '__main__':
00065     args = sys.argv
00066     args.pop(0)
00067     if len( args ) == 2 and args[0] == 'listen':
00068         RMSEListener( args[1] )
00069     elif len( args ) == 3 and args[0] == 'read':
00070         RMSEBagReader( args[1], args[2] )
00071     else:
00072         print "Usage: rmse <subcommand> [args]\n"
00073         print "Available subcommands:"
00074         print "\tlisten [topic]"
00075         print "\tread [filename] [topic]"
00076         


kelsey_sandbox
Author(s): kelsey
autogenerated on Wed Nov 27 2013 11:52:04