需求
- 读取伺服电机反馈电流参数
- 分析参数确定最佳的适用工作区间
方法
- 利用伺服电机配套控制软件导出运行参数
- python分析处理数据,绘制图形
- 进行平均和差分时使用了滑动窗口
结果
Figure_1.png
2.png
附
#! -*- coding: utf-8 -*-
import pandas as pd
import csv
import matplotlib.pyplot as plt
import sys
def skipFile( inFile, r, outFile, maxLine=7680 ):
with open( outFile, 'w') as csvout:
writer = csv.writer( csvout, lineterminator='\n' )
with open( inFile, 'r' ) as csvfile:
reader = csv.reader( csvfile )
iNow = 0
rowSum = 0
for line in reader:
if ( iNow < r ):
pass
else:
# write to the file
if ( rowSum >= maxLine ):
break
writer.writerow( ( line ) )
rowSum += 1
# print( line )
# print( iNow, line )
iNow += 1
# sum range
def sumRange( data, rng ):
thisSum = 0.0
for i in range( rng[0], rng[1] ):
thisSum += data[i]
return thisSum
# rng = [x, y)
def avgRange( data, rng ):
thisSum = sumRange( data, rng )
return thisSum / ( rng[1] - rng[0] )
def windowFilter( data, wnd, dataOut ):
dataLen = len( data )
halfWnd = int(wnd/2)
print( dataLen )
for i in range( halfWnd, dataLen - halfWnd ):
dataOut[ i ] = avgRange( data, ( i-halfWnd, i - halfWnd + wnd ) )
# fill the head
for i in range( 0, halfWnd ):
dataOut[i] = dataOut[ halfWnd]
# fill the tail
for i in range( dataLen-halfWnd, dataLen ):
dataOut[ i ] = dataOut[ dataLen-halfWnd-1 ]
def diffFilter( data, wnd, dataOut ):
dataLen = len( data )
halfWnd = int(wnd/2)
print( dataLen )
for i in range( halfWnd, dataLen - halfWnd ):
a = sumRange( data, ( i - halfWnd, i ) )
b = sumRange( data, ( i, i + halfWnd ) )
dataOut[ i ] = ( b - a ) / halfWnd
# fill the head
for i in range( 0, halfWnd ):
dataOut[i] = dataOut[ halfWnd]
# fill the tail
for i in range( dataLen-halfWnd, dataLen ):
dataOut[ i ] = dataOut[ dataLen-halfWnd-1 ]
import os
if __name__=="__main__":
if len(sys.argv)>1:
print( sys.argv )
print( os.path.abspath(os.path.curdir) )
filePath = sys.path[0]
print( filePath )
# skipFile( 'E:/work/2.csv',
skipFile( sys.argv[1],
5,
filePath + '/out.csv' )
else:
exit( 0 )
csv_data = pd.read_csv( filePath + '/out.csv', header=None )
print( csv_data.shape )
totalSum = max( csv_data[0] )
print( totalSum )
dataOut = [ x for x in range( len( csv_data[2] ) ) ]
windowFilter( csv_data[2].tolist(), 200, dataOut )
dataDiff = [ x for x in range( len( csv_data[2] ) ) ]
diffFilter( dataOut, 500, dataDiff )
plt.plot( csv_data[0]* 360/totalSum, csv_data[2], label='current' )
plt.plot( csv_data[0]* 360/totalSum, dataOut, label='average' )
plt.plot( csv_data[0]* 360/totalSum, dataDiff, label='diff' )
plt.xlabel( 'Degree')
plt.ylabel( 'Current')
plt.grid( which='both')
plt.legend( ['current', 'average', 'diff'] )
plt.gca().set_xticks( [ tick for tick in range( 0,360,10) ], True )
plt.show()
网友评论