Apollo 应用与源码分析:Monitor监控 - Monitor_manage&recurrent_runner分析

目录

monitor_manager 分析

结构分析

单例模式宏定义

描述现在的系统状态

HMI上显示的状态信息

仿真判断状态

判断是不是自动驾驶状态

日志缓存

当前node

所有monitor 的reader 管理map

开启一次监控

start frame分析

end frame分析

recurrent_runner 分析

结构分析

Tick 函数分析


monitor_manager 分析

结构分析

// Centralized monitor config and status manager.
class MonitorManager {
public:
void Init(const std::shared_ptr<apollo::cyber::Node>& node);

// Start and end a monitoring frame.
bool StartFrame(const double current_time);
void EndFrame();

// Getters.
const apollo::dreamview::HMIMode& GetHMIMode() const { return mode_config_; }
bool IsInAutonomousMode() const { return in_autonomous_driving_; }
SystemStatus* GetStatus() { return &status_; }
apollo::common::monitor::MonitorLogBuffer& LogBuffer() { return log_buffer_; }

// Cyber reader / writer creator.
template <class T>
std::shared_ptr<cyber::Reader<T>> CreateReader(const std::string& channel) {
    if (readers_.find(channel) == readers_.end()) {
        readers_.emplace(channel, node_->CreateReader<T>(channel));
    }
    return std::dynamic_pointer_cast<cyber::Reader<T>>(readers_[channel]);
}

template <class T>
std::shared_ptr<cyber::Writer<T>> CreateWriter(const std::string& channel) {
    return node_->CreateWriter<T>(channel);
}

private:
SystemStatus status_;

// Input statuses.
std::string current_mode_;
const apollo::dreamview::HMIConfig hmi_config_;
apollo::dreamview::HMIMode mode_config_;
bool in_autonomous_driving_ = false;
bool CheckAutonomousDriving(const double current_time);

apollo::common::monitor::MonitorLogBuffer log_buffer_;
std::shared_ptr<apollo::cyber::Node> node_;
std::unordered_map<std::string, std::shared_ptr<cyber::ReaderBase>> readers_;

DECLARE_SINGLETON(MonitorManager)
};
DECLARE_SINGLETON(MonitorManager)

单例模式宏定义

#define DECLARE_SINGLETON(classname)                                      \
 public:                                                                  \
  static classname *Instance(bool create_if_needed = true) {              \
    static classname *instance = nullptr;                                 \
    if (!instance && create_if_needed) {                                  \
      static std::once_flag flag;                                         \
      std::call_once(flag,                                                \
                     [&] { instance = new (std::nothrow) classname(); }); \
    }                                                                     \
    return instance;                                                      \
  }                                                                       \
                                                                          \
  static void CleanUp() {                                                 \
    auto instance = Instance(false);                                      \
    if (instance != nullptr) {                                            \
      CallShutdown(instance);                                             \
    }                                                                     \
  }                                                                       \
                                                                          \
 private:                                                                 \
  classname();                                                            \
  DISALLOW_COPY_AND_ASSIGN(classname)

描述现在的系统状态

SystemStatus status_

HMI上显示的状态信息

std::string current_mode_

仿真判断状态

const apollo::dreamview::HMIConfig hmi_config_;
apollo::dreamview::HMIMode mode_config_;

判断是不是自动驾驶状态

bool in_autonomous_driving_ = false;
bool CheckAutonomousDriving(const double current_time);

日志缓存

  apollo::common::monitor::MonitorLogBuffer log_buffer_;

当前node

std::shared_ptr<apollo::cyber::Node> node_;

所有monitor 的reader 管理map

 std::unordered_map<std::string, std::shared_ptr<cyber::ReaderBase>> readers_;

开启一次监控

bool StartFrame(const double current_time);
void EndFrame();

bool MonitorManager::StartFrame(const double current_time) {
  // Get latest HMIStatus.
  static auto hmi_status_reader =
      CreateReader<apollo::dreamview::HMIStatus>(FLAGS_hmi_status_topic);
  hmi_status_reader->Observe();
  const auto hmi_status = hmi_status_reader->GetLatestObserved();
  if (hmi_status == nullptr) {
    AERROR << "No HMIStatus was received.";
    return false;
  }

  if (current_mode_ != hmi_status->current_mode()) {
    // Mode changed, update configs and monitored.
    current_mode_ = hmi_status->current_mode();
    mode_config_ = HMIWorker::LoadMode(hmi_config_.modes().at(current_mode_));
    status_.clear_hmi_modules();
    for (const auto& iter : mode_config_.modules()) {
      status_.mutable_hmi_modules()->insert({iter.first, {}});
    }
    status_.clear_components();
    for (const auto& iter : mode_config_.monitored_components()) {
      status_.mutable_components()->insert({iter.first, {}});
    }
    status_.clear_other_components();
    for (const auto& iter : mode_config_.other_components()) {
      status_.mutable_other_components()->insert({iter.first, {}});
    }
  } else {
    // Mode not changed, clear component summary from the last frame.
    for (auto& iter : *status_.mutable_components()) {
      iter.second.clear_summary();
    }
  }

  in_autonomous_driving_ = CheckAutonomousDriving(current_time);
  return true;
}

void MonitorManager::EndFrame() {
  // Print and publish all monitor logs.
  log_buffer_.Publish();
}

start frame分析

1. 获取最近的HMI状态

        1.1. 如果获取不到就返回false

2. 如果当前的状态和上一次的状态(HMI状态)不同,就update

         2.1. status_ update 最新的状态

3. 如果当前的状态与上一次的状态(HMI状态)相同,就清除component summary

 // Mode not changed, clear component summary from the last frame.
    for (auto& iter : *status_.mutable_components()) {
      iter.second.clear_summary();
    }

end frame分析

把当前的监控过程中利用MonitorLogBuffrt::AddMonitorMsgItem 添加的日志信息打印并发布出去

发布topic: /apollo/monitor

// modules/common/monitor_log/monitor_log_buffer.cc
void MonitorLogBuffer::AddMonitorMsgItem(
    const MonitorMessageItem::LogLevel log_level, const std::string &msg) {
  level_ = log_level;
  monitor_msg_items_.push_back(std::make_pair(log_level, msg));
}
//modules/common/monitor_log/monitor_logger.cc
MonitorLogger::MonitorLogger() {
  const std::string node_name =
      absl::StrCat("monitor_logger", Time::Now().ToNanosecond());
  node_ = cyber::CreateNode(node_name);
  if (node_ != nullptr) {
    monitor_msg_writer_ =
        node_->CreateWriter<MonitorMessage>("/apollo/monitor");
  }
}

recurrent_runner 分析

结构分析

class RecurrentRunner {
 public:
  RecurrentRunner(const std::string &name, const double interval);
  virtual ~RecurrentRunner() = default;

  // Tick once, which may or may not execute the RunOnce() function, based on
  // the interval setting.
  void Tick(const double current_time);

  // Do the actual work.
  virtual void RunOnce(const double current_time) = 0;

 protected:
  std::string name_;
  unsigned int round_count_ = 0;

 private:
  double interval_;
  double next_round_ = 0;
};

从类的命名上可以看出,这个类是要负责重复工作的。

Tick 的含义是钟表滴答,可以代表周期性执行任务,上面也有注视写了,Tick 函数可能会执行RunOnce函数,但是也可能不会,执行的周期是以下面的interval_来规定的。

roundcount指的是执行了多少轮。

nextround指的是下次执行的时间。

RunOnce是一个纯虚函数,所以这个类是一个接口,并不可以直接new。

Tick 函数分析

void RecurrentRunner::Tick(const double current_time) {
  if (next_round_ <= current_time) {
    ++round_count_;
    AINFO_EVERY(100) << name_ << " is running round #" << round_count_;
    next_round_ = current_time + interval_;
    RunOnce(current_time);
  }
}

执行一次更新下一次要执行的时间,然后执行runOnce 函数,并把当前时间传入。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
root@in_dev_docker:/apollo# bash scripts/msf_create_lossless_map.sh /apollo/hdmap/pcd_apollo/ 50 /apollo/hdmap/ /apollo/bazel-bin WARNING: Logging before InitGoogleLogging() is written to STDERR E0715 22:08:35.399576 6436 lossless_map_creator.cc:162] num_trials = 1 Pcd folders are as follows: /apollo/hdmap/pcd_apollo/ Resolution: 0.125 Dataset: /apollo/hdmap/pcd_apollo Dataset: /apollo/hdmap/pcd_apollo/ Loaded the map configuration from: /apollo/hdmap//lossless_map/config.xml. Saved the map configuration to: /apollo/hdmap//lossless_map/config.xml. Saved the map configuration to: /apollo/hdmap//lossless_map/config.xml. E0715 22:08:35.767315 6436 lossless_map_creator.cc:264] ieout_poses = 1706 Failed to find match for field 'intensity'. Failed to find match for field 'timestamp'. E0715 22:08:35.769896 6436 velodyne_utility.cc:46] Un-organized-point-cloud E0715 22:08:35.781770 6436 lossless_map_creator.cc:275] Loaded 245443D Points at Trial: 0 Frame: 0. F0715 22:08:35.781791 6436 base_map_node_index.cc:101] Check failed: false *** Check failure stack trace: *** scripts/msf_create_lossless_map.sh: line 11: 6436 Aborted (core dumped) $APOLLO_BIN_PREFIX/modules/localization/msf/local_tool/map_creation/lossless_map_creator --use_plane_inliers_only true --pcd_folders $1 --pose_files $2 --map_folder $IN_FOLDER --zone_id $ZONE_ID --coordinate_type UTM --map_resolution_type single root@in_dev_docker:/apollo# bash scripts/msf_create_lossless_map.sh /apollo/hdmap/pcd_apollo/ 50 /apollo/hdmap/
07-16

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ym影子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值