Apollo 10.0
自动驾驶开放平台
ultrasonic_radar_canbus.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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
18
19#include "cyber/common/file.h"
21#include "modules/common_msgs/sensor_msgs/ultrasonic_radar.pb.h"
23
28namespace apollo {
29namespace drivers {
30namespace ultrasonic_radar {
31
33 : monitor_logger_buffer_(
34 common::monitor::MonitorMessageItem::ULTRASONIC_RADAR) {}
35
37 can_receiver_.Stop();
38 can_client_->Stop();
39}
40
41std::string UltrasonicRadarCanbus::Name() const { return "ultrasonic_radar"; }
42
44 const std::string& config_path,
45 const std::shared_ptr<::apollo::cyber::Writer<Ultrasonic>>& writer) {
46 if (!cyber::common::GetProtoFromFile(config_path, &ultrasonic_radar_conf_)) {
47 return OnError("Unable to load canbus conf file: " + config_path);
48 }
49
50 AINFO << "The canbus conf file is loaded: " << config_path;
51 ADEBUG << "Canbus_conf:" << ultrasonic_radar_conf_.ShortDebugString();
52
53 // Init can client
54 auto can_factory = CanClientFactory::Instance();
55 can_factory->RegisterCanClients();
56 can_client_ = can_factory->CreateCANClient(
57 ultrasonic_radar_conf_.can_conf().can_card_parameter());
58 if (!can_client_) {
59 return OnError("Failed to create can client.");
60 }
61 AINFO << "Can client is successfully created.";
62
63 sensor_message_manager_ = std::unique_ptr<UltrasonicRadarMessageManager>(
64 new UltrasonicRadarMessageManager(ultrasonic_radar_conf_.entrance_num(),
65 writer));
66 if (sensor_message_manager_ == nullptr) {
67 return OnError("Failed to create message manager.");
68 }
69 sensor_message_manager_->set_can_client(can_client_);
70 AINFO << "Sensor message manager is successfully created.";
71
72 bool enable_receiver_log =
73 ultrasonic_radar_conf_.can_conf().enable_receiver_log();
74 if (can_receiver_.Init(can_client_.get(), sensor_message_manager_.get(),
75 enable_receiver_log) != ErrorCode::OK) {
76 return OnError("Failed to init can receiver.");
77 }
78 AINFO << "The can receiver is successfully initialized.";
79
80 return Status::OK();
81}
82
84 // 1. init and start the can card hardware
85 if (can_client_->Start() != ErrorCode::OK) {
86 return OnError("Failed to start can client");
87 }
88 AINFO << "Can client is started.";
89
90 // 2. start receive first then send
91 if (can_receiver_.Start() != ErrorCode::OK) {
92 return OnError("Failed to start can receiver.");
93 }
94 AINFO << "Can receiver is started.";
95
96 // last step: publish monitor messages
97 monitor_logger_buffer_.INFO("Canbus is started.");
98
99 return Status::OK();
100}
101
102// Send the error to monitor and return it
103Status UltrasonicRadarCanbus::OnError(const std::string& error_msg) {
104 monitor_logger_buffer_.ERROR(error_msg);
105 return Status(ErrorCode::CANBUS_ERROR, error_msg);
106}
107
108} // namespace ultrasonic_radar
109} // namespace drivers
110} // namespace apollo
A general class to denote the return status of an API call.
Definition status.h:43
static Status OK()
generate a success status.
Definition status.h:60
apollo::common::Status Start()
module start function
apollo::common::Status Init(const std::string &config_path, const std::shared_ptr<::apollo::cyber::Writer< Ultrasonic > > &writer)
module initialization function
#define ADEBUG
Definition log.h:41
#define AINFO
Definition log.h:42
Some 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
class register implement
Definition arena_queue.h:37
optional apollo::drivers::canbus::CANCardParameter can_card_parameter
The class of UltrasonicRadarMessageManager