diff --git a/rokae/aio/aio.py b/rokae/aio/aio.py index 83a1b21..32a0ea3 100644 --- a/rokae/aio/aio.py +++ b/rokae/aio/aio.py @@ -1,4 +1,5 @@ from os.path import exists +from os import getcwd from threading import Thread import tkinter.messagebox import customtkinter @@ -13,7 +14,6 @@ customtkinter.set_window_scaling(1.1) # window geometry dimensions class App(customtkinter.CTk): - def __init__(self): super().__init__() self.my_font = customtkinter.CTkFont(family="Consolas", size=16, weight="bold") @@ -70,7 +70,7 @@ class App(customtkinter.CTk): self.label_path = customtkinter.CTkLabel(self.frame_param, width=self.w_param//10, text="Path", font=self.my_font) self.label_path.grid(row=0, column=2, sticky='e', pady=(10, 5)) self.entry_path = customtkinter.CTkEntry(self.frame_param, width=680, placeholder_text="数据文件夹路径", font=self.my_font) - self.entry_path.grid(row=0, column=3, columnspan=7, padx=(5, 10), pady=(10, 5), sticky='w') + self.entry_path.grid(row=0, column=3, columnspan=7, padx=(5, 10), pady=(10, 5), sticky='we') self.entry_path.configure(state='disabled') self.label_av = customtkinter.CTkLabel(self.frame_param, width=self.w_param//10, text="AV", font=self.my_font) @@ -122,7 +122,7 @@ class App(customtkinter.CTk): def thread_it(self, func, *args): """ 将函数打包进线程 """ self.myThread = Thread(target=func, args=args) - self.myThread.daemon = True # 主线程退出就直接让子线程跟随退出,不论是否运行完成。 + self.myThread.daemon = True # 主线程退出就直接让子线程跟随退出,不论是否运行完成。 self.myThread.start() def initialization(self): @@ -199,17 +199,13 @@ class App(customtkinter.CTk): def write2textbox(self, text, wait=0): if wait != 0: - self.textbox.configure(state="normal") self.textbox.insert(index='end', text=text) self.textbox.update() self.textbox.see('end') - self.textbox.configure(state="disabled") else: - self.textbox.configure(state="normal", text_color='blue') self.textbox.insert(index='end', text=text + '\n') self.textbox.update() self.textbox.see('end') - self.textbox.configure(state="disabled") def check_param(self): func_name = self.menu_main.get() @@ -251,7 +247,7 @@ class App(customtkinter.CTk): try: _ = float(rc) c3 = True - except Exception as Err: + except ValueError: c3 = False flag = 2 if c1 and c2 and c3 else 0 @@ -263,7 +259,6 @@ class App(customtkinter.CTk): self.textbox.configure(state='normal') self.textbox.delete(index1='1.0', index2='end') - self.textbox.configure(state='disabled') flag, *args = self.check_param() func_dict = {1: brake.main, 2: current.main} @@ -271,16 +266,18 @@ class App(customtkinter.CTk): if flag == 1: func_dict[flag](path=args[0], av=args[1], rr=args[2], rpm=args[3], axis=args[4], vel=args[5], trq=args[6], w2t=self.write2textbox) elif flag == 2: - func_dict[flag](path=args[0], rc=args[1], sub_func= args[2]) + func_dict[flag](path=args[0], rc=args[1], sub_func=args[2]) else: tkinter.messagebox.showerror(title="参数错误", message="请检查对应参数是否填写正确!", ) + self.textbox.configure(state='disabled') + def func_check_callback(self): flag, *args = self.check_param() if flag: - tkinter.messagebox.showinfo(title="参数正确", message="所有参数形式上填写无误,请确定后开始运行!") + tkinter.messagebox.showinfo(title="参数正确", message="所有参数形式上填写无误,可以开始运行!") else: - tkinter.messagebox.showerror(title="参数错误", message="请检查对应参数是否填写正确!", ) + tkinter.messagebox.showerror(title="参数错误", message="需要检查对应参数是否填写正确!", ) def func_log_callback(self): content = self.textbox.get(index1='1.0', index2='end') @@ -290,7 +287,7 @@ class App(customtkinter.CTk): log_name = f"{now}_aio.log" with open(f'{log_name}', 'w', encoding='utf-8') as objlog: objlog.write(content) - tkinter.messagebox.showinfo(title="保存成功", message=f'{log_name}已被保存存至↓↓↓\n{os.getcwd()}') + tkinter.messagebox.showinfo(title="保存成功", message=f'{log_name}已被保存存至↓↓↓\n{getcwd()}') except Exception as Err: print(Err) diff --git a/rokae/aio/brake.py b/rokae/aio/brake.py index d9c475b..aa5f1c7 100644 --- a/rokae/aio/brake.py +++ b/rokae/aio/brake.py @@ -8,150 +8,6 @@ from threading import Thread from pandas import read_csv -def traversal_files(path, w2t): - # 功能:以列表的形式分别返回指定路径下的文件和文件夹,不包含子目录 - # 参数:路径 - # 返回值:路径下的文件夹列表 路径下的文件列表 - if not exists(path): - msg = f'数据文件夹{path}不存在,请确认后重试......' - w2t(msg) - - else: - dirs = [] - files = [] - for item in scandir(path): - if item.is_dir(): - dirs.append(item.path) - elif item.is_file(): - files.append(item.path) - - return dirs, files - - -def get_threshold_step(excel_file, axis): - # 功能:负载和速度100%,且是j2的时候,做特殊处理 - # 参数:新生成的excel,轴号 - # 返回值:速度差阈值,处理步长 - 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 == 2: - threshold = 30 - step = 5 - else: - threshold = 10 - step = 5 - - return threshold, step - - -def find_row_start(data_file, df, conditions, av, rr, axis, vel, trq, w2t, rpm): - # 功能:查找数据文件中有效数据的行号,也即最后一个速度下降的点位 - # 参数:如上 - # 返回值:速度下降点位,最后的数据点位 - ratio = float(conditions[1].removeprefix('speed'))/100 - speed_max = av * ratio * rr / 6 - row_max = row_start = df.index[-1] - - threshold, step = get_threshold_step(data_file, axis) - while row_start > step+1: - speed = df.iloc[row_start, vel-1] * rpm - if int(speed) < 1: - row_start -= 50 - continue - - _ = [] - for i in range(row_start, row_start-step+1, -1): - _.append(df.iloc[i, vel-1] * rpm) - speed_avg = abs(sum(_))/len(_) - - if abs(speed_avg-speed_max) < threshold: - row_start = row_start - 10 - break - else: - row_start -= step - else: - msg = f"可能是{data_file}这个文件数据采集有问题,比如未采集理论速度值,也有可能是程序步长设定问题,请检查......" - w2t(msg) - - return row_max, row_start - - -def find_result_sheet_name(conditions, count): - # 功能:获取结果文件准确的sheet页名称 - # 参数:臂展和速度的列表 - # 返回值:结果文件对应的sheet name - # 33%臂展_33%速度_正1 - reach = conditions[0].removeprefix('reach') - speed = conditions[1].removeprefix('speed') - result_sheet_name = f"{reach}%臂展_{speed}%速度_正{count}" - - return result_sheet_name - - -def copy_data_to_result(df, ws_result, row_max, row_start, vel, trq, rpm): - # 功能:将数据文件中有效数据拷贝至结果文件对应的 sheet - # 参数:如上 - # 返回值:- - # 结果文件数据清零 - for row in ws_result.iter_rows(min_row=2, min_col=1, max_row=2000, max_col=2): - for cell in row: - cell.value = None - - # 将合适的数据复制到结果文件 - row_max = row_start + 399 if row_max-row_start > 400 else row_max - rc = 1 if rpm == 1 else 1000 - - data = [] - for i in range(row_start, row_max+1): - data.append(df.iloc[i, vel-1] * rpm) - data.append(df.iloc[i, trq-1] * rc) - - i = 0 - for row in ws_result.iter_rows(min_row=2, min_col=1, max_row=row_max - row_start + 2, max_col=2): - for cell in row: - cell.value = data[i] - i = i + 1 - - -def single_file_process(data_file, wb_result, count, av, rr, axis, vel, trq, w2t, rpm): - # 功能:完成单个数据文件的处理 - # 参数:如上 - # 返回值:- - if data_file.endswith('.data'): - sep = '\t' - df = read_csv(data_file, sep=sep) - elif data_file.endswith('.csv'): - sep = ',' - df = read_csv(data_file, sep=sep, encoding='gbk', header=8) - - conditions = sorted(data_file.split('\\')[-2].split('_')[1:]) - result_sheet_name = find_result_sheet_name(conditions, count) - ws_result = wb_result[result_sheet_name] - - row_max, row_start = find_row_start(data_file, df, conditions, av, rr, axis, vel, trq, w2t, rpm) - - copy_data_to_result(df, ws_result, row_max, row_start, vel, trq, rpm) - ws_result["C2"] = int(2) - ws_result["G2"] = int(10+4) - - -def now_doing_msg(docs, flag, w2t): - # 功能:输出正在处理的文件或目录 - # 参数:文件或目录,start 或 done 标识 - # 返回值:- - now = strftime('%Y-%m-%d %H:%M:%S', localtime(time())) - file_type = 'file' if isfile(docs) else 'dir' - if flag == 'start' and file_type == 'dir': - w2t(f"[{now}] 正在处理目录 {docs} 中的数据......") - elif flag == 'start' and file_type == 'file': - w2t(f"[{now}] 正在处理文件 {docs} 中的数据......") - elif flag == 'done' and file_type == 'dir': - w2t(f"[{now}] 目录 {docs} 数据文件已处理完毕......") - elif flag == 'done' and file_type == 'file': - w2t(f"[{now}] 文件 {docs} 数据已处理完毕......") - - class GetThreadResult(Thread): def __init__(self, func, args=()): super(GetThreadResult, self).__init__() @@ -171,16 +27,6 @@ class GetThreadResult(Thread): return None -def w2t_local(msg, wait, w2t): - while True: - global stop - if stop == 0 and wait != 0: - sleep(1) - w2t(msg, wait) - else: - break - - def data_process(result_file, raw_data_dirs, av, rr, axis, vel, trq, w2t, rpm): # 功能:完成一个结果文件的数据处理 # 参数:结果文件,数据目录,以及预读取的参数 @@ -219,8 +65,8 @@ def data_process(result_file, raw_data_dirs, av, rr, axis, vel, trq, w2t, rpm): Thread(target=single_file_process, args=(data_files[2], wb_result, 3, av, rr, axis, vel, trq, w2t, rpm))] [t.start() for t in threads] [t.join() for t in threads] - now_doing_msg(raw_data_dir, 'done', w2t) # --------------------------------------------------- + now_doing_msg(raw_data_dir, 'done', w2t) now_doing_msg(result_file, 'done', w2t) w2t(f"保存文件 {file_name} 需要 1min 左右", 1) @@ -282,7 +128,160 @@ def check_files(raw_data_dirs, result_files, w2t): w2t("数据目录合规性检查结束,未发现问题......") -def execution(path, av, rr, rpm, axis,vel, trq, w2t): +def now_doing_msg(docs, flag, w2t): + # 功能:输出正在处理的文件或目录 + # 参数:文件或目录,start 或 done 标识 + # 返回值:- + now = strftime('%Y-%m-%d %H:%M:%S', localtime(time())) + file_type = 'file' if isfile(docs) else 'dir' + if flag == 'start' and file_type == 'dir': + w2t(f"[{now}] 正在处理目录 {docs} 中的数据......") + elif flag == 'start' and file_type == 'file': + w2t(f"[{now}] 正在处理文件 {docs} 中的数据......") + elif flag == 'done' and file_type == 'dir': + w2t(f"[{now}] 目录 {docs} 数据文件已处理完毕......") + elif flag == 'done' and file_type == 'file': + w2t(f"[{now}] 文件 {docs} 数据已处理完毕......") + + +def w2t_local(msg, wait, w2t): + while True: + global stop + if stop == 0 and wait != 0: + sleep(1) + w2t(msg, wait) + else: + break + + +def single_file_process(data_file, wb_result, count, av, rr, axis, vel, trq, w2t, rpm): + # 功能:完成单个数据文件的处理 + # 参数:如上 + # 返回值:- + if data_file.endswith('.data'): + sep = '\t' + df = read_csv(data_file, sep=sep) + elif data_file.endswith('.csv'): + sep = ',' + df = read_csv(data_file, sep=sep, encoding='gbk', header=8) + + conditions = sorted(data_file.split('\\')[-2].split('_')[1:]) + result_sheet_name = find_result_sheet_name(conditions, count) + ws_result = wb_result[result_sheet_name] + + row_max, row_start = find_row_start(data_file, df, conditions, av, rr, axis, vel, w2t, rpm) + + copy_data_to_result(df, ws_result, row_max, row_start, vel, trq, rpm) + ws_result["C2"] = int(2) + ws_result["G2"] = int(10+4) + + +def copy_data_to_result(df, ws_result, row_max, row_start, vel, trq, rpm): + # 功能:将数据文件中有效数据拷贝至结果文件对应的 sheet + # 参数:如上 + # 返回值:- + # 结果文件数据清零 + for row in ws_result.iter_rows(min_row=2, min_col=1, max_row=2000, max_col=2): + for cell in row: + cell.value = None + + # 将合适的数据复制到结果文件 + row_max = row_start + 399 if row_max-row_start > 400 else row_max + rc = 1 if rpm == 1 else 1000 + + data = [] + for i in range(row_start, row_max+1): + data.append(df.iloc[i, vel-1] * rpm) + data.append(df.iloc[i, trq-1] * rc) + + i = 0 + for row in ws_result.iter_rows(min_row=2, min_col=1, max_row=row_max - row_start + 2, max_col=2): + for cell in row: + cell.value = data[i] + i = i + 1 + + +def find_result_sheet_name(conditions, count): + # 功能:获取结果文件准确的sheet页名称 + # 参数:臂展和速度的列表 + # 返回值:结果文件对应的sheet name + # 33%臂展_33%速度_正1 + reach = conditions[0].removeprefix('reach') + speed = conditions[1].removeprefix('speed') + result_sheet_name = f"{reach}%臂展_{speed}%速度_正{count}" + + return result_sheet_name + + +def find_row_start(data_file, df, conditions, av, rr, axis, vel, w2t, rpm): + # 功能:查找数据文件中有效数据的行号,也即最后一个速度下降的点位 + # 参数:如上 + # 返回值:速度下降点位,最后的数据点位 + ratio = float(conditions[1].removeprefix('speed'))/100 + speed_max = av * ratio * rr / 6 + row_max = row_start = df.index[-1] + + threshold, step = get_threshold_step(data_file, axis) + while row_start > step+1: + speed = df.iloc[row_start, vel-1] * rpm + if int(speed) < 1: + row_start -= 50 + continue + + _ = [] + for i in range(row_start, row_start-step+1, -1): + _.append(df.iloc[i, vel-1] * rpm) + speed_avg = abs(sum(_))/len(_) + + if abs(speed_avg-speed_max) < threshold: + row_start = row_start - 10 + break + else: + row_start -= step + else: + msg = f"可能是{data_file}这个文件数据采集有问题,比如未采集理论速度值,也有可能是程序步长设定问题,请检查......" + w2t(msg) + + return row_max, row_start + + +def get_threshold_step(excel_file, axis): + # 功能:负载和速度100%,且是j2的时候,做特殊处理 + # 参数:新生成的excel,轴号 + # 返回值:速度差阈值,处理步长 + 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 == 2: + threshold = 30 + step = 5 + else: + threshold = 10 + step = 5 + + return threshold, step + + +def traversal_files(path, w2t): + # 功能:以列表的形式分别返回指定路径下的文件和文件夹,不包含子目录 + # 参数:路径 + # 返回值:路径下的文件夹列表 路径下的文件列表 + if not exists(path): + msg = f'数据文件夹{path}不存在,请确认后重试......' + w2t(msg) + else: + dirs = [] + files = [] + for item in scandir(path): + if item.is_dir(): + dirs.append(item.path) + elif item.is_file(): + files.append(item.path) + + return dirs, files + + +def main(path, av, rr, rpm, axis, vel, trq, w2t): # 功能:执行处理所有数据文件 # 参数:initialization函数的返回值 # 返回值:- @@ -307,7 +306,7 @@ def execution(path, av, rr, rpm, axis,vel, trq, w2t): # [t.start() for t in threads] # [t.join() for t in threads] except Exception as Err: - msg = "程序运行错误,请检查配置文件是否准确设定,以及数据文件组织是否正确,也有可能是结果文件损坏,尝试重新复制一份,再运行!" + msg = f"出现错误:{Err}\n程序运行错误,请检查配置文件是否准确设定,以及数据文件组织是否正确,也有可能是结果文件损坏,尝试重新复制一份,再运行!" w2t(msg) w2t("----------------------------------------------------------") @@ -318,10 +317,6 @@ def execution(path, av, rr, rpm, axis,vel, trq, w2t): w2t(msg) -def main(path, av, rr, rpm, axis,vel, trq, w2t): - execution(path, av, rr, rpm, axis,vel, trq, w2t) - - if __name__ == "__main__": stop = 0 main(path=argv[1], av=argv[2], rr=argv[3], rpm=argv[4], axis=argv[5], vel=argv[6], trq=argv[7], w2t=argv[8])