Apollo 10.0
自动驾驶开放平台
message_converter.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2024 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#ifndef CYBER_MESSAGE_CONVERTER_H_
18#define CYBER_MESSAGE_CONVERTER_H_
19
20#include <atomic> // NOLINT
21#include <memory> // NOLINT
22#include <string> // NOLINT
23#include <thread> // NOLINT
24#include <utility> // NOLINT
25#include <vector> // NOLINT
26
27#include <cxxabi.h> // NOLINT
28
29#include "cyber/ros_bridge/proto/converter_conf.pb.h"
30
31#include "cyber/cyber.h"
36
37#if __has_include("rclcpp/rclcpp.hpp")
38#include "message_filters/subscriber.h"
39#include "message_filters/sync_policies/approximate_time.h"
40#include "message_filters/synchronizer.h"
41#include "rclcpp/rclcpp.hpp"
42#include "ros_adapter/ros_distro.h"
43#endif
44
45namespace apollo {
46namespace cyber {
47
49 public:
50 MessageConverter() : init_(false) {}
51
52 virtual ~MessageConverter() {}
53
54 virtual bool Init() {
55 if (init_.exchange(true)) {
56 return true;
57 }
59 cyber_node_ = std::move(
60 CreateNode(node_name_ + "_" + converter_conf_.name() + "_apollo"));
61#ifdef RCLCPP__RCLCPP_HPP_
62 ros_node_ = std::make_shared<::rclcpp::Node>(
63 node_name_ + "_" + converter_conf_.name() + "_ros");
64 ros_node_exec_ =
65 std::make_shared<::rclcpp::executors::SingleThreadedExecutor>();
66#endif
67 return true;
68 }
69
70#ifdef RCLCPP__RCLCPP_HPP_
71 void NodeSpin() {
72 ros_node_exec_->add_node(std::shared_ptr<rclcpp::Node>(ros_node_));
73 ros_node_exec_->spin();
74 }
75#endif
76
77 bool IsInit() const { return init_.load(); }
78
79 protected:
80 bool LoadConfig(ConverterConf* config) {
81 int status;
82 std::string class_name =
83 abi::__cxa_demangle(typeid(*this).name(), 0, 0, &status);
84 std::string delimiter = "::";
85 std::string sub_class_name;
86 std::string conf_file_prefix;
87
88 auto pos = class_name.rfind(delimiter);
89 if (pos == std::string::npos) {
90 sub_class_name = class_name;
91 } else {
92 sub_class_name = class_name.substr(pos + delimiter.length());
93 }
94
95 for (int i = 0; i < sub_class_name.length(); i++) {
96 if (std::isupper(sub_class_name[i]) && i > 0) {
97 conf_file_prefix.push_back('_');
98 }
99 conf_file_prefix.push_back(std::tolower(sub_class_name[i]));
100 }
101
102 std::string config_path =
105 class_name, "conf/" + conf_file_prefix + ".pb.txt");
106 if (!apollo::cyber::common::PathExists(config_path)) {
109 class_name, std::string("conf/default.pb.txt"));
110 }
111
112 if (!apollo::cyber::common::GetProtoFromFile(config_path, config)) {
113 AERROR << "Load config of " << class_name << " failed!";
114 return false;
115 }
116 AINFO << "Load the [" << class_name
117 << "] config file successfully, file path: " << config_path;
118 return true;
119 }
120
121 protected:
122 std::atomic<bool> init_;
123 std::unique_ptr<apollo::cyber::Node> cyber_node_;
124 std::vector<std::shared_ptr<apollo::cyber::proto::RoleAttributes>>
126 std::vector<std::shared_ptr<apollo::cyber::ReaderBase>> apollo_readers_;
127 std::vector<std::shared_ptr<apollo::cyber::WriterBase>> apollo_writers_;
128#ifdef RCLCPP__RCLCPP_HPP_
129 std::vector<std::shared_ptr<::rclcpp::PublisherBase>> ros_publishers_;
130 std::vector<std::shared_ptr<::rclcpp::SubscriptionBase>> ros_subscriptions_;
131#if defined(ROS_DISTRO_FOXY) || defined(ROS_DISTRO_GALACTIC)
132 std::vector<std::shared_ptr<::message_filters::SubscriberBase>>
133#else
134 std::vector<std::shared_ptr<::message_filters::SubscriberBase<rclcpp::Node>>>
135#endif
136 ros_msg_subs_;
137 std::shared_ptr<::rclcpp::Node> ros_node_ = nullptr;
138 std::shared_ptr<::rclcpp::executors::SingleThreadedExecutor> ros_node_exec_ =
139 nullptr;
140 std::shared_ptr<std::thread> ros_spin_thread_;
141#endif
142 const std::string node_name_ = "converter_base";
144};
145
146} // namespace cyber
147} // namespace apollo
148
149#endif // CYBER_MESSAGE_CONVERTER_H_
std::unique_ptr< apollo::cyber::Node > cyber_node_
std::vector< std::shared_ptr< apollo::cyber::proto::RoleAttributes > > apollo_attrs_
std::vector< std::shared_ptr< apollo::cyber::ReaderBase > > apollo_readers_
std::vector< std::shared_ptr< apollo::cyber::WriterBase > > apollo_writers_
bool LoadConfig(ConverterConf *config)
std::string GetPluginConfPath(const std::string &class_name, const std::string &conf_name)
get plugin configuration file location
static PluginManager * Instance()
get singleton instance of PluginManager
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
bool PathExists(const std::string &path)
Check if the path exists.
Definition file.cc:195
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
std::unique_ptr< Node > CreateNode(const std::string &node_name, const std::string &name_space)
Definition cyber.cc:33
class register implement
Definition arena_queue.h:37