resistance_plot.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 PKG = 'mtrace_tools'
00004 
00005 import roslib; roslib.load_manifest(PKG)
00006 import rospy
00007 from mtrace_tools.msg import MotorResistance
00008 from pylab import *
00009 from numpy import *
00010 import numpy
00011 import re
00012 import rosrecord
00013 import getopt
00014 import math
00015 
00016 class struct_of_arrays(object):
00017     def __init__(self, list_of_structs):
00018         # make sure type of input is correct
00019         #print list_of_structs
00020         #print type(list_of_structs)
00021         if type(list_of_structs).__name__ != 'list':
00022             print "object is not a list"
00023             return
00024         if len(list_of_structs) == 0:
00025             print "list is empty"
00026             return
00027         # pull in non __XXXX__ names, define them in this class as empty lists
00028         names = dir(list_of_structs[0])
00029         struct_names=[]
00030         for name in names:
00031             if name[0] != '_':
00032                 struct_names.append(name)
00033                 self.__setattr__(name,[])
00034         # Append values to lists
00035         for struct in list_of_structs:
00036             for name in struct_names:
00037                 tmp = self.__getattribute__(name)
00038                 tmp.append(struct.__getattribute__(name))
00039         # Convert lists into arrays        
00040         for name in struct_names:
00041             tmp = self.__getattribute__(name)
00042             array_tmp = array(tmp)
00043             self.__setattr__(name,array_tmp)
00044 
00045 
00046 def callback(samples):
00047     #print time.strftime("Data was collected on %a, %d %Y %b %I:%M%a +0000", data.header.stamp)
00048 
00049     # assume samples is a list of structures
00050     min_current = 0.01
00051     no_zeros = []
00052     for s in samples:
00053         if abs(s.current) > min_current:
00054             no_zeros.append(s)
00055     a = struct_of_arrays(no_zeros)
00056 
00057     
00058     encoder_reduction = -1.0
00059     speed_constant = 158.0
00060     backemf_constant = 1.0 / (speed_constant * 2.0 * math.pi * 1.0/60.0);
00061 
00062     #float64 current
00063     #float64 voltage
00064     #float64 velocity
00065     #float64 position
00066     #bool    positive_direction
00067     backemf_voltage = a.velocity * encoder_reduction * backemf_constant 
00068     angular_position = mod(a.position,2.0 * math.pi)
00069 
00070     figure(1)
00071     resistance = (a.voltage-backemf_voltage) / a.current
00072     title("Resistance vs Position")
00073     plot(angular_position,resistance,'r.', label='Motor resitance vs position')
00074 
00075     figure(2)
00076     resistance_adj = backemf_voltage / a.current
00077     title("Backemf adjust resistance vs Position")
00078     plot(angular_position,resistance_adj,'b.', label='Back emf resitance vs position')
00079 
00080     figure(3)
00081     title("Current")
00082     plot(angular_position,a.current,'b.', label='current')
00083 
00084     show()
00085 
00086 
00087 
00088 
00089 def usage():
00090     print "usage : resistance_plot.py filename"
00091     print "   filename - bag file with resistance measurements"
00092 
00093 def mtrace_plot(argv):
00094     if len(argv) < 2:
00095         usage()
00096         return 1
00097 
00098     rospy.init_node("mtrace_plot", anonymous=True)
00099 
00100     bagfn = argv[1]
00101     samples = []
00102     for (topic, msg, t) in rosrecord.logplayer(bagfn, raw=False):
00103         samples += msg.samples
00104     
00105     print len(samples)
00106     callback(samples)
00107 
00108 
00109 if __name__ == '__main__':    
00110    exit(mtrace_plot(sys.argv))
00111 
00112 
00113 
00114 


mtrace_tools
Author(s): Derek
autogenerated on Sat Dec 28 2013 17:58:08