Ejemplo n.º 1
0
    def run(self):
        self.running = True
        while self.running:
            # read framebuffer
            # rospy.loginfo("try to read fb...")
            fb = pyopenmv.fb_dump()

            if fb is None:
                continue

            w, h, data = fb
            # create Image message
            img = Image()
            img.header.stamp = rospy.Time.now()
            img.header.frame_id = "openmv_cam"
            img.width = w
            img.height = h
            img.data = list(data.flat[0:])
            img.step = w * 3
            img.encoding = "rgb8"
            # publish it
            self.pub_image.publish(img)
Ejemplo n.º 2
0
# Set higher timeout after connecting for lengthy transfers.
pyopenmv.set_timeout(1*2) # SD Cards can cause big hicups.
pyopenmv.stop_script()
pyopenmv.enable_fb(True)
pyopenmv.exec_script(script)

# init screen
running = True
Clock = pygame.time.Clock()
font = pygame.font.SysFont("monospace", 15)

while running:
    Clock.tick(100)

    # read framebuffer
    fb = pyopenmv.fb_dump()
    if fb != None:
        # create image from RGB888
        image = pygame.image.frombuffer(fb[2].flat[0:], (fb[0], fb[1]), 'RGB')
        # TODO check if res changed
        screen = pygame.display.set_mode((fb[0], fb[1]), pygame.DOUBLEBUF, 32)

        fps = Clock.get_fps()
        # blit stuff
        screen.blit(image, (0, 0))
        screen.blit(font.render("FPS %.2f"%(fps), 1, (255, 0, 0)), (0, 0))

        # update display
        pygame.display.flip()

    for event in pygame.event.get():
Ejemplo n.º 3
0
# Set higher timeout after connecting for lengthy transfers.
pyopenmv.set_timeout(1*2) # SD Cards can cause big hicups.
pyopenmv.stop_script()
pyopenmv.enable_fb(True)
pyopenmv.exec_script(script)

# init screen
running = True
Clock = pygame.time.Clock()
font = pygame.font.SysFont("monospace", 15)

while running:
    Clock.tick(100)

    # read framebuffer
    fb = pyopenmv.fb_dump()
    if fb != None:
        # create image from RGB888
        image = pygame.image.frombuffer(fb[2].flat[0:], (fb[0], fb[1]), 'RGB')
        # TODO check if res changed
        screen = pygame.display.set_mode((fb[0], fb[1]), pygame.DOUBLEBUF, 32)

        fps = Clock.get_fps()
        # blit stuff
        screen.blit(image, (0, 0))
        screen.blit(font.render("FPS %.2f"%(fps), 1, (255, 0, 0)), (0, 0))

        # update display
        pygame.display.flip()

    for event in pygame.event.get():
Ejemplo n.º 4
0
def camera_exec():
    """
    Main camera execution loop that runs until program is aborted.
    1. Camera tries to connect continuously until successful
    2. Camera outputs frame buffer -> video stream
    3. Camera outputs text buffer -> data about target object
        - Data is passed to robot for evaluation
    """
    pygame.init()
    locals()

    plot_num = 0
    running, Clock, font = camera_connect()
    while running:
        Clock.tick(100)

        # read framebuffer
        fb = None
        while (True):
            try:
                fb = pyopenmv.fb_dump()
                break
            except Exception as e:
                # try and reconnect on failure
                camera_connect()

        # signal to UArm that camera has connected
        camera_started.set()
        if fb is not None:
            # create image from RGB888
            image = pygame.image.frombuffer(fb[2].flat[0:], (fb[0], fb[1]),
                                            'RGB')
            screen = pygame.display.set_mode((fb[0], fb[1]), pygame.DOUBLEBUF,
                                             32)

            fps = Clock.get_fps()
            # blit stuff
            screen.blit(image, (0, 0))
            screen.blit(font.render("FPS %.2f" % (fps), 1, (255, 0, 0)),
                        (0, 0))

            # update display
            pygame.display.flip()

        # get output from text buffer
        tx_len = pyopenmv.tx_buf_len()

        # object was found by camera if there is outputted text
        if tx_len:
            '''
            if UArm has signaled to the camera to identify the object and the camera has not already
            assigned values to the global variables associated with the object's location
            '''
            if camera_event.is_set() and (data_ready.is_set() is False):

                # read the most recent data at index 0 from the text buffer
                buff = pyopenmv.tx_buf(tx_len).decode()
                split_buff = str(buff).splitlines()
                if h_angle_key in split_buff[0]:

                    # Most recent line in buff contains needed information
                    global h_angle, v_angle, is_centered
                    tok = split_buff[0].split()

                    # set angles to corresponding values determined by camera
                    h_angle, v_angle = float(tok[1]), float(tok[3])
                    if tok[5] == "True":
                        is_centered = True
                    else:
                        is_centered = False
                    # signal that global variables have been set
                    data_ready.set()

        if plot_ready.is_set():
            print("success_rate: ", success_history)
            plot_distance(distance_history, plot_num)
            plot_success(success_history, plot_num)
            plot_num += 1
            plot_ready.clear()
            print("success rate for ", len(success_history), " tests: ",
                  success_history.count(True) / len(success_history))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False
                if event.key == pygame.K_c:
                    pygame.image.save(image, "capture.png")

    pygame.quit()
    pyopenmv.stop_script()