diff --git a/rokae/brake/brake.py b/rokae/brake/brake.py index 99eade7..290abf5 100644 --- a/rokae/brake/brake.py +++ b/rokae/brake/brake.py @@ -6,7 +6,7 @@ from win32com.client import DispatchEx from time import time, strftime, localtime, sleep from threading import Thread import pythoncom -import pandas as pd +from pandas import read_csv def just_open(filename): @@ -26,7 +26,7 @@ def just_open(filename): xlbook.close() if xlapp is not None: xlapp.Quit() - print(f"just open 第 {i} 次操作失败,静默三秒钟,重新执行......") + print(f"使用win32com打开【{filename}】文件,第 {i} 次操作失败,静默三秒钟,等待重新执行......") sleep(3) @@ -42,24 +42,44 @@ def traversal_files(path): return dirs, files -def find_row_start(excel_file, ws_data, conditions, AV, RR): +def get_threshold_step(excel_file, AXIS): + conditions = sorted(excel_file.split('\\')[-2].split('_')) + # 只有负载和速度是100%时,才会启用更敏感的step + flg = 1 if conditions[0][-3:] == '100' and conditions[2][-3:] == '100' else 0 + if flg == 1 and AXIS == 'j2': + threshold = 50 + step = 20 + else: + threshold = 50 + step = 100 + + return threshold, step + + +def find_row_start(excel_file, ws_data, conditions, AV, RR, AXIS): ratio = float(conditions[1].removeprefix('speed'))/100 speed_max = AV * ratio * RR / 6 row_max = ws_data.max_row row_start = row_max - 1000 + threshold, step = get_threshold_step(excel_file, AXIS) while row_start > 0: + row_end = row_start - step + if row_end < 2: + msg = f"可能是{excel_file.replace('xlsx', 'data')}, 这个文件数据采集有问题,也有可能是程序步长设定问题......" \ + f"建议重新采集,或者先删除该文件夹,重新运行程序,先手动处理" + warn_pause_exit(msg, 1, 10) _a = ws_data[f"A{row_start}"].value - _b = ws_data[f"A{row_start - 200}"].value - if abs(_a-speed_max) < 50 and abs(_b-speed_max) < 50 and abs(_a - _b) < 70: - row_start -= 200 + _b = ws_data[f"A{row_end}"].value + if abs(_a-speed_max) < threshold and abs(_b-speed_max) < threshold and abs(_a-_b) < threshold: + row_start -= (step + 200) break else: - row_start -= 200 + row_start -= step else: remove(excel_file) - msg = f"{excel_file.replace('xlsx', 'data')}, 这个文件数据有问题,请检查......" + msg = f"可能是{excel_file.replace('xlsx', 'data')},这个文件数据采集有问题,比如采集的时机不对,请检查......" warn_pause_exit(msg, 1, 9) return row_max, row_start @@ -123,17 +143,16 @@ def copy_data_to_excel_file(wb_data, ws_result, row_max, row_start, excel_file, return wb_data, ws_dp -def find_row_start_dp(ws_dp, row_max, row_start, conditions, AV): +def find_row_start_dp(data_file, ws_dp, row_max, row_start, conditions, AV): ratio = float(conditions[1].removeprefix('speed'))/100 av_max = AV * ratio row_max_dp = row_max - row_start + 1 + 1 # title row row_start_dp = row_max_dp - 5 - while row_start_dp > 1: + while row_start_dp > 6: # 处理异常数据:当从数据文件中拷贝的有效数据超过5000时,会触发该代码块 if ws_dp.cell(row=row_start_dp, column=4).value is None: row_start_dp -= 100 continue - _a = float(ws_dp.cell(row=row_start_dp, column=4).value) _b = float(ws_dp.cell(row=row_start_dp - 1, column=4).value) _c = float(ws_dp.cell(row=row_start_dp - 2, column=4).value) @@ -152,10 +171,10 @@ def find_row_start_dp(ws_dp, row_max, row_start, conditions, AV): return row_start_dp -def single_file_process(data_file, wb_result, count, AV, RR, RC): +def single_file_process(data_file, wb_result, count, AV, RR, RC, AXIS): excel_file = data_file.replace('data', 'xlsx') sheet_name = data_file.split('\\')[-1].removesuffix('.data') - df = pd.read_csv(data_file, sep='\t') + df = read_csv(data_file, sep='\t') df.to_excel(excel_file, sheet_name=sheet_name, index=False) conditions = sorted(data_file.split('\\')[-2].split('_')[1:]) @@ -165,11 +184,11 @@ def single_file_process(data_file, wb_result, count, AV, RR, RC): wb_data = load_workbook(excel_file) ws_data = wb_data[sheet_name] - row_max, row_start = find_row_start(excel_file, ws_data, conditions, AV, RR) + row_max, row_start = find_row_start(excel_file, ws_data, conditions, AV, RR, AXIS) copy_data_to_result(ws_data, ws_result, row_max, row_start) wb_data, ws_dp = copy_data_to_excel_file(wb_data, ws_result, row_max, row_start, excel_file, RC, RR) - row_start_dp = find_row_start_dp(ws_dp, row_max, row_start, conditions, AV) + row_start_dp = find_row_start_dp(data_file, ws_dp, row_max, row_start, conditions, AV) ws_result["G2"] = int(row_start_dp) @@ -177,7 +196,7 @@ def single_file_process(data_file, wb_result, count, AV, RR, RC): wb_data.close() -def data_process(result_file, raw_data_dirs, AV, RR, RC): +def data_process(result_file, raw_data_dirs, AV, RR, RC, AXIS): prefix = result_file.split('\\')[-1].split('_')[0] wb_result = load_workbook(result_file) # 打开和关闭结果文件夹十分耗时间 for raw_data_dir in raw_data_dirs: @@ -186,9 +205,9 @@ def data_process(result_file, raw_data_dirs, AV, RR, RC): print(f"[{now}] 正在处理目录【{raw_data_dir}】中的数据......") _, data_files = traversal_files(raw_data_dir) - threads = [Thread(target=single_file_process, args=(data_files[0], wb_result, 1, AV, RR, RC)), - Thread(target=single_file_process, args=(data_files[1], wb_result, 2, AV, RR, RC)), - Thread(target=single_file_process, args=(data_files[2], wb_result, 3, AV, RR, RC))] + threads = [Thread(target=single_file_process, args=(data_files[0], wb_result, 1, AV, RR, RC, AXIS)), + Thread(target=single_file_process, args=(data_files[1], wb_result, 2, AV, RR, RC, AXIS)), + Thread(target=single_file_process, args=(data_files[2], wb_result, 3, AV, RR, RC, AXIS))] [t.start() for t in threads] [t.join() for t in threads] now = strftime('%Y-%m-%d %H:%M:%S', localtime(time())) @@ -203,7 +222,7 @@ def data_process(result_file, raw_data_dirs, AV, RR, RC): def warn_pause_exit(msg, pause_num, exit_num): print(msg + '\n') for i in range(pause_num): - _ = input("Press ENTER to continue......") + _ = input("Press ENTER to continue......\n") exit(exit_num) @@ -257,9 +276,7 @@ def check_files(raw_data_dirs, result_files): print("数据目录合规性检查结束,未发现问题......") -def delete_excel_files(): - global data_dir - raw_data_dirs, _ = traversal_files(data_dir) +def delete_excel_files(raw_data_dirs): for raw_data_dir in raw_data_dirs: _, raw_data_files = traversal_files(raw_data_dir) for raw_data_file in raw_data_files: @@ -271,18 +288,14 @@ def initialization(): time_start = time() # 记录开始时间 try: # read init configurations from config file - wb_conf = load_workbook('./configuration.xlsx') + wb_conf = load_workbook('./configuration.xlsx', read_only=True) ws_conf = wb_conf['conf'] - global data_dir - global AV - global RR - global RC - data_dir = ws_conf.cell(row=2, column=2).value AV = int(ws_conf.cell(row=3, column=2).value) RR = int(ws_conf.cell(row=4, column=2).value) RC = float(ws_conf.cell(row=5, column=2).value) + AXIS = ws_conf.cell(row=6, column=2).value wb_conf.close() raw_data_dirs, result_files = traversal_files(data_dir) @@ -291,7 +304,7 @@ def initialization(): # print(f'数据目录:{raw_data_dirs}') check_files(raw_data_dirs, result_files) - return raw_data_dirs, result_files, time_start, AV, RR, RC + return raw_data_dirs, result_files, time_start, AV, RR, RC, AXIS except Exception as Err: msg = "无法在当前路径下找到【configuration.xlsx】文件,请确认!" @@ -299,7 +312,7 @@ def initialization(): def execution(args): - raw_data_dirs, result_files, time_start, AV, RR, RC = args + raw_data_dirs, result_files, time_start, AV, RR, RC, AXIS = args prefix = [] for raw_data_dir in raw_data_dirs: prefix.append(raw_data_dir.split('\\')[-1].split("_")[0]) @@ -312,16 +325,16 @@ def execution(args): else: now = strftime('%Y-%m-%d %H:%M:%S', localtime(time())) print(f"[{now}] 正在整理结果文件【{result_file}】的数据......") - # data_process(result_file, raw_data_dirs, AV, RR, RC) - threads.append(Thread(target=data_process, args=(result_file, raw_data_dirs, AV, RR, RC))) + # data_process(result_file, raw_data_dirs, AV, RR, RC, AXIS) + threads.append(Thread(target=data_process, args=(result_file, raw_data_dirs, AV, RR, RC, AXIS))) [t.start() for t in threads] [t.join() for t in threads] print("#---------------------------------------------------------") print("全部处理完毕") - delete_excel_files() + delete_excel_files(raw_data_dirs) except Exception as Err: print("程序运行错误,请检查配置文件是否准确设定,以及数据文件组织是否正确!") - delete_excel_files() # 运行结束之后,删除中间临时文件 + delete_excel_files(raw_data_dirs) # 运行结束之后,删除中间临时文件 time_end = time() # 记录结束时间 time_total = time_end - time_start # 计算的时间差为程序的执行时间,单位为秒/s @@ -333,12 +346,5 @@ def main(): execution(initialization()) -# 定义初始参数,数据文件夹路径/最大角速度/减速比/额定电流 -global data_dir # path for data -global AV # AV for Angular velocity -global RR # RR for Angular velocity -global RC # RC for Rated Current - - if __name__ == "__main__": main() diff --git a/rokae/brake/configuration.xlsx b/rokae/brake/configuration.xlsx index 4e67186..e4da2ce 100644 Binary files a/rokae/brake/configuration.xlsx and b/rokae/brake/configuration.xlsx differ diff --git a/rokae/brake/file_version_info.txt b/rokae/brake/file_version_info.txt index 7332828..aac89fe 100644 --- a/rokae/brake/file_version_info.txt +++ b/rokae/brake/file_version_info.txt @@ -6,8 +6,8 @@ VSVersionInfo( ffi=FixedFileInfo( # filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4) # Set not needed items to zero 0. - filevers=(0, 0, 1, 5), - prodvers=(0, 0, 1, 5), + filevers=(0, 0, 3, 0), + prodvers=(0, 0, 3, 0), # Contains a bitmask that specifies the valid bits 'flags'r mask=0x3f, # Contains a bitmask that specifies the Boolean attributes of the file. @@ -31,12 +31,12 @@ VSVersionInfo( '040904b0', [StringStruct('CompanyName', 'Rokae - https://www.rokae.com/'), StringStruct('FileDescription', 'All in one automatic operating tool'), - StringStruct('FileVersion', '0.0.2 (2024-05-20)'), + StringStruct('FileVersion', '0.0.3 (2024-05-20)'), StringStruct('InternalName', 'AIO.exe'), StringStruct('LegalCopyright', '© 2024-2024 Manford Fan'), StringStruct('OriginalFilename', 'AIO.exe'), StringStruct('ProductName', 'AIO'), - StringStruct('ProductVersion', '0.0.2 (2024-05-20)')]) + StringStruct('ProductVersion', '0.0.3 (2024-05-20)')]) ]), VarFileInfo([VarStruct('Translation', [1033, 1200])]) ]