From cc58cf9925255e0e42466d8a454c6803c606d75c Mon Sep 17 00:00:00 2001 From: Maximilian Gruendinger Date: Sun, 28 Jul 2024 22:28:03 +0200 Subject: [PATCH] refactor: Remove unused code and improve switch function --- Code/compiler_gui.py | 55 +++++++++++++++++++++++++++++++++++++------- Code/conf.json | 3 ++- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/Code/compiler_gui.py b/Code/compiler_gui.py index 0f318ff..5afe6b9 100644 --- a/Code/compiler_gui.py +++ b/Code/compiler_gui.py @@ -11,6 +11,7 @@ # Variables content_compile = [] +last_function = "" file_name = "" conf_file = "conf.json" __version__ = "0.0.0" @@ -26,6 +27,7 @@ sensor = content["sensor"] variables = content["variables"] ai = content["ai"] + switch = content["switch"] # Functions def compile(file): @@ -103,24 +105,59 @@ def write_function(function,file,value=False): case "variable.init": for line in variables: f.write(line) + case "switch.init": + for line in switch: + f.write(line) case "drive": - f.write(f"\n await drive({value})\n") + if last_function == "main.init" or last_function == "generate_ab" or last_function == "module" or last_function == "drive" or last_function == "tank" or last_function == "obstacle" or last_function == "ai.run" or last_function == "calibrate" : + f.write(f"\n await drive({value})\n") + elif last_function == "switch": + f.write(f"\n await drive({value})\n") case "tank": - f.write(f"\n await tank({value})\n") + if last_function == "main.init" or last_function == "generate_ab" or last_function == "module" or last_function == "drive" or last_function == "tank" or last_function == "obstacle" or last_function == "ai.run" or last_function == "calibrate" : + f.write(f"\n await tank({value})\n") + elif last_function == "switch": + f.write(f"\n await tank({value})\n") case "obstacle": - f.write(f"\n await obstacle({value})\n") + if last_function == "main.init" or last_function == "generate_ab" or last_function == "module" or last_function == "drive" or last_function == "tank" or last_function == "obstacle" or last_function == "ai.run" or last_function == "calibrate" : + f.write(f"\n await obstacle({value})\n") + elif last_function == "switch": + f.write(f"\n await obstacle({value})\n") case "module": - f.write(f"\n await module({value})\n") + if last_function == "main.init" or last_function == "generate_ab" or last_function == "module" or last_function == "drive" or last_function == "tank" or last_function == "obstacle" or last_function == "ai.run" or last_function == "calibrate" : + f.write(f"\n await module({value})\n") + elif last_function == "switch": + f.write(f"\n await module({value})\n") case "calibrate": - f.write("\n await calibrate()\n") + if last_function == "main.init" or last_function == "generate_ab" or last_function == "module" or last_function == "drive" or last_function == "tank" or last_function == "obstacle" or last_function == "ai.run" or last_function == "calibrate" : + f.write("\n await calibrate()\n") + elif last_function == "switch": + f.write("\n await calibrate()\n") case "main.init": f.write("\nasync def main():") case "main.run": f.write("\nrunloop.run(main())") + case "switch": + if last_function == "main.init" or last_function == "generate_ab" or last_function == "module" or last_function == "drive" or last_function == "tank" or last_function == "obstacle" or last_function == "ai.run" or last_function == "calibrate" : + f.write(" if await switch():") + elif last_function == "switch": + pass + case "call": + if last_function == "main.init" or last_function == "generate_ab" or last_function == "module" or last_function == "drive" or last_function == "tank" or last_function == "obstacle" or last_function == "ai.run" or last_function == "calibrate" : + f.write(f"\n await{value}()") + elif last_function == "switch": + f.write(f"\n await{value}()") + case "generate_ab": + f.write(f"\nasync def({value}):") # async dev (value) <- function_name() case "ai.run": - f.write(f"\n new_data_point = {value}") # {'Kalibrierung': calibration, 'Batterieladestand': 85, 'Reifennutzung': 0.95} - f.write("\n calibration = knn_predict(data, new_data_point, k=3)") - f.write("\n print(f'Vorhergesagte Multiplikation: {calibration}')") + if last_function == "main.init" or last_function == "generate_ab" or last_function == "module" or last_function == "drive" or last_function == "tank" or last_function == "obstacle" or last_function == "ai.run" or last_function == "calibrate" : + f.write(f"\n new_data_point = {value}") # {'Kalibrierung': calibration, 'Batterieladestand': 85, 'Reifennutzung': 0.95} + f.write("\n calibration = knn_predict(data, new_data_point, k=3)") + f.write("\n print(f'Vorhergesagte Multiplikation: {calibration}')") + elif last_function == "switch": + f.write(f"\n new_data_point = {value}") # {'Kalibrierung': calibration, 'Batterieladestand': 85, 'Reifennutzung': 0.95} + f.write("\n calibration = knn_predict(data, new_data_point, k=3)") + f.write("\n print(f'Vorhergesagte Multiplikation: {calibration}')") case _: if function == "//": f.write(f"# {value}") @@ -129,6 +166,7 @@ def write_function(function,file,value=False): else: print(f"Error: The function {function} does not exist.") sys.exit(1) + last_function = function def debug_function(function,value=False): """ @@ -184,6 +222,7 @@ def debug_function(function,value=False): else: print(f"Error: The function {function} does not exist.") sys.exit(1) + last_function = function def compile_llsp3(file, directory, project_name): diff --git a/Code/conf.json b/Code/conf.json index 60c0fe6..d5ca054 100644 --- a/Code/conf.json +++ b/Code/conf.json @@ -5,5 +5,6 @@ "motor": "\nasync def drive(distance=0, multiplier=14, speed=1000, acceleration=1000):\n if (distance > 0):\n degrees = (distance * calibration)\n await motor_pair.move_for_degrees(pair, degrees, 0, velocity=speed, acceleration=acceleration)\n elif (distance < 0):\n degrees = distance * calibration\n await motor_pair.move_for_degrees(pair, degrees, 0, velocity=speed, acceleration=acceleration)\n elif (distance == 0):\n print('Null Value Error')\n\n\nasync def tank(degrees=0, left_speed=1000, right_speed=1000, acceleration=1000):\n #180 degrees = 90 Grad\n if (degrees > 0):\n await motor_pair.move_tank_for_degrees(pair, -degrees, left_speed, -right_speed, acceleration=acceleration)\n elif (degrees < 0):\n await motor_pair.move_tank_for_degrees(pair, degrees, -left_speed, right_speed, acceleration=acceleration)\n elif (degrees == 0):\n print('Null Value Error')\n \n\nasync def obstacle(distance=0, speed=1000, acceleration=1000):\n if (distance > 0):\n while distance_sensor.distance(port.C) > distance:\n await drive(2, 14, speed, acceleration)\n print('Obstacle detected!')\n elif (distance <= 0):\n print('Null Value Error')", "sensor": "\nasync def switch(switch=False):\n while switch == False:\n if (force_sensor.force(force_module) >= 50):\n switch = True\n return True", "variables": "\npair = motor_pair.PAIR_1\nmotor_pair.pair(pair, port.F, port.B)\nmotor_module = port.E\nmotor_module1 = port.A\nforce_module = port.D\ncalibration = 1\naverage = 111\ntimes = 1\ntimes1 = 1\naverage_calibration = []\naverage_obs = []\nai_data = []\n\ndef write_ai_data(file):\n with open('{file}', 'w') as f:\n for line in ai_data:\n f.write(line)", - "ai": "\n\ndata = [\n {'Calibration': 1.0, 'Akku': 100, 'Wheelusage': 1.0, 'Multiplication': 1.00},\n {'Calibration': 1.0, 'Akku': 900, 'Wheelusage': 0.9, 'Multiplication': 1.10},\n {'Calibration': 1.0, 'Akku': 800, 'Wheelusage': 0.8, 'Multiplication': 1.25},\n {'Calibration': 0.9, 'Akku': 100, 'Wheelusage': 1.0, 'Multiplication': 1.10},\n {'Calibration': 1.1, 'Akku': 100, 'Wheelusage': 1.0, 'Multiplication': 0.95}\n]\n\ndef euclidean_distance(point1, point2):\n multiplication = 0.0\n for key in point1:\n if key != 'Multiplication':\n multiplication += (point1[key] - point2[key]) ** 2\n return math.sqrt(multiplication)\n\ndef knn_predict(data, new_data_point, k=3):\n multiplication = []\n for item in data:\n dist = euclidean_distance(new_data_point, item)\n multiplication.append((dist, item['Multiplication']))\n\n multiplication.sort(key=lambda x: x[0])\n neighbors = multiplication[:k]\n\n total_multiplication = sum(neighbor[1] for neighbor in neighbors)\n predicted_multiplication = total_multiplication / k\n\n return predicted_multiplication\n" + "ai": "\n\ndata = [\n {'Calibration': 1.0, 'Akku': 100, 'Wheelusage': 1.0, 'Multiplication': 1.00},\n {'Calibration': 1.0, 'Akku': 900, 'Wheelusage': 0.9, 'Multiplication': 1.10},\n {'Calibration': 1.0, 'Akku': 800, 'Wheelusage': 0.8, 'Multiplication': 1.25},\n {'Calibration': 0.9, 'Akku': 100, 'Wheelusage': 1.0, 'Multiplication': 1.10},\n {'Calibration': 1.1, 'Akku': 100, 'Wheelusage': 1.0, 'Multiplication': 0.95}\n]\n\ndef euclidean_distance(point1, point2):\n multiplication = 0.0\n for key in point1:\n if key != 'Multiplication':\n multiplication += (point1[key] - point2[key]) ** 2\n return math.sqrt(multiplication)\n\ndef knn_predict(data, new_data_point, k=3):\n multiplication = []\n for item in data:\n dist = euclidean_distance(new_data_point, item)\n multiplication.append((dist, item['Multiplication']))\n\n multiplication.sort(key=lambda x: x[0])\n neighbors = multiplication[:k]\n\n total_multiplication = sum(neighbor[1] for neighbor in neighbors)\n predicted_multiplication = total_multiplication / k\n\n return predicted_multiplication\n", + "switch": "\nasync def switch(switch=False):\n while switch == False:\n if (force_sensor.force(force_module) >= 50):\n switch = True\n return True" } \ No newline at end of file