import line_follower
import time
import light

xunhuan = None

def 直行检测右路口右转():
    global xunhuan
    xunhuan = 1
    while xunhuan == 1:
        我的巡线1.follow()
        if 我的巡线1.check_crossing('right'):
            xunhuan = 0
    我的巡线1.stop(270)
    line_follower.turn_in_crossing('right', 1, turn_sensor_thr=1400)
    time.sleep(0.2)
    我的巡线1.go_on()

def 直行中检测左路口左转():
    global xunhuan
    xunhuan = 1
    while xunhuan == 1:
        我的巡线1.follow()
        if 我的巡线1.check_crossing('left'):
            xunhuan = 0
    我的巡线1.stop(270)
    line_follower.turn_in_crossing('left', 1, turn_sensor_thr=1400)
    time.sleep(0.2)
    我的巡线1.go_on()

def 直行检测右路口越过():
    global xunhuan
    xunhuan = 1
    while xunhuan == 1:
        我的巡线1.follow()
        if 我的巡线1.check_crossing('right'):
            xunhuan = 0
    我的巡线1.stop(300)
    time.sleep(0.2)
    我的巡线1.go_on()

def 直行检测左路口越过():
    global xunhuan
    xunhuan = 1
    while xunhuan == 1:
        我的巡线1.follow()
        if 我的巡线1.check_crossing('left'):
            xunhuan = 0
    我的巡线1.stop(300)
    time.sleep(0.2)
    我的巡线1.go_on()

def 直行中越过十字路口():
    global xunhuan
    xunhuan = 1
    while xunhuan == 1:
        我的巡线1.follow()
        if light.get_optical_sensor(1) < 1000 and light.get_optical_sensor(6) < 1000:
            xunhuan = 0
    我的巡线1.stop(300)
    time.sleep(0.2)
    我的巡线1.go_on()

def 直行中十字路口左转():
    global xunhuan
    xunhuan = 1
    while xunhuan == 1:
        我的巡线1.follow()
        if light.get_optical_sensor(1) < 1000 and light.get_optical_sensor(6) < 1000:
            xunhuan = 0
    我的巡线1.stop(270)
    line_follower.turn_in_crossing('left', 1, turn_sensor_thr=1400)
    time.sleep(0.2)
    我的巡线1.go_on()

def 直行中十字路口右转():
    global xunhuan
    xunhuan = 1
    while xunhuan == 1:
        我的巡线1.follow()
        if light.get_optical_sensor(1) < 1000 and light.get_optical_sensor(6) < 1000:
            xunhuan = 0
    我的巡线1.stop(270)
    line_follower.turn_in_crossing('right', 1, turn_sensor_thr=1400)
    time.sleep(0.2)
    我的巡线1.go_on()

def 直行中检测T路口左转():
    global xunhuan
    xunhuan = 1
    while xunhuan == 1:
        我的巡线1.follow()
        if light.get_optical_sensor(1) < 1000 and light.get_optical_sensor(6) < 1000:
            xunhuan = 0
    我的巡线1.stop(270)
    line_follower.turn_in_crossing('left', 1, turn_sensor_thr=1400)
    time.sleep(0.2)
    我的巡线1.go_on()

def 直行中检测T路口右转():
    global xunhuan
    xunhuan = 1
    while xunhuan == 1:
        我的巡线1.follow()
        if light.get_optical_sensor(1) < 1000 and light.get_optical_sensor(6) < 1000:
            xunhuan = 0
    我的巡线1.stop(270)
    line_follower.turn_in_crossing('right', 1, turn_sensor_thr=1400)
    time.sleep(0.2)
    我的巡线1.go_on()


我的巡线1 = line_follower.LineFollower(base_speed=60, pid_p=5, pid_i=0, pid_d=0.4, pid_imax=50, in_line_diff_thr=600, in_line_sum_thr=3000, L_crossing_thr=1500, T_crossing_thr=5000, pass_crossing_time=300, pass_T_crossing_thr=70)
