コード例 #1
0
ファイル: drive.py プロジェクト: brhanu/self_driving_car
class Car(object):
    """docstring for Car"""
    def __init__(self, control):
        super(Car, self).__init__()
        self.live_stream = None
        self.camera = PiVideoStream(resolution=(320, 240), framerate=16)
        self.control = control
        self.driver = HumanDriver() 
    
    def exit(self):
        self.camera.stop()
        self.control.stop()
        if self.live_stream:
            self.live_stream.stop()
        print('exit')

    def start(self):
        i2c.setup(mode_motors=3)
        self.control.start()
        self.camera.start()

    @get_sensors
    @record # inputs (camera + sensor) and output
    def drive(self, img, sensors):
        if self.live_stream:
            self.live_stream.send(frame=img, sensors=sensors)

        command = self.control.read()

        if command == 'quit':
            self.exit()
            sys.exit(1)
        elif command == 'stream':
            try:
                if not self.live_stream:
                    self.live_stream = LiveStreamClient().start()
            except Exception as e:
                print('live_stream', e)
        elif command == 'stop':
            i2c.stop()
        
        if command == 'auto_logic_based':
            if not isinstance(self.driver, LogicBasedDriver):
                self.driver = LogicBasedDriver()
        elif command == 'auto_neural_network':
            if not isinstance(self.driver, AutonomousDriver):
                ai = AutonomousDriver().start()
                ai.learn()
                self.driver = ai # utonomousDriver().start()
        else:
            self.driver = HumanDriver()
            pass # human control

        speed, turn = self.driver.action(command, img)

        i2c.set_speed(speed)
        i2c.set_turn(turn)

        # CONSTRAINTS
        for sonar in i2c.SONARS:
            if sonar == i2c.I2C_SONAR_2:
                continue
            if sensors.get(str(sonar), [30])[0] < 20:
                i2c.stop()
        
        return {'command': command, 'speed': speed, 'turn': turn}
コード例 #2
0
    motor_x.stop()
    motor_y.stop()
    sys.exit(0)


# Run this file to start
if __name__ == '__main__':
    # Set up QApplication
    app = QApplication(sys.argv)

    # Set up gui
    main_window = MainWindow()

    # Set up camera video stream
    vs = PiVideoStream(resolution=RESOLUTION, framerate=FRAME_RATE, use_video_port=USE_VIDEO_PORT)
    vs.start()

    # Set up Stepper motor driver
    pio = pigpio.pi()
    motor_x = Stepper(pio, MM_PER_STEP, NEN_pin=X_NEN_pin, DIR_pin=X_DIR_pin, STP_pin=X_STP_pin)
    motor_y = Stepper(pio, MM_PER_STEP, NEN_pin=Y_NEN_pin, DIR_pin=Y_DIR_pin, STP_pin=Y_STP_pin)

    # Set up well position controller and evaluators
    target_coordinates = (0, 0)  # to be determined
    e1 = (WellBottomFeaturesEvaluator(RESOLUTION, ENABLE_DEBUG_MODE_EVALUATOR, qtui=main_window), 1)
    # e2 = (HoughTransformEvaluator(RESOLUTION, ENABLE_DEBUG_MODE), 1)
    wpc = WellPositionController(SETPOINTS_FILE,
                                 MAX_ALLOWED_ERROR_MM,
                                 motor_x,
                                 motor_y,
                                 MM_PER_PIXEL,