#! /usr/bin/env python # The MIT License (MIT) # # Copyright (c) 2015, EPFL Reconfigurable Robotics Laboratory, # Philip Moseley, philip.moseley@gmail.com # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. import argparse, sys, os, shutil, time import os.path as path import numpy as np import utility from matplotlib import pyplot,axes #-------------------------------------------------------------------------------- # Create energy plots from extracted FEM data files, only called from run_tests. # axis = axis handle of figure. # rpt = rpt file to plot. #-------------------------------------------------------------------------------- def plot_fem_energy(axis,rpt): try: data = np.loadtxt(rpt,skiprows=3) except: utility.print_error('Problem reading .rpt file: '+rpt,False) return if len(data.shape)==1: print '\tNot enough datapoints, skipping file.' return axis.plot(data[:,0], data[:,1], 'ko-', label='Internal Energy', markersize=3) axis.plot(data[:,0], data[:,2], 'rs-', label='Kinetic Energy', markersize=3) axis.set_xlabel('Time (meaningless units)') axis.set_ylabel('Energy') #-------------------------------------------------------------------------------- # Create the plots from extracted FEM data files. # axis = axis handle of figure. # rpt = rpt file to plot. # test = type of test to analyse. # color = choose a color for all lines from this datafile. # labelpref = prefix for labels. # fulllabel = full label. # dist_to_force = distance to the blocked force plate, if applicable. #-------------------------------------------------------------------------------- def plot_fem_data(axis,rpt,test='unknown',color='',labelpref='',fulllabel='',dist_to_force=0): # Load the file. try: data = np.loadtxt(rpt,skiprows=3) except: try: data = np.loadtxt(rpt,skiprows=4) except: utility.print_error('Problem reading .rpt file: '+rpt,False) return if len(data.shape)==1: print '\tNot enough datapoints, skipping file.' return # Get the initial length of the actuator. L = data[0,-1] - data[0,-2] if test=='unknown': # Columns are: X (time), Pressure, Fx, Fy, Fz, Zmin, Zmax # or maybe: X (time), Pressure, Ux, Uy, Uz, Zmin, Zmax if data.shape[1]==7: labels = ('$X$','$Y$','$Z$') ylabel = 'Value - Blocked Force (N) or Displacement (mm)' title = 'Todo' # Columns are: X (time), Fz, Uz elif data.shape[1]==3: axis.set_xlabel('Displacement (mm)') axis.set_ylabel('Force (N/mm2)') axis.set_title('Displacement versus Force') axis.grid() axis.plot(data[:,2], data[:,1], label='$F_z$') return # Columns are X (time), Pressure, Ux, Uy, Uz elif data.shape[1]==5: labels = ('$X$','$Y$','$Z$') ylabel = 'Displacement (mm)' title = 'Bubble Displacement' else: utility.print_error('Didn\'t recognize data format for plotting.',False) print data return if axis.get_ylabel()=='': axis.set_ylabel(ylabel) axis.set_title(title) axis.grid() elif axis.get_ylabel()!=ylabel: utility.print_error('Multiple plot types. Plotting anyway...',False) else: if test=='linU': data[:,2] = -data[:,4] labels = ('$U_z$',) elif test=='linF': data[:,2] = data[:,4] labels = ('$F_z$',) elif test=='bndU': # Calculate the "bending angle". Assumes positive z-displacement is a shorter actuator. data[:,2] = np.degrees(np.arctan(-data[:,3]/(L-data[:,4]))) data[data[:,2]<0.0,2] = data[data[:,2]<0.0,2] + 180 labels = ('angle',) elif test=='bndF': alpha = np.radians(dist_to_force) beta = np.radians(90 - dist_to_force) f_normal = np.abs(data[:,3]*np.cos(alpha)) + np.abs(data[:,4]*np.cos(beta)) data[:,2] = f_normal # Pressure, Normal-Force. labels = ('$F_y$',) elif test=='bubU': data[:,2] = data[:,4] labels = ('$U_z$',) else: utility.print_error('Invalid test type in plot_fem_data.',True) # Set the plot properties and create the plot. # axis.set_xlabel(r'Internal Pressure $\left({}^N\!/{}_{mm^2}\right)$') data[:,1] = data[:,1] * 1000.0 axis.set_xlabel('Internal Pressure (kPa)') linetypes = ['o-','s-','^-'] for i in range(len(labels)): if not fulllabel: if labelpref=='': label = labels[i] else: label = labelpref + ' - ' + labels[i] else: label = fulllabel if color=='': axis.plot(data[:,1], data[:,2+i], linetypes[i], label=label) else: axis.plot(data[:,1], data[:,2+i], linetypes[i], color=color, label=label, markersize=3, markeredgecolor=color) # ,linewidth=3) #-------------------------------------------------------------------------------- # Create the plots from extracted experimental data files. # axis = axis handle of figure. # fname = file to plot. # test = type of test to analyse. # color = choose a color for all lines from this datafile. # labelpref = prefix for labels. # fulllabel = full label. #-------------------------------------------------------------------------------- def plot_exp_data(axis, fname, test, color='', labelpref='', fulllabel=''): # Load the file. try: if fname.endswith('.csv'): alldata = np.loadtxt(fname,delimiter=',') elif fname.endswith('.txt'): alldata = np.loadtxt(fname) else: raise('Unsupported file format.') except: utility.print_error('Problem reading experimental data file: '+fname,True) if test=='linU': try: T = path.splitext(fname)[0] T = float(T[T.rindex('-t')+2:]) npress = alldata.shape[1]/2 data = np.zeros([npress,2]) for i in range(npress): # Note that we skip the first row, since it contains the pressure data. row = 0 col = 2*i for r in range(alldata.shape[0]): if alldata[r,col]>=T: break row = r data[i,:] = alldata[0,col] data[i,1] = alldata[row,col+1] descr = '$U_z(t='+str(alldata[row,col])+'s)$' except Exception: utility.print_error('Problem determining cutoff time from filename.\n' 'Filename should be in this format: -t