Skip to content

Commit

Permalink
Refactor file_test.py: Add async functions for module control and obs…
Browse files Browse the repository at this point in the history
…tacle detection
  • Loading branch information
AIIrondev committed Jul 3, 2024
1 parent b87c65d commit b55433a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
10 changes: 5 additions & 5 deletions Code/conf.json
Original file line number Diff line number Diff line change
@@ -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)"
}
63 changes: 62 additions & 1 deletion Code/file_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,4 +22,64 @@
def write_ai_data(file):
with open('{file}', 'w') as f:
for line in ai_data:
f.write(line)
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())

0 comments on commit b55433a

Please sign in to comment.