From b55433a1f63fb76c11a67e42c32e2e33dc2945a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Gr=C3=BCndinger?= Date: Wed, 3 Jul 2024 15:16:31 +0200 Subject: [PATCH] Refactor file_test.py: Add async functions for module control and obstacle detection --- Code/conf.json | 10 ++++---- Code/file_test.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/Code/conf.json b/Code/conf.json index 6ef14da..e350ee5 100644 --- a/Code/conf.json +++ b/Code/conf.json @@ -1,8 +1,8 @@ { "version": "1.1.2", - "calibrate" : "async def calibrate(speed=1000, acceleration=1000):\n if (distance_sensor.distance(port.C) < 110):\n print('Calibration not possible! | Distance to small!')\n elif (distance_sensor.distance(port.C) > 110):\n try:\n await drive(-1, 14, speed, acceleration)\n distance0 = distance_sensor.distance(port.C)\n await drive(10, 14, speed, acceleration)\n distance1 = distance_sensor.distance(port.C)\n calibration = distance0 - distance1\n #print('Calibration done!')\n print(calibration)\n wait(0.5)\n except:\n print('Calibration not possible! | Error!')", - "module": "async def module(degrees=0, speed=1110, acceleration=10000):\n if (degrees > 0):\n await motor.run_for_degrees(motor_module, degrees, speed, acceleration=acceleration)\n elif (degrees < 0):\n await motor.run_for_degrees(motor_module, degrees, speed, acceleration=acceleration)\n elif (degrees == 0):\n print('Error')", - "motor": "async def drive(distance=0, multiplier=14, speed=1000, acceleration=1000):\n if (distance > 0):\n degrees = multiplier*(distance - calibration)\n await motor_pair.move_for_degrees(pair, degrees, 0, velocity=speed, acceleration=acceleration)\n elif (distance < 0):\n degrees = multiplier*(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\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 \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": "async def switch(switch=False):\n while switch == False:\n if (force_sensor.force(force_module) >= 50):\n switch = True\n return True", - "variables": "pair = 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)" + "calibrate" : "\nasync def calibrate(speed=1000, acceleration=1000):\n if (distance_sensor.distance(port.C) < 110):\n print('Calibration not possible! | Distance to small!')\n elif (distance_sensor.distance(port.C) > 110):\n try:\n await drive(-1, 14, speed, acceleration)\n distance0 = distance_sensor.distance(port.C)\n await drive(10, 14, speed, acceleration)\n distance1 = distance_sensor.distance(port.C)\n calibration = distance0 - distance1\n #print('Calibration done!')\n print(calibration)\n wait(0.5)\n except:\n print('Calibration not possible! | Error!')", + "module": "\nasync def module(degrees=0, speed=1110, acceleration=10000):\n if (degrees > 0):\n await motor.run_for_degrees(motor_module, degrees, speed, acceleration=acceleration)\n elif (degrees < 0):\n await motor.run_for_degrees(motor_module, degrees, speed, acceleration=acceleration)\n elif (degrees == 0):\n print('Error')", + "motor": "\nasync def drive(distance=0, multiplier=14, speed=1000, acceleration=1000):\n if (distance > 0):\n degrees = multiplier*(distance - calibration)\n await motor_pair.move_for_degrees(pair, degrees, 0, velocity=speed, acceleration=acceleration)\n elif (distance < 0):\n degrees = multiplier*(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)" } \ No newline at end of file diff --git a/Code/file_test.py b/Code/file_test.py index 9576cad..d7a0a36 100644 --- a/Code/file_test.py +++ b/Code/file_test.py @@ -5,6 +5,7 @@ import runloop from math import * import random + pair = motor_pair.PAIR_1 motor_pair.pair(pair, port.F, port.B) motor_module = port.E @@ -21,4 +22,64 @@ def write_ai_data(file): with open('{file}', 'w') as f: for line in ai_data: - f.write(line) \ No newline at end of file + f.write(line) +async def module(degrees=0, speed=1110, acceleration=10000): + if (degrees > 0): + await motor.run_for_degrees(motor_module, degrees, speed, acceleration=acceleration) + elif (degrees < 0): + await motor.run_for_degrees(motor_module, degrees, speed, acceleration=acceleration) + elif (degrees == 0): + print('Error') +async def drive(distance=0, multiplier=14, speed=1000, acceleration=1000): + if (distance > 0): + degrees = multiplier*(distance - calibration) + await motor_pair.move_for_degrees(pair, degrees, 0, velocity=speed, acceleration=acceleration) + elif (distance < 0): + degrees = multiplier*(distance + calibration) + await motor_pair.move_for_degrees(pair, degrees, 0, velocity=speed, acceleration=acceleration) + elif (distance == 0): + print('Null Value Error') + + +async def tank(degrees=0, left_speed=1000, right_speed=1000, acceleration=1000): + #180 degrees = 90 Grad + if (degrees > 0): + await motor_pair.move_tank_for_degrees(pair, -degrees, left_speed, -right_speed, acceleration=acceleration) + elif (degrees < 0): + await motor_pair.move_tank_for_degrees(pair, degrees, -left_speed, right_speed, acceleration=acceleration) + elif (degrees == 0): + print('Null Value Error') + + +async def obstacle(distance=0, speed=1000, acceleration=1000): + if (distance > 0): + while distance_sensor.distance(port.C) > distance: + await drive(2, 14, speed, acceleration) + print('Obstacle detected!') + elif (distance <= 0): + print('Null Value Error') +async def switch(switch=False): + while switch == False: + if (force_sensor.force(force_module) >= 50): + switch = True + return True +async def calibrate(speed=1000, acceleration=1000): + if (distance_sensor.distance(port.C) < 110): + print('Calibration not possible! | Distance to small!') + elif (distance_sensor.distance(port.C) > 110): + try: + await drive(-1, 14, speed, acceleration) + distance0 = distance_sensor.distance(port.C) + await drive(10, 14, speed, acceleration) + distance1 = distance_sensor.distance(port.C) + calibration = distance0 - distance1 + #print('Calibration done!') + print(calibration) + wait(0.5) + except: + print('Calibration not possible! | Error!')print('Running main Function') +async def main(): + await drive(10) + await tank(30) + await drive(10) +runloop.run(main())