239 lines
10 KiB
Python
239 lines
10 KiB
Python
import json
|
||
import os.path
|
||
import time
|
||
import pandas
|
||
import threading
|
||
import openpyxl
|
||
import re
|
||
from common import clibs
|
||
|
||
|
||
def check_files(rawdata_dirs, result_files, w2t):
|
||
msg_wrong = "需要有四个文件和若干个数据文件夹,可参考如下确认:\n"
|
||
msg_wrong += "1. reach33_XXXXXXX.xlsx\n2. reach66_XXXXXXX.xlsx\n3. reach100_XXXXXXX.xlsx\n4. *.cfg\n"
|
||
msg_wrong += "- reach33_load33_speed33\nreach33_load33_speed66\n......\nreach100_load100_speed66\nreach100_load100_speed100\n"
|
||
|
||
if len(result_files) != 4 or len(rawdata_dirs) == 0:
|
||
w2t(msg_wrong, "red", "InitFileError")
|
||
|
||
config_file, reach33_file, reach66_file, reach100_file = None, None, None, None
|
||
for result_file in result_files:
|
||
filename = result_file.split("/")[-1]
|
||
if re.match(".*\\.cfg", filename):
|
||
config_file = result_file
|
||
elif filename.startswith("reach33_") and filename.endswith(".xlsx"):
|
||
reach33_file = result_file
|
||
elif filename.startswith("reach66_") and filename.endswith(".xlsx"):
|
||
reach66_file = result_file
|
||
elif filename.startswith("reach100_") and filename.endswith(".xlsx"):
|
||
reach100_file = result_file
|
||
else:
|
||
if not (config_file and reach33_file and reach66_file and reach100_file):
|
||
w2t(msg_wrong, "red", "InitFileError")
|
||
|
||
reach_s = ['reach33', 'reach66', 'reach100']
|
||
load_s = ['load33', 'load66', 'load100']
|
||
speed_s = ['speed33', 'speed66', 'speed100']
|
||
prefix = []
|
||
for rawdata_dir in rawdata_dirs:
|
||
components = rawdata_dir.split("/")[-1].split('_') # reach_load_speed
|
||
prefix.append(components[0])
|
||
if components[0] not in reach_s or components[1] not in load_s or components[2] not in speed_s:
|
||
msg = f"报错信息:数据目录 {rawdata_dir} 命名不合规,请参考如下形式\n"
|
||
msg += "命名规则:reachAA_loadBB_speedCC,AA/BB/CC 指的是臂展/负载/速度的比例\n"
|
||
msg += "规则解释:reach66_load100_speed33,表示 66% 臂展,100% 负载以及 33% 速度情况下的测试结果文件夹\n"
|
||
w2t(msg, "red", "WrongDataFolder")
|
||
|
||
_, rawdata_files = clibs.traversal_files(rawdata_dir, w2t)
|
||
if len(rawdata_files) != 3:
|
||
msg = f"数据目录 {rawdata_dir} 下数据文件个数错误,每个数据目录下有且只能有三个以 .data 为后缀的数据文件\n"
|
||
w2t(msg, "red", "WrongDataFile")
|
||
for rawdata_file in rawdata_files:
|
||
if not rawdata_file.endswith(".data"):
|
||
msg = f"数据文件 {rawdata_file} 后缀错误,每个数据目录下有且只能有三个以 .data 为后缀的数据文件\n"
|
||
w2t(msg, "red", "WrongDataFile")
|
||
|
||
result_files = []
|
||
for _ in [reach33_file, reach66_file, reach100_file]:
|
||
if _.split("/")[-1].split("_")[0] in set(prefix):
|
||
result_files.append(_)
|
||
|
||
w2t("数据目录合规性检查结束,未发现问题......\n")
|
||
return config_file, result_files
|
||
|
||
|
||
def get_configs(config_file, w2t):
|
||
try:
|
||
with open(config_file, mode="r", encoding="utf-8") as f_config:
|
||
configs = json.load(f_config)
|
||
except Exception as Err:
|
||
clibs.insert_logdb("ERROR", "current", f"get_config: 无法打开 {config_file},获取配置文件参数错误 {Err}")
|
||
w2t(f"无法打开 {config_file}", color="red", desc="OpenFileError")
|
||
|
||
p_dir = config_file.split('/')[-2]
|
||
if not re.match("^[jJ][123]$", p_dir):
|
||
w2t("被处理的根文件夹命名必须是 [Jj][123] 的格式", "red", "DirNameError")
|
||
axis = int(p_dir[-1])
|
||
|
||
rrs = [abs(_) for _ in configs["TRANSMISSION"]["REDUCTION_RATIO_NUMERATOR"]] # 减速比,rr for reduction ratio
|
||
avs = configs["MOTION"]["JOINT_MAX_SPEED"]
|
||
rr = rrs[axis-1]
|
||
av = avs[axis-1]
|
||
|
||
return av, rr
|
||
|
||
|
||
def now_doing_msg(docs, flag, w2t):
|
||
now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
|
||
file_type = 'file' if os.path.isfile(docs) else 'dir'
|
||
if flag == 'start' and file_type == 'dir':
|
||
w2t(f"[{now}] 正在处理目录 {docs} 中的数据......\n")
|
||
elif flag == 'start' and file_type == 'file':
|
||
w2t(f"[{now}] 正在处理文件 {docs} 中的数据......\n")
|
||
elif flag == 'done' and file_type == 'dir':
|
||
w2t(f"[{now}] 目录 {docs} 数据文件已处理完毕\n")
|
||
elif flag == 'done' and file_type == 'file':
|
||
w2t(f"[{now}] 文件 {docs} 数据已处理完毕\n")
|
||
|
||
|
||
def data2result(df, ws_result, row_start, row_end, vel, trq, estop):
|
||
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, estop - 1])
|
||
|
||
i = 0
|
||
row_max = 1000 if row_end - row_start < 1000 else row_end - row_start + 100
|
||
for row in range(2, row_max):
|
||
try:
|
||
ws_result.cell(row=row, column=1).value = data[i]
|
||
ws_result.cell(row=row, column=2).value = data[i + 1]
|
||
ws_result.cell(row=row, column=3).value = data[i + 2]
|
||
i += 3
|
||
except Exception:
|
||
ws_result.cell(row=row, column=1).value = None
|
||
ws_result.cell(row=row, column=2).value = None
|
||
ws_result.cell(row=row, column=3).value = None
|
||
|
||
|
||
def get_row_range(data_file, df, conditions, av, rr, vel, estop, w2t):
|
||
row_start, row_end = 0, 0
|
||
ratio = float(conditions[2].removeprefix('speed')) / 100
|
||
av_max = av * ratio
|
||
threshold = 0.95
|
||
|
||
for row in range(df.index[-1] - 1, -1, -10):
|
||
if df.iloc[row, estop - 1] != 0:
|
||
row_start = row - 20 if row - 20 > 0 else 0 # 急停前找 20 个点
|
||
break
|
||
else:
|
||
w2t(f"数据文件 {data_file} 采集的数据中没有 ESTOP 为非 0 的情况,需要确认\n", "red", "StartNotFoundError")
|
||
|
||
for row in range(row_start, df.index[-1] - 1, 10):
|
||
speed_row = df.iloc[row, vel - 1] * clibs.RADIAN * rr * 60 / 360
|
||
if abs(speed_row) < 1:
|
||
row_end = row + 100 if row + 100 <= df.index[-1] - 1 else df.index[-1] - 1
|
||
break
|
||
else:
|
||
w2t(f"数据文件 {data_file} 最后的速度未降为零\n", "red", "SpeedNotZeroError")
|
||
|
||
av_estop = abs(df.iloc[row_start - 20:row_start, vel - 1].abs().mean() * clibs.RADIAN)
|
||
if abs(av_estop / av_max) < threshold:
|
||
filename = data_file.split("/")[-1]
|
||
w2t(f"[av_estop: {av_estop:.2f} | shouldbe: {av_max:.2f}] 数据文件 {filename} 触发 ESTOP 时未采集到指定百分比的最大速度,需要检查\n", "#8A2BE2")
|
||
|
||
return row_start, row_end
|
||
|
||
|
||
def get_shtname(conditions, count):
|
||
# 33%负载_33%速度_1 - reach/load/speed
|
||
load = conditions[1].removeprefix('load')
|
||
speed = conditions[2].removeprefix('speed')
|
||
result_sheet_name = f"{load}%负载_{speed}%速度_{count}"
|
||
|
||
return result_sheet_name
|
||
|
||
|
||
def single_file_process(data_file, wb, count, av, rr, vel, trq, estop, w2t):
|
||
df = pandas.read_csv(data_file, sep='\t')
|
||
conditions = data_file.split("/")[-2].split("_") # reach/load/speed
|
||
shtname = get_shtname(conditions, count)
|
||
ws = wb[shtname]
|
||
|
||
row_start, row_end = get_row_range(data_file, df, conditions, av, rr, vel, estop, w2t)
|
||
data2result(df, ws, row_start, row_end, vel, trq, estop)
|
||
|
||
|
||
def data_process(result_file, rawdata_dirs, av, rr, vel, trq, estop, w2t):
|
||
filename = result_file.split("/")[-1]
|
||
|
||
clibs.stop = True
|
||
w2t(f"正在打开文件 {filename} 需要 1min 左右......\n", "blue")
|
||
t_excel = clibs.GetThreadResult(openpyxl.load_workbook, args=(result_file, ))
|
||
t_excel.daemon = True
|
||
t_excel.start()
|
||
t_progress = threading.Thread(target=clibs.tl_prg, args=("Processing......", ))
|
||
t_progress.daemon = True
|
||
t_progress.start()
|
||
wb = t_excel.get_result()
|
||
|
||
prefix = filename.split('_')[0]
|
||
for rawdata_dir in rawdata_dirs:
|
||
if rawdata_dir.split("/")[-1].split('_')[0] == prefix:
|
||
now_doing_msg(rawdata_dir, 'start', w2t)
|
||
_, data_files = clibs.traversal_files(rawdata_dir, w2t)
|
||
# 数据文件串行处理模式---------------------------------
|
||
# count = 1
|
||
# for data_file in data_files:
|
||
# now_doing_msg(data_file, 'start', w2t)
|
||
# single_file_process(data_file, wb_result, count, av, rr, vel, trq, estop, w2t)
|
||
# count += 1
|
||
# now_doing_msg(data_file, 'done', w2t)
|
||
# ---------------------------------------------------
|
||
# 数据文件并行处理模式---------------------------------
|
||
threads = [
|
||
threading.Thread(target=single_file_process, args=(data_files[0], wb, 1, av, rr, vel, trq, estop, w2t)),
|
||
threading.Thread(target=single_file_process, args=(data_files[1], wb, 2, av, rr, vel, trq, estop, w2t)),
|
||
threading.Thread(target=single_file_process, args=(data_files[2], wb, 3, av, rr, vel, trq, estop, w2t))
|
||
]
|
||
[t.start() for t in threads]
|
||
[t.join() for t in threads]
|
||
# ---------------------------------------------------
|
||
now_doing_msg(rawdata_dir, 'done', w2t)
|
||
|
||
w2t(f"正在保存文件 {filename} 需要 1min 左右......\n\n", "blue")
|
||
t_excel = threading.Thread(target=wb.save, args=(result_file, ))
|
||
t_excel.daemon = True
|
||
t_excel.start()
|
||
t_excel.join()
|
||
wb.close()
|
||
clibs.stop = False
|
||
t_progress.join()
|
||
|
||
|
||
def main():
|
||
time_start = time.time()
|
||
path = clibs.data_dp["_path"]
|
||
vel = int(clibs.data_dp["_vel"])
|
||
trq = int(clibs.data_dp["_trq"])
|
||
estop = int(clibs.data_dp["_estop"])
|
||
w2t = clibs.w2t
|
||
|
||
rawdata_dirs, result_files = clibs.traversal_files(path, w2t)
|
||
config_file, result_files = check_files(rawdata_dirs, result_files, w2t)
|
||
av, rr = get_configs(config_file, w2t)
|
||
|
||
for result_file in result_files:
|
||
data_process(result_file, rawdata_dirs, av, rr, vel, trq, estop, w2t)
|
||
|
||
w2t("-"*60 + "\n全部处理完毕\n")
|
||
time_end = time.time()
|
||
time_total = time_end - time_start
|
||
msg = f"数据处理时间:{time_total // 3600:02.0f} h {time_total % 3600 // 60:02.0f} m {time_total % 60:02.0f} s\n"
|
||
w2t(msg)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|