Apollo 10.0
自动驾驶开放平台
lincoln_vehicle_factory.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
18
19#include "cyber/common/log.h"
26
30
31namespace apollo {
32namespace canbus {
33
34bool LincolnVehicleFactory::Init(const CanbusConf *canbus_conf) {
35 // Init can client
36 auto can_factory = CanClientFactory::Instance();
37 can_factory->RegisterCanClients();
38 can_client_ = can_factory->CreateCANClient(canbus_conf->can_card_parameter());
39 if (!can_client_) {
40 AERROR << "Failed to create can client.";
41 return false;
42 }
43 AINFO << "Can client is successfully created.";
44
45 message_manager_ = this->CreateMessageManager();
46 if (message_manager_ == nullptr) {
47 AERROR << "Failed to create message manager.";
48 return false;
49 }
50 AINFO << "Message manager is successfully created.";
51
52 if (can_receiver_.Init(can_client_.get(), message_manager_.get(),
53 canbus_conf->enable_receiver_log()) != ErrorCode::OK) {
54 AERROR << "Failed to init can receiver.";
55 return false;
56 }
57 AINFO << "The can receiver is successfully initialized.";
58
59 if (can_sender_.Init(can_client_.get(), message_manager_.get(),
60 canbus_conf->enable_sender_log()) != ErrorCode::OK) {
61 AERROR << "Failed to init can sender.";
62 return false;
63 }
64 AINFO << "The can sender is successfully initialized.";
65
66 vehicle_controller_ = CreateVehicleController();
67 if (vehicle_controller_ == nullptr) {
68 AERROR << "Failed to create vehicle controller.";
69 return false;
70 }
71 AINFO << "The vehicle controller is successfully created.";
72
73 if (vehicle_controller_->Init(canbus_conf->vehicle_parameter(), &can_sender_,
74 message_manager_.get()) != ErrorCode::OK) {
75 AERROR << "Failed to init vehicle controller.";
76 return false;
77 }
78
79 AINFO << "The vehicle controller is successfully"
80 << " initialized with canbus conf as : "
81 << canbus_conf->vehicle_parameter().ShortDebugString();
82
83 node_ = ::apollo::cyber::CreateNode("chassis_detail");
84
85 chassis_detail_writer_ = node_->CreateWriter<::apollo::canbus::Lincoln>(
86 FLAGS_chassis_detail_topic);
87
88 return true;
89}
90
92 // 1. init and start the can card hardware
93 if (can_client_->Start() != ErrorCode::OK) {
94 AERROR << "Failed to start can client";
95 return false;
96 }
97 AINFO << "Can client is started.";
98
99 // 2. start receive first then send
100 if (can_receiver_.Start() != ErrorCode::OK) {
101 AERROR << "Failed to start can receiver.";
102 return false;
103 }
104 AINFO << "Can receiver is started.";
105
106 // 3. start send
107 if (can_sender_.Start() != ErrorCode::OK) {
108 AERROR << "Failed to start can sender.";
109 return false;
110 }
111
112 // 4. start controller
113 if (!vehicle_controller_->Start()) {
114 AERROR << "Failed to start vehicle controller.";
115 return false;
116 }
117
118 return true;
119}
120
122 can_sender_.Stop();
123 can_receiver_.Stop();
124 can_client_->Stop();
125 vehicle_controller_->Stop();
126 AINFO << "Cleanup cansender, canreceiver, canclient, vehicle controller.";
127}
128
130 const apollo::control::ControlCommand *control_command) {
131 if (vehicle_controller_->Update(*control_command) != ErrorCode::OK) {
132 AERROR << "Failed to process callback function OnControlCommand because "
133 "vehicle_controller_->Update error.";
134 return;
135 }
136 can_sender_.Update();
137}
138
140 const apollo::external_command::ChassisCommand *chassis_command) {
141 if (vehicle_controller_->Update(*chassis_command) != ErrorCode::OK) {
142 AERROR << "Failed to process callback function OnControlCommand because "
143 "vehicle_controller_->Update error.";
144 return;
145 }
146 can_sender_.Update();
147}
148
150 Chassis chassis = vehicle_controller_->chassis();
151 ADEBUG << chassis.ShortDebugString();
152 return chassis;
153}
154
156 Lincoln chassis_detail;
157 message_manager_->GetSensorData(&chassis_detail);
158 ADEBUG << chassis_detail.ShortDebugString();
159 chassis_detail_writer_->Write(chassis_detail);
160}
161
162std::unique_ptr<VehicleController<::apollo::canbus::Lincoln>>
163LincolnVehicleFactory::CreateVehicleController() {
164 return std::unique_ptr<VehicleController<::apollo::canbus::Lincoln>>(
166}
167
168std::unique_ptr<MessageManager<::apollo::canbus::Lincoln>>
169LincolnVehicleFactory::CreateMessageManager() {
170 return std::unique_ptr<MessageManager<::apollo::canbus::Lincoln>>(
171 new lincoln::LincolnMessageManager());
172}
173
174} // namespace canbus
175} // namespace apollo
Defines the CanClientFactory class.
void UpdateCommand(const apollo::control::ControlCommand *control_command) override
update control command
void PublishChassisDetail() override
publish chassis for vehicle messages
bool Init(const CanbusConf *canbus_conf) override
init vehicle factory
Chassis publish_chassis() override
publish chassis messages
void Stop() override
stop canclient, cansender, canreceiver, vehicle controller
bool Start() override
start canclient, cansender, canreceiver, vehicle controller
this class implements the vehicle controller for lincoln vehicle.
CanClientFactory inherites apollo::common::util::Factory.
The class of LincolnController
the class of LincolnMessageManager
#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