目录
std::unique_ptr apollo::cyber::CreateNode(const std::string& node_name, const std::string& name_space = "");
Node_name: 节点名,全局唯一标识符
Name_space:节点所在的空间名称
Name_space 默认为空。是与node_name连在一起的空间名。格式为/namespace/node_name
返回值-指向Node的独占智能指针
错误条件—当cyber::Init()未被调用时,系统处于未初始化状态,无法创建节点,返回nullptr
- template <typename MessageT>
- auto CreateWriter(const std::string& channel_name)
- -> std::shared_ptr
>; - template <typename MessageT>
- auto CreateWriter(const proto::RoleAttributes& role_attr)
- -> std::shared_ptr
>;
Channel_name:要写入的通道名
MessageT:要写入的消息类型
返回值- Writer对象的共享指针
- template <typename MessageT>
- auto CreateReader(const std::string& channel_name, const std::function<void(const std::shared_ptr
&)>& reader_func) - -> std::shared_ptr
>; -
- template <typename MessageT>
- auto CreateReader(const ReaderConfig& config,
- const CallbackFunc
& reader_func = nullptr) - -> std::shared_ptr
>; -
- template <typename MessageT>
- auto CreateReader(const proto::RoleAttributes& role_attr,
- const CallbackFunc
& reader_func = nullptr) - -> std::shared_ptr
>;
MessageT:要读取的消息类型
Channel_name:要接收的通道的名称
Reader_func:处理消息的回调函数
返回值—Reader对象的共享指针
- #include "cyber/cyber.h"
-
- #include "cyber/proto/chatter.pb.h"
-
- #include "cyber/time/rate.h"
-
- #include "cyber/time/time.h"
-
- using apollo::cyber::Rate;
-
- using apollo::cyber::Time;
-
- using apollo::cyber::proto::Chatter;
-
- int main(int argc, char *argv[]) {
-
- // init cyber framework
-
- apollo::cyber::Init(argv[0]);
-
- // create talker node
-
- std::shared_ptr
talker_node( - apollo::cyber::CreateNode("talker"));
-
- // create talker
-
- auto talker = talker_node->CreateWriter
("channel/chatter"); -
- Rate rate(1.0);
-
- while (apollo::cyber::OK()) {
-
- static uint64_t seq = 0;
-
- auto msg = std::make_shared
(); -
- msg->set_timestamp(Time::Now().ToNanosecond());
-
- msg->set_lidar_timestamp(Time::Now().ToNanosecond());
-
- msg->set_seq(seq++);
-
- msg->set_content("Hello, apollo!");
-
- talker->Write(msg);
-
- AINFO << "talker sent a message!";
-
- rate.Sleep();
-
- }
-
- return 0;
-
- }
- #include "cyber/cyber.h"
-
- #include "cyber/proto/chatter.pb.h"
-
- void MessageCallback(
-
- const std::shared_ptr
& msg) { -
- AINFO << "Received message seq-> " << msg->seq();
-
- AINFO << "msgcontent->" << msg->content();
-
- }
-
- int main(int argc, char *argv[]) {
-
- // init cyber framework
-
- apollo::cyber::Init(argv[0]);
-
- // create listener node
-
- auto listener_node = apollo::cyber::CreateNode("listener");
-
- // create listener
-
- auto listener =
-
- listener_node->CreateReader
( -
- "channel/chatter", MessageCallback);
-
- apollo::cyber::WaitForShutdown();
-
- return 0;
-
- }
- cc_binary(
-
- name = "talker",
-
- srcs = [ "talker.cc", ],
-
- deps = [
-
- "//cyber",
-
- "//cyber/examples/proto:examples_cc_proto",
-
- ],
-
- )
-
-
-
- cc_binary(
-
- name = "listener",
-
- srcs = [ "listener.cc", ],
-
- deps = [
-
- "//cyber",
-
- "//cyber/examples/proto:examples_cc_proto",
-
- ],
-
- )