Apollo 11.0
自动驾驶开放平台
speed_command_processor.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 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
22
23#include "modules/external_command/command_processor/command_processor_base/proto/command_processor_config.pb.h"
24
28
29namespace apollo {
30namespace external_command {
31
32bool SpeedCommandProcessor::Init(const std::shared_ptr<cyber::Node>& node) {
33 // hook: Apollo License Verification: v_apollo_park
34 if (!CommandProcessorBase::Init(node)) {
35 return false;
36 }
37 last_received_command_.set_command_id(-1);
38 message_reader_ = MessageReader::Instance();
39 const auto& config = GetProcessorConfig();
40 command_service_ = node->CreateService<SpeedCommand, CommandStatus>(
41 config.input_command_name(),
42 [this](const std::shared_ptr<SpeedCommand>& command, std::shared_ptr<CommandStatus>& status) {
43 this->OnCommand(command, status);
44 });
45 // Create writer for output command.
46 CHECK(config.output_command_name().size() > 0 && config.input_command_status_name().size() > 0);
47 auto message_writer = MessageWriter::Instance();
48 planning_command_writer_
49 = message_writer->RegisterMessage<apollo::planning::PlanningCommand>(config.output_command_name().Get(0));
50 planning_command_status_name_ = config.input_command_status_name().Get(0);
51 message_reader_->RegisterMessage<CommandStatus>(planning_command_status_name_);
52 return true;
53}
54
55bool SpeedCommandProcessor::GetCommandStatus(int64_t command_id, CommandStatus* status) const {
56 CHECK_NOTNULL(status);
57 if (command_id != last_received_command_.command_id()) {
58 return false;
59 }
60 status->set_command_id(command_id);
61 // todo: Get planning speed setting status.
63 return true;
64}
65
66void SpeedCommandProcessor::OnCommand(
67 const std::shared_ptr<SpeedCommand>& command,
68 std::shared_ptr<CommandStatus>& status) {
69 auto planning_command = std::make_shared<apollo::planning::PlanningCommand>();
70 std::string module_name = "UNKNOWN";
71 if (command->has_header()) {
72 module_name = command->header().module_name();
73 double timestamp = apollo::cyber::Clock::NowInSeconds();
74 AINFO << std::setprecision(12) << "timestamp: " << timestamp << " " << "request for "
75 << command->header().timestamp_sec();
76 if (timestamp - command->header().timestamp_sec() > 2.0) {
77 AINFO << "request for " << command->header().module_name() << " has been timeouted";
78 return;
79 }
80 }
81 common::util::FillHeader(module_name, planning_command.get());
82 auto custom_command = planning_command->mutable_custom_command();
83 custom_command->PackFrom(*command);
84 planning_command_writer_->Write(planning_command);
85 last_received_command_.CopyFrom(*command);
86 status->set_command_id(command->command_id());
88}
89
90} // namespace external_command
91} // namespace apollo
static double NowInSeconds()
gets the current time in second.
Definition clock.cc:56
virtual bool Init(const std::shared_ptr< cyber::Node > &node)
const CommandProcessorConfig & GetProcessorConfig() const
void RegisterMessage(const std::string &channel_name)
Register the message reader with the given channel name.
bool GetCommandStatus(int64_t command_id, CommandStatus *status) const override
Get the command status.
bool Init(const std::shared_ptr< cyber::Node > &node) override
#define AINFO
Definition log.h:42
Some string util functions.
class register implement
Definition arena_queue.h:37