Better commenting

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

28
code.py
View file

@ -29,6 +29,7 @@ def backward():
m3.throttle = -speed
m4.throttle = -speed
# define strafing
def stleft():
m1.throttle = -speed
m2.throttle = speed
@ -41,6 +42,7 @@ def stright():
m3.throttle = -speed
m4.throttle = speed
# define diagonal movement
def dgright():
m1.throttle =speed
m2.throttle = 0
@ -53,6 +55,7 @@ def dgleft():
m3.throttle =speed
m4.throttle = 0
# define turning/steering
def turnright():
m1.throttle = speed
m2.throttle = speed
@ -65,6 +68,7 @@ def turnleft():
m3.throttle = 0
m4.throttle = speed
#define rotating
def rotright():
m1.throttle = speed
m2.throttle = - speed
@ -77,10 +81,12 @@ def rotleft():
m3.throttle = 0
m4.throttle = 0
# Main control loop
while True:
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:
if front.distance <= 35: # What to do when there is an obstacle ahead
if front.distance <= 35:
if left.distance > right.distance: # Diagonally left
dgleft()
time.sleep(0.8)
@ -88,38 +94,38 @@ while True:
dgright()
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()
time.sleep(0.7)
if left.distance > right.distance: # Diagonally left
if left.distance > right.distance:
rotleft()
time.sleep(0.5)
if right.distance > left.distance: # Diagonally right
if right.distance > left.distance:
rotright()
time.sleep(0.5)
if left.distance < 50: # Strafe right
if left.distance < 50: # Steer right
turnright()
time.sleep(0.4)
if right.distance < 50: # Strafe left
if right.distance < 50: # Steer left
turnleft()
time.sleep(0.4)
if left.distance < 15: # Strafe right
#turnright()
# When the robot's side gets dangerously close it moves diagonally and the rotates a bit to escape
if left.distance < 15:
dgright()
time.sleep(0.5)
rotright()
time.sleep(0.4)
if right.distance < 15: # Strafe left
#turnleft()
if right.distance < 15:
dgleft()
time.sleep(0.5)
rotleft()
time.sleep(0.4)
else:
else: # If no obstacle is seen, keep going forward
forward()
except RuntimeError: