Esempio n. 1
0
def cleanup():
    Zero()()
    release_lock()
    shm.vision_modules.set(vision_state)
    shm.settings_control.set(control_settings)
    shm.navigation_settings.set(navigation_settings)
    print('')
    logger(
        'Mission completed or interrupted. Restored running vision module state and zeroed submarine desires.',
        copy_to_stdout=True)

    end_time = time.time()
    duration = end_time - start_time
    logger("Mission finished in %i seconds!" % \
               (duration), copy_to_stdout = True)

    if initially_killed and was_ever_unkilled and shm.switches.hard_kill.get():
        reload_self()

    if not args.no_record:
        logger('Disabling "Record" vision module', copy_to_stdout=True)
        shm.vision_modules.Debug.set(False)
        if not initially_recording:
            shm.vision_modules.Record.set(False)

        # Stop shmlogging
        shmlog_proc.kill()

        active_mission = shm.active_mission.get()
        if not initially_recording:
            active_mission.active = False
        active_mission.log_path = bytes("", encoding="utf-8")
        active_mission.name = bytes("", encoding="utf-8")
        shm.active_mission.set(active_mission)
Esempio n. 2
0
def cleanup():
    Zero()()
    release_lock()
    shm.vision_modules.set(vision_state)
    print('')
    logger('Mission completed or interrupted. Restored running vision module state and zeroed submarine desires.', copy_to_stdout = True)
    logger('Disabling "Record" vision module', copy_to_stdout = True)
    shm.vision_modules.Record.set(False)
    os.system('pkill -f auv-shmlogd')
    if initially_killed and was_ever_unkilled and shm.switches.hard_kill.get():
      reload_self()
Esempio n. 3
0
    def __call__(self, reload_on_enable=True, reload_on_change=True):
        m_logger = getattr(logger.module, self.module_name)
        m_logger("Running: " + str(self.module_name), True)

        # create the accessor for the capture source framework
        self.update_CMFs()

        quit = threading.Event()
        watcher = shm.watchers.watcher()
        watcher.watch(shm.vision_modules)

        def unblock():
            quit.set()
            watcher.disable()
            [csf.unblock() for csf in self.capture_source_frameworks]

            # This is only to unblock update_CMFs for a second time...
            self.running = False
            camera_message_framework.running = False

        def cleanup():
            main_thread.join(2)

            if main_thread.is_alive():
                m_logger("Failed to kill module thread!", True)
                # How to kill the thread nicely here?

            self.module_framework.cleanup()
            m_logger("All cleaned up.", True)

        def sigh(sig, frame):
            unblock()
            cleanup()
            sys.exit(0)

        def reload_callback():
            self.should_reload = True
            unblock()

        if reload_on_change:
            aph.detect_changes_to_self(reload_callback)

        if hasattr(shm.vision_modules, self.module_name):
            m_logger(
                "Module has shm variable! Now controlled by "
                "shm.vision_modules.%s" % self.module_name, True)
            module_shm = getattr(shm.vision_modules, self.module_name)
        else:
            module_shm = None

        def func():
            has_been_disabled = False
            while not quit.is_set():
                if module_shm is not None and not module_shm.get():
                    has_been_disabled = True
                    watcher.wait(new_update=False)
                    continue

                if has_been_disabled and reload_on_enable:
                    self.should_reload = True
                    break

                self.posted_images = []

                # Grab images from all capture sources.
                next_images = []
                acq_times = []
                reset = False
                for f in self.capture_source_frameworks:
                    res = f.get_next_frame()
                    if res == camera_message_framework.FRAMEWORK_QUIT:
                        return
                    elif res == camera_message_framework.FRAMEWORK_DELETED:
                        self.update_CMFs()
                        reset = True
                        break

                    next_images.append(res[0])
                    acq_times.append(res[1])

                if reset:
                    continue

                # Feed images to the module.
                with self.option_lock:
                    try:
                        original_options = {
                            option_name: self.options_dict[option_name].value
                            for option_name in self.options_dict
                        }
                        curr_time = time.time() * 1000
                        avg_latency = sum(
                            map(lambda t: curr_time - t,
                                acq_times)) / len(acq_times)
                        m_logger.log('{} image latency to {}: {}ms'.format(
                            self.directions, self.module_name, avg_latency))

                        self.acq_time = acq_times
                        self._next_images = self.preprocessor.process(
                            *next_images)
                        self.process(*self._next_images)

                    except Exception as e:
                        sys.stderr.write('{}\n'.format(e))
                        traceback.print_exc(file=sys.stderr)
                        break

                    if quit.is_set():
                        break

                    # if the option value has changed, notify the watchers
                    for option_name in original_options:
                        option = self.options_dict[option_name]
                        original_value = original_options[option_name]
                        if original_value != option.value:
                            self.module_framework.write_option(
                                option.name, option.get_pack_values())

                # Deal with any posted images.
                for (tag, image) in self.posted_images:
                    if tag not in self.posted_images_set:
                        self.module_framework.create_image(
                            tag, self.max_buffer_size)
                        img_ordering = {
                            tag: i
                            for (i, (tag, _)) in enumerate(self.posted_images)
                        }
                        self.module_framework.set_image_ordering(
                            lambda x: img_ordering[x])
                        self.posted_images_set.add(tag)
                    self.module_framework.write_image(tag, image,
                                                      min(acq_times))

        self.should_reload = False
        main_thread = threading.Thread(target=func)

        register_exit_signals(sigh)

        main_thread.start()
        main_thread.join()

        cleanup()

        if self.should_reload:
            aph.reload_self()
Esempio n. 4
0
    def __call__(self, reload_on_enable=True, reload_on_change=True):
        m_logger = getattr(logger.module, self.module_name)
        m_logger("Running: " + str(self.module_name), True)

        # create the accessor for the capture source framework
        self.update_CMFs()

        quit = threading.Event()
        watcher = shm.watchers.watcher()
        watcher.watch(shm.vision_modules)

        def unblock():
            quit.set()
            watcher.disable()
            [csf.unblock() for csf in self.capture_source_frameworks]

            # This is only to unblock update_CMFs for a second time...
            self.running = False
            camera_message_framework.running = False

        def cleanup():
            main_thread.join(2)

            if main_thread.is_alive():
                m_logger("Failed to kill module thread!", True)
                # How to kill the thread nicely here?

            self.module_framework.cleanup()
            m_logger("All cleaned up.", True)

        def sigh(sig, frame):
            unblock()
            cleanup()
            sys.exit(0)

        def reload_callback():
            self.should_reload = True
            unblock()

        if reload_on_change:
            aph.detect_changes_to_self(reload_callback)

        if hasattr(shm.vision_modules, self.module_name):
            m_logger("Module has shm variable! Now controlled by "
                     "shm.vision_modules.%s" % self.module_name, True)
            module_shm = getattr(shm.vision_modules, self.module_name)
        else:
            module_shm = None

        def func():
            has_been_disabled = False
            while not quit.is_set():
                if module_shm is not None and not module_shm.get():
                    has_been_disabled = True
                    watcher.wait(new_update=False)
                    continue

                if has_been_disabled and reload_on_enable:
                    self.should_reload = True
                    break

                self.posted_images = []

                # Grab images from all capture sources.
                next_images = []
                acq_times = []
                reset = False
                for f in self.capture_source_frameworks:
                    res = f.get_next_frame()
                    if res == camera_message_framework.FRAMEWORK_QUIT:
                        return
                    elif res == camera_message_framework.FRAMEWORK_DELETED:
                        self.update_CMFs()
                        reset = True
                        break

                    next_images.append(res[0])
                    acq_times.append(res[1])

                if reset:
                    continue

                # Feed images to the module.
                with self.option_lock:
                    try:
                        original_options = {option_name: self.options_dict[option_name].value for option_name in self.options_dict}
                        curr_time = time.time()*1000
                        avg_latency = sum(map(lambda t: curr_time - t, acq_times)) / len(acq_times)
                        m_logger.log('{} image latency to {}: {}ms'.format(self.directions, self.module_name, avg_latency))

                        self.acq_time = acq_times
                        self.process(*next_images)

                    except Exception as e:
                        sys.stderr.write('{}\n'.format(e))
                        traceback.print_exc(file=sys.stderr)

                    if quit.is_set():
                        break

                    # if the option value has changed, notify the watchers
                    for option_name in original_options:
                        option = self.options_dict[option_name]
                        original_value = original_options[option_name]
                        if original_value != option.value:
                            self.module_framework.write_option(option.name,
                                                               option.get_pack_values())

                # Deal with any posted images.
                for (tag, image) in self.posted_images:
                    if tag not in self.posted_images_set:
                        self.module_framework.create_image(tag, max(self.buffer_size))
                        img_ordering = {tag: i for (i, (tag, _)) in enumerate(self.posted_images)}
                        self.module_framework.set_image_ordering(lambda x: img_ordering[x])
                        self.posted_images_set.add(tag)
                    self.module_framework.write_image(tag, image, min(acq_times))

        self.should_reload = False
        main_thread = threading.Thread(target=func)

        register_exit_signals(sigh)

        main_thread.start()
        main_thread.join()

        cleanup()

        if self.should_reload:
            aph.reload_self()