Apollo 11.0
自动驾驶开放平台
chassis_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
27
28namespace apollo {
29namespace external_command {
30
31bool ChassisCommandProcessor::Init(const std::shared_ptr<cyber::Node>& node) {
32 // hook: Apollo License Verification: v_apollo_park
33 if (!CommandProcessorBase::Init(node)) {
34 return false;
35 }
36 last_received_command_.set_command_id(-1);
37 message_reader_ = MessageReader::Instance();
38 const auto& config = GetProcessorConfig();
39 command_service_ = node->CreateService<ChassisCommand, CommandStatus>(
40 config.input_command_name(),
41 [this](const std::shared_ptr<ChassisCommand>& command, std::shared_ptr<CommandStatus>& status) {
42 this->OnCommand(command, status);
43 });
44 // Create writer for output command.
45 CHECK(config.output_command_name().size() > 0 && config.input_command_status_name().size() > 0);
46 auto message_writer = MessageWriter::Instance();
47 chassis_command_writer_ = message_writer->RegisterMessage<ChassisCommand>(config.output_command_name().Get(0));
48 chassis_status_name_ = config.input_command_status_name().Get(0);
49 message_reader_->RegisterMessage<apollo::canbus::Chassis>(chassis_status_name_);
50 return true;
51}
52
53// command = request.command_id status = response.get();
54bool ChassisCommandProcessor::GetCommandStatus(int64_t command_id, CommandStatus* status) const {
55 CHECK_NOTNULL(status);
56 if (command_id != last_received_command_.command_id()) {
57 return false;
58 }
59 status->set_command_id(command_id);
60 auto* latest_chassis_status = message_reader_->GetMessage<apollo::canbus::Chassis>(chassis_status_name_);
61 if (nullptr == latest_chassis_status) {
63 status->set_message("Cannot get chassis status!");
64 return true;
65 }
66 // Process basic vehicle signals.
67 if (last_received_command_.has_basic_signal()) {
68 const auto& basic_signal = last_received_command_.basic_signal();
69 // Check turn signal.
70 if (basic_signal.has_turn_signal()) {
71 if (!latest_chassis_status->has_signal() || !latest_chassis_status->signal().has_turn_signal()
72 || latest_chassis_status->signal().turn_signal() != basic_signal.turn_signal()) {
74 return true;
75 }
76 }
77 // Check high beam.
78 if (basic_signal.has_high_beam()) {
79 if (!latest_chassis_status->has_signal() || !latest_chassis_status->signal().has_high_beam()
80 || latest_chassis_status->signal().high_beam() != basic_signal.high_beam()) {
82 return true;
83 }
84 }
85 // Check low beam.
86 if (basic_signal.has_low_beam()) {
87 if (!latest_chassis_status->has_signal() || !latest_chassis_status->signal().has_low_beam()
88 || latest_chassis_status->signal().low_beam() != basic_signal.low_beam()) {
90 return true;
91 }
92 }
93 // Check horn.
94 if (basic_signal.has_horn()) {
95 if (!latest_chassis_status->has_signal() || !latest_chassis_status->signal().has_horn()
96 || latest_chassis_status->signal().horn() != basic_signal.horn()) {
98 return true;
99 }
100 }
101 // Check emergency light.
102 if (basic_signal.has_emergency_light()) {
103 if (!latest_chassis_status->has_signal() || !latest_chassis_status->signal().has_emergency_light()
104 || latest_chassis_status->signal().emergency_light() != basic_signal.emergency_light()) {
106 return true;
107 }
108 }
109 }
111 return true;
112}
113
114void ChassisCommandProcessor::OnCommand(
115 const std::shared_ptr<ChassisCommand>& command,
116 std::shared_ptr<CommandStatus>& status) {
117 if (command->has_header()) {
118 double timestamp = apollo::cyber::Clock::NowInSeconds();
119 AINFO << std::setprecision(12) << "timestamp: " << timestamp << " " << "request for "
120 << command->header().timestamp_sec();
121 if (timestamp - command->header().timestamp_sec() > 2.0) {
122 AINFO << "request for " << command->header().module_name() << " has been timeouted";
123 return;
124 }
125 }
126 chassis_command_writer_->Write(command);
127 status->set_command_id(command->command_id());
129 last_received_command_.CopyFrom(*command);
130}
131
132} // namespace external_command
133} // namespace apollo
static double NowInSeconds()
gets the current time in second.
Definition clock.cc:56
bool GetCommandStatus(int64_t command_id, CommandStatus *status) const override
Get the command status.
bool Init(const std::shared_ptr< cyber::Node > &node) override
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.
const T * GetMessage(const std::string &channel_name) const
Get the message with the channel name.
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37
optional apollo::common::VehicleSignal basic_signal