Compare commits

..

No commits in common. "master" and "feat/distanz" have entirely different histories.

1 changed files with 48 additions and 93 deletions

View File

@ -2,6 +2,7 @@ from hub import port, motion_sensor as ms
import motor_pair as mp
import distance_sensor as ds
import color_sensor as cs
import color
import sys
import runloop
import time
@ -20,12 +21,12 @@ import time
# Productive Code ###########################################################################
async def turn_right(angle=85):
async def turn_right(angle=90):
mp.move_tank(mp.PAIR_1, 200, -200)
await runloop.sleep_ms(int(angle * 7))# 6 ms per degree → tune this!
mp.stop(mp.PAIR_1)
async def turn_left(angle=85):
async def turn_left(angle=90):
mp.move_tank(mp.PAIR_1, -200, 200)
await runloop.sleep_ms(int(angle * 7))
mp.stop(mp.PAIR_1)
@ -37,133 +38,87 @@ async def drive_cm(cm, speed=200):
await runloop.sleep_ms(cm * 70)
mp.stop(mp.PAIR_1)
async def avoid_obstacle(speed=100):
async def avoid_obstacle():
mp.stop(mp.PAIR_1)
await runloop.sleep_ms(500)
mp.move_tank(mp.PAIR_1, -speed, -speed)
mp.move_tank(mp.PAIR_1, -100, -100)
await turn_right(90)
await drive_cm(20)
await turn_left(90)
await drive_cm(40)
await turn_left(90)
await drive_cm(20)
await turn_right(90)
await turn_right(85)
await drive_cm(15)
await turn_left(85)
await drive_cm(30)
await turn_left(85)
await drive_cm(15)
await turn_right(85)
old = 0
async def move_on_color():
won = False
old = time.ticks_ms()
speed = 100
saw_green_left = False
saw_green_right = False
while not won:
while True:
# Farbcodes lesen
left_color = cs.color(port.B)
right_color = cs.color(port.F)
left_color = cs.color(port.C)
right_color = cs.color(port.E)
gradient = ms.tilt_angles()[2]
# Farbcodes
BLACK = 0
GREEN = 6
WHITE = 10
RED = 9
straight = False
turn_way_if_lost_right = True
# Distanzsensor
ds_front = ds.distance(port.D)
if gradient < -120:
speed = -80
elif gradient > 50:
speed = -50
else:
speed = -100
ds_front = ds.distance(port.F)
# --- LOGIK NUR MIT FARBEN ---
# Finish line logik
if left_color == RED or right_color == RED:
won = True
break
# Beide Sensoren sehen Grün (z.B. Kreuzung / Markierung)
if ds_front < 75:
await avoid_obstacle()
elif ds_front < 75 and ds_front > -1:
#await avoid_obstacle()
pass
elif left_color == GREEN and right_color == GREEN:
mp.move_tank(mp.PAIR_1, -100, 100)
time.sleep_ms(2300)
# Linker Sensor sieht Grün → Linksabbiegen
elif left_color == GREEN and right_color != GREEN:
saw_green_left = True
elif right_color == GREEN and left_color != GREEN:
saw_green_right = True
# Beide Sensoren sehen Grün (z.B. Kreuzung / Markierung)
elif left_color == GREEN and right_color == GREEN:
saw_green_right = False
saw_green_left = False
mp.move_tank(mp.PAIR_1, -speed, speed)
time.sleep_ms(2300)
elif saw_green_left and left_color == BLACK:
saw_green_left = False
mp.move_tank(mp.PAIR_1, speed, speed)
mp.move_tank(mp.PAIR_1, 100, 100)
time.sleep_ms(700)
mp.move_tank(mp.PAIR_1, -100, 100)
time.sleep_ms(1200)
mp.move_tank(mp.PAIR_1, speed, -speed)
time.sleep_ms(1050)
mp.move_tank(mp.PAIR_1, speed, speed)
mp.move_tank(mp.PAIR_1, 100, 100)
time.sleep_ms(300)
# Rechter Sensor sieht Grün → Rechtsabbiegen
elif saw_green_right and right_color == BLACK:
saw_green_right = False
mp.move_tank(mp.PAIR_1, speed, speed)
elif right_color == GREEN and left_color != GREEN:
mp.move_tank(mp.PAIR_1, 100, 100)
time.sleep_ms(700)
mp.move_tank(mp.PAIR_1, 100, -100)
time.sleep_ms(1200)
mp.move_tank(mp.PAIR_1, -speed, speed)
time.sleep_ms(1050)
mp.move_tank(mp.PAIR_1, speed, speed)
mp.move_tank(mp.PAIR_1, 100, 100)
time.sleep_ms(300)
elif right_color == BLACK or left_color == BLACK:
if right_color == BLACK and left_color == BLACK:
mp.move_tank(mp.PAIR_1, speed, speed)
time.sleep_ms(20)
# Linker Sensor ist Schwarz, rechter Weiß → sanft nach links
elif left_color == BLACK and right_color == WHITE:
mp.move_tank(mp.PAIR_1, speed, -(int (speed / 4)))
elif left_color == BLACK and right_color == WHITE:
mp.move_tank(mp.PAIR_1, -50, 50)
# Rechter Schwarz, linker Weiß → sanft nach rechts
elif right_color == BLACK and left_color == WHITE:
mp.move_tank(mp.PAIR_1, -(int (speed / 4)), speed)
elif right_color == BLACK and left_color == WHITE:
mp.move_tank(mp.PAIR_1, 50, -50)
# Beide Weiß → geradeaus (vermutlich neben der Linie)
elif left_color == WHITE and right_color == WHITE:
straight = True
mp.move_tank(mp.PAIR_1, speed, speed)
runloop.sleep_ms(50)
mp.move(mp.PAIR_1, 0, velocity=150)
if straight:
if time.ticks_diff(time.ticks_ms(), old) > 2750:
old = time.ticks_ms()
if turn_way_if_lost_right:
mp.move_tank(mp.PAIR_1, speed, 0)
await runloop.sleep_ms(200)
turn_way_if_lost_right = not turn_way_if_lost_right
else:
mp.move_tank(mp.PAIR_1, 0, speed)
await runloop.sleep_ms(200)
turn_way_if_lost_right = not turn_way_if_lost_right
# TODO: ---- Weiß auf beiden Seiten = Linie verloren ----
# wenn links == weiß UND rechts == weiß:
# langsam drehen bis schwarz gefunden
else:
old = time.ticks_ms()
elif gradient < -150:
mp.move(mp.PAIR_1, 0, velocity=350)
elif gradient > 50:
mp.move(mp.PAIR_1, 0, velocity=100)
async def main():
mp.pair(mp.PAIR_1, port.A, port.E)
mp.pair(mp.PAIR_1, port.A, port.D)
await move_on_color()
sys.exit()
runloop.run(main())
runloop.run(main())