Apollo 10.0
自动驾驶开放平台
neolix_edu_vehicle_factory.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2020 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/log.h"
24
28
29namespace apollo {
30namespace canbus {
31
33 // Init can client
34 auto can_factory = CanClientFactory::Instance();
35 can_factory->RegisterCanClients();
36 can_client_ = can_factory->CreateCANClient(canbus_conf->can_card_parameter());
37 if (!can_client_) {
38 AERROR << "Failed to create can client.";
39 return false;
40 }
41 AINFO << "Can client is successfully created.";
42
43 message_manager_ = this->CreateMessageManager();
44 if (message_manager_ == nullptr) {
45 AERROR << "Failed to create message manager.";
46 return false;
47 }
48 AINFO << "Message manager is successfully created.";
49
50 if (can_receiver_.Init(can_client_.get(), message_manager_.get(),
51 canbus_conf->enable_receiver_log()) != ErrorCode::OK) {
52 AERROR << "Failed to init can receiver.";
53 return false;
54 }
55 AINFO << "The can receiver is successfully initialized.";
56
57 if (can_sender_.Init(can_client_.get(), message_manager_.get(),
58 canbus_conf->enable_sender_log()) != ErrorCode::OK) {
59 AERROR << "Failed to init can sender.";
60 return false;
61 }
62 AINFO << "The can sender is successfully initialized.";
63
64 vehicle_controller_ = CreateVehicleController();
65 if (vehicle_controller_ == nullptr) {
66 AERROR << "Failed to create vehicle controller.";
67 return false;
68 }
69 AINFO << "The vehicle controller is successfully created.";
70
71 if (vehicle_controller_->Init(canbus_conf->vehicle_parameter(), &can_sender_,
72 message_manager_.get()) != ErrorCode::OK) {
73 AERROR << "Failed to init vehicle controller.";
74 return false;
75 }
76
77 AINFO << "The vehicle controller is successfully"
78 << " initialized with canbus conf as : "
79 << canbus_conf->vehicle_parameter().ShortDebugString();
80
81 node_ = ::apollo::cyber::CreateNode("chassis_detail");
82
83 chassis_detail_writer_ = node_->CreateWriter<::apollo::canbus::Neolix_edu>(
84 FLAGS_chassis_detail_topic);
85
86 chassis_detail_sender_writer_ =
87 node_->CreateWriter<::apollo::canbus::Neolix_edu>(
88 FLAGS_chassis_detail_sender_topic);
89
90 return true;
91}
92
94 // 1. init and start the can card hardware
95 if (can_client_->Start() != ErrorCode::OK) {
96 AERROR << "Failed to start can client";
97 return false;
98 }
99 AINFO << "Can client is started.";
100
101 // 2. start receive first then send
102 if (can_receiver_.Start() != ErrorCode::OK) {
103 AERROR << "Failed to start can receiver.";
104 return false;
105 }
106 AINFO << "Can receiver is started.";
107
108 // 3. start send
109 if (can_sender_.Start() != ErrorCode::OK) {
110 AERROR << "Failed to start can sender.";
111 return false;
112 }
113
114 // 4. start controller
115 if (!vehicle_controller_->Start()) {
116 AERROR << "Failed to start vehicle controller.";
117 return false;
118 }
119
120 return true;
121}
122
124 can_sender_.Stop();
125 can_receiver_.Stop();
126 can_client_->Stop();
127 vehicle_controller_->Stop();
128 AINFO << "Cleanup cansender, canreceiver, canclient, vehicle controller.";
129}
130
132 const apollo::control::ControlCommand *control_command) {
133 if (vehicle_controller_->Update(*control_command) != ErrorCode::OK) {
134 AERROR << "Failed to process callback function OnControlCommand because "
135 "vehicle_controller_->Update error.";
136 return;
137 }
138 can_sender_.Update();
139}
140
142 const apollo::external_command::ChassisCommand *chassis_command) {
143 if (vehicle_controller_->Update(*chassis_command) != ErrorCode::OK) {
144 AERROR << "Failed to process callback function OnControlCommand because "
145 "vehicle_controller_->Update error.";
146 return;
147 }
148 can_sender_.Update();
149}
150
152 Chassis chassis = vehicle_controller_->chassis();
153 ADEBUG << chassis.ShortDebugString();
154 return chassis;
155}
156
158 Neolix_edu chassis_detail = vehicle_controller_->GetNewRecvChassisDetail();
159 ADEBUG << "latest chassis_detail is " << chassis_detail.ShortDebugString();
160 chassis_detail_writer_->Write(chassis_detail);
161}
162
164 Neolix_edu sender_chassis_detail =
165 vehicle_controller_->GetNewSenderChassisDetail();
166 ADEBUG << "latest sender_chassis_detail is "
167 << sender_chassis_detail.ShortDebugString();
168 chassis_detail_sender_writer_->Write(sender_chassis_detail);
169}
170
172 can_sender_.Update_Heartbeat();
173}
174
176 if (vehicle_controller_->CheckChassisCommunicationError()) {
177 return true;
178 }
179 return false;
180}
181
183 vehicle_controller_->AddSendMessage();
184}
185
187 can_sender_.ClearMessage();
188}
189
191 if (can_sender_.IsMessageClear()) {
192 return true;
193 }
194 return false;
195}
196
198 return vehicle_controller_->driving_mode();
199}
200
201std::unique_ptr<VehicleController<::apollo::canbus::Neolix_edu>>
202Neolix_eduVehicleFactory::CreateVehicleController() {
203 return std::unique_ptr<VehicleController<::apollo::canbus::Neolix_edu>>(
205}
206
207std::unique_ptr<MessageManager<::apollo::canbus::Neolix_edu>>
208Neolix_eduVehicleFactory::CreateMessageManager() {
209 return std::unique_ptr<MessageManager<::apollo::canbus::Neolix_edu>>(
210 new neolix_edu::Neolix_eduMessageManager());
211}
212
213} // namespace canbus
214} // namespace apollo
Defines the CanClientFactory class.
void UpdateHeartbeat() override
create cansender heartbeat
void AddSendProtocol() override
add the can sender messages
void ClearSendProtocol() override
clear the can sender messages
void Stop() override
create ch vehicle controller
bool IsSendProtocolClear() override
check the sender message clear or not
bool Init(const CanbusConf *canbus_conf) override
init vehicle factory
void UpdateCommand(const apollo::control::ControlCommand *control_command) override
update control command
Chassis::DrivingMode Driving_Mode() override
get the latest chassis driving mode
void PublishChassisDetailSender() override
publish chassis for apollo sender messages
void PublishChassisDetail() override
publish chassis for vehicle messages
bool Start() override
start canclient, cansender, canreceiver, vehicle controller
Chassis publish_chassis() override
publish chassis messages
bool CheckChassisCommunicationFault() override
check chassis can receiver lost
CanClientFactory inherites apollo::common::util::Factory.
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
Some util functions.
std::unique_ptr< Node > CreateNode(const std::string &node_name, const std::string &name_space)
Definition cyber.cc:33
class register implement
Definition arena_queue.h:37
optional apollo::drivers::canbus::CANCardParameter can_card_parameter
optional VehicleParameter vehicle_parameter