Apollo 11.0
自动驾驶开放平台
pad_terminal.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 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#include "modules/common_msgs/planning_msgs/pad_msg.pb.h"
18#include "modules/planning/planning_base/proto/planning_config.pb.h"
19
20#include "cyber/common/log.h"
21#include "cyber/common/macros.h"
22#include "cyber/cyber.h"
23#include "cyber/init.h"
24#include "cyber/time/time.h"
28
29namespace {
30
37
38class PadTerminal {
39 public:
40 PadTerminal() : node_(CreateNode("planning_pad_terminal")) {}
41 void init() {
42 const std::string planning_config_file =
43 "/apollo/modules/planning/planning_component/conf/"
44 "planning_config.pb.txt";
45 PlanningConfig planning_config;
46 ACHECK(GetProtoFromFile(planning_config_file, &planning_config))
47 << "failed to load planning config file " << planning_config_file;
48
49 pad_writer_ = node_->CreateWriter<PadMessage>(
50 planning_config.topic_config().planning_pad_topic());
51 terminal_thread_.reset(new std::thread([this] { terminal_thread_func(); }));
52 }
53 void help() {
54 AINFO << "COMMAND:0~10\n";
55 AINFO << "\t0: follow";
56 AINFO << "\t1: change left";
57 AINFO << "\t2: change right";
58 AINFO << "\t3: pull over";
59 AINFO << "\t4: stop";
60 AINFO << "\t5: resume cruise";
61 AINFO << "\t10: exit";
62 AINFO << "\tother number: print help";
63 }
64
65 void send(int action) {
66 PadMessage pad;
67 pad.set_action(PadMessage::DrivingAction(action));
68 if (action == PadMessage::FOLLOW) {
69 AINFO << "sending FOLLOW action command.";
70 } else if (action == PadMessage::CHANGE_LEFT) {
71 AINFO << "sending CHANGE LEFT action command.";
72 } else if (action == PadMessage::CHANGE_RIGHT) {
73 AINFO << "sending CHANGE RIGHT action command.";
74 } else if (action == PadMessage::PULL_OVER) {
75 AINFO << "sending PULL OVER action command.";
76 } else if (action == PadMessage::STOP) {
77 AINFO << "sending STOP action command.";
78 } else if (action == PadMessage::RESUME_CRUISE) {
79 AINFO << "sending RESUME CRUISE action command.";
80 }
81 apollo::common::util::FillHeader("terminal", &pad);
82 pad_writer_->Write(pad);
83 AINFO << "send pad_message OK";
84 }
85
86 void terminal_thread_func() {
87 int mode = 0;
88 bool should_exit = false;
89 while (std::cin >> mode) {
90 switch (mode) {
91 case 0:
92 send(PadMessage::FOLLOW);
93 break;
94 case 1:
95 send(PadMessage::CHANGE_LEFT);
96 break;
97 case 2:
98 send(PadMessage::CHANGE_RIGHT);
99 break;
100 case 3:
101 send(PadMessage::PULL_OVER);
102 break;
103 case 4:
104 send(PadMessage::STOP);
105 break;
106 case 5:
107 send(PadMessage::RESUME_CRUISE);
108 break;
109 case 10:
110 should_exit = true;
111 break;
112 default:
113 help();
114 break;
115 }
116 if (should_exit) {
117 break;
118 }
119 }
120 }
121 void stop() { terminal_thread_->join(); }
122
123 private:
124 std::unique_ptr<std::thread> terminal_thread_;
125 std::shared_ptr<Writer<PadMessage>> pad_writer_;
126 std::shared_ptr<Node> node_;
127};
128
129} // namespace
130
131int main(int argc, char **argv) {
132 apollo::cyber::Init("planning_pad_terminal");
133 FLAGS_alsologtostderr = true;
134 FLAGS_v = 3;
135 google::ParseCommandLineFlags(&argc, &argv, true);
136 PadTerminal pad_terminal;
137 pad_terminal.init();
138 pad_terminal.help();
140 pad_terminal.stop();
141 return 0;
142}
Node is the fundamental building block of Cyber RT.
Definition node.h:44
int main(int argc, char **argv)
#define ACHECK(cond)
Definition log.h:80
#define AINFO
Definition log.h:42
Some string util functions.
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
void WaitForShutdown()
Definition state.h:50
bool Init(const char *binary_name, const std::string &dag_info)
Definition init.cc:99
std::unique_ptr< Node > CreateNode(const std::string &node_name, const std::string &name_space)
Definition cyber.cc:33