Apollo 10.0
自动驾驶开放平台
nano_radar_canbus_component.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2021 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 "Eigen/Geometry"
24
25#include "modules/common_msgs/sensor_msgs/nano_radar.pb.h"
26#include "modules/drivers/canbus/proto/sensor_canbus_conf.pb.h"
27
32
37namespace apollo {
38namespace drivers {
39namespace nano_radar {
40
45
47 : monitor_logger_buffer_(
48 apollo::common::monitor::MonitorMessageItem::NANO_RADAR) {}
50
52 if (!GetProtoConfig(&nano_radar_conf_)) {
53 return OnError("Unable to load canbus conf file: " + ConfigFilePath());
54 }
55
56 AINFO << "The canbus conf file is loaded: " << ConfigFilePath();
57 ADEBUG << "Canbus_conf:" << nano_radar_conf_.ShortDebugString();
58
59 // Init can client
60 auto can_factory = CanClientFactory::Instance();
61 can_factory->RegisterCanClients();
62 can_client_ = can_factory->CreateCANClient(
63 nano_radar_conf_.can_conf().can_card_parameter());
64 if (!can_client_) {
65 return OnError("Failed to create can client.");
66 }
67 AINFO << "Can client is successfully created.";
68 nano_radar_writer_ =
69 node_->CreateWriter<NanoRadar>(nano_radar_conf_.radar_channel());
70
71 sensor_message_manager_ = std::unique_ptr<NanoRadarMessageManager>(
72 new NanoRadarMessageManager(nano_radar_writer_));
73 if (sensor_message_manager_ == nullptr) {
74 return OnError("Failed to create message manager.");
75 }
76 sensor_message_manager_->set_radar_conf(nano_radar_conf_.radar_conf());
77 sensor_message_manager_->set_can_client(can_client_);
78 AINFO << "Sensor message manager is successfully created.";
79
80 if (can_receiver_.Init(can_client_.get(), sensor_message_manager_.get(),
81 nano_radar_conf_.can_conf().enable_receiver_log()) !=
82 ErrorCode::OK) {
83 return OnError("Failed to init can receiver.");
84 }
85 AINFO << "The can receiver is successfully initialized.";
86
87 start_success_ = Start();
88 return start_success_;
89}
90
91apollo::common::ErrorCode NanoRadarCanbusComponent::ConfigureRadar() {
92 RadarConfig200 radar_config;
93 radar_config.set_radar_conf(nano_radar_conf_.radar_conf());
94 SenderMessage<NanoRadar> sender_message(RadarConfig200::ID, &radar_config);
95 sender_message.Update();
96 return can_client_->SendSingleFrame({sender_message.CanFrame()});
97}
98
99apollo::common::ErrorCode NanoRadarCanbusComponent::ConfigureRadarRegion() {
100 RegionConfig401 radar_config;
101 radar_config.set_radar_conf(nano_radar_conf_.radar_conf());
102 SenderMessage<NanoRadar> sender_message(RegionConfig401::ID, &radar_config);
103 sender_message.Update();
104 return can_client_->SendSingleFrame({sender_message.CanFrame()});
105}
106
107bool NanoRadarCanbusComponent::Start() {
108 // 1. init and start the can card hardware
109 if (can_client_->Start() != ErrorCode::OK) {
110 return OnError("Failed to start can client");
111 }
112 AINFO << "Can client is started.";
113 if (ConfigureRadar() != ErrorCode::OK) {
114 return OnError("Failed to configure radar.");
115 }
116 AINFO << "The radar is successfully configured.";
117 if (ConfigureRadarRegion() != ErrorCode::OK) {
118 return OnError("Failed to configure radar region.");
119 }
120 AINFO << "The radar region is successfully configured.";
121 // 2. start receive first then send
122 if (can_receiver_.Start() != ErrorCode::OK) {
123 return OnError("Failed to start can receiver.");
124 }
125 AINFO << "Can receiver is started.";
126
127 // last step: publish monitor messages
128 monitor_logger_buffer_.INFO("Canbus is started.");
129
130 return true;
131}
132
133void NanoRadarCanbusComponent::Stop() {
134 if (start_success_) {
135 can_receiver_.Stop();
136 can_client_->Stop();
137 }
138}
139
140// Send the error to monitor and return it
141bool NanoRadarCanbusComponent::OnError(const std::string& error_msg) {
142 monitor_logger_buffer_.ERROR(error_msg);
143 AERROR << error_msg;
144 return false;
145}
146
147} // namespace nano_radar
148} // namespace drivers
149} // namespace apollo
Defines the CanClientFactory class.
Defines SenderMessage class and CanSender class.
bool GetProtoConfig(T *config) const
const std::string & ConfigFilePath() const
std::shared_ptr< Node > node_
CanClientFactory inherites apollo::common::util::Factory.
This class defines the message to send.
Definition can_sender.h:56
RadarConfig200 * set_radar_conf(RadarConf radar_conf)
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37
The class of NanoRadarMessageManager
optional apollo::drivers::canbus::CANCardParameter can_card_parameter