Apollo 10.0
自动驾驶开放平台
conti_radar_canbus_component.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 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
21#include "Eigen/Geometry"
22
26#include "modules/drivers/canbus/proto/sensor_canbus_conf.pb.h"
27#include "modules/common_msgs/sensor_msgs/conti_radar.pb.h"
30
35namespace apollo {
36namespace drivers {
37namespace conti_radar {
38
44
46 : monitor_logger_buffer_(
47 apollo::common::monitor::MonitorMessageItem::CONTI_RADAR) {}
49
51 if (!GetProtoConfig(&conti_radar_conf_)) {
52 return OnError("Unable to load canbus conf file: " + ConfigFilePath());
53 }
54
55 AINFO << "The canbus conf file is loaded: " << ConfigFilePath();
56 ADEBUG << "Canbus_conf:" << conti_radar_conf_.ShortDebugString();
57
58 // Init can client
59 auto can_factory = CanClientFactory::Instance();
60 can_factory->RegisterCanClients();
61 can_client_ = can_factory->CreateCANClient(
62 conti_radar_conf_.can_conf().can_card_parameter());
63 if (!can_client_) {
64 return OnError("Failed to create can client.");
65 }
66 AINFO << "Can client is successfully created.";
67 conti_radar_writer_ =
68 node_->CreateWriter<ContiRadar>(conti_radar_conf_.radar_channel());
69 pose_reader_ = node_->CreateReader<LocalizationEstimate>(
70 FLAGS_localization_topic,
71 [&](const std::shared_ptr<LocalizationEstimate>& pose) {
72 PoseCallback(pose);
73 });
74
75 sensor_message_manager_ = std::unique_ptr<ContiRadarMessageManager>(
76 new ContiRadarMessageManager(conti_radar_writer_));
77 if (sensor_message_manager_ == nullptr) {
78 return OnError("Failed to create message manager.");
79 }
80 sensor_message_manager_->set_radar_conf(conti_radar_conf_.radar_conf());
81 sensor_message_manager_->set_can_client(can_client_);
82 AINFO << "Sensor message manager is successfully created.";
83
84 if (can_receiver_.Init(can_client_.get(), sensor_message_manager_.get(),
85 conti_radar_conf_.can_conf().enable_receiver_log()) !=
86 ErrorCode::OK) {
87 return OnError("Failed to init can receiver.");
88 }
89 AINFO << "The can receiver is successfully initialized.";
90
91 start_success_ = Start();
92 return start_success_;
93}
94
95apollo::common::ErrorCode ContiRadarCanbusComponent::ConfigureRadar() {
96 RadarConfig200 radar_config;
97 radar_config.set_radar_conf(conti_radar_conf_.radar_conf());
98 SenderMessage<ContiRadar> sender_message(RadarConfig200::ID, &radar_config);
99 sender_message.Update();
100 return can_client_->SendSingleFrame({sender_message.CanFrame()});
101}
102
103bool ContiRadarCanbusComponent::Start() {
104 // 1. init and start the can card hardware
105 if (can_client_->Start() != ErrorCode::OK) {
106 return OnError("Failed to start can client");
107 }
108 AINFO << "Can client is started.";
109 if (ConfigureRadar() != ErrorCode::OK) {
110 return OnError("Failed to configure radar.");
111 }
112 AINFO << "The radar is successfully configured.";
113 // 2. start receive first then send
114 if (can_receiver_.Start() != ErrorCode::OK) {
115 return OnError("Failed to start can receiver.");
116 }
117 AINFO << "Can receiver is started.";
118
119 // last step: publish monitor messages
120 monitor_logger_buffer_.INFO("Canbus is started.");
121
122 return true;
123}
124
125void ContiRadarCanbusComponent::Stop() {
126 if (start_success_) {
127 can_receiver_.Stop();
128 can_client_->Stop();
129 }
130}
131
132// Send the error to monitor and return it
133bool ContiRadarCanbusComponent::OnError(const std::string& error_msg) {
134 monitor_logger_buffer_.ERROR(error_msg);
135 AERROR << error_msg;
136 return false;
137}
138
139void ContiRadarCanbusComponent::PoseCallback(
140 const std::shared_ptr<LocalizationEstimate>& pose_msg) {
141 auto send_interval = conti_radar_conf_.radar_conf().input_send_interval();
142 uint64_t now_nsec = cyber::Time().Now().ToNanosecond();
143 if (last_nsec_ != 0 && (now_nsec - last_nsec_) < send_interval) {
144 return;
145 }
146 last_nsec_ = now_nsec;
147 Eigen::Quaterniond orientation_vehicle_world(
148 pose_msg->pose().orientation().qw(), pose_msg->pose().orientation().qx(),
149 pose_msg->pose().orientation().qy(), pose_msg->pose().orientation().qz());
150 Eigen::Matrix3d rotation_matrix =
151 orientation_vehicle_world.toRotationMatrix().inverse();
152 Eigen::Vector3d speed_v(pose_msg->pose().linear_velocity().x(),
153 pose_msg->pose().linear_velocity().y(),
154 pose_msg->pose().linear_velocity().z());
155 float speed = static_cast<float>((rotation_matrix * speed_v).y());
156 float yaw_rate = static_cast<float>(pose_msg->pose().angular_velocity().z() *
157 180.0f / M_PI);
158
159 AINFO << "radar speed:" << speed << ";yaw rate:" << yaw_rate;
160 MotionInputSpeed300 input_speed;
161 input_speed.SetSpeed(speed);
162 SenderMessage<ContiRadar> sender_message_speed(MotionInputSpeed300::ID,
163 &input_speed);
164 sender_message_speed.Update();
165 can_client_->SendSingleFrame({sender_message_speed.CanFrame()});
166
167 MotionInputYawRate301 input_yawrate;
168 input_yawrate.SetYawRate(yaw_rate);
169 SenderMessage<ContiRadar> sender_message_yawrate(MotionInputYawRate301::ID,
170 &input_yawrate);
171 sender_message_yawrate.Update();
172 can_client_->SendSingleFrame({sender_message_yawrate.CanFrame()});
173}
174
175} // namespace conti_radar
176} // namespace drivers
177} // 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)
The class of ContiRadarMessageManager
#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
optional apollo::drivers::canbus::CANCardParameter can_card_parameter