Better commenting

This commit is contained in:
Γιώργος Αντρέου 2026-07-09 00:37:19 +03:00
parent ca16f2a746
commit 48d9907b39

30
code.py
View file

@ -29,6 +29,7 @@ def backward():
m3.throttle = -speed m3.throttle = -speed
m4.throttle = -speed m4.throttle = -speed
# define strafing
def stleft(): def stleft():
m1.throttle = -speed m1.throttle = -speed
m2.throttle = speed m2.throttle = speed
@ -41,6 +42,7 @@ def stright():
m3.throttle = -speed m3.throttle = -speed
m4.throttle = speed m4.throttle = speed
# define diagonal movement
def dgright(): def dgright():
m1.throttle =speed m1.throttle =speed
m2.throttle = 0 m2.throttle = 0
@ -53,6 +55,7 @@ def dgleft():
m3.throttle =speed m3.throttle =speed
m4.throttle = 0 m4.throttle = 0
# define turning/steering
def turnright(): def turnright():
m1.throttle = speed m1.throttle = speed
m2.throttle = speed m2.throttle = speed
@ -65,6 +68,7 @@ def turnleft():
m3.throttle = 0 m3.throttle = 0
m4.throttle = speed m4.throttle = speed
#define rotating
def rotright(): def rotright():
m1.throttle = speed m1.throttle = speed
m2.throttle = - speed m2.throttle = - speed
@ -77,10 +81,12 @@ def rotleft():
m3.throttle = 0 m3.throttle = 0
m4.throttle = 0 m4.throttle = 0
# Main control loop
while True: while True:
print("F=", f"{front.distance:.2f}", "R=", f"{right.distance:.2f}", "L=", f"{left.distance:.2f}") print("F=", f"{front.distance:.2f}", "R=", f"{right.distance:.2f}", "L=", f"{left.distance:.2f}")
# The obstacle distance thresholds can be set however you want. I set them with the values I found to be optimal in folkrace
try: try:
if front.distance <= 35: # What to do when there is an obstacle ahead if front.distance <= 35:
if left.distance > right.distance: # Diagonally left if left.distance > right.distance: # Diagonally left
dgleft() dgleft()
time.sleep(0.8) time.sleep(0.8)
@ -88,38 +94,38 @@ while True:
dgright() dgright()
time.sleep(0.8) time.sleep(0.8)
if front.distance < 10: if front.distance < 10: # Robot gets dangerously close to an obstacle or even hits it
# The robot will move backwards and rotare to the direction with the most space
backward() backward()
time.sleep(0.7) time.sleep(0.7)
if left.distance > right.distance: # Diagonally left if left.distance > right.distance:
rotleft() rotleft()
time.sleep(0.5) time.sleep(0.5)
if right.distance > left.distance: # Diagonally right if right.distance > left.distance:
rotright() rotright()
time.sleep(0.5) time.sleep(0.5)
if left.distance < 50: # Strafe right if left.distance < 50: # Steer right
turnright() turnright()
time.sleep(0.4) time.sleep(0.4)
if right.distance < 50: # Strafe left if right.distance < 50: # Steer left
turnleft() turnleft()
time.sleep(0.4) time.sleep(0.4)
if left.distance < 15: # Strafe right # When the robot's side gets dangerously close it moves diagonally and the rotates a bit to escape
#turnright() if left.distance < 15:
dgright() dgright()
time.sleep(0.5) time.sleep(0.5)
rotright() rotright()
time.sleep(0.4) time.sleep(0.4)
if right.distance < 15: # Strafe left if right.distance < 15:
#turnleft()
dgleft() dgleft()
time.sleep(0.5) time.sleep(0.5)
rotleft() rotleft()
time.sleep(0.4) time.sleep(0.4)
else: else: # If no obstacle is seen, keep going forward
forward() forward()
except RuntimeError: except RuntimeError: