Apollo 10.0
自动驾驶开放平台
vehicle_controller.h
浏览该文件的文档.
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
22#pragma once
23
24#include <unordered_map>
25
26#include "modules/canbus/proto/canbus_conf.pb.h"
27#include "modules/common_msgs/basic_msgs/error_code.pb.h"
28#include "modules/common_msgs/chassis_msgs/chassis.pb.h"
29#include "modules/common_msgs/control_msgs/control_cmd.pb.h"
30#include "modules/common_msgs/external_command_msgs/chassis_command.pb.h"
31
32#include "cyber/common/log.h"
33#include "cyber/time/time.h"
40
45namespace apollo {
46namespace canbus {
47
49using ::apollo::drivers::canbus::CanReceiver;
50using ::apollo::drivers::canbus::CanSender;
51using ::apollo::drivers::canbus::MessageManager;
52
59template <typename SensorType>
61 public:
62 virtual ~VehicleController() = default;
63
71 const VehicleParameter &params, CanSender<SensorType> *const can_sender,
72 MessageManager<SensorType> *const message_manager) = 0;
73
78 virtual bool Start() = 0;
79
83 virtual void Stop() = 0;
84
89 virtual Chassis chassis() = 0;
90
99
107
109
110 virtual void AddSendMessage();
111
112 virtual SensorType GetNewRecvChassisDetail();
113
114 virtual SensorType GetNewSenderChassisDetail();
115
117
118 private:
119 /*
120 * @brief main logical function for operation the car enter or exit the auto
121 * driving
122 */
123 virtual void Emergency() = 0;
124
125 virtual common::ErrorCode EnableAutoMode() = 0;
126 virtual common::ErrorCode DisableAutoMode() = 0;
127 virtual common::ErrorCode EnableSteeringOnlyMode() = 0;
128 virtual common::ErrorCode EnableSpeedOnlyMode() = 0;
129
130 /*
131 * @brief NEUTRAL, REVERSE, DRIVE
132 */
133 virtual void Gear(Chassis::GearPosition state) = 0;
134
135 /*
136 * @brief detail function for auto driving brake with new acceleration
137 * acceleration:0.00~99.99, unit:%
138 */
139 virtual void Brake(double acceleration) = 0;
140
141 /*
142 * @brief drive with old acceleration gas:0.00~99.99 unit:%
143 */
144 virtual void Throttle(double throttle) = 0;
145
146 /*
147 * @brief drive with new acceleration/deceleration:-7.0~7.0, unit:m/s^2,
148 * acc:-7.0~7.0, unit:m/s^2
149 */
150 virtual void Acceleration(double acc) = 0;
151
152 /*
153 * @brief drive with new speed:-xx.0~xx.0, unit:m/s
154 */
155 virtual void Speed(double speed) {}
156
157 /*
158 * @brief steering with old angle speed angle:-99.99~0.00~99.99, unit:%,
159 * left:+, right:-
160 */
161 virtual void Steer(double angle) = 0;
162
163 /*
164 * @brief steering with new angle speed angle:-99.99~0.00~99.99, unit:%,
165 * left:+, right:- angle_spd:0.00~99.99, unit:deg/s
166 */
167 virtual void Steer(double angle, double angle_spd) = 0;
168
169 /*
170 * @brief set Electrical Park Brake
171 */
172 virtual void SetEpbBreak(const control::ControlCommand &command) = 0;
173 virtual void SetLimits() {}
174
175 /*
176 * @brief Handle user-defined chassis actions
177 */
178 virtual common::ErrorCode HandleCustomOperation(
179 const external_command::ChassisCommand &command) = 0;
180
181 /*
182 * @brief Handle Apollo chassis actions
183 */
184 virtual void HandleVehicleSignal(const common::VehicleSignal &signal) {
185 if (signal.has_high_beam() || signal.has_low_beam()) SetBeam(signal);
186 if (signal.has_horn()) SetHorn(signal);
187 if (signal.has_turn_signal()) SetTurningSignal(signal);
188 }
189
190 virtual void SetBeam(const common::VehicleSignal &signal) = 0;
191 virtual void SetHorn(const common::VehicleSignal &signal) = 0;
192 virtual void SetTurningSignal(const common::VehicleSignal &signal) = 0;
193
198 virtual common::VehicleSignal ProcessCommandChange(
199 const common::VehicleSignal &signal, common::VehicleSignal *last_command);
200
205 virtual bool VerifyID() = 0;
206
207 protected:
209
210 protected:
215 CanSender<SensorType> *can_sender_ = nullptr;
216 CanReceiver<SensorType> *can_receiver_ = nullptr;
217 MessageManager<SensorType> *message_manager_ = nullptr;
218 bool is_initialized_ = false; // own by derviative concrete controller
220 bool is_reset_ = false; // reset command from control command
221 std::mutex mode_mutex_; // only use in this base class
222 uint32_t lost_chassis_reveive_detail_count_ = 0; // check chassis detail lost
223 bool is_need_count_ = true; // check chassis detail lost
224 size_t sender_data_size_previous_ = 0.0; // check apollo sender preiod
225 int64_t start_time_ = 0; // check apollo sender preiod
226 bool is_chassis_communication_error_ = false; // check chassis communication
227};
228
232
233template <typename SensorType>
235 SensorType chassis_detail_sender;
236 if (message_manager_->GetSensorCheckSenderData(&chassis_detail_sender) !=
237 ErrorCode::OK) {
238 AERROR_EVERY(100) << "Get " << typeid(SensorType).name()
239 << " chassis receive detail failed.";
240 }
241 size_t sender_data_size = chassis_detail_sender.ByteSizeLong();
242 ADEBUG << "check chassis detail sender_data_size is " << sender_data_size;
243 int64_t end_time = 0;
244 if ((sender_data_size_previous_ < 2) && (sender_data_size > 2)) {
246 ADEBUG << "end_time is " << end_time;
247 if (start_time_ > 0) {
248 const double sender_diff = (end_time - start_time_) * 1e-3;
249 ADEBUG << "sender protocol preiod is " << sender_diff;
250 }
251 } else if ((sender_data_size_previous_ > 2) && (sender_data_size < 2)) {
253 ADEBUG << "start_time_ is " << start_time_;
254 }
255 sender_data_size_previous_ = sender_data_size;
256 message_manager_->ClearSensorCheckSenderData();
257
258 SensorType chassis_detail_receiver;
259 if (message_manager_->GetSensorCheckRecvData(&chassis_detail_receiver) !=
260 ErrorCode::OK) {
261 AERROR_EVERY(100) << "Get " << typeid(SensorType).name()
262 << " chassis receive detail failed.";
263 }
264 ADEBUG << "chassis_detail_receiver is "
265 << chassis_detail_receiver.ShortDebugString();
266 size_t receiver_data_size = chassis_detail_receiver.ByteSizeLong();
267 ADEBUG << "check chassis detail receiver_data_size is " << receiver_data_size;
268 // check receiver data is null
269 if (receiver_data_size < 2) {
270 if (is_need_count_) {
271 lost_chassis_reveive_detail_count_++;
272 }
273 } else {
274 lost_chassis_reveive_detail_count_ = 0;
275 is_need_count_ = true;
276 }
277 ADEBUG << "lost_chassis_reveive_detail_count_ is "
278 << lost_chassis_reveive_detail_count_;
279 // check receive data lost threshold is (100 * 10)ms
280 if (lost_chassis_reveive_detail_count_ > 100) {
281 is_need_count_ = false;
282 is_chassis_communication_error_ = true;
283 AERROR << typeid(SensorType).name()
284 << " chassis detail is lost, please check the "
285 "communication error.";
286 message_manager_->ClearSensorCheckRecvData();
287 message_manager_->ResetSendMessages();
288 return true;
289 } else {
290 is_chassis_communication_error_ = false;
291 }
292 message_manager_->ClearSensorCheckRecvData();
293
294 return false;
295}
296
297template <typename SensorType>
299
300template <typename SensorType>
302 SensorType receiver_chassis_detail;
303 if (message_manager_->GetSensorRecvData(&receiver_chassis_detail) !=
304 ErrorCode::OK) {
305 AERROR_EVERY(100) << "Get " << typeid(SensorType).name()
306 << " chassis detail receiver failed.";
307 return receiver_chassis_detail;
308 }
309 ADEBUG << "receiver_chassis_detail is "
310 << receiver_chassis_detail.ShortDebugString();
311 if (is_chassis_communication_error_) {
312 message_manager_->ClearSensorRecvData();
313 }
314 return receiver_chassis_detail;
315}
316
317template <typename SensorType>
319 SensorType sender_chassis_detail;
320 if (message_manager_->GetSensorSenderData(&sender_chassis_detail) !=
321 ErrorCode::OK) {
322 AERROR_EVERY(100) << "Get " << typeid(SensorType).name()
323 << " chassis detail receiver failed.";
324 return sender_chassis_detail;
325 }
326 message_manager_->GetSensorSenderData(&sender_chassis_detail);
327 ADEBUG << "sender_chassis_detail is "
328 << sender_chassis_detail.ShortDebugString();
329 if (can_sender_->IsMessageClear()) {
330 message_manager_->ClearSensorSenderData();
331 }
332 return sender_chassis_detail;
333}
334
335template <typename SensorType>
337 std::lock_guard<std::mutex> lock(mode_mutex_);
338 return driving_mode_;
339}
340
341template <typename SensorType>
343 const Chassis::DrivingMode &driving_mode) {
344 std::lock_guard<std::mutex> lock(mode_mutex_);
345 driving_mode_ = driving_mode;
346}
347
348template <typename SensorType>
350 const Chassis::DrivingMode &driving_mode) {
351 if (driving_mode == Chassis::EMERGENCY_MODE) {
352 AINFO << "Can't set vehicle to EMERGENCY_MODE driving mode.";
353 return ErrorCode::CANBUS_ERROR;
354 }
355
356 // vehicle in emergency mode only response to manual mode to reset.
357 if (this->driving_mode() == Chassis::EMERGENCY_MODE &&
358 driving_mode != Chassis::COMPLETE_MANUAL) {
359 AINFO
360 << "Vehicle in EMERGENCY_MODE, only response to COMPLETE_MANUAL mode.";
361 AINFO << "Only response to RESET ACTION.";
362 return ErrorCode::CANBUS_ERROR;
363 }
364
365 // if current mode is same as previous, no need to set.
366 if (this->driving_mode() == driving_mode) {
367 return ErrorCode::OK;
368 }
369
370 switch (driving_mode) {
372 if (EnableAutoMode() != ErrorCode::OK) {
373 AERROR << "Failed to enable auto mode.";
374 return ErrorCode::CANBUS_ERROR;
375 }
376 break;
377 }
379 if (DisableAutoMode() != ErrorCode::OK) {
380 AERROR << "Failed to disable auto mode.";
381 return ErrorCode::CANBUS_ERROR;
382 }
383 break;
384 }
386 if (EnableSteeringOnlyMode() != ErrorCode::OK) {
387 AERROR << "Failed to enable steer only mode.";
388 return ErrorCode::CANBUS_ERROR;
389 }
390 break;
391 }
393 if (EnableSpeedOnlyMode() != ErrorCode::OK) {
394 AERROR << "Failed to enable speed only mode";
395 return ErrorCode::CANBUS_ERROR;
396 }
397 break;
398 }
399 default:
400 break;
401 }
402 return ErrorCode::OK;
403}
404
405template <typename SensorType>
407 const ControlCommand &control_command) {
408 if (!is_initialized_) {
409 AERROR << "Controller is not initialized.";
410 return ErrorCode::CANBUS_ERROR;
411 }
412
413 if (control_command.has_pad_msg() && control_command.pad_msg().has_action()) {
414 ADEBUG << "Canbus received pad msg: "
415 << control_command.pad_msg().ShortDebugString();
416 const double current_timestamp = Time::Now().ToSecond();
417 // pad_msg_time_diff: s
418 const double pad_msg_time_diff =
419 current_timestamp - control_command.pad_msg().header().timestamp_sec();
420 // Execute action to transform driving mode
421 if ((FLAGS_chassis_debug_mode ||
422 (pad_msg_time_diff < FLAGS_pad_msg_delay_interval)) &&
423 !is_chassis_communication_error_) {
424 if (control_command.pad_msg().action() ==
426 if (!VerifyID()) {
427 AINFO << "Response vid failed, please request again.";
428 } else {
429 AINFO << "Response vid success!";
430 }
431 } else {
433 switch (control_command.pad_msg().action()) {
436 break;
437 }
439 // In COMPLETE_MANUAL mode
440 AINFO << "Into the Reset action.";
441 break;
442 }
443 default: {
444 AERROR << "No response for this action.";
445 break;
446 }
447 }
448 auto error_code = SetDrivingMode(mode);
449 if (error_code != ErrorCode::OK) {
450 AERROR << "Failed to set driving mode.";
451 } else {
452 AINFO << "Set driving mode success.";
453 }
454 }
455 } else {
456 ADEBUG << "pad msg time out, current time interval is "
457 << pad_msg_time_diff << " s, threshold is "
458 << FLAGS_pad_msg_delay_interval << " s";
459 }
460 }
461
462 if (driving_mode() == Chassis::COMPLETE_AUTO_DRIVE ||
463 driving_mode() == Chassis::AUTO_SPEED_ONLY) {
464 Gear(control_command.gear_location());
465 Throttle(control_command.throttle());
466 Acceleration(control_command.acceleration());
467 Speed(control_command.speed());
468 Brake(control_command.brake());
469 SetEpbBreak(control_command);
470 SetLimits();
471 }
472
473 if (driving_mode() == Chassis::COMPLETE_AUTO_DRIVE ||
474 driving_mode() == Chassis::AUTO_STEER_ONLY) {
475 const double steering_rate_threshold = 1.0;
476 if (control_command.steering_rate() > steering_rate_threshold) {
477 Steer(control_command.steering_target(), control_command.steering_rate());
478 } else {
479 Steer(control_command.steering_target());
480 }
481 }
482
483 if ((driving_mode() == Chassis::COMPLETE_AUTO_DRIVE ||
484 driving_mode() == Chassis::AUTO_SPEED_ONLY ||
485 driving_mode() == Chassis::AUTO_STEER_ONLY) &&
486 control_command.has_signal()) {
487 HandleVehicleSignal(
488 ProcessCommandChange(control_command.signal(), &last_control_command_));
489 }
490
491 return ErrorCode::OK;
492}
493
494template <typename SensorType>
496 const ChassisCommand &chassis_command) {
497 if (!is_initialized_) {
498 AERROR << "Controller is not initialized.";
499 return ErrorCode::CANBUS_ERROR;
500 }
501
502 if ((driving_mode() == Chassis::COMPLETE_AUTO_DRIVE ||
503 driving_mode() == Chassis::AUTO_SPEED_ONLY ||
504 driving_mode() == Chassis::AUTO_STEER_ONLY) &&
505 chassis_command.has_basic_signal()) {
506 HandleVehicleSignal(ProcessCommandChange(chassis_command.basic_signal(),
507 &last_chassis_command_));
508 }
509
510 if (chassis_command.has_custom_operation()) {
511 auto result = HandleCustomOperation(chassis_command);
512 if (result != ErrorCode::OK) {
513 return result;
514 }
515 }
516
517 return ErrorCode::OK;
518}
519
520template <typename SensorType>
522 const common::VehicleSignal &vehicle_signal,
523 common::VehicleSignal *last_command) {
524 common::VehicleSignal vehicle_signal_end;
525 if (vehicle_signal.has_high_beam()) {
526 if ((last_command->has_high_beam() &&
527 last_command->high_beam() != vehicle_signal.has_high_beam()) ||
528 !last_command->has_high_beam()) {
529 vehicle_signal_end.set_high_beam(vehicle_signal.high_beam());
530 }
531 }
532
533 if (vehicle_signal.has_low_beam()) {
534 if ((last_command->has_low_beam() &&
535 last_command->low_beam() != vehicle_signal.has_low_beam()) ||
536 !last_command->has_low_beam()) {
537 vehicle_signal_end.set_low_beam(vehicle_signal.low_beam());
538 }
539 }
540
541 if (vehicle_signal.has_horn()) {
542 if ((last_command->has_horn() &&
543 last_command->horn() != vehicle_signal.horn()) ||
544 !last_command->horn()) {
545 vehicle_signal_end.set_horn(vehicle_signal.horn());
546 }
547 }
548
549 if (vehicle_signal.has_turn_signal()) {
550 if ((last_command->has_turn_signal() &&
551 last_command->turn_signal() != vehicle_signal.turn_signal()) ||
552 !last_command->turn_signal()) {
553 vehicle_signal_end.set_turn_signal(vehicle_signal.turn_signal());
554 }
555 }
556 *last_command = vehicle_signal_end;
557 return vehicle_signal_end;
558}
559
560} // namespace canbus
561} // namespace apollo
Defines CanReceiver class.
Defines SenderMessage class and CanSender class.
This is the interface class of vehicle controller.
CanSender< SensorType > * can_sender_
virtual common::ErrorCode SetDrivingMode(const Chassis::DrivingMode &driving_mode)
set vehicle to appointed driving mode.
common::VehicleSignal last_chassis_command_
virtual void set_driving_mode(const Chassis::DrivingMode &driving_mode)
CanReceiver< SensorType > * can_receiver_
virtual SensorType GetNewSenderChassisDetail()
virtual common::ErrorCode Init(const VehicleParameter &params, CanSender< SensorType > *const can_sender, MessageManager< SensorType > *const message_manager)=0
initialize the vehicle controller.
virtual Chassis chassis()=0
calculate and return the chassis.
virtual ~VehicleController()=default
MessageManager< SensorType > * message_manager_
virtual void Stop()=0
stop the vehicle controller.
virtual common::ErrorCode Update(const external_command::ChassisCommand &command)
virtual common::ErrorCode Update(const control::ControlCommand &command)
update the vehicle controller.
virtual bool Start()=0
start the vehicle controller.
common::VehicleSignal last_control_command_
virtual SensorType GetNewRecvChassisDetail()
virtual Chassis::DrivingMode driving_mode()
Cyber has builtin time type Time.
Definition time.h:31
uint64_t ToMicrosecond() const
convert time to microsecond (us).
Definition time.cc:85
static Time Now()
get the current time.
Definition time.cc:57
double ToSecond() const
convert time to second.
Definition time.cc:77
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AERROR_EVERY(freq)
Definition log.h:86
#define AINFO
Definition log.h:42
The class of MessageManager
class register implement
Definition arena_queue.h:37
The class of ProtocolData
optional double timestamp_sec
Definition header.proto:9
optional apollo::common::VehicleSignal signal
optional apollo::canbus::Chassis::GearPosition gear_location
optional DrivingAction action
Definition pad_msg.proto:17
optional apollo::common::Header header
Definition pad_msg.proto:14
optional apollo::common::VehicleSignal basic_signal