hydra code - src/common/hydra_pipeline.cpp

About_work·2024년 10월 15일
0

lifelong scene graph

목록 보기
14/56

종합 정리:

  • 로봇 시스템의 여러 모듈을 하나의 파이프라인으로 통합하고, 각 모듈 간의 상태를 관리하는 데 사용
  • 이 코드는 Hydra 파이프라인의 전체적인 구조모듈 관리를 담당
  • 각 모듈의 생명 주기 관리(시작, 중지, 저장)와 상태 출력이 주요 기능

코드 구성 요소 설명:

1. HydraPipeline 클래스:

class HydraPipeline {
 public:
  HydraPipeline(const PipelineConfig& config,
                int robot_id = 0,
                int config_verbosity = 1);

  virtual ~HydraPipeline();

  virtual void init();

  virtual void start();

  virtual void stop();

  virtual void save();

  template <typename Derived = Module>
  Derived* getModule(const std::string& name);
  
 protected:
  void showModules() const;
  std::string getModuleInfo(const std::string& name, const Module* module) const;

 protected:
  int config_verbosity_;
  SharedDsgInfo::Ptr frontend_dsg_;
  SharedDsgInfo::Ptr backend_dsg_;
  SharedModuleState::Ptr shared_state_;

  Module::Ptr input_module_;
  std::map<std::string, Module::Ptr> modules_;
};

HydraPipeline 클래스는 여러 모듈을 관리하며, 각 모듈의 생명 주기(start, stop, save)를 처리합니다.

  • 생성자(HydraPipeline):

    • PipelineConfig 객체와 robot_id, config_verbosity를 인자로 받아서 전역 설정(GlobalInfo)을 초기화합니다.
    • SharedDsgInfo분산된 동적 장면 그래프(Distributed Scene Graph, DSG)를 나타내는 객체로,
      • frontend_dsg_backend_dsg_를 통해 DSG의 전방(frontend)과 후방(backend) 정보를 공유
    • sharedstate모듈 간에 공유되는 상태를 나타내는 포인터
  • init 함수:

    • 파이프라인을 초기화하는 역할을 하지만, 여기서는 아직 구체적인 초기화 작업은 정의되지 않았습니다. 추후에 필요한 초기화 로직을 여기에 넣을 수 있습니다.
  • start, stop, save 함수:

    • 파이프라인 내에 등록된 모든 모듈들을 시작(start), 중지(stop), 저장(save)하는 기능을 합니다.
  • getModule 함수:

    • 파이프라인 내에서 모듈을 이름으로 찾아 반환하는 함수입니다.
      • 이 함수는 주어진 이름을 가진 모듈을 찾아서 해당 모듈이 있으면 반환하고, 없으면 nullptr을 반환합니다.
    • 모듈의 타입을 Derived로 캐스팅하여 반환합니다.
  • showModulesgetModuleInfo 함수:

    • showModules는 파이프라인에 포함된 모든 모듈들의 정보를 출력하는 역할을 합니다.
    • getModuleInfo는 각 모듈의 상태와 정보를 문자열 형태로 반환하며, 이를 통해 각 모듈의 상태를 확인할 수 있습니다.

2. 생성자 HydraPipeline(const PipelineConfig&, int, int):

HydraPipeline::HydraPipeline(const PipelineConfig& pipeline_config,
                             int robot_id,
                             int config_verbosity)
    : config_verbosity_(config_verbosity) {
  const auto& config = GlobalInfo::init(pipeline_config, robot_id, true);
  frontend_dsg_ = config.createSharedDsg();
  backend_dsg_ = config.createSharedDsg();
  shared_state_.reset(new SharedModuleState());
  shared_state_->lcd_graph = config.createSharedDsg();
  shared_state_->backend_graph = config.createSharedDsg();
}
  • GlobalInfo::init: 주어진 파이프라인 설정(PipelineConfig)을 사용하여 전역 정보를 초기화합니다.
    • 전역 정보는 로봇의 ID와 함께 설정되고, 시스템의 모든 모듈이 이 정보를 공유합니다.
  • frontenddsgbackenddsg:
    • Distributed Scene Graph (DSG)의 전방 및 후방 그래프를 나타냅니다.
    • 이는 로봇의 환경을 모델링하는 중요한 데이터 구조로, 로봇의 탐색 중 얻은 정보를 저장하는 역할을 합니다.
  • shared_state_:
    • 모듈 간의 공유 상태로, 여기에는 로봇의 루프 클로저 탐지(lcd_graph)와 후방 그래프(backend_graph) 등이 포함
    • frontend / backend / loopclosure 모듈 3개 끼리 공유하는 정보

4. start() 함수:

void HydraPipeline::start() {
  VLOG(config_verbosity_) << std::endl << getModuleInfo("input", input_module_.get());
  showModules();

  if (input_module_) {
    input_module_->start();
  }

  for (auto&& [name, module] : modules_) {
    if (!module) {
      LOG(FATAL) << "Found unitialized module: " << name;
      continue;
    }

    module->start();
  }

  if (input_module_) {
    modules_["input"] = input_module_;
  }
}
  • start()는 파이프라인의 모든 모듈을 시작합니다.
    • 먼저, 입력 모듈(inputmodule)이 초기화되어 있으면 이를 시작합니다.
    • 이후, 모듈 맵(modules_)에 저장된 모든 모듈을 시작합니다. 모듈이 초기화되지 않았으면 Fatal Error를 기록합니다.
    • 마지막으로 입력 모듈을 modules_에 추가하여 관리합니다.

5. stop() 함수:

void HydraPipeline::stop() {
  for (auto&& [name, module] : modules_) {
    if (!module) {
      LOG(FATAL) << "Found unitialized module: " << name;
      continue;
    }

    module->stop();
  }
}
  • stop()는 파이프라인의 모든 모듈을 중지합니다. 각 모듈을 순회하면서 stop()을 호출하여 종료합니다.
    • 만약 모듈이 초기화되지 않았으면 Fatal Error를 기록합니다.

6. save() 함수:

void HydraPipeline::save() {
  const auto& logs = GlobalInfo::instance().getLogs();
  if (!logs || !logs->valid()) {
    return;
  }

  for (auto&& [name, module] : modules_) {
    if (!module) {
      LOG(FATAL) << "Found unitialized module: " << name;
      continue;
    }

    module->save(*logs);
  }
}
  • save()는 파이프라인의 모든 모듈의 상태를 저장합니다.
    • GlobalInfo에서 로그 객체를 가져오고, 로그가 유효할 때만 모듈의 상태를 저장합니다.
    • 각 모듈이 제대로 초기화되었는지 확인한 후, 각 모듈의 save() 함수를 호출하여 상태를 기록합니다.

3. showModules()getModuleInfo() 함수:

std::string HydraPipeline::getModuleInfo(const std::string& name,
                                         const Module* module) const {
  const auto print_width = config::Settings().print_width;
  std::stringstream ss;
  ss << makeBanner(name, print_width, '*', true, true);
  if (!module) {
    ss << "UNITIALIZED MODULE!" << std::endl;
  } else {
    const auto info = module->printInfo();
    if (!info.empty()) {
      ss << info << std::endl;
    }
  }
  ss << std::string(print_width, '*') << std::endl;
  return ss.str();
}
  • getModuleInfo(): 주어진 모듈의 상태를 문자열로 변환하여 반환합니다. 모듈이 초기화되지 않았으면 UNITIALIZED MODULE!이라는 메시지를 출력합니다.
  • makeBanner() 함수를 사용하여 각 모듈의 정보를 보기 쉽게 표시합니다.
void HydraPipeline::showModules() const {
  const auto print_width = config::Settings().print_width;
  std::stringstream ss;
  ss << std::endl << makeBanner("Modules", print_width, '=', true, true);
  for (auto&& [name, module] : modules_) {
    ss << std::endl << getModuleInfo(name, module.get());
  }
  VLOG(config_verbosity_) << ss.str();
}
  • showModules(): 모든 모듈의 정보를 출력합니다. modules_모듈의 이름과 포인터를 가지는 맵(map)으로, 이를 순회하며 각 모듈의 정보를 출력합니다. VLOG는 설정된 로깅 수준에 따라 로그를 출력합니다.


profile
새로운 것이 들어오면 이미 있는 것과 충돌을 시도하라.

0개의 댓글