master
Cedric Hermann 2025-11-20 20:12:01 +01:00
parent a80071a0df
commit b75bcda25a
1 changed files with 29 additions and 10 deletions

View File

@ -52,25 +52,34 @@ async def avoid_obstacle():
await turn_right(90)
async def move_on_color():
while True:
won = False
old = time.ticks_ms
while not won:
# Farbcodes lesen
left_color = cs.color(port.C)
right_color = cs.color(port.E)
gradient = ms.tilt_angles()[2]
gradient = ms.tilt_angles()[2]
# Farbcodes
BLACK = 0
GREEN = 6
WHITE = 10
RED = 9
strait = False
# Distanzsensor
ds_front = ds.distance(port.F)
# --- LOGIK NUR MIT FARBEN ---
# Beide Sensoren sehen Grün (z.B. Kreuzung / Markierung)
if ds_front < 75:
# Finish line logik
if left_color == RED or right_color == RED:
won = True
break
elif ds_front < 75:
await avoid_obstacle()
# Beide Sensoren sehen Grün (z.B. Kreuzung / Markierung)
elif left_color == GREEN and right_color == GREEN:
mp.move_tank(mp.PAIR_1, -100, 100)
time.sleep_ms(2300)
@ -80,7 +89,7 @@ async def move_on_color():
mp.move_tank(mp.PAIR_1, 100, 100)
time.sleep_ms(700)
mp.move_tank(mp.PAIR_1, -100, 100)
time.sleep_ms(1200)
time.sleep_ms(1050)
mp.move_tank(mp.PAIR_1, 100, 100)
time.sleep_ms(300)
@ -89,10 +98,12 @@ async def move_on_color():
mp.move_tank(mp.PAIR_1, 100, 100)
time.sleep_ms(700)
mp.move_tank(mp.PAIR_1, 100, -100)
time.sleep_ms(1200)
time.sleep_ms(1050)
mp.move_tank(mp.PAIR_1, 100, 100)
time.sleep_ms(300)
elif right_color == BLACK and left_color == BLACK:
mp.move_tank(mp.PAIR_1, 100, 100)
# Linker Sensor ist Schwarz, rechter Weiß → sanft nach links
elif left_color == BLACK and right_color == WHITE:
mp.move_tank(mp.PAIR_1, -50, 50)
@ -103,17 +114,25 @@ async def move_on_color():
# Beide Weiß → geradeaus (vermutlich neben der Linie)
elif left_color == WHITE and right_color == WHITE:
strait = True
mp.move(mp.PAIR_1, 0, velocity=150)
# TODO: ---- Weiß auf beiden Seiten = Linie verloren ----
# wenn links == weiß UND rechts == weiß:
# langsam drehen bis schwarz gefunden
# langsam drehen bis schwarz gefunden
elif gradient < -150:
mp.move(mp.PAIR_1, 0, velocity=350)
elif gradient > 50:
mp.move(mp.PAIR_1, 0, velocity=100)
mp.move(mp.PAIR_1, 0, velocity=100)
if strait:
if time.ticks_diff(time.ticks_ms(), old) > 2000:
old = time.ticks_ms
mp.move_tank(mp.PAIR_1, 100, 50)
else:
old = time.ticks_ms
async def main():
mp.pair(mp.PAIR_1, port.A, port.D)
@ -121,4 +140,4 @@ async def main():
sys.exit()
runloop.run(main())
runloop.run(main())