Exemplo n.º 1
0
def main():
    tag = TagRecognition()
    bt = Bluetoothctl()
    BT_ADDR = "5C:BA:37:26:6D:9A"

    # If an ARTag is detected, the vehicle will become a follower.
    # If an ARTag is not detected, the vehicle will become a leader.
    if tag.detect():
        vehicle = Follower()
    else:
        if not is_controller_connected():
            # If a controller is not connected, remove it to avoid problems
            # connecting with it again.
            disconnect_and_remove_device(bt, BT_ADDR)
            bt.start_scan()

            while not is_controller_connected():
                bt.connect(BT_ADDR)
                sleep(Follower.CYCLE_TIME)

        vehicle = Leader()

    # This loop makes the vehicle move. If the vehicle sees an ARTag then
    # it is a follower vehicle, otherwise it is a leader vehicle.
    timer_set = False
    start_time = time.time()

    while True:
        tag_visible = tag.detect()
        if isinstance(vehicle, Leader):
            vehicle.lead()

            if tag_visible:
                while is_controller_connected():
                    bt.disconnect('5C:BA:37:26:6D:9A')
                    sleep(Follower.CYCLE_TIME)
                vehicle = Follower()
        else:
            vehicle.follow()
            if tag_visible:
                timer_set = False
            elif not timer_set:
                start_time = time.time()
                timer_set = True

            if (time.time() - start_time) > 5 and timer_set:
                timer_set = False

                disconnect_and_remove_device(bt,BT_ADDR)
                bt.start_scan()
                bt.connect('5C:BA:37:26:6D:9A')
                sleep(Follower.CYCLE_TIME)

                if is_controller_connected():
                    vehicle = Leader()
                else:
                    start_time = time.time()
                    timer_set = True