def autonomous_control(model):
    """Run the car autonomously"""
    predictor = Predictor(model)
    with picamera.PiCamera() as camera:
        camera.resolution = configuration.PICAMERA_RESOLUTION
        camera.framerate = configuration.PICAMERA_FRAMERATE
        time.sleep(configuration.PICAMERA_WARM_UP_TIME)
        camera.rotation = 180
        pwm = motor_driver_helper.get_pwm_imstance()
        motor_driver_helper.start_pwm(pwm)
        forward_cycle_count = left_cycle_count = right_cycle_count = 0
        should_brake = False

        while True:
            stream = io.BytesIO()
            camera.capture(stream, format='jpeg', use_video_port=True)
            direction = predictor.predict(stream)
            image_helper.save_image_with_direction(stream, direction)
            stream.flush()
            if direction == 'forward':
                should_brake = True
                left_cycle_count = right_cycle_count = 0
                forward_cycle_count = reduce_speed(pwm, forward_cycle_count)
                motor_driver_helper.set_front_motor_to_idle()
                motor_driver_helper.set_forward_mode()
            elif direction == 'left':
                should_brake = True
                forward_cycle_count = right_cycle_count = 0
                left_cycle_count = increase_speed_on_turn(
                    pwm, left_cycle_count)
                motor_driver_helper.set_left_mode()
                motor_driver_helper.set_forward_mode()
            elif direction == 'right':
                should_brake = True
                forward_cycle_count = left_cycle_count = 0
                right_cycle_count = increase_speed_on_turn(
                    pwm, right_cycle_count)
                motor_driver_helper.set_right_mode()
                motor_driver_helper.set_forward_mode()
            elif direction == 'reverse':
                should_brake = True
                #motor_driver_helper.set_front_motor_to_idle()
                motor_driver_helper.set_right_mode()
                motor_driver_helper.set_reverse_mode()

            else:
                if should_brake:
                    print("braking...")
                    #motor_driver_helper.set_reverse_mode()
                    time.sleep(0.2)
                    should_brake = False
                motor_driver_helper.set_idle_mode()
                forward_cycle_count = left_cycle_count = right_cycle_count = 0
                motor_driver_helper.change_pwm_duty_cycle(pwm, 100)
            print(direction)
def interactive_control():
    """Runs the interactive control"""
    setup_interactive_control()
    clock = pygame.time.Clock()
    with picamera.PiCamera() as camera:
        camera.resolution = configuration.PICAMERA_RESOLUTION
        camera.framerate = configuration.PICAMERA_FRAMERATE
        time.sleep(configuration.PICAMERA_WARM_UP_TIME)
        # GPIO.output(BACK_MOTOR_ENABLE_PIN, True)
        pwm = motor_driver_helper.get_pwm_imstance()
        motor_driver_helper.start_pwm(pwm)
        command = 'idle'
        duty_cycle = configuration.INITIAL_PWM_DUTY_CYCLE
        while True:
            up_key, down, left, right, change, accelerate, decelerate, stop = get_keys(
            )
            if stop:
                break
            if accelerate:
                duty_cycle = duty_cycle + 3 if (duty_cycle +
                                                3) <= 100 else duty_cycle
                motor_driver_helper.change_pwm_duty_cycle(pwm, duty_cycle)
                print("speed: " + str(duty_cycle))
            if decelerate:
                duty_cycle = duty_cycle - 3 if (duty_cycle -
                                                3) >= 0 else duty_cycle
                motor_driver_helper.change_pwm_duty_cycle(pwm, duty_cycle)
                print("speed: " + str(duty_cycle))
            if change:
                command = 'idle'
                motor_driver_helper.set_idle_mode()
                if up_key:
                    command = 'forward'
                    print(duty_cycle)
                    motor_driver_helper.set_forward_mode()
                elif down:
                    command = 'reverse'
                    motor_driver_helper.set_reverse_mode()

                append = lambda x: command + '_' + x if command != 'idle' else x

                if left:
                    command = append('left')
                    motor_driver_helper.set_left_mode()
                elif right:
                    command = append('right')
                    motor_driver_helper.set_right_mode()
            print(command)
            stream = io.BytesIO()
            camera.capture(stream, format='jpeg', use_video_port=True)
            image_helper.save_image_with_direction(stream, command)
            stream.flush()

            clock.tick(30)
        pygame.quit()
def interactive_control():
    """Runs the interactive control"""
    setup_interactive_control()
    clock = pygame.time.Clock()
    with picamera.PiCamera() as camera:
        camera.resolution = (640, 480)
        camera.framerate = 60
        time.sleep(2)
        # GPIO.output(BACK_MOTOR_ENABLE_PIN, True)
        pwm = motor_driver_helper.get_pwm_imstance()
        motor_driver_helper.start_pwm(pwm)
        command = 'idle'
        duty_cycle = configuration.INITIAL_PWM_DUTY_CYCLE
        while True:
            up_key, down, left, right, change, accelerate, decelerate, stop = get_keys()
            if stop:
                break
            if accelerate:
                duty_cycle = duty_cycle + 3 if (duty_cycle + 3) <= 100 else duty_cycle
                motor_driver_helper.change_pwm_duty_cycle(pwm, duty_cycle)
                print("speed: " + str(duty_cycle))
            if decelerate:
                duty_cycle = duty_cycle - 3 if (duty_cycle - 3) >= 0 else duty_cycle
                motor_driver_helper.change_pwm_duty_cycle(pwm, duty_cycle)
                print("speed: " + str(duty_cycle))
            if change:
                command = 'idle'
                motor_driver_helper.set_idle_mode()
                if up_key:
                    command = 'forward'
                    print(duty_cycle)
                    motor_driver_helper.set_forward_mode()
                elif down:
                    command = 'reverse'
                    motor_driver_helper.set_reverse_mode()

                append = lambda x: command + '_' + x if command != 'idle' else x

                if left:
                    command = append('left')
                    motor_driver_helper.set_left_mode()
                elif right:
                    command = append('right')
                    motor_driver_helper.set_right_mode()
            print(command)
            stream = io.BytesIO()
            camera.capture(stream, format='jpeg', use_video_port=True)
            image_helper.save_image_with_direction(stream, command)
            stream.flush()

            clock.tick(30)
        pygame.quit()
예제 #4
0
def autonomous_control(model):
    """Run the car autonomously"""
    predictor = Predictor(model)
    with picamera.PiCamera() as camera:
        camera.resolution = configuration.PICAMERA_RESOLUTION
        camera.framerate = configuration.PICAMERA_FRAMERATE
        time.sleep(configuration.PICAMERA_WARM_UP_TIME)
        pwm = motor_driver_helper.get_pwm_imstance()
        motor_driver_helper.start_pwm(pwm)
        forward_cycle_count = left_cycle_count = right_cycle_count = 0
        should_brake = False

        while True:
            stream = io.BytesIO()
            camera.capture(stream, format='jpeg', use_video_port=True)
            direction = predictor.predict(stream)
            image_helper.save_image_with_direction(stream, direction)
            stream.flush()
            if direction == 'forward':
                should_brake = True
                left_cycle_count = right_cycle_count = 0
                forward_cycle_count = reduce_speed(pwm, forward_cycle_count)
                motor_driver_helper.set_front_motor_to_idle()
                motor_driver_helper.set_forward_mode()
            elif direction == 'left':
                should_brake = True
                forward_cycle_count = right_cycle_count = 0
                left_cycle_count = increase_speed_on_turn(pwm, left_cycle_count)
                motor_driver_helper.set_left_mode()
                motor_driver_helper.set_forward_mode()
            elif direction == 'right':
                should_brake = True
                forward_cycle_count = left_cycle_count = 0
                right_cycle_count = increase_speed_on_turn(pwm, right_cycle_count)
                motor_driver_helper.set_right_mode()
                motor_driver_helper.set_forward_mode()
            elif direction == 'reverse':
                should_brake = True
                motor_driver_helper.set_front_motor_to_idle()
                motor_driver_helper.set_reverse_mode()
            else:
                if should_brake:
                    print("braking...")
                    motor_driver_helper.set_reverse_mode()
                    time.sleep(0.2)
                    should_brake = False
                motor_driver_helper.set_idle_mode()
                forward_cycle_count = left_cycle_count = right_cycle_count = 0
                motor_driver_helper.change_pwm_duty_cycle(pwm, 100)
            print(direction)