예제 #1
0
    def run(self):
        self.robot.set_pan(0)
        self.robot.set_tilt(0)
        camera = camera_stream.setup_camera()

        speed_pid = PIController(proportional_constant=0.8,
            integral_constant=0.1 , windup_limit=100)
        direction_pid = PIController(proportional_constant=0.25,
            integral_constant=0.05, windup_limit=400)

        time.sleep(0.1)
        self.robot.servos.stop_all()
        print("Setup Complete")
        print('Radius, Radius error, speed value, direction error, direction value')
        for frame in camera_stream.start_stream(camera):
            (x, y), radius = self.process_frame(frame)

            self.process_control()
            if self.running and radius > 20:
                radius_error = self.correct_radius - radius
                speed_value = speed_pid.get_value(radius_error)
                direction_error = self.center - x
                direction_value = direction_pid.get_value(direction_error)

                print(f"{radius}, {radius_error}, {speed_value:.2f}, {direction_error}, {direction_value:.2f}")
                # Now produce left and right motor speeds
                self.robot.set_left(speed_value - direction_value)
                self.robot.set_right(speed_value + direction_value)
            else:
                self.robot.stop_motors()
                if not self.running:
                    speed_pid.reset()
                    direction_pid.reset()
예제 #2
0
def frame_generator():
    """This is our main video feed"""
    camera = camera_stream.setup_camera()

    # allow the camera to warm up
    time.sleep(0.1)
    for frame in camera_stream.start_stream(camera):
        encoded_bytes = camera_stream.get_encoded_bytes_for_frame(frame)
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + encoded_bytes + b'\r\n')
예제 #3
0
def controlled_image_server_behavior():
    camera = camera_stream.setup_camera()
    time.sleep(0.1)

    for frame in camera_stream.start_stream(camera):
        encoded_bytes = camera_stream.get_encoded_bytes_for_frame(frame)
        put_output_image(encoded_bytes)

        instruction = get_control_instruction()
        if instruction and instruction['command'] == "exit":
                print("Stopping")
                return
예제 #4
0
 def run(self):
     camera = camera_stream.setup_camera()
     time.sleep(0.1)
     print("Setup Complete")
     for frame in camera_stream.start_stream(camera):
         (x, y, w, h) = self.process_frame(frame)
         self.process_control()
         if self.running and h > self.min_size:
             pan_error = self.center_x - (x + (w / 2))
             pan_value = self.pan_pid.get_value(pan_error)
             self.robot.set_pan(int(pan_value))
             tilt_error = self.center_y - (y + (h / 2))
             tilt_value = self.tilt_pid.get_value(tilt_error)
             self.robot.set_tilt(int(tilt_value))
             print(
                 f"x: {x}, y: {y}, pan_error: {pan_error}, tilt_error: {tilt_error}, pan_value: {pan_value:.2f}, tilt_value: {tilt_value:.2f}"
             )
예제 #5
0
    def run(self):
        # Set pan and tilt to middle, then clear it.
        self.robot.set_pan(0)
        self.robot.set_tilt(0)
        # start camera
        camera = camera_stream.setup_camera()
        # warm up and servo move time
        time.sleep(0.1)
        # Servo's will be in place - stop them for now.
        self.robot.servos.stop_all()
        print("Setup Complete")

        # Main loop
        for frame in camera_stream.start_stream(camera):
            self.make_display(frame)
            self.process_control()
            # Auto stop
            if time.time() > self.last_time + TIMEOUT_IN:
                self.robot.stop_motors()
예제 #6
0
    def run(self):
        self.robot.set_pan(0)
        self.robot.set_tilt(90)
        camera = camera_stream.setup_camera()
        direction_pid = PIController(proportional_constant=0.4,
                                     integral_constant=0.01,
                                     windup_limit=400)

        time.sleep(1)
        self.robot.servos.stop_all()
        print("Setup Complete")
        last_time = time.time()
        for frame in camera_stream.start_stream(camera):
            x, magnitude = self.process_frame(frame)
            self.process_control()
            if self.running and magnitude > self.diff_threshold:
                direction_error = self.center - x
                new_time = time.time()
                dt = new_time - last_time
                direction_value = direction_pid.get_value(direction_error,
                                                          delta_time=dt)
                last_time = new_time

                print(
                    f"Error: {direction_error}, Value:{direction_value:2f}, t: {new_time}"
                )
                # self.last_error = direction_error
                # self.last_value = direction_value
                # speed = self.speed
                # speed -= abs(direction_value) / 3
                self.robot.set_left(self.speed - direction_value)
                self.robot.set_right(self.speed + direction_value)
            else:
                self.robot.stop_motors()
                self.running = False
                direction_pid.reset()
                last_time = time.time()