v0.1.4(2024/06/06)
1. AV/RR支持小数 2. 可处理电机电流单轴以及多轴数据,可根据需要进行参数设定处理不同轴的数据 3. 界面初始位置修改,以及删除所有entry的长度设定,因为设定无效 4. 修改了layout.xlsx布局,增加了duration/trqH/STO字段,以及额外的RC行,整体扩展了区域 5. 更改label/entry/optionmenu等控件的生成方式,使用循环实现,更加简洁和容易维护(暂未实现) 6. 支持工业/协作两条产品线的电机电流数据处理,包括单轴,场景,max/avg计算
This commit is contained in:
195
aio/current.py
195
aio/current.py
@ -2,11 +2,11 @@ from openpyxl import load_workbook
|
||||
from os import scandir
|
||||
from os.path import exists
|
||||
from sys import argv
|
||||
from pandas import read_csv
|
||||
from pandas import read_csv, concat, set_option
|
||||
from re import match
|
||||
from threading import Thread
|
||||
from time import sleep
|
||||
|
||||
from csv import reader
|
||||
class GetThreadResult(Thread):
|
||||
def __init__(self, func, args=()):
|
||||
super(GetThreadResult, self).__init__()
|
||||
@ -42,7 +42,7 @@ def traversal_files(path, w2t):
|
||||
# 返回值:路径下的文件夹列表 路径下的文件列表
|
||||
if not exists(path):
|
||||
msg = f'数据文件夹{path}不存在,请确认后重试......'
|
||||
w2t(msg, 0, 1)
|
||||
w2t(msg, 0, 8)
|
||||
else:
|
||||
dirs = []
|
||||
files = []
|
||||
@ -64,16 +64,16 @@ def initialization(path, sub, w2t):
|
||||
if sub != 'cycle':
|
||||
if not (match('j[1-7].*\\.data', filename) or match('j[1-7].*\\.csv', filename)):
|
||||
msg = f"所有文件必须以 jx_ 开头,以 .data/csv 结尾(x取值1-7),请检查后重新运行。"
|
||||
w2t(msg, 0, 2)
|
||||
w2t(msg, 0, 6)
|
||||
else:
|
||||
if filename.endswith('.xlsx'):
|
||||
count += 1
|
||||
elif not (match('j[1-7].*\\.data', filename) or match('j[1-7].*\\.csv', filename)):
|
||||
msg = f"所有文件必须以 jx_ 开头,以 .data/csv 结尾(x取值1-7),请检查后重新运行。"
|
||||
w2t(msg, 0, 3)
|
||||
w2t(msg, 0, 7)
|
||||
|
||||
if sub == 'cycle' and count != 1:
|
||||
w2t("未找到电机电流数据处理excel表格,确认后重新运行!", 0, 4)
|
||||
w2t("未找到电机电流数据处理excel表格,确认后重新运行!", 0, 5)
|
||||
|
||||
return data_files
|
||||
|
||||
@ -125,7 +125,7 @@ def current_avg(data_files, rcs, trqh, w2t):
|
||||
return current
|
||||
|
||||
|
||||
def current_cycle(dur, data_files, rcs, vel, trq, trqh, w2t):
|
||||
def current_cycle(dur, data_files, rcs, vel, trq, trqh, rpm, w2t):
|
||||
result = None
|
||||
hold = []
|
||||
single = []
|
||||
@ -162,9 +162,9 @@ def current_cycle(dur, data_files, rcs, vel, trq, trqh, w2t):
|
||||
pass
|
||||
|
||||
if dur == 0:
|
||||
p_single(wb, single, vel, trq, w2t)
|
||||
p_single(wb, single, vel, trq, rpm, w2t)
|
||||
else:
|
||||
p_scenario()
|
||||
p_scenario(wb, single, vel, trq, rpm, dur, w2t)
|
||||
|
||||
w2t(f"正在保存文件 {result},需要 10s 左右", 1)
|
||||
stop = 0
|
||||
@ -180,94 +180,171 @@ def current_cycle(dur, data_files, rcs, vel, trq, trqh, w2t):
|
||||
w2t("全部处理完毕")
|
||||
|
||||
|
||||
def p_single(wb, single, vel, trq, w2t):
|
||||
def find_point(flag, df, _row_s, _row_e, w2t, exitcode, threshold, step, end_point):
|
||||
if flag == 'lt':
|
||||
while _row_e > end_point:
|
||||
speed_avg = df.iloc[_row_s:_row_e, 0].abs().mean()
|
||||
if speed_avg < threshold:
|
||||
_row_e -= step
|
||||
_row_s -= step
|
||||
continue
|
||||
else:
|
||||
return _row_s, _row_e
|
||||
else:
|
||||
w2t(f"数据有误,需要检查,无法找到第{exitcode}个有效点...", 0, exitcode)
|
||||
elif flag == 'gt':
|
||||
while _row_e > end_point:
|
||||
speed_avg = df.iloc[_row_s:_row_e, 0].abs().mean()
|
||||
if speed_avg > threshold:
|
||||
_row_e -= step
|
||||
_row_s -= step
|
||||
continue
|
||||
else:
|
||||
return _row_s, _row_e
|
||||
else:
|
||||
w2t(f"数据有误,需要检查,无法找到有效起始点或结束点...", 0, exitcode)
|
||||
|
||||
|
||||
def p_single(wb, single, vel, trq, rpm, w2t):
|
||||
# 1. 先找到第一个速度为零的点,数据从后往前找,一开始就是零的情况不予考虑
|
||||
# 2. 记录第一个点的位置,继续向前查找第二个速度为零的点,同理,一开始为零的点不予考虑
|
||||
# 3. 记录第二个点的位置,并将其中的数据拷贝至对应位置
|
||||
for data_file in single:
|
||||
rpm = 1 if rpm == 0 else rpm
|
||||
scale = 1000 if data_file.endswith('.csv') else 1
|
||||
axis = int(data_file.split('\\')[-1].split('_')[0].removeprefix('j'))
|
||||
shtname = f"J{axis}"
|
||||
ws = wb[shtname]
|
||||
|
||||
set_option("display.precision", 2)
|
||||
if data_file.endswith('.data'):
|
||||
df = read_csv(data_file, sep='\t')
|
||||
elif data_file.endswith('.csv'):
|
||||
df = read_csv(data_file, sep=',', encoding='gbk', header=8)
|
||||
csv_reader = reader(open(data_file))
|
||||
i = 0
|
||||
cycle = 0.001
|
||||
for row in csv_reader:
|
||||
i += 1
|
||||
if i == 3:
|
||||
cycle = float(row[0].split(':')[1].split('ms')[0]) / 1000
|
||||
break
|
||||
ws["H11"] = cycle
|
||||
|
||||
# 过滤尾部无效数据
|
||||
col_names = list(df.columns)
|
||||
df_1 = df[col_names[vel-1]].multiply(rpm)
|
||||
df_2 = df[col_names[trq-1]].multiply(scale)
|
||||
df = concat([df_1, df_2], axis=1)
|
||||
|
||||
_step = 5 if data_file.endswith('.csv') else 50
|
||||
_end_point = 30 if data_file.endswith('.csv') else 200
|
||||
_adjust = 0 if data_file.endswith('.csv') else 150
|
||||
_row_e = df.index[-1]
|
||||
_row_s = _row_e - 200
|
||||
while _row_e > 200:
|
||||
speed_avg = df.iloc[_row_s:_row_e, vel-1].abs().mean()
|
||||
if speed_avg > 2:
|
||||
_row_e -= 50
|
||||
_row_s -= 50
|
||||
continue
|
||||
else:
|
||||
break
|
||||
else:
|
||||
w2t("数据有误,需要检查,无法找到第一个有效起始点...", 0, 1)
|
||||
_row_s = _row_e - _end_point
|
||||
speed_avg = df.iloc[_row_s:_row_e, 0].abs().mean()
|
||||
if speed_avg < 2:
|
||||
# 过滤尾部为零无效数据
|
||||
_row_s, _row_e = find_point('lt', df, _row_s, _row_e, w2t, 1, threshold=2, step=_step, end_point=_end_point)
|
||||
# 找到第一个起始点 row_end,继续找到有数据的部分,后面有一段有效数据区
|
||||
row_end = _row_e - _adjust
|
||||
_row_e -= _end_point
|
||||
_row_s -= _end_point
|
||||
_row_s, _row_e = find_point('gt', df, _row_s, _row_e, w2t, 3, threshold=2, step=_step, end_point=_end_point)
|
||||
# 速度已经快要降为零了,继续寻找下一个速度上升点
|
||||
_row_e -= _end_point
|
||||
_row_s -= _end_point
|
||||
_row_s, _row_e = find_point('lt', df, _row_s, _row_e, w2t, 3, threshold=2, step=_step, end_point=_end_point)
|
||||
elif speed_avg > 2:
|
||||
# 过滤尾部非零无效数据
|
||||
_row_s, _row_e = find_point('gt', df, _row_s, _row_e, w2t, 2, threshold=2, step=_step, end_point=_end_point)
|
||||
# 找到第一个起始点 row_end,继续找到有数据的部分,后面有一段零数据区
|
||||
row_end = _row_e - _adjust
|
||||
_row_e -= _end_point
|
||||
_row_s -= _end_point
|
||||
_row_s, _row_e = find_point('lt', df, _row_s, _row_e, w2t, 4, threshold=2, step=_step, end_point=_end_point)
|
||||
#目前已经有一点的速度值了,继续往前搜寻下一个速度为零的点
|
||||
_row_e -= _end_point
|
||||
_row_s -= _end_point
|
||||
_row_s, _row_e = find_point('gt', df, _row_s, _row_e, w2t, 4, threshold=2, step=_step, end_point=_end_point)
|
||||
|
||||
# 找到第一个起始点 row_end,继续找到有数据的部分
|
||||
row_end = _row_e - 100
|
||||
_row_e -= 200
|
||||
_row_s -= 200
|
||||
while _row_e > 200:
|
||||
speed_avg = df.iloc[_row_s:_row_e, vel-1].abs().mean()
|
||||
if speed_avg < 2:
|
||||
_row_e -= 50
|
||||
_row_s -= 50
|
||||
continue
|
||||
else:
|
||||
break
|
||||
else:
|
||||
w2t("数据有误,需要检查,无法找到第二个有效起始点...", 0, 2)
|
||||
|
||||
# 目前已经有一点的速度值了,继续往前搜寻下一个速度为零的点
|
||||
_row_e -= 200
|
||||
_row_s -= 200
|
||||
while _row_e > 200:
|
||||
speed_avg = df.iloc[_row_s:_row_e, vel-1].abs().mean()
|
||||
if speed_avg > 2:
|
||||
_row_e -= 50
|
||||
_row_s -= 50
|
||||
continue
|
||||
else:
|
||||
break
|
||||
else:
|
||||
w2t("数据有误,需要检查,无法找到第三个有效起始点...", 0, 3)
|
||||
|
||||
row_start = _row_s + 180
|
||||
row_start = _row_s + _adjust
|
||||
data = []
|
||||
for row in range(row_start, row_end):
|
||||
data.append(df.iloc[row, vel-1])
|
||||
data.append(df.iloc[row, trq-1])
|
||||
data.append(df.iloc[row, 0])
|
||||
data.append(df.iloc[row, 1])
|
||||
|
||||
i = 0
|
||||
for row in ws.iter_rows(min_row=2, min_col=2, max_row=15000, max_col=3):
|
||||
for cell in row:
|
||||
try:
|
||||
cell.value = data[i]
|
||||
_ = f"{data[i]:.2f}"
|
||||
cell.value = float(_)
|
||||
i += 1
|
||||
except:
|
||||
cell.value = None
|
||||
|
||||
|
||||
def p_scenario():
|
||||
pass
|
||||
def p_scenario(wb, single, vel, trq, rpm, dur, w2t):
|
||||
for data_file in single:
|
||||
cycle = 0.001
|
||||
rpm = 1 if rpm == 0 else rpm
|
||||
scale = 1000 if data_file.endswith('.csv') else 1
|
||||
axis = int(data_file.split('\\')[-1].split('_')[0].removeprefix('j'))
|
||||
shtname = f"J{axis}"
|
||||
ws = wb[shtname]
|
||||
|
||||
set_option("display.precision", 2)
|
||||
if data_file.endswith('.data'):
|
||||
df = read_csv(data_file, sep='\t')
|
||||
elif data_file.endswith('.csv'):
|
||||
df = read_csv(data_file, sep=',', encoding='gbk', header=8)
|
||||
csv_reader = reader(open(data_file))
|
||||
i = 0
|
||||
for row in csv_reader:
|
||||
i += 1
|
||||
if i == 3:
|
||||
cycle = float(row[0].split(':')[1].split('ms')[0]) / 1000
|
||||
break
|
||||
|
||||
ws["H11"] = cycle
|
||||
|
||||
col_names = list(df.columns)
|
||||
df_1 = df[col_names[vel-1]].multiply(rpm)
|
||||
df_2 = df[col_names[trq-1]].multiply(scale)
|
||||
df = concat([df_1, df_2], axis=1)
|
||||
|
||||
row_start = 300
|
||||
row_end = row_start + int(dur/cycle)
|
||||
if row_end > df.index[-1]:
|
||||
w2t(f"位置超限:{data_file} 共有 {df.index[-1]} 条数据,无法取到第 {row_end} 条数据...", 0, 9)
|
||||
|
||||
data = []
|
||||
for row in range(row_start, row_end):
|
||||
data.append(df.iloc[row, 0])
|
||||
data.append(df.iloc[row, 1])
|
||||
|
||||
i = 0
|
||||
for row in ws.iter_rows(min_row=2, min_col=2, max_row=15000, max_col=3):
|
||||
for cell in row:
|
||||
try:
|
||||
_ = f"{data[i]:.2f}"
|
||||
cell.value = float(_)
|
||||
i += 1
|
||||
except:
|
||||
cell.value = None
|
||||
|
||||
|
||||
# =======================================
|
||||
|
||||
|
||||
def main(path, sub, rcs, vel, trq, trqh, dur, w2t):
|
||||
def main(path, sub, rcs, vel, trq, trqh, dur, rpm, w2t):
|
||||
data_files = initialization(path, sub, w2t)
|
||||
if sub == 'max':
|
||||
current_max(data_files, rcs, trqh, w2t)
|
||||
elif sub == 'avg':
|
||||
current_avg(data_files, rcs, trqh, w2t)
|
||||
elif sub == 'cycle':
|
||||
current_cycle(dur, data_files, rcs, vel, trq, trqh, w2t)
|
||||
current_cycle(dur, data_files, rcs, vel, trq, trqh, rpm, w2t)
|
||||
else:
|
||||
pass
|
||||
|
||||
|
Reference in New Issue
Block a user